|
106 | 106 | isDragging = false; |
107 | 107 | } |
108 | 108 |
|
| 109 | + $effect(() => { |
| 110 | + // this was easier in svelte 4 :/ |
| 111 | + window.addEventListener('touchmove', handleTouchMove, { passive: false }); |
| 112 | + return () => window.removeEventListener('touchmove', handleTouchMove); |
| 113 | + }); |
| 114 | +
|
| 115 | + function handleTouchStart(event: TouchEvent) { |
| 116 | + isDragging = true; |
| 117 | + const touch = event.touches?.[0]; |
| 118 | + if (touch === undefined) return; |
| 119 | + startY = touch.clientY; |
| 120 | + startValue = normalizedValue; |
| 121 | + } |
| 122 | +
|
| 123 | + function handleTouchMove(event: TouchEvent) { |
| 124 | + if (!isDragging) return; |
| 125 | +
|
| 126 | + event.preventDefault(); |
| 127 | + if (disabled) return; |
| 128 | +
|
| 129 | + const touch = event.touches?.[0]; |
| 130 | + if (touch === undefined) return; |
| 131 | +
|
| 132 | + const deltaY = startY - touch.clientY; |
| 133 | + const deltaValue = deltaY / 200; |
| 134 | + setValue(Math.max(0, Math.min(1, startValue + deltaValue))); |
| 135 | + } |
| 136 | +
|
109 | 137 | function setValue(newNormalizedValue: number) { |
110 | 138 | if (param.type === 'enum-param') { |
111 | 139 | const newValue = unnormalizeToString(newNormalizedValue, param); |
|
182 | 210 | } |
183 | 211 | </script> |
184 | 212 |
|
185 | | -<svelte:window onmousemove={handleMouseMove} onmouseup={handleMouseUp} /> |
| 213 | +<svelte:window onmousemove={handleMouseMove} onmouseup={handleMouseUp} ontouchend={handleMouseUp} /> |
186 | 214 |
|
187 | 215 | <div class="container" {style}> |
188 | 216 | <svg |
|
197 | 225 | stroke-linejoin="round" |
198 | 226 | stroke-width={lineWidth} |
199 | 227 | onmousedown={handleMouseDown} |
| 228 | + ontouchstart={handleTouchStart} |
200 | 229 | > |
201 | 230 | <circle cx={center} cy={center} r={circleRadius} fill={bgColor}></circle> |
202 | 231 | {#if snapValues.length > 0 || param.type === 'enum-param'} |
|
0 commit comments