Skip to content

Commit 0b3e72a

Browse files
committed
Raster: Download PNG link
canvas.toBlob('image/png') re-encodes the rendered buffer. Mirrors the Vector tab's SVG/PDF download UX (button + size tag), wired up after every render. Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent c50709c commit 0b3e72a

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

index.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,11 @@ <h3>Per-table breakdown</h3>
319319
<a href="https://harfbuzz.github.io/integration-freetype.html">hb-ft</a>
320320
if you need hinted output at small sizes.</p>
321321
<div class="render"><canvas id="raster-canvas"></canvas></div>
322+
<div class="controls demo-controls">
323+
<a id="raster-dl-png" href="#" download="text.png">
324+
Download PNG <span class="size-tag" id="raster-png-size"></span>
325+
</a>
326+
</div>
322327
<p class="raster-stats" id="raster-stats"></p>
323328
</section>
324329

js/app.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,9 @@
373373
const rasterCanvas = document.getElementById ("raster-canvas");
374374
const rasterCtx = rasterCanvas.getContext ("2d");
375375
const rasterStats = document.getElementById ("raster-stats");
376+
const rasterDl = document.getElementById ("raster-dl-png");
377+
const rasterPngSize = document.getElementById ("raster-png-size");
378+
let rasterPngUrl = null;
376379
function renderRaster () {
377380
withText ((textPtr) => {
378381
const wPtr = Module._malloc (4);
@@ -412,6 +415,16 @@
412415
w + " × " + h + " px"
413416
+ " (" + (w / dpr).toFixed (0) + " × " + (h / dpr).toFixed (0) + " CSS px"
414417
+ " · " + dpr + "× DPR)";
418+
/* Re-encode the canvas as PNG for the download link.
419+
* toBlob is async; the previous URL is revoked once the
420+
* new one lands so we don't leak. */
421+
rasterCanvas.toBlob ((blob) => {
422+
if (!blob) return;
423+
if (rasterPngUrl) URL.revokeObjectURL (rasterPngUrl);
424+
rasterPngUrl = URL.createObjectURL (blob);
425+
rasterDl.href = rasterPngUrl;
426+
rasterPngSize.textContent = fmtBytes (blob.size);
427+
}, "image/png");
415428
});
416429
}
417430

0 commit comments

Comments
 (0)