r/GraphicsProgramming • u/berkantdev • 19d ago
Source Code What I learned drawing 5,000 live DOM screens with WebGPU through Chrome's HTML-in-Canvas
there's barely anything written about Chrome's new HTML-in-Canvas API yet, so here's what I hit building gpucanvas, an infinite canvas where every node is real, live DOM (React screens, forms, charts) but WebGPU draws it. you can still click and type into any of them.
the pipeline
- nodes are packed into 2048 px "pages". Chrome records a page as one snapshot, so a single
drawElementImageToTexturecall rasterizes dozens of nodes into a texture-array atlas - once drawn, a page is parked with
content-visibility: hidden. layout stays, paint is skipped, so it costs nothing per frame - every node keeps a 1/16 thumbnail that's never evicted, so the zoomed-out view of 5,000 screens is always filled. sharper snapshots go on top at power-of-two scales, so zooming reuses them
- past 100% zoom only the visible part is rasterized, as tiles, center first
- each frame is a cull plus one instanced quad draw (one per run of nodes sharing a WGSL effect). panning and zooming never touch the DOM
what surprised me in Chrome 157 (some of it differs from the WICG explainer)
- each
drawElementImageToTexturecall allocates and fully rasterizes. I measured roughly 250 to 550 calls a second in total, and that's the real fill-rate limit, not compositing. hence the pages - a source rect still rasterizes the whole element before cropping, so zoomed in, a node moves to a page of its own
- recording cost scales with how many elements are drawable on every
requestPaint. 10,000 always-drawable nodes sat at 22 fps doing nothing. parking fixed it - hit testing only works after
canvas.updateElementGeometry(el, {}), and the hit area is the element's CSS box, so I move it with a transform fromgetElementTransform - an element that just became drawable isn't in
changedElements, its snapshot shows up a paint later
on an M-series Mac (Chrome 157, 1600x900, DPR 1) a 30 s scripted tour with typing, a 500% zoom and selection runs 113 to 115 fps at 5,000 screens, vs 18 to 20 fps for the same screens as plain DOM in one transformed container.
still open: text is rasterized, not SDF, so mid-zoom it goes soft for a moment until the tiles redraw. and Tab can't reach parked nodes yet, I need a proper focus model. curious how others would handle either.
code (MIT): github.com/berkantay/gpucanvas
demo: berkantay.github.io/gpucanvas (needs chrome://flags/#canvas-draw-element)
AI disclosure: I built this with Claude Code as my coding assistant, and it helped me draft this post.
•
u/AutoModerator 19d ago
Recently updated rules. Please check that your post is in compliance with Rules 1, 4, 5, and 6 on the sidebar. TL;DR:
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.