Motion playground

Drag the shapes to experiment. Focus a shape for details. Arrow keys move it; Shift moves farther. Home or Escape returns it to its starting point.

↖ portfolio
drag something. see what happens.
IMPLEMENTATION REFERENCE

Sticky edges

Distance from each edge controls attraction, stretch, and individual corner radii. The 70px range and squared falloff determine how sticky it feels.

Svelte 5 · standalone component

<script lang="ts">
  import { onMount } from 'svelte';

  const w = 160, h = 104;
  const clamp = (n: number, min: number, max: number) =>
    Math.max(min, Math.min(max, n));
  let width = $state(800), height = $state(600);
  let x = $state(240), y = $state(180);
  let tx = 240, ty = 180, homeX = 240, homeY = 180;
  let vx = 0, vy = 0, dragging = false, reduced = false;
  let offsetX = 0, offsetY = 0, lastX = 0, lastY = 0, lastTime = 0;

  const paint = $derived.by(() => {
    const distances = [x - w / 2, width - x - w / 2,
      y - h / 2, height - y - h / 2];
    const [left, right, top, bottom] = distances.map(distance =>
      Math.pow(clamp(1 - distance / 70, 0, 1), 2));
    const drawW = w * (1 + .25 * Math.max(top, bottom) - .18 * Math.max(left, right));
    const drawH = h * (1 + .42 * Math.max(left, right) - .18 * Math.max(top, bottom));
    const drawX = x + (drawW / 2 - x) * left + (width - drawW / 2 - x) * right;
    const drawY = y + (drawH / 2 - y) * top + (height - drawH / 2 - y) * bottom;
    const radius = [Math.max(left, top), Math.max(right, top),
      Math.max(right, bottom), Math.max(left, bottom)]
      .map(pull => (h / 2 * (1 - pull)) + 'px').join(' ');
    return { x: clamp(drawX, drawW / 2, width - drawW / 2),
      y: clamp(drawY, drawH / 2, height - drawH / 2), w: drawW, h: drawH, radius };
  });

  function down(e: PointerEvent) {
    if (e.button !== 0) return;
    (e.currentTarget as HTMLElement).setPointerCapture(e.pointerId);
    dragging = true;
    offsetX = e.clientX - paint.x;
    offsetY = e.clientY - paint.y;
    lastX = e.clientX; lastY = e.clientY;
    lastTime = performance.now();
    vx = vy = 0;

  }

  function move(e: PointerEvent) {
    if (!dragging) return;

    const now = performance.now();
    const elapsed = Math.max(8, now - lastTime);
    tx = clamp(e.clientX - offsetX, w / 2, width - w / 2);
    ty = clamp(e.clientY - offsetY, h / 2, height - h / 2);
    x = tx; y = ty;

    lastX = e.clientX; lastY = e.clientY; lastTime = now;
  }

  function up(e: PointerEvent) {
    if (!dragging) return;
    dragging = false;
    if (e.type === 'pointercancel' || performance.now() - lastTime > 100)
      vx = vy = 0;

  }

  onMount(() => {
    const media = matchMedia('(prefers-reduced-motion: reduce)');
    const preference = () => { reduced = media.matches; };
    preference();
    media.addEventListener('change', preference);
    const resize = () => {
      width = innerWidth; height = innerHeight;
      x = tx = homeX = width / 2;
      y = ty = homeY = height / 2;
      vx = vy = 0; dragging = false;
    };
    resize();
    window.addEventListener('resize', resize);
    let frame = 0, previous = 0;
    function tick(now: number) {
      // Normalize time to a 60 Hz frame; cap after a background-tab pause.
      const dt = Math.min((now - (previous || now)) / 16.67, 2);
      previous = now;



      frame = requestAnimationFrame(tick);
    }
    frame = requestAnimationFrame(tick);
    return () => {

      cancelAnimationFrame(frame);
      window.removeEventListener('resize', resize);
      media.removeEventListener('change', preference);
    };
  });
</script>

<div class="stage">

  <button
    class="shape" aria-label="Drag to explore; arrow keys to move"
    style:left={paint.x + 'px'} style:top={paint.y + 'px'}
    style:width={paint.w + 'px'} style:height={paint.h + 'px'}
    style:border-radius={paint.radius}
    onpointerdown={down} onpointermove={move}
    onpointerup={up} onpointercancel={up} onlostpointercapture={up}
    onkeydown={(e) => {
      const directions: Record<string, [number, number]> = {
        ArrowLeft: [-1, 0], ArrowRight: [1, 0],
        ArrowUp: [0, -1], ArrowDown: [0, 1]
      };
      const direction = directions[e.key];
      if (!direction) return;
      e.preventDefault();

      x = tx = clamp(x + direction[0] * 20, w / 2, width - w / 2);
      y = ty = clamp(y + direction[1] * 20, h / 2, height - h / 2);

    }}
  ></button>
</div>

<style>
  .stage { position: fixed; inset: 0; overflow: hidden; background: #111412; }
  .shape {
    z-index: 3; /* Stay grabbable above the demo input and toast. */
    position: absolute; transform: translate(-50%, -50%);
    border: 0; background: #b6ed91; cursor: grab;
    touch-action: none; user-select: none;
  }
  .shape:active { cursor: grabbing; }
  .shape:focus-visible { outline: 2px solid white; outline-offset: 5px; }

</style>