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.
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.
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
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.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 toplugin.addHit().addHit()appends a marker to the saved cloneOn 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.
snapdom.toPng()runs again, plugin includedThe demo calls
snapdom.toPng(target, { plugins: [plugin] })again, same as the first shot.afterCloneswaps in the saved cloneThe plugin replaces
ctx.clonewith the clone it has been building, holes included, instead of the hole-free source.afterRendercheckpoints the resultThe plugin clones the rendered result as the baseline for the next shot.
The output
<img>replaces the boardThe 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.
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.