TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Show HN: Dropflow, a CSS layout engine for node or <canvas>

404 pointsby chearonabout 1 year ago
For the last 5 years I&#x27;ve been working on a layout engine that targets CSS2 and some more modern properties.<p>Live demo: <a href="https:&#x2F;&#x2F;chearon.github.io&#x2F;dropflow&#x2F;" rel="nofollow">https:&#x2F;&#x2F;chearon.github.io&#x2F;dropflow&#x2F;</a><p>It matches browsers in all cases I can find where they agree, and it&#x27;s fast. It supports `position`, `inline-block`, `z-index`, and complex properties like `float` and `vertical-align`. It doesn&#x27;t support high-level layout like flexbox or grid yet, but you can get intrinsics to easily divide space yourself and paint multiple layout trees. It has a great text layout implementation, and supporting non-Latin scripts is a top priority.<p>I&#x27;ve wanted this to exist as far back as 2013, and the desire kept coming up: for a way to get detailed intrinsics, for high quality rich text layout to canvas and SVG, and for server-side rich text. We currently use it in CellEngine for our new canvas-based spreadsheet library to layout text in hundreds of thousands of cells, and will be using it soon to render PDFs with thousands of pages in a few seconds.

35 comments

lewisjoeabout 1 year ago
This is an amazing piece of work. Thanks for making this open-source.<p>The default way of generating beautiful PDFs in the backend these days is, running a headless browser and using browser APIs to turn HTML&#x2F;CSS into PDFs. And apparently, it&#x27;s a bit costly running instances of browser in the server and scale it properly for huge workloads.<p>This is literally a game changer. Now it&#x27;s possible to design PDFs using HTML&#x2F;css and generate them without the browser overhead!
评论 #39779973 未加载
评论 #39783367 未加载
评论 #39781156 未加载
评论 #39783266 未加载
评论 #39799241 未加载
评论 #39788497 未加载
评论 #39793869 未加载
评论 #39794505 未加载
moron4hireabout 1 year ago
This sounds great.<p>In a past role, my job was to develop an immersive, online learning platform. We used Oculus Quest 2s to do foreign language training for DoD personnel. WebXR, Three.js, etc, because I had had enough Unity3D for one lifetime and we didn&#x27;t want to submit to app store reviews. We had a fleet of our own devices, so it was fine.<p>One of the biggest challenges with the project was creating a workflow for didactic content. By myself. I had an employee who I supervised working for me, but most of the work was of such high technical level that it was way over their head and we couldn&#x27;t afford to hire anyone else. I eventually landed on having our actual language instructors use PowerPoint to create PDFs, use a bespoke editor I created to upload the PDF into a content database and position them in the training environments, and then used PDFJS to render them to canvas elements to then texture on a 3D quad.<p>Something like this would have made it possible for me to avoid having people go out of band into PowerPoint to make those materials. The PowerPoint route did dramatically improve our workflow speed over a previous attempt to get people to author images in Photoshop. But if I could have built the &quot;sign&quot; editor into the app, it would have improved it even more by eliminating the &quot;guess what will look good in the environment, export to PDF, upload to the database (oh, BTW, not a lot of people know how to keep files well organized), then find out how it really looks&quot; cycle.<p>Oh well. We didn&#x27;t have a business development team or market department that knew anything about selling products instead of services, so I guess the point is moot anyway.
mattmar96about 1 year ago
Oh wonderful. Thank you! I was looking for something like this for my project <a href="https:&#x2F;&#x2F;htwins.net&#x2F;scale2" rel="nofollow">https:&#x2F;&#x2F;htwins.net&#x2F;scale2</a> and others which use svg or canvas
评论 #39786754 未加载
评论 #39786318 未加载
lovegrenobleabout 1 year ago
For anyone struggling with flexbox, you can use this tool to streamline the process of creating<p>responsive layouts, removing the need to focus on multiple properties: <a href="https:&#x2F;&#x2F;flexboxcss.com" rel="nofollow">https:&#x2F;&#x2F;flexboxcss.com</a>
评论 #39784392 未加载
评论 #39781376 未加载
评论 #39781784 未加载
webprofusionabout 1 year ago
Brilliant! It&#x27;s super important that stuff like this exists, demystifying the magic boxes of browser rendering engines.<p>It would be great if we could create a full machine readable spec for html and CSS rendering, so that renderers can be generated. Browser quirks could then be extensions to that. Like <a href="https:&#x2F;&#x2F;github.com&#x2F;tawesoft&#x2F;html5spec">https:&#x2F;&#x2F;github.com&#x2F;tawesoft&#x2F;html5spec</a> but used for real engines.
评论 #39786702 未加载
bobajeffabout 1 year ago
This sounds close to what I&#x27;ve been wondering about lately. I was wondering if css and svg could be used as abstraction over graphics and UI libraries. This is my first time hearing of node-canvas looks like it fills the drawing part of the solution. While this may do the layout portion (which is all I need from a UI library).<p>I wonder how hard it was to implement css. I&#x27;ve heard it can be pretty complex.
评论 #39779491 未加载
atoavabout 1 year ago
Truly a service to the world. I think this is a classic example of &quot;somebody ought to do $X&quot; and nobody ever having done it. Thanks.<p>As someone who loves working with CSS for layout, I am mostly relying on Flexbox and Grids these days — it is totally understandable that these are not supported yet — but do you plan to do so at some point? If so, how can others help?
评论 #39794613 未加载
eobabout 1 year ago
This is incredible --- @chearon thank you for open sourcing this!<p>I think most folks probably don&#x27;t realize how difficult it is to go from HTML -&gt; PNG programmatically. You get hit with a thousand papercuts related to either Node&lt;&gt;Browser differences or HTML&lt;&gt;Canvas differences.
评论 #39794809 未加载
big_papsabout 1 year ago
Well this looks like something useful. Can&#x27;t imaging how much work is needed to first understand css and then to build a layout engine around it.
评论 #39778931 未加载
awesomeklingabout 1 year ago
Super cool! Nice job, chearon :)
评论 #39779562 未加载
fgutmannabout 1 year ago
Great achievement, congratulations!<p>This reminds me of flying saucer, a CSS render written in pure Java. Successfully used it in multiple projects for rendering PDFs in the past. It has some great features to handle paged media. For example, it can repeat table headers on a new page, if there is a page-break within the table.<p>Unfortunately, it seems that it doesn&#x27;t get much active development anymore.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;flyingsaucerproject&#x2F;flyingsaucer">https:&#x2F;&#x2F;github.com&#x2F;flyingsaucerproject&#x2F;flyingsaucer</a>
robbiejsabout 1 year ago
This is a big achievement, congrats! A lot of time must have been put into this I am sure. And you are also making a spreadsheet product AND a PDF (preview?) product? How do you combine it?
评论 #39780347 未加载
niutechabout 1 year ago
There is also HTML&#x2F;CSS layout engine litehtml (<a href="https:&#x2F;&#x2F;github.com&#x2F;litehtml&#x2F;litehtml">https:&#x2F;&#x2F;github.com&#x2F;litehtml&#x2F;litehtml</a>) and even a full-blown web engine in JS: WebKit.js (<a href="https:&#x2F;&#x2F;github.com&#x2F;trevorlinton&#x2F;webkit.js">https:&#x2F;&#x2F;github.com&#x2F;trevorlinton&#x2F;webkit.js</a>)
评论 #39795131 未加载
LinguaBrowseabout 1 year ago
Thanks for sharing this. I’ll be checking its potential as a base element for something like NativeScript or Node.js.<p>Text is about the most complex UI element that a UI framework offers, and if you’ve got Flow Layout working, that’s very encouraging! Will be interested how far along things like gestures (particularly text highlighting) and IME integration are. In any case, kudos for opensourcing this!
Vt71fcAqt7about 1 year ago
This looks great. As you mention PDF, do you plan to support the various @page properties that add pagination? For example like pagedjs[0] but native? Major usecases are books and invoices<p>[0] <a href="https:&#x2F;&#x2F;pagedjs.org&#x2F;about&#x2F;" rel="nofollow">https:&#x2F;&#x2F;pagedjs.org&#x2F;about&#x2F;</a>
评论 #39781909 未加载
usrusrabout 1 year ago
Feels a bit like history repeating (well, rhyming) with the time back when Java was &quot;the language of web applets&quot; and then Sun created the Hotjava browser and confusion reached the point where minds simply blank out. Few technological novelties have been forgotten more decidedly than Hotjava. Despite marking an intersection of two technological fields (browsers and the JVM) that have shown extreme staying power through the last quarter century.<p>But the idea of using html for text styling has stuck, Swing UI do text styling with html (and rudimentary css!) to this day. Html would not be a lingua franca if its use was limited to the equivalent of native speakers (browsers).
YorickPeterseabout 1 year ago
How suitable would this be for a text editor of sorts, and what latency would one expect? Using the demo it&#x27;s difficult to gauge the latency by just eyeballing it.
justhwabout 1 year ago
This looks great. I&#x27;ve been using html2canvas but it doesn&#x27;t support the &#x27;filter&#x27; property yet. This will be an awesome alternative.
joelanmanabout 1 year ago
this wouldn&#x27;t be accessible would it? For example to screen readers, so just wondering what the use case is over accessible tech
jasonjmcgheeabout 1 year ago
Are you planning to add support for the missing standard tags like img and table (both very useful for pdf rendering)?
评论 #39779931 未加载
vthommeretabout 1 year ago
Does anyone have a similar solution for drawing graphs &#x2F; charts in a Node environment without a browser dependency? Last time I explored this I couldn&#x27;t find any good solutions.
评论 #39783740 未加载
评论 #39784502 未加载
评论 #39785821 未加载
andrewmcwattersabout 1 year ago
The layout algorithm for CSS 2.1 is not well defined. How did you interpret the algorithm? I may be missing it from a quick glance, but it’s not obvious from your code.
AndriyKunitsynabout 1 year ago
I wonder if there’s something opposite — an abstraction for the Web to provide some layout rules that are more sane than CSS.
wg0about 1 year ago
This is pure craftsmanship. A heavy undertaking TBH, kind of reinventing browser flow on top of browser primitives.
XCSmeabout 1 year ago
Could I use this together with Pixi.JS ?
pietroppeterabout 1 year ago
I would be very much interested in articles on how to write a CSS layout engine from scratch
评论 #39780034 未加载
评论 #39785232 未加载
foreignerabout 1 year ago
What is the API to produce SVG?
1oooqooqabout 1 year ago
why not ship a desktop application at this point with support for a special schema?<p>i can understand the abuse for server side rendering, but at this poit you pretty much have a bad browser engine. running inside a 2d hack in a browser engine. sigh.
评论 #39779679 未加载
mwit2023about 1 year ago
will give it a try for opengraph images, thank you!
rrgokabout 1 year ago
Perhaps I&#x27;m missing something, but why can&#x27;t we render HTML directly onto the canvas in the browser? The parser is there, the layout engine is already implemented, and the calculation of box layout is already done. It should be doable without going in circles. If only the browser had a flag indicating which &quot;surface&quot; to render on.<p>I was looking at Glide Grid the other day, and it renders so fast, even with 1 million rows; it&#x27;s somehow responsive. There should be an easier way to render HTML to canvas without resorting to low-level primitives. Why is canvas faster than the &quot;regular&quot; DOM renderer?
评论 #39781731 未加载
评论 #39782002 未加载
评论 #39781738 未加载
评论 #39794971 未加载
评论 #39781668 未加载
评论 #39781933 未加载
shepherdjerredabout 1 year ago
I&#x27;ve used satori [0] on the backend with TypeScript&#x2F;Deno to render React JSX + tailwind CSS as an SVG (which is then rendered to a PNG). Of course you could use another flavor of JSX (or even plain HTML) or omit tailwind, but it&#x27;s really cool that you can use the same stack as a typical frontend and render it as an image.<p>Satori is meant for rendering Open Graph images (e.g. the little images that come up when you post a link on Twitter&#x2F;Slack&#x2F;Facebook), but I found that it works well for rendering arbitrary images. Satori has no native dependencies, so it kinda &quot;just works&quot; on the backend. It supports a subset of modern CSS, including flexbox.<p>My use case is posting match reports for League of Legends into a Discord text channel, e.g. person X just played a match, here are their stats.<p>It&#x27;s quite nice because there are almost zero server-side native dependencies (the one exception is the library to convert svg -&gt; png requires some native libraries).<p>Here&#x27;s what a match report looks like: [1]<p>Here&#x27;s an example of what the JSX looks like: [2]<p>I also built a small project [3] that renders the JSX in a browser to make developing the images just as easy as developing a normal website.<p>If others are interested in this, I would by happy to write a blog post about the process.<p>[0]: <a href="https:&#x2F;&#x2F;github.com&#x2F;vercel&#x2F;satori">https:&#x2F;&#x2F;github.com&#x2F;vercel&#x2F;satori</a><p>[1]: <a href="https:&#x2F;&#x2F;github.com&#x2F;shepherdjerred&#x2F;glitter&#x2F;blob&#x2F;main&#x2F;assets&#x2F;postmatch.png">https:&#x2F;&#x2F;github.com&#x2F;shepherdjerred&#x2F;glitter&#x2F;blob&#x2F;main&#x2F;assets&#x2F;p...</a><p>[2]: <a href="https:&#x2F;&#x2F;github.com&#x2F;shepherdjerred&#x2F;glitter&#x2F;blob&#x2F;main&#x2F;packages&#x2F;backend&#x2F;src&#x2F;league&#x2F;image&#x2F;html&#x2F;report.tsx">https:&#x2F;&#x2F;github.com&#x2F;shepherdjerred&#x2F;glitter&#x2F;blob&#x2F;main&#x2F;packages...</a><p>[3]: <a href="https:&#x2F;&#x2F;github.com&#x2F;shepherdjerred&#x2F;glitter&#x2F;tree&#x2F;main&#x2F;packages&#x2F;preview">https:&#x2F;&#x2F;github.com&#x2F;shepherdjerred&#x2F;glitter&#x2F;tree&#x2F;main&#x2F;packages...</a>
评论 #39781944 未加载
eigenvalueabout 1 year ago
I worry that this sort of technology is going to lead to webapps that aren&#x27;t really what we&#x27;ve grown to think of as &quot;the web&quot; but which will instead be closer to a VNC window where you just see pixels and can&#x27;t interact with anything or see how stuff works in devtools.
评论 #39780016 未加载
评论 #39779988 未加载
评论 #39780210 未加载
评论 #39784158 未加载
评论 #39780359 未加载
评论 #39785517 未加载
评论 #39780313 未加载
评论 #39781953 未加载
ape4about 1 year ago
Its too bad browsers don&#x27;t support this.<p><pre><code> &#x2F;&#x2F; Do CSS layout in the regular document const doc = new CssLayout(document); doc.layout(...); &#x2F;&#x2F; Do CSS layout on your own canvas const mycanvas = new CssLayout(document.getElementById(&#x27;mycanvas&#x27;)); mycanvas.layout(...);</code></pre>
评论 #39785269 未加载
评论 #39779337 未加载
peer2payabout 1 year ago
I have nostalgia for a lot of things in webdev, hacking together floating layouts is not one of them. &#x2F;s<p>But congrats on the work! I can definitely see this being useful for text formatting and layouting PDFs. Neat!
lloydatkinsonabout 1 year ago
It doesn’t support flex box? I really can’t tell what the purpose of this library is then.
评论 #39786303 未加载
评论 #39786399 未加载