{
  "files": [
    {
      "path": "src/registry/items/dot-matrix-dom/dot-matrix-dom.ts",
      "content": "import { pitchOf } from \"../dot-matrix/dot-matrix-lattice\";\nimport {\n  applyLook,\n  cellLook,\n  coreOf,\n  paletteWeight,\n  placeLook,\n  type CellLook,\n  type Declarations,\n} from \"../dot-matrix/dot-matrix-look\";\nimport type {\n  DotMatrixPlayer,\n  DotMatrixRenderer,\n  DotMatrixScene,\n} from \"../dot-matrix/dot-matrix-renderer\";\nimport { SCALE_MAX, type DotMatrixTrack } from \"../dot-matrix/dot-matrix-track\";\n\ntype DotFrames = Readonly<{\n  light: readonly number[];\n  scale: readonly number[] | undefined;\n  angle: readonly number[] | undefined;\n  tint: readonly number[] | undefined;\n  hue: readonly number[] | undefined;\n}>;\n\nexport type Ink = Readonly<{\n  color: string | undefined;\n  share: (frames: DotFrames, frame: number) => number;\n}>;\n\nexport type DotPart = Readonly<{ element: Element; ink: Ink; core: boolean }>;\n\nexport type DotParts = Readonly<{ dot: number; parts: readonly DotPart[] }>;\n\ntype MotionProperties = \"individual\" | \"transform\";\n\nfunction stepOf(track: DotMatrixTrack, input: number): number {\n  return Math.round(Math.min(1, Math.max(0, input)) * (track.steps - 1));\n}\n\nfunction channelOf(data: Uint8Array | undefined, track: DotMatrixTrack, dot: number, step: number) {\n  if (data === undefined) {\n    return undefined;\n  }\n  const base = (step * track.lattice.count + dot) * track.frames;\n  return Array.from(data.subarray(base, base + track.frames), (value) => {\n    return value / 255;\n  });\n}\n\nfunction unwrapTurns(turns: readonly number[]): number[] {\n  const out: number[] = [];\n  for (const turn of turns) {\n    const before = out.at(-1);\n    out.push(before === undefined ? turn : turn + Math.round(before - turn));\n  }\n  return out;\n}\n\nfunction dotFrames(track: DotMatrixTrack, dot: number, step: number): DotFrames {\n  const angle = channelOf(track.angle, track, dot, step);\n  return {\n    light: channelOf(track.light, track, dot, step) ?? [],\n    scale: channelOf(track.scale, track, dot, step),\n    angle: angle === undefined ? undefined : unwrapTurns(angle),\n    tint: channelOf(track.tint, track, dot, step),\n    hue: channelOf(track.hue, track, dot, step),\n  };\n}\n\nfunction hueAt(frames: DotFrames, frame: number): number {\n  return frames.hue?.[frame] ?? 1;\n}\n\nexport function inksOf(scene: DotMatrixScene): Ink[] {\n  const { track, colors, from } = scene;\n  const palette = track.tint !== undefined && colors !== undefined && colors.length > 0;\n  const own: Ink[] = palette\n    ? colors.map((color, index) => {\n        return {\n          color,\n          share: (frames, frame) => {\n            return paletteWeight(colors.length, index, frames.tint?.[frame] ?? 0);\n          },\n        };\n      })\n    : [{ color: undefined, share: () => 1 }];\n  if (from === undefined || track.hue === undefined) {\n    return own;\n  }\n  return [\n    ...own.map((ink): Ink => {\n      return {\n        color: ink.color,\n        share: (frames, frame) => {\n          return ink.share(frames, frame) * hueAt(frames, frame);\n        },\n      };\n    }),\n    {\n      color: from,\n      share: (frames, frame) => {\n        return 1 - hueAt(frames, frame);\n      },\n    },\n  ];\n}\n\nfunction opacityAt(frames: DotFrames, ink: Ink, core: boolean, frame: number): number {\n  const light = frames.light[frame] ?? 0;\n  return (core ? coreOf(light) : light) * ink.share(frames, frame);\n}\n\nexport function litParts(\n  track: DotMatrixTrack,\n  inks: readonly Ink[],\n  dot: number,\n): { ink: Ink; core: boolean }[] {\n  const lights = new Set<Ink>();\n  const cores = new Set<Ink>();\n  for (let step = 0; step < track.steps; step += 1) {\n    const frames = dotFrames(track, dot, step);\n    for (let frame = 0; frame < track.frames; frame += 1) {\n      for (const ink of inks) {\n        if (opacityAt(frames, ink, false, frame) > 0) {\n          lights.add(ink);\n        }\n        if (opacityAt(frames, ink, true, frame) > 0) {\n          cores.add(ink);\n        }\n      }\n    }\n  }\n  return [\n    ...[...lights].map((ink) => {\n      return { ink, core: false };\n    }),\n    ...[...cores].map((ink) => {\n      return { ink, core: true };\n    }),\n  ];\n}\n\nfunction round(value: number, places: number): number {\n  const by = 10 ** places;\n  return Math.round(value * by) / by;\n}\n\nfunction motionOf(frames: DotFrames, frame: number, motion: MotionProperties): Keyframe {\n  const scale = frames.scale === undefined ? undefined : round(frames.scale[frame] ?? 0, 3);\n  const turn = frames.angle === undefined ? undefined : round(frames.angle[frame] ?? 0, 4);\n  const size = scale === undefined ? undefined : round(scale * SCALE_MAX, 3);\n  if (motion === \"individual\") {\n    return {\n      ...(size === undefined ? {} : { scale: String(size) }),\n      ...(turn === undefined ? {} : { rotate: `${turn}turn` }),\n    };\n  }\n  const list = [\n    ...(turn === undefined ? [] : [`rotate(${turn}turn)`]),\n    ...(size === undefined ? [] : [`scale(${size})`]),\n  ];\n  return list.length === 0 ? {} : { transform: list.join(\" \") };\n}\n\nfunction keyframesOf(\n  track: DotMatrixTrack,\n  frames: DotFrames,\n  part: Readonly<{ ink: Ink; core: boolean }>,\n  motion: MotionProperties,\n): Keyframe[] | undefined {\n  const keyframes: Keyframe[] = [];\n  let peak = 0;\n  for (let frame = 0; frame < track.frames; frame += 1) {\n    const opacity = round(opacityAt(frames, part.ink, part.core, frame), 3);\n    peak = Math.max(peak, opacity);\n    keyframes.push({ opacity, ...motionOf(frames, frame, motion) });\n  }\n  if (peak <= 0) {\n    return undefined;\n  }\n  if (!track.once) {\n    const first = frames.angle?.[0] ?? 0;\n    const last = frames.angle?.at(-1) ?? 0;\n    const closing: DotFrames = {\n      ...frames,\n      angle: frames.angle === undefined ? undefined : [first + Math.round(last - first)],\n    };\n    keyframes.push({ ...keyframes[0], ...motionOf(closing, 0, motion) });\n  }\n  return keyframes;\n}\n\nfunction timingOf(track: DotMatrixTrack): KeyframeAnimationOptions {\n  return {\n    duration: track.duration,\n    iterations: track.once ? 1 : Number.POSITIVE_INFINITY,\n    fill: track.once ? \"forwards\" : \"auto\",\n    easing: \"linear\",\n  };\n}\n\ntype PlayerPlan = Readonly<{\n  track: DotMatrixTrack;\n  dots: readonly DotParts[];\n  motion: MotionProperties;\n}>;\n\ntype PlayerState = Readonly<{\n  animations: readonly Animation[];\n  step: number;\n  speed: number;\n  playing: boolean;\n}>;\n\nfunction placeAnimation(\n  plan: PlayerPlan,\n  state: PlayerState,\n  animation: Animation,\n  time: CSSNumberish | null,\n): void {\n  animation.playbackRate = state.speed;\n  if (!state.playing) {\n    animation.pause();\n    if (time !== null) {\n      animation.currentTime = time;\n    }\n    return;\n  }\n  if (!plan.track.once) {\n    animation.startTime = 0;\n  } else if (time !== null) {\n    animation.currentTime = time;\n  }\n}\n\nfunction startStep(plan: PlayerPlan, state: PlayerState, step: number): PlayerState {\n  const time = state.animations[0]?.currentTime ?? null;\n  for (const animation of state.animations) {\n    animation.cancel();\n  }\n  const timing = timingOf(plan.track);\n  const animations: Animation[] = [];\n  for (const { dot, parts } of plan.dots) {\n    const frames = dotFrames(plan.track, dot, step);\n    for (const part of parts) {\n      const keyframes = keyframesOf(plan.track, frames, part, plan.motion);\n      if (keyframes !== undefined) {\n        const animation = part.element.animate(keyframes, timing);\n        placeAnimation(plan, state, animation, time);\n        animations.push(animation);\n      }\n    }\n  }\n  return { ...state, animations, step };\n}\n\nexport function animationPlayer(\n  scene: DotMatrixScene,\n  plan: Omit<PlayerPlan, \"track\">,\n  clear: () => void,\n): DotMatrixPlayer {\n  const full: PlayerPlan = { ...plan, track: scene.track };\n  const first: PlayerState = { animations: [], step: -1, speed: scene.speed, playing: true };\n  let state = startStep(full, first, stepOf(scene.track, scene.input));\n  return {\n    play: () => {\n      state = { ...state, playing: true };\n      for (const animation of state.animations) {\n        if (!scene.track.once) {\n          animation.startTime = 0;\n        }\n        animation.play();\n      }\n    },\n    pause: () => {\n      state = { ...state, playing: false };\n      for (const animation of state.animations) {\n        animation.pause();\n      }\n    },\n    setSpeed: (speed) => {\n      state = { ...state, speed };\n      for (const animation of state.animations) {\n        animation.updatePlaybackRate(speed);\n      }\n    },\n    setInput: (input) => {\n      const step = stepOf(scene.track, input);\n      if (step !== state.step) {\n        state = startStep(full, state, step);\n      }\n    },\n    destroy: () => {\n      for (const animation of state.animations) {\n        animation.cancel();\n      }\n      clear();\n    },\n  };\n}\n\nfunction spanIn(parent: Element, look: Declarations): HTMLSpanElement {\n  const span = parent.ownerDocument.createElement(\"span\");\n  applyLook(span, look);\n  parent.append(span);\n  return span;\n}\n\nfunction lightIn(cell: Element, look: CellLook, color: string | undefined): HTMLSpanElement {\n  const light = spanIn(cell, { ...look.light, opacity: \"0\" });\n  if (look.mark !== undefined) {\n    spanIn(light, look.mark);\n  }\n  if (color !== undefined) {\n    light.style.color = color;\n  }\n  return light;\n}\n\nfunction mountDot(\n  layer: HTMLElement,\n  scene: DotMatrixScene,\n  look: CellLook,\n  inks: readonly Ink[],\n  dot: number,\n): DotParts {\n  const { track } = scene;\n  const cell = spanIn(layer, { ...look.cell, ...placeLook(track.lattice, dot) });\n  if (look.floor !== undefined) {\n    spanIn(cell, look.floor);\n  }\n  const parts = litParts(track, inks, dot).map(({ ink, core }): DotPart => {\n    if (!core) {\n      return { element: lightIn(cell, look, ink.color), ink, core };\n    }\n    const element = spanIn(cell, { ...look.core, opacity: \"0\" });\n    if (ink.color !== undefined) {\n      element.style.color = ink.color;\n    }\n    return { element, ink, core };\n  });\n  return { dot, parts };\n}\n\nexport const domRenderer: DotMatrixRenderer = {\n  name: \"dom\",\n  mount: (layer, scene) => {\n    const { track } = scene;\n    const pitch = pitchOf(track.lattice, scene.size);\n    const look = cellLook(scene.dot, scene.glow, pitch, scene.floor);\n    const inks = inksOf(scene);\n    const dots: DotParts[] = [];\n    for (let dot = 0; dot < track.lattice.count; dot += 1) {\n      dots.push(mountDot(layer, scene, look, inks, dot));\n    }\n    return animationPlayer(scene, { dots, motion: \"individual\" }, () => {\n      layer.replaceChildren();\n    });\n  },\n};\n",
      "type": "registry:ui",
      "target": "@ui/dot-matrix-dom/dot-matrix-dom.ts"
    }
  ],
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "dot-matrix-dom",
  "title": "Dot Matrix DOM",
  "description": "Draws a Dot Matrix as spans with Web Animations, run by the compositor.",
  "registryDependencies": [
    "https://dotsengine.dev/r/dot-matrix.json"
  ],
  "meta": {
    "added": "2026-11-30"
  },
  "categories": [
    "dot-matrix"
  ],
  "type": "registry:ui"
}
