A Browser-Only Image Diff Heatmap With Canvas

2026-12-03来源 dev.toEnglish

When you edit an image, crop, retouch, remove an object, it is useful to see exactly which pixels changed. Desktop tools can do this, but a tiny in-browser diff viewer is handy for reviews, documentation and QA of image pipelines. Here is one in plain JavaScript with the Canvas API: no libraries, no uploads, everything stays on the user's machine.

Load both images into canvases

async function toImageData(file, w, h) { const bmp = await createImageBitmap(file); const c = new OffscreenCanvas(w ?? bmp.width, h ?? bmp.height); const ctx = c.getContext('2d'); ctx.drawImage(bmp, 0, 0, c.width, c.height); return ctx.getImageData(0, 0, c.width, c.height); } 

Both images must have the same dimensions. Load the first one at its natural size and force the second to match, otherwise every pixel will look different.

Compute a per-pixel difference

function diffHeatmap(a, b, threshold = 12) { const out = new ImageData(a.width, a.height); let changed = 0; for (let i = 0; i < a.data.length; i += 4) { const d = (Math.abs(a.data[i] - b.data[i]) + Math.abs(a.data[i + 1] - b.data[i + 1]) + Math.abs(a.data[i + 2] - b.data[i + 2])) / 3; if (d > threshold) changed++; const v = Math.min(255, d * 4); // amplify small changes out.data[i] = v; // red channel = change out.data[i + 1] = 0; out.data[i + 2] = 255 - v; // blue = unchanged out.data[i + 3] = 255; } return { out, ratio: changed / (a.width * a.height) }; } 

The threshold matters. JPEG re-compression changes almost every pixel by a few levels, so a threshold around 10-15 ignores compression noise and highlights real edits.

Show it

const { out, ratio } = diffHeatmap(imgA, imgB); const canvas = document.querySelector('#heatmap'); canvas.width = out.width; canvas.height = out.height; canvas.getContext('2d').putImageData(out, 0, 0); label.textContent = `${(ratio * 100).toFixed(2)}% of pixels changed`; 

Red areas changed, blue areas did not. A small, compact red patch means a local edit; red speckle everywhere means the whole image was re-encoded or resized.

Make it a slider instead

Heatmaps are precise, but people understand a before/after slider faster. Stack the two images, clip the top one with clip-path: inset(0 calc(100% - var(--pos)) 0 0) and drive --pos from a range input. Offer both views: the slider to show what changed, the heatmap to prove nothing else did.

For a real-world reference of that presentation, GGemini's before-and-after gallery uses comparison sliders to show local edits on AI-generated images, which is exactly the case where a diff heatmap should come back mostly blue with one small red region.

Performance notes

  • A 12-megapixel image is 48 MB of RGBA data. Run the loop in a Web Worker for large files.
  • Downscale both images to 1,000 pixels wide for a quick preview, then compute full size on demand.
  • OffscreenCanvas is available in workers, so the whole pipeline can leave the main thread.

Forty lines of code, zero dependencies, and you have an honest answer to the question every image reviewer asks: what exactly did this edit change?

原文:https://www.a8q9.cn/en/article/a-browser-only-image-diff-heatmap-with-canvas-5d5353.html

相关文章