r/javascript • u/lavrton • Mar 31 '26
I built the fastest way to render rich text on canvas 5x faster than SVG foreignObject
https://polotno.com/render-tag/
44
Upvotes
r/javascript • u/lavrton • Mar 31 '26
11
u/lavrton Mar 31 '26
Demo: https://polotno.com/render-tag/
GitHub: https://github.com/polotno-project/render-tag
I work on a design editor (Polotno) and rendering rich text on canvas has been one of the biggest pain points. The standard approach - SVG foreignObject works, but it is slow. The SVG path forces the browser to re-serialize and re-rasterize the font data, even though it's already loaded in the page.
So I built render-tag. It takes HTML/CSS rich text and draws it directly onto canvas with the 2D API. No SVG. No DOM cloning. Just parse -> resolve styles -> measure text -> draw.
The result: 1ms per render vs 5ms for snapdom (the fastest SVG foreignObject library I tested). The difference will be even bigger if a large font is used. The canvas approach wins because it uses fonts that are already in the page context no serialization overhead.
What it handles:
- Bold, italic, underline, strikethrough, text shadows, gradients
- Multiple fonts, sizes, colors, backgrounds
- Ordered/unordered lists with nesting
- RTL text, letter spacing, line height
It's not a full browser engine. Just the subset you need for rich text. That constraint is what keeps it fast.
Would love feedback from anyone dealing with canvas text rendering.
2
u/ferrybig Mar 31 '26
The demo is way too slow.
Once I got on the page on mobile, I saw a html input and a canvas with a large white area at the bottom of the page.I was scrolling up and down seeing what the project in question was until suddenly the rest of the UI loaded. I was about to press back and say it doesn't work.
30 seconds before the demo is shown is too long, if you choose not use the loading bar of the browser by using client side rendering, add a loader to your app
2
u/33ff00 Apr 01 '26
How difficult would it be to get this to work with selection ranges for copy/paste etc?
4
u/noobmeister_69 Mar 31 '26
Looks pretty cool, great work mate! Took a brief look at the demo, looks decent on mobile.
4
u/jiayihu Mar 31 '26
Nice project. I work in a canvas company, I’d be curious to see a benchmark where it renders 1000 different texts (with unique layouts/texts). That would be a more realistic scenario, representing for instance a medium sized project in Miro or Figma. Either way that being said, well done! I know very well how painful it is working with text and canvas.
1
u/TheThingCreator Mar 31 '26
How does your tool compare against html2canvas and others in terms of accuracy. Like the last time i tested it, it was way off in terms of accuracy. Lots of missing css, background and glows not getting rendered correctly. That (accuracy) matter to me a lot more than speed. Got any insight on that?
1
u/lionep Mar 31 '26
Does it work in nodejs with node-canvas?
1
u/TheThingCreator Mar 31 '26
i don't know what "5x faster than SVG foreignObject" means in context to rich text rendering on canvas, but is this different from https://github.com/niklasvh/html2canvas?
EDIT: Oh its that its faster than html2canvas, and alike?
2
u/25_vijay May 16 '26 edited May 17 '26
This is one of those optimizations users don’t notice directly but massively improves UX under the hood, and Runable is useful for documenting these performance improvements across versions