Skip to content

Commit

Permalink
feat: add mobile suport
Browse files Browse the repository at this point in the history
  • Loading branch information
eye-wave committed Oct 24, 2024
1 parent 976e990 commit 100a569
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "svelte-knobs",
"description": "Svelte component library for building customizable knob controls.",
"version": "0.2.0",
"version": "0.2.1",
"repository": {
"url": "https://github.com/eye-wave/svelte-knobs"
},
Expand Down
31 changes: 30 additions & 1 deletion src/lib/Knob.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,34 @@
isDragging = false;
}
$effect(() => {
// this was easier in svelte 4 :/
window.addEventListener('touchmove', handleTouchMove, { passive: false });
return () => window.removeEventListener('touchmove', handleTouchMove);
});
function handleTouchStart(event: TouchEvent) {
isDragging = true;
const touch = event.touches?.[0];
if (touch === undefined) return;
startY = touch.clientY;
startValue = normalizedValue;
}
function handleTouchMove(event: TouchEvent) {
if (!isDragging) return;
event.preventDefault();
if (disabled) return;
const touch = event.touches?.[0];
if (touch === undefined) return;
const deltaY = startY - touch.clientY;
const deltaValue = deltaY / 200;
setValue(Math.max(0, Math.min(1, startValue + deltaValue)));
}
function setValue(newNormalizedValue: number) {
if (param.type === 'enum-param') {
const newValue = unnormalizeToString(newNormalizedValue, param);
Expand Down Expand Up @@ -182,7 +210,7 @@
}
</script>

<svelte:window onmousemove={handleMouseMove} onmouseup={handleMouseUp} />
<svelte:window onmousemove={handleMouseMove} onmouseup={handleMouseUp} ontouchend={handleMouseUp} />

<div class="container" {style}>
<svg
Expand All @@ -197,6 +225,7 @@
stroke-linejoin="round"
stroke-width={lineWidth}
onmousedown={handleMouseDown}
ontouchstart={handleTouchStart}
>
<circle cx={center} cy={center} r={circleRadius} fill={bgColor}></circle>
{#if snapValues.length > 0 || param.type === 'enum-param'}
Expand Down

0 comments on commit 100a569

Please sign in to comment.