{
  "$schema": "../../schema.json",
  "id": "magic-move",
  "type": "component",
  "title": "Magic move",
  "summary": "Each part of A flies on its own spring into the matching part of B; text cross-blurs; the box reshapes.",
  "tags": [
    "component",
    "shared-element",
    "spring",
    "ui"
  ],
  "version": "1.0.0",
  "license": "MIT",
  "export": "magicMove",
  "signature": "magicMove(a, b, p, { duration, stagger, stiffness, damping })",
  "params": [
    {
      "name": "duration",
      "type": "number",
      "default": 1.1,
      "description": "length of the transition in seconds; springs use it to keep their feel",
      "min": 0.2,
      "max": 3,
      "step": 0.05,
      "group": "main"
    },
    {
      "name": "stagger",
      "type": "number",
      "default": 0.045,
      "description": "delay between parts (as a fraction of the transition)",
      "min": 0,
      "max": 0.15,
      "step": 0.005,
      "group": "main"
    },
    {
      "name": "stiffness",
      "type": "number",
      "default": 190,
      "description": "spring stiffness (higher is snappier)",
      "min": 40,
      "max": 600,
      "step": 5,
      "group": "main"
    },
    {
      "name": "damping",
      "type": "number",
      "default": 17,
      "description": "spring damping (lower bounces more)",
      "min": 4,
      "max": 40,
      "step": 0.5,
      "group": "main"
    }
  ],
  "install": {
    "kind": "code"
  },
  "files": [
    {
      "path": "moves/magic-move.js",
      "content": "import { ease, clamp, lerp, progress } from 'paper-motion';\nimport { show, sp, settle, blur, orig, mixRgba, rectIn, lerpRect, layer, hide, resetAll, partsOf } from 'paper-motion/kit';\n\n/**\n * Magic move: parts with the same `data-part` in A and B fly from A's place and size to B's, each on its own spring,\n * a few frames apart; text parts scale evenly and cross-blur; parts only in A leave, parts only in B pop in.\n * The box behind (background, border, corners) reshapes from A's to B's.\n */\nexport function magicMove(a, b, p, { duration = 1.1, stagger = 0.045, stiffness = 190, damping = 17 } = {}) {\n  const slot = a.parentElement, PA = partsOf(a), PB = partsOf(b);\n  const bgA = orig(a, 'backgroundColor'), bgB = orig(b, 'backgroundColor'), bcA = orig(a, 'borderTopColor'), bcB = orig(b, 'borderTopColor');\n  const parts = [...PA.values(), ...PB.values()];\n  if (p <= 0 || p >= 1) {\n    hide(slot, 'mmBox'); resetAll(...parts);\n    a.style.backgroundColor = bgA.inline; b.style.backgroundColor = bgB.inline; a.style.borderColor = bcA.inline; b.style.borderColor = bcB.inline;\n    a.style.overflow = ''; b.style.overflow = ''; show(a, b, p); return;\n  }\n  const RA = rectIn(a, slot), RB = rectIn(b, slot), cq = settle(sp(p, 0, duration, { stiffness: stiffness * 0.65, damping: damping * 0.9 }), p), R = lerpRect(RA, RB, cq);\n  const k = progress(p, 0.14, 0.5, ease.inOutCubic); // colour, border and corners change while the content swaps\n  const bw = lerp(parseFloat(orig(a, 'borderTopWidth').computed) || 0, parseFloat(orig(b, 'borderTopWidth').computed) || 0, k);\n  const rad = lerp(parseFloat(orig(a, 'borderTopLeftRadius').computed) || 0, parseFloat(orig(b, 'borderTopLeftRadius').computed) || 0, k);\n  layer(slot, 'mmBox', `left:${R.x}px;top:${R.y}px;width:${R.w}px;height:${R.h}px;box-sizing:border-box;z-index:1;background:${mixRgba(bgA.computed, bgB.computed, k)};border:${bw}px solid ${mixRgba(bcA.computed, bcB.computed, k)};border-radius:${rad}px`);\n  a.style.backgroundColor = 'transparent'; b.style.backgroundColor = 'transparent'; a.style.borderColor = 'transparent'; b.style.borderColor = 'transparent';\n  a.style.overflow = 'visible'; b.style.overflow = 'visible';\n  a.style.opacity = 1; b.style.opacity = 1; a.style.zIndex = 2; b.style.zIndex = 3;\n  [...new Set([...PA.keys(), ...PB.keys()])].forEach((key, i) => {\n    const ea = PA.get(key), eb = PB.get(key), t0 = 0.04 + i * stagger;\n    if (ea && eb) {\n      const q = settle(sp(p, t0, duration, { stiffness, damping }), p), ra = rectIn(ea, slot), rb = rectIn(eb, slot), r = lerpRect(ra, rb, q);\n      const text = !ea.children.length && !eb.children.length;\n      const place = (e, e0) => {\n        const sx = r.w / e0.w, sy = r.h / e0.h;\n        e.style.transformOrigin = '50% 50%';\n        e.style.transform = `translate(${r.x + r.w / 2 - (e0.x + e0.w / 2)}px, ${r.y + r.h / 2 - (e0.y + e0.h / 2)}px) scale(${text ? sy : sx}, ${sy})`;\n      };\n      place(ea, ra); place(eb, rb);\n      // boxes stretch when their shapes differ, so they hand over sooner than text does\n      const fa = text ? progress(p, t0 + 0.08, t0 + 0.36) : progress(p, t0 + 0.02, t0 + 0.18), fb = text ? progress(p, t0 + 0.16, t0 + 0.46) : progress(p, t0 + 0.08, t0 + 0.34);\n      ea.style.opacity = 1 - fa; ea.style.filter = blur(fa < 1 ? 9 * fa : 0);\n      eb.style.opacity = fb; eb.style.filter = blur(9 * (1 - fb));\n    } else if (ea) {\n      const l = progress(p, t0, t0 + 0.3, ease.inCubic);\n      ea.style.transform = `translateY(${-26 * l}px) scale(${1 - 0.1 * l})`; ea.style.opacity = 1 - l; ea.style.filter = blur(10 * l);\n    } else {\n      const q = settle(sp(p, 0.35 + t0, duration, { stiffness: 260, damping: 15 }), p);\n      eb.style.transform = `scale(${0.7 + 0.3 * q})`; eb.style.opacity = clamp(q * 1.5); eb.style.filter = blur(8 * clamp(1 - q));\n    }\n  });\n}\n"
    }
  ],
  "requires": {
    "engine": ">=0.2.0"
  },
  "usage": "import { magicMove } from './moves/magic-move.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) => magicMove(cardA, cardB, progress(t, 2.0, 3.1), { duration: 1.1 }));",
  "notes": [
    "Magic move: parts with the same `data-part` in A and B fly from A's place and size to B's, each on its own spring, a few frames apart; text parts scale evenly and cross-blur; parts only in A leave, parts only in B pop in. The box behind (background, border, corners) reshapes from A's to B's.",
    "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.",
    "Parts match by data-part (label, title, body, meta, media…); unmatched parts leave or pop in."
  ],
  "preview": {
    "video": "previews/magic-move.webm",
    "poster": "previews/magic-move.jpg"
  },
  "mood": [
    "calm",
    "technical",
    "luxury",
    "editorial"
  ],
  "energy": 3,
  "role": "swap",
  "when": [
    "a UI card changes state and parts carry over",
    "product and app videos",
    "continuity matters more than impact"
  ]
}