Active Cursor Grab and Debounce Update

This commit is contained in:
cmack 2020-07-10 14:14:21 +01:00
parent cb26992ef3
commit 8e6f4301ce
4 changed files with 20 additions and 21 deletions

View File

@ -28,6 +28,8 @@
let errorMsg = null let errorMsg = null
const dispatch = createEventDispatcher() const dispatch = createEventDispatcher()
const dispatchValue = value =>
debounce(() => dispatch("change", value), 300, true)
beforeUpdate(() => { beforeUpdate(() => {
format = getColorFormat(value) format = getColorFormat(value)
@ -47,8 +49,7 @@
function onColorChange(color) { function onColorChange(color) {
value = color.detail value = color.detail
const fn = debounce(() => dispatch("change", color.detail), 300) dispatchValue(value)
fn()
} }
function calculateDimensions() { function calculateDimensions() {

View File

@ -11,7 +11,6 @@
export let a = 1 export let a = 1
let palette let palette
let cursor = "grab"
let paletteHeight, let paletteHeight,
paletteWidth = 0 paletteWidth = 0
@ -35,8 +34,7 @@
` `
$: style = `background: ${paletteGradient};` $: style = `background: ${paletteGradient};`
$: pickerStyle = `transform: translate(${pickerX - 8}px, ${pickerY - $: pickerStyle = `transform: translate(${pickerX - 8}px, ${pickerY - 8}px);`
8}px); cursor: ${cursor};`
</script> </script>
<CheckedBackground width="100%"> <CheckedBackground width="100%">
@ -52,9 +50,7 @@
{style}> {style}>
<div <div
use:drag use:drag
on:dragstart={() => (cursor = 'grabbing')}
on:drag={event => handePaletteChange(event.detail)} on:drag={event => handePaletteChange(event.detail)}
on:dragend={() => (cursor = 'grab')}
class="picker" class="picker"
style={pickerStyle} /> style={pickerStyle} />
</div> </div>
@ -71,10 +67,15 @@
.picker { .picker {
position: absolute; position: absolute;
cursor: grab;
width: 10px; width: 10px;
height: 10px; height: 10px;
background: transparent; background: transparent;
border: 2px solid white; border: 2px solid white;
border-radius: 50%; border-radius: 50%;
} }
.picker:active {
cursor: grabbing;
}
</style> </style>

View File

@ -11,17 +11,12 @@
let sliderWidth = 0 let sliderWidth = 0
let upperLimit = type === "hue" ? 360 : 1 let upperLimit = type === "hue" ? 360 : 1
let incrementFactor = type === "hue" ? 1 : 0.01 let incrementFactor = type === "hue" ? 1 : 0.01
let cursor = "grab"
const isWithinLimit = value => value >= 0 && value <= upperLimit const isWithinLimit = value => value >= 0 && value <= upperLimit
function onSliderChange({ mouseX }, isDrag = false) { function onSliderChange({ mouseX }, isDrag = false) {
const { left, width } = slider.getBoundingClientRect() const { left, width } = slider.getBoundingClientRect()
if (isDrag && cursor !== "grabbing") {
cursor = "grabbing"
}
let clickPosition = mouseX - left let clickPosition = mouseX - left
let percentageClick = (clickPosition / sliderWidth).toFixed(2) let percentageClick = (clickPosition / sliderWidth).toFixed(2)
@ -48,19 +43,14 @@
} }
} }
function handleDragStart() {
cursor = "grabbing"
}
function handleDragEnd() { function handleDragEnd() {
cursor = "grab"
dispatch("dragend") dispatch("dragend")
} }
$: thumbPosition = $: thumbPosition =
type === "hue" ? sliderWidth * (value / 360) : sliderWidth * value type === "hue" ? sliderWidth * (value / 360) : sliderWidth * value
$: style = `transform: translateX(${thumbPosition - 6}px); cursor: ${cursor};` $: style = `transform: translateX(${thumbPosition - 6}px);`
</script> </script>
<div <div
@ -75,7 +65,6 @@
<div <div
use:drag use:drag
on:drag={e => onSliderChange(e.detail, true)} on:drag={e => onSliderChange(e.detail, true)}
on:dragstart={handleDragStart}
on:dragend={handleDragEnd} on:dragend={handleDragEnd}
class="slider-thumb" class="slider-thumb"
{style} /> {style} />
@ -122,4 +111,8 @@
background-color: #ffffff; background-color: #ffffff;
cursor: grab; cursor: grab;
} }
.slider-thumb:active {
cursor: grabbing;
}
</style> </style>

View File

@ -13,8 +13,12 @@ export const convertCamel = str => {
return str.replace(/[A-Z]/g, match => `-${match.toLowerCase()}`) return str.replace(/[A-Z]/g, match => `-${match.toLowerCase()}`)
} }
export const debounce = (fn, milliseconds) => { export const debounce = (fn, milliseconds, callImmediately) => {
return () => { const debouncedFn = () => {
setTimeout(fn, milliseconds) setTimeout(fn, milliseconds)
} }
if (callImmediately) {
debouncedFn()
}
return debouncedFn
} }