Skip to content
SnapDOMTry SnapStudioGitHub★ 8K
Labs Experiment

Every shot hits the clone

A shooting gallery where the board is a PNG. Each shot re-captures on top of the shots before it; the live target in the DOM stays clean.

TL;DR

A plugin's afterRender hook saves the rendered clone; afterClone swaps it back in on the next capture with a new bullet hole appended. The accumulation lives in the plugin's closure.

What you're looking at

Ten shots, a swaying crosshair, a ring-scored target. The board is one <img>, replaced after every shot with a new snapdom.toPng() result.

Try it

Aim, click to fire. The reticle drifts and kicks after every shot, and the drift grows as ammo runs out. The marker that appears is the exported PNG.

0Score
10Shots
0Best

Ready?

10 shots. The reticle sways and kicks with every shot, so compensate for the drift.

The download button uses the <img> returned by the last snapdom.toPng() call; no second capture.

How it works

  1. A hidden source element

    A 340×340 <div> off-screen holds one SVG target built from a ring definition. SnapDOM captures this element, not the visible board.

  2. The shot is scored locally

    On click, the reticle's position after sway and recoil (not the raw mouse position) is converted to a percentage and scored against the ring radii. The resulting { xPct, yPct, color } goes to plugin.addHit().

  3. addHit() appends a marker to the saved clone

    On the first shot the plugin clones the live source once. Each call appends a jagged, randomized torn-paper hole, positioned in percentage coordinates so it survives the render round trip.

  4. snapdom.toPng() runs again, plugin included

    The demo calls snapdom.toPng(target, { plugins: [plugin] }) again, same as the first shot.

  5. afterClone swaps in the saved clone

    The plugin replaces ctx.clone with the clone it has been building, holes included, instead of the hole-free source.

  6. afterRender checkpoints the result

    The plugin clones the rendered result as the baseline for the next shot.

  7. The output <img> replaces the board

    The PNG goes into the board.

Why this needed a plugin

SnapDOM 3 can reuse eligible captures, but stateful hooks run on fresh captures and the accumulated holes belong to the plugin. afterClone and afterRender share one closure with addHit(): it adds the mark to savedClone, afterClone swaps that clone in, afterRender saves the result for the next call, independent of capture reuse.

Copy-paste example

The plugin factory, usable for any "keep building on the last capture" case:

function createHitboardPlugin (sourceEl) {
  let savedClone = null

  return {
    name: 'hitboard',
    afterClone (ctx) {
      if (savedClone) ctx.clone = savedClone.cloneNode(true)
    },
    afterRender (ctx) {
      savedClone = ctx.clone.cloneNode(true)
    },
    addHit (xPct, yPct, color) {
      if (!savedClone) savedClone = sourceEl.cloneNode(true)
      if (getComputedStyle(savedClone).position === 'static') {
        savedClone.style.position = 'relative'
      }
      const hit = document.createElement('div')
      hit.style.cssText = `
        position: absolute; left: ${xPct}%; top: ${yPct}%;
        width: 24px; height: 24px; margin: -12px 0 0 -12px;
        pointer-events: none;
      `
      hit.innerHTML = bulletHoleMarkup(color)
      savedClone.appendChild(hit)
    },
    reset () { savedClone = null }
  }
}

The usage: score the shot, tell the plugin, capture again:

const plugin = createHitboardPlugin(target)

function shoot (xPct, yPct) {
  const { color } = scoreAt(xPct, yPct)
  plugin.addHit(xPct, yPct, color)
  return snapdom.toPng(target, { plugins: [plugin] })
}

// on restart: plugin = createHitboardPlugin(target) — a fresh closure,
// not plugin.reset(), so a stale reference from the finished game can't
// leak a hole into the new one.
shot addHit() snapdom.toPng() afterClone (restore + append) afterRender (checkpoint) <img>

Limits and things to watch

addHit() initializes savedClone from the live source on first call, so the plugin needs no priming. reset() sets it back to null and the next capture re-clones the pristine source. Simpler than Paint Canvas's skipNext flag: a restart is a hard reset, not a stroke boundary to detect mid-stream.

Hook contracts and custom exports: plugin API and PLUGIN_SPEC.md.