<!-- Source: https://motion.svelte.page/examples/vanilla-drag -->

# Vanilla drag()

> Motion 14.4 drag() on a plain element in Svelte — a slider knob constrained to its track, reset with animate() because both share the knob’s x value.

**Source:** [https://motion.svelte.page/examples/vanilla-drag](https://motion.svelte.page/examples/vanilla-drag)

**Markdown mirror:** [https://motion.svelte.page/examples/vanilla-drag.md](https://motion.svelte.page/examples/vanilla-drag.md)

---

This mirror preserves the prose, implementation notes, and runnable Svelte source behind the live example page.

## FIG-001: a slider knob on a plain element.

drag() makes the knob draggable on x and keeps it inside its track. Reset animates the knob home with animate(); both work on the same x value, so the next drag starts from wherever the knob is.

**Metadata:** tag: `DRAG` | constraints: `track`

### Notes

- `axis: 'x'` locks the knob to one axis and sets `touch-action: pan-y`, so a vertical swipe on a phone still scrolls the page.
- `constraints: track` keeps the knob inside the track element. Release it with some speed and it glides, then settles against the edge.
- `drag()` and `animate()` share the knob's `x`, so Reset springs it home and the next drag continues from where it is.

### Source

#### SliderKnob.svelte

Source file: [src/lib/examples/vanilla-drag/demos/SliderKnob.svelte](https://github.com/humanspeak/svelte-motion/blob/main/docs/src/lib/examples/vanilla-drag/demos/SliderKnob.svelte)

```svelte
<script lang="ts">
    import { animate, drag } from '@humanspeak/svelte-motion'

    // A slider knob on a plain <div>. drag() makes it draggable on x and keeps
    // it inside the track; "Reset" animates the same x back to 0 with
    // animate(), because drag() and animate() share the element's x value.
    // No motion component.

    let track = $state<HTMLElement | null>(null)
    let knob = $state<HTMLElement | null>(null)
    let value = $state(0)

    /** Knob position along the track, as 0–100. */
    const measure = () => {
        if (!track || !knob) return
        const t = track.getBoundingClientRect()
        const k = knob.getBoundingClientRect()
        const travel = t.width - k.width
        value = travel > 0 ? Math.round(((k.left - t.left) / travel) * 100) : 0
    }

    // Follow the knob through the drag and its release glide.
    let raf = 0
    const follow = () => {
        cancelAnimationFrame(raf)
        const tick = () => {
            measure()
            raf = requestAnimationFrame(tick)
        }
        tick()
    }
    const stopFollowing = () => {
        cancelAnimationFrame(raf)
        measure()
    }

    $effect(() => {
        if (!knob || !track) return
        const stop = drag(knob, {
            axis: 'x',
            constraints: track,
            elastic: 0.15,
            onStart: follow,
            onTransitionEnd: stopFollowing
        })
        return () => {
            stop()
            cancelAnimationFrame(raf)
        }
    })

    const reset = async () => {
        if (!knob) return
        follow()
        await animate(knob, { x: 0 }, { type: 'spring', visualDuration: 0.4, bounce: 0.2 })
        stopFollowing()
    }
</script>

<!-- dk-strip: docs-kit positioning shell — stripped from the published code. -->
<div class="dk-demo-shell">
    <div class="strip">
        <div class="strip-head">
            <span class="micro">// slider knob</span>
            <span class="micro readout">value · {value}</span>
        </div>

        <div class="stage">
            <div class="track" bind:this={track}>
                <div class="fill" style:width="{value}%"></div>
                <div class="knob" bind:this={knob} aria-hidden="true"></div>
            </div>
        </div>

        <div class="strip-foot">
            <span class="micro">drag(knob, &#123; axis: 'x', constraints: track &#125;)</span>
            <button type="button" onclick={reset}>Reset</button>
        </div>
    </div>
</div>

<style>
    .dk-demo-shell {
        display: flex;
        align-items: center;
        justify-content: center;
        padding: 1.5rem;
        min-height: 400px;
    }

    .strip {
        display: flex;
        flex-direction: column;
        gap: 0.75rem;
        width: min(100%, 360px);
    }

    .micro {
        font-family: var(--brut-mono, monospace);
        font-size: 0.6875rem;
        letter-spacing: 0.08em;
        text-transform: uppercase;
        color: var(--brut-ink-3, #9a9a9a);
    }

    .strip-head,
    .strip-foot {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 1rem;
        border-bottom: 1px dashed var(--brut-rule-2, #bbc4c0);
        padding-bottom: 0.5rem;
    }

    .strip-foot {
        border-bottom: none;
        border-top: 1px dashed var(--brut-rule-2, #bbc4c0);
        padding-top: 0.75rem;
        padding-bottom: 0;
    }

    .readout {
        color: var(--brut-accent, #247768);
    }

    .stage {
        display: flex;
        align-items: center;
        justify-content: center;
        height: 200px;
    }

    .track {
        position: relative;
        box-sizing: border-box;
        width: 100%;
        height: 48px;
        border: 1px solid var(--brut-ink, #0a0a0a);
        background: var(--brut-bg, #f8fcfb);
    }

    .fill {
        position: absolute;
        inset: 0 auto 0 0;
        background: var(--brut-accent, #247768);
        opacity: 0.25;
        pointer-events: none;
    }

    .knob {
        position: absolute;
        top: -1px;
        left: -1px;
        box-sizing: border-box;
        width: 48px;
        height: 48px;
        border: 1px solid var(--brut-ink, #0a0a0a);
        background: var(--brut-ink, #0a0a0a);
        cursor: grab;
    }

    .knob:active {
        cursor: grabbing;
    }

    button {
        padding: 0.5rem 1rem;
        border: 1px solid var(--brut-ink, #0a0a0a);
        background: var(--brut-ink, #0a0a0a);
        color: var(--brut-bg, #f8fcfb);
        font-family: var(--brut-mono, monospace);
        font-size: 0.75rem;
        text-transform: uppercase;
        cursor: pointer;
    }
</style>
```

## FIG-002: a dial driven by a motion value.

Motion 14.10 lets drag() write into x/y motion values instead of moving the element. The dial stays where it is; a horizontal drag turns it, and a flick spins it with momentum.

**Metadata:** tag: `HEADLESS` | x: `motionValue(0)`

### Notes

- `drag(dial, &#123; axis: 'x', x &#125;)` writes the drag into `x`, not the dial's own transform, so the dial never moves.
- `x.on('change')` turns the value into the dial's `rotate`. Release with some speed and momentum keeps writing to `x`, so the dial spins down.

### Source

#### Dial.svelte

Source file: [src/lib/examples/vanilla-drag/demos/Dial.svelte](https://github.com/humanspeak/svelte-motion/blob/main/docs/src/lib/examples/vanilla-drag/demos/Dial.svelte)

```svelte
<script lang="ts">
    import { drag, motionValue } from '@humanspeak/svelte-motion'

    // A rotary dial on a plain <div>. drag() gets an `x` motion value
    // (Motion 14.10), so it writes the horizontal drag into that value instead
    // of moving the dial. The dial stays put and turns: 1px of drag is 1deg.
    // No motion component.

    let dial = $state<HTMLElement | null>(null)
    const x = motionValue(0)
    let degrees = $state(0)

    $effect(() => {
        if (!dial) return
        const el = dial
        const unsubscribe = x.on('change', (v) => {
            el.style.rotate = `${v}deg`
            degrees = Math.round(v)
        })
        const stop = drag(el, { axis: 'x', x })
        return () => {
            unsubscribe()
            stop()
        }
    })
</script>

<!-- dk-strip: docs-kit positioning shell — stripped from the published code. -->
<div class="dk-demo-shell">
    <div class="strip">
        <div class="strip-head">
            <span class="micro">// rotary dial</span>
            <span class="micro readout">rotate · {degrees}°</span>
        </div>

        <div class="stage">
            <div class="dial" bind:this={dial} aria-hidden="true">
                <span class="needle"></span>
            </div>
        </div>

        <div class="strip-foot">
            <span class="micro">drag(dial, &#123; axis: 'x', x &#125;)</span>
        </div>
    </div>
</div>

<style>
    .dk-demo-shell {
        display: flex;
        align-items: center;
        justify-content: center;
        padding: 1.5rem;
        min-height: 400px;
    }

    .strip {
        display: flex;
        flex-direction: column;
        gap: 0.75rem;
        width: min(100%, 360px);
    }

    .micro {
        font-family: var(--brut-mono, monospace);
        font-size: 0.6875rem;
        letter-spacing: 0.08em;
        text-transform: uppercase;
        color: var(--brut-ink-3, #9a9a9a);
    }

    .strip-head,
    .strip-foot {
        display: flex;
        align-items: center;
        justify-content: space-between;
        gap: 1rem;
        border-bottom: 1px dashed var(--brut-rule-2, #bbc4c0);
        padding-bottom: 0.5rem;
    }

    .strip-foot {
        border-bottom: none;
        border-top: 1px dashed var(--brut-rule-2, #bbc4c0);
        padding-top: 0.75rem;
        padding-bottom: 0;
    }

    .readout {
        color: var(--brut-accent, #247768);
    }

    .stage {
        display: flex;
        align-items: center;
        justify-content: center;
        height: 200px;
    }

    .dial {
        position: relative;
        box-sizing: border-box;
        width: 140px;
        height: 140px;
        border: 1px solid var(--brut-ink, #0a0a0a);
        border-radius: 50%;
        background: var(--brut-bg, #f8fcfb);
        cursor: ew-resize;
    }

    .needle {
        position: absolute;
        top: 10px;
        left: calc(50% - 3px);
        width: 6px;
        height: 50px;
        background: var(--brut-accent, #247768);
        pointer-events: none;
    }
</style>
```
