I wanted a way to turn code snippets into clean, shareable images for RapidKit Tools — the kind of card you see in technical blog posts and on X. Plain screenshots look inconsistent: wrong fonts, no padding, a window frame that doesn't match anything. So I built a Code to Image tool with one rule carried over from my other tools: the code never leaves the browser.
I didn't write the rendering engine. The tool stands on two libraries: react-syntax-highlighter (Prism under the hood) for tokenizing and coloring, and html-to-image for turning the rendered DOM into a PNG. Reading how the second one actually works explained a few things that had been quietly confusing me.
How the export actually works
html-to-image doesn't take a screenshot. It serializes the target DOM subtree into an SVG , inlines the computed styles and assets, then draws that SVG onto an in-memory and reads the pixels back out. That's why the whole thing runs client-side with no server and no upload — it's just the browser rendering its own DOM into a bitmap.
It also explains the tool's most important gotcha, which I'll get to in a moment.
The "2x" is a fixed multiplier, not your device
The export calls toPng(node, { pixelRatio: 2, ... }). That 2 is hardcoded — it is not window.devicePixelRatio. The upside is that even on a 1x monitor you get a genuinely supersampled, crisp image. The honest downside: on a 3x phone screen the export is still 2x, not 3x. I chose the fixed multiplier on purpose so output is predictable across devices, but it's a trade-off, not magic.
The export is not always the preview
Because the rasterization goes through an SVG foreignObject, it only understands what that path can render. Two CSS features I use in the preview don't survive it:
backdrop-filter (my "frosted glass" background). The blur you see live is simply not rendered in the exported PNG — it comes out flat. The preview and the file genuinely disagree here.
Large box-shadow glows. The canvas is sized to the element's bounding box, so if padding is small and the glow blur is large, the shadow gets clipped at the image edge.
Neither is a bug in my code so much as a limit of the rendering path. Knowing it changed how I describe the tool: the preview is a guide, not a promise.
What I got wrong
I requested a font I never loaded. My code asked for "JetBrains Mono" first in the font stack, but I never actually loaded that font anywhere in the app. So on Windows it silently fell back to Consolas, on macOS to Monaco, and the exported image looked different per platform while I assumed it was consistent. The fix was to stop asking for a font I don't ship: I switched to the monospace font the app already loads (Geist Mono), and I now await document.fonts.ready before exporting so the raster never races font loading. The lesson: a font-family string is a request, not a guarantee.
I shipped the same Safari clipboard bug twice. In my Signature Pad tool I'd already learned that await-ing image generation before navigator.clipboard.write() burns the user-gesture token in WebKit, so the write rejects. Then I found the exact same pattern in this tool: await toBlob(...) followed by clipboard.write, with only a console.error on failure. The fix is the same one that worked before — pass the Promise straight into new ClipboardItem({ "image/png": blobPromise }) so the write is issued inside the gesture, and show a visible "Copy Failed" state instead of failing silently. I have to be honest about one thing: I don't own a Safari device, so I couldn't verify this on real WebKit. I applied the documented pattern and added the error state as a safety net, but if you're on iPhone or Mac and try the copy button, I'd really like to know whether it works for you.
Limits worth knowing
The frosted-glass blur and edge-clipped glows above are rendering-path limits, not settings you can toggle away.
Prism wraps every token in its own . A 1,000-line snippet becomes tens of thousands of DOM nodes, and html-to-image walks computed styles on each — export can jank the main thread on very large inputs.
The library uses the DOM you see; there's no hidden clone, so what's in the preview node is what gets rasterized (minus the unsupported CSS).
The code and the export stay on-device. The site does load analytics for page views, so I only make that claim about the snippet and the image, not the whole page.
Try it
RapidKit Code to Image is free, no sign-up.
If you've built DOM-to-image export before, I'd like to hear how you handle the foreignObject gaps — and especially whether the clipboard copy behaves on Safari, since that's the one thing I couldn't test myself.

Top comments (0)