GestureGate

Draw-a-shape-to-unlock content gate β€” circle/check/line recognition, zero deps, keyboard bypass.

Live demo

01

Last playground outcome: β€”

Playground β€” pick a shape, tune the tolerance

The vault opens

This panel was frosted and inert until your gesture passed the maths.

Variant β€” circle + frost (the classic)

Members-only content, unlocked by a loop.

Variant β€” check mark + fade

Draw a βœ“ to confirm you’re done.

Variant β€” line + shatter

One confident swipe right (or any straight stroke).

Keyboard users: Tab to the gate, press Enter β€” bypass is first-class.

Implementation

02
GestureGate.svelte
<script lang="ts">
  import GestureGate from '$lib/components/GestureGate.svelte';
​
  let unlocked = $state(false);
</script>
​
<GestureGate shape="circle" revealEffect="frost" onUnlock={() => (unlocked = true)}>
  <p>This content stays frosted until the visitor draws a circle (or presses Enter).</p>
</GestureGate>

GestureGate keeps the locked content mounted but veiled (CSS blur + inert), overlays a pointer-capturing canvas, and validates each stroke geometrically on pointerup β€” closure, winding angle, and roundness CV for circles; chord deviation for lines; direction change for checks. The canvas is a focusable role="button", so Enter/Space unlocks too β€” the gesture is the fun path, never the only one. Recognition maths are exported from the component module so they unit-test without a DOM.

Logic explainer

03

What Does It Do? (Plain English)

GestureGate veils content behind a frosted overlay and asks the visitor to draw a shape to unlock it β€” a circle, a check mark, or a straight line. Draw it well and the gate dissolves with a reveal effect; draw it badly and you get a gentle hint. Keyboard and screen-reader users press Enter instead β€” the gesture is the fun path, never the only path.

How It Works (Pseudo-Code)

on pointerdown: start a stroke, capture the pointer
on pointermove: append points (2px sampling), draw the trail incrementally
on pointerup:
    run the matcher for the configured shape
    match  β†’ play reveal effect, fire onUnlock
    miss   β†’ count it; after maxAttempts, show a self-drawing hint
on Enter/Space (canvas is a focusable role="button"): bypass-unlock

The Recognition Maths

No ML, no libraries β€” three geometric checks, all O(n) over the sampled points:

  • Closure β€” distance between first and last point is under one mean radius. Rejects open arcs.
  • Winding β€” the total signed turning angle around the centroid is β‰ˆ Β±2Ο€ for a real loop. Rejects horseshoes and spirals-that-quit.
  • Roundness β€” the coefficient of variation (Οƒ/ΞΌ) of every point's radius from the centroid stays under tolerance (default 0.35). Rejects scribbles and ovals-that-are-really-lines.

check and line presets swap in direction-change and chord-deviation matchers, and a matcher prop accepts a fully custom function.

Accessibility & the Bypass Contract

A drawing gate would be a wall for anyone not using a pointer, so the canvas is a real role="button" with tabindex="0", an aria-label that says what to draw, and Enter/Space as a first-class unlock. Every attempt is announced through an aria-live status node, and prefers-reduced-motion collapses the reveal to a plain fade. The locked content is wrapped in inert so keyboard users can't tab into veiled controls.

State Flow Diagram

locked ──pointer stroke──▢ recognising ──match──▢ unlocked (reveal effect)
  β–²                           β”‚
  β”‚                           miss
  └───────── hint after N β—€β”€β”€β”€β”˜
locked ──Enter/Space──▢ unlocked (bypass)

Props Reference

Prop Type Default Description
shape 'circle' | 'check' | 'line' 'circle' Gesture preset
tolerance number 0.35 Roundness CV / fit slack
strokeColor string 'currentColor' Trail colour
strokeWidth number 3 Trail width (px)
revealEffect 'frost' | 'fade' | 'shatter' 'frost' Unlock transition
maxAttempts number 2 Misses before the hint appears
bypassLabel string 'Press Enter to unlock' SR / keyboard affordance text
matcher (pts: Point[]) => boolean β€” Custom gesture matcher
onUnlock () => void β€” Unlock callback
onAttempt (ok: boolean) => void β€” Per-attempt callback
class string '' Extra classes

Edge Cases

  • Tiny strokes (< 12 points / < 24px) are rejected outright β€” they're taps, not gestures.
  • Touch scrolling is suppressed on the canvas (touch-action: none) so the stroke owns the gesture.
  • Rapid retries: the failed trail lingers ~350ms so it feels "seen", then wipes.
  • Resize: the canvas re-measures with devicePixelRatio capped at 2 (retina-sharp, memory-safe).
  • Custom matcher returning true on any input is treated as a valid unlock β€” keep matchers honest.

Dependencies

Zero. Canvas 2D, Pointer Events, and CSS transforms only.

File Structure

src/lib/components/GestureGate.svelte   β€” component (module script exports the matchers for tests)
src/lib/components/GestureGate.md       β€” this doc
src/lib/components/GestureGate.test.ts  β€” 10 unit tests over the recognition maths
src/routes/gesturegate/+page.svelte     β€” demo (three reveal variants + tolerance playground)