{
  "$schema": "../../schema.json",
  "id": "tile-swirl",
  "type": "component",
  "title": "Tile swirl",
  "summary": "A breaks into tiles that lift and swirl in a wave, flipping over in flight to land as B.",
  "tags": [
    "component",
    "3d",
    "particles",
    "ui"
  ],
  "version": "1.0.0",
  "license": "MIT",
  "export": "tileSwirl",
  "signature": "tileSwirl(a, b, p, { cols, rows, lift, perspective })",
  "params": [
    {
      "name": "cols",
      "type": "number",
      "default": 6,
      "description": "grid columns",
      "min": 2,
      "max": 24,
      "step": 1,
      "group": "main"
    },
    {
      "name": "rows",
      "type": "number",
      "default": 4,
      "description": "grid rows",
      "min": 2,
      "max": 16,
      "step": 1,
      "group": "main"
    },
    {
      "name": "lift",
      "type": "number",
      "default": 280,
      "description": "lift toward the camera in px",
      "min": 0,
      "max": 400,
      "step": 5,
      "group": "main"
    },
    {
      "name": "perspective",
      "type": "number",
      "default": 1600,
      "description": "3D perspective in px",
      "min": 400,
      "max": 4000,
      "step": 50,
      "group": "main"
    }
  ],
  "install": {
    "kind": "code"
  },
  "files": [
    {
      "path": "moves/tile-swirl.js",
      "content": "import { ease, progress } from 'paper-motion';\nimport { dims, show, copy } from 'paper-motion/kit';\n\n/** A breaks into tiles that lift and swirl outward in a diagonal wave, flipping over in flight, and land as B. */\nexport function tileSwirl(a, b, p, { cols = 6, rows = 4, lift = 280, perspective = 1600 } = {}) {\n  const slot = a.parentElement, [w, h] = dims(a), cw = w / cols, ch = h / rows;\n  slot._pmTiles ??= [];\n  const tiles = slot._pmTiles;\n  if (p <= 0 || p >= 1) { tiles.forEach((t) => { t.style.display = 'none'; }); show(a, b, p); return; }\n  slot.style.perspective = `${perspective}px`;\n  while (tiles.length < cols * rows) {\n    const t = document.createElement('div'); t.className = 'pm-fx';\n    t.innerHTML = '<div class=\"f\"></div><div class=\"f\"></div>';\n    slot.append(t); tiles.push(t);\n  }\n  a.style.opacity = 0; b.style.opacity = 0;\n  for (let j = 0; j < rows; j++) for (let i = 0; i < cols; i++) {\n    const t = tiles[j * cols + i], [front, back] = t.children, x = i * cw, y = j * ch;\n    const d = (i / (cols - 1)) * 0.32 + (j / (rows - 1)) * 0.12, q = ease.inOutCubic(progress(p, d, d + 0.56)), arc = Math.sin(Math.PI * q);\n    const ox = ((i + 0.5) / cols - 0.5) * w * 0.55 * arc, oy = ((j + 0.5) / rows - 0.5) * h * 0.55 * arc - 50 * arc, spin = (((i * 7 + j * 3) % 5) - 2) * 9 * arc;\n    t.style.cssText = `position:absolute;left:${x}px;top:${y}px;width:${Math.ceil(cw) + 1}px;height:${Math.ceil(ch) + 1}px;z-index:${5 + Math.round(arc * 10)};transform-style:preserve-3d;`\n      + `transform:translate3d(${ox}px,${oy}px,${lift * arc}px) rotateZ(${spin}deg) rotateY(${180 * q}deg) scale(${1 - 0.18 * arc})`;\n    for (const [face, src, flip] of [[front, a, ''], [back, b, 'rotateY(180deg)']]) {\n      face.style.cssText = `position:absolute;inset:0;overflow:hidden;backface-visibility:hidden;transform:${flip || 'none'};filter:brightness(${1 - 0.35 * arc})`;\n      const c = copy(src); c.style.left = `${-x}px`; c.style.top = `${-y}px`;\n      face.replaceChildren(c);\n    }\n  }\n}\n"
    }
  ],
  "requires": {
    "engine": ">=0.2.0"
  },
  "usage": "import { tileSwirl } from './moves/tile-swirl.js';\n\n// A and B (two states of a card) stacked in one positioned slot; 1.1 s from 2.0 s:\ns.update((t) => tileSwirl(cardA, cardB, progress(t, 2.0, 3.1), { duration: 1.1 }));",
  "notes": [
    "A breaks into tiles that lift and swirl outward in a diagonal wave, flipping over in flight, and land as B.",
    "Stack A and B in one positioned slot (their parent); give the slot overflow:hidden to keep travel inside it.",
    "Pass the real duration in seconds and call it through p = 1: that is when it hides the layers it made.",
    "Render with --motion-blur 5.",
    "Draws live copies of the card: a Paper shader canvas inside comes out blank."
  ],
  "preview": {
    "video": "previews/tile-swirl.webm",
    "poster": "previews/tile-swirl.jpg"
  },
  "mood": [
    "playful",
    "bold",
    "technical"
  ],
  "energy": 5,
  "role": "swap",
  "when": [
    "a dramatic swap with particles",
    "launches, reveals, hero moments"
  ]
}