News for hackers. Stuff that matters.
Re:Show HN: I created snapDOM to capture DOM nodes as images... by streptomycin on Sunday April 27, 2025 @01:30PM (#43813611)
I've been using html2canvas for a long time in https://play.basketball-gm.com/ so I gave your library a try. It was much slower (I know your README has benchmarks saying the opposite so idk) and the result looked a lot worse.

html2canvas: https://i.imgur.com/zfSwNR1.png

snapdom: https://i.imgur.com/FxowTzp.png

Also I recommend putting the npm package name clearly in your README. I guess I don't really know what other people are doing these days, but I think most people are like me and consume packages like this from npm rather than a <script> tag.

Re:Show HN: I created snapDOM to capture DOM nodes as images... by tinchox6 on Sunday April 27, 2025 @01:44PM (#43813761)
Thank you for testing it out and sharing the screenshots!

I’ve run some performance tests using Vitest Bench, and SnapDOM was faster. I also created a few manual demos, and SnapDOM won in both speed and accuracy.

That said, I still need to run more real-world tests. So, thanks again for your help!

Re:Show HN: I created snapDOM to capture DOM nodes as images... by hirako2000 on Sunday April 27, 2025 @03:10PM (#43814341)
How can svg be faster, and especially more accurate (lets throw some IMG or CSS shadows for fun) than canvas?
Re:Show HN: I created snapDOM to capture DOM nodes as images... by kaoD on Sunday April 27, 2025 @03:41PM (#43814538)
You can embed HTML in an SVG via foreignObject. Seems like that's exactly what the library is doing[0].

Whether that's faster or more reliable, I don't know. Seems likely to render different depending on where you view the SVG (especially out of browsers).

[0]: https://github.com/search?q=repo%3Azumerlab%2Fsnapdom%20fore...

Re:Show HN: I created snapDOM to capture DOM nodes as images... by tinchox6 on Monday April 28, 2025 @03:49PM (#43825314)
I'm working on enabling a special full mode for those who don't mind the final size:

https://github.com/zumerlab/snapdom/issues/1

Re:Show HN: I created snapDOM to capture DOM nodes as images... by tinchox6 on Sunday April 27, 2025 @02:02PM (#43813883)
I added to README the npm / yarn reference. Thanks!
Re:Show HN: I created snapDOM to capture DOM nodes as images... by tinchox6 on Wednesday April 30, 2025 @09:10AM (#43844742)
If interested I solved almost all issues you pointed out (speedy and accuracy).

https://github.com/zumerlab/snapdom/issues/3

Re:Show HN: I created snapDOM to capture DOM nodes as images... by rs186 on Sunday April 27, 2025 @04:13PM (#43814739)
Re:Show HN: I created snapDOM to capture DOM nodes as images... by tinchox6 on Sunday April 27, 2025 @04:23PM (#43814820)
It is pretty the same idea. There are many good solutions like dom-to-imge-more: the battle tested html2canvas, dom-to-image, modern-screenshot, etc. SnapDom is focused on avoiding long-taks whenever is possible because was designed to a zoomble UI engine that needs the capture doesnt affect the transition. But this is the first public version and there many things to adjust.
Re:Show HN: I created snapDOM to capture DOM nodes as images... by jdiff on Sunday April 27, 2025 @01:41PM (#43813740)
Capturing HTML as scalable SVGs is huge, how do you manage condensing all of CSS and its quirks into an SVG? Do you only support a subset of styling properties and rely on the browser to calculate layouts for you?
Re:Show HN: I created snapDOM to capture DOM nodes as images... by tinchox6 on Sunday April 27, 2025 @01:47PM (#43813788)
I was upset about the size of generated svg file because at first all styles were inlined in each element. So I created a function to make mini css classes (.c1, c2, c3,...) So the final size is quite small.
Re:Show HN: I created snapDOM to capture DOM nodes as images... by darrenf on Sunday April 27, 2025 @03:40PM (#43814523)
I’ve been writing a browser extension recently, and part of the functionality I want is to export part of a view as an image, shareable for the socials. I wanted to use html2canvas, but the docs explicitly advise against using it in a browser extension. Would snapDOM be suitable for inclusion in an extension?
Re:Show HN: I created snapDOM to capture DOM nodes as images... by tinchox6 on Sunday April 27, 2025 @04:34PM (#43814892)
Sincerely, I don't know if snapdom would work on a browser extension. I've never tested it on this scenery
Re:Show HN: I created snapDOM to capture DOM nodes as images... by genewitch on Monday April 28, 2025 @04:30AM (#43818938)
Firefox has screenshot that auto-picks elements, or you can click and drag a crop, or you can save the whole page. That and ad nauseam are the reason I use firefox; that singlefile also works is great (single html file dump of a web page, somehow).
Re:Show HN: I created snapDOM to capture DOM nodes as images... by braebo on Sunday April 27, 2025 @03:44PM (#43814555)
I found puppeteer or playwright to be good at this with their screenshot method. I made a cli tool for this recently that worked quite well.
Re:Show HN: I created snapDOM to capture DOM nodes as images... by tinchox6 on Sunday April 27, 2025 @03:45PM (#43814566)
Yes they are so good. But in my case I need to work only on the client side
Re:Show HN: I created snapDOM to capture DOM nodes as images... by ashishact on Sunday April 27, 2025 @03:20PM (#43814405)
Image in the readme would really be helpful. In fact anytime there is a visual output it makes sense to put an image. Thanks for creating this though - Will give it a try for an upcoming project.
Re:Show HN: I created snapDOM to capture DOM nodes as images... by tinchox6 on Sunday April 27, 2025 @04:35PM (#43814896)
Thank you for the suggestion!
Re:Show HN: I created snapDOM to capture DOM nodes as images... by maxloh on Sunday April 27, 2025 @02:42PM (#43814143)
Maybe add a function to convert to PDF? I've always dreamed of converting an element or the document body to a long-scrolling PDF. I tried to implement it with Playwright Python, but I had no luck. The resulting PDF height is messed up.
Re:Show HN: I created snapDOM to capture DOM nodes as images... by tinchox6 on Sunday April 27, 2025 @04:29PM (#43814857)
Its a bit out of scope right now, but I thing it would be possible using some external libraries such as jsPDF or svg2pdf.js
Re:Show HN: I created snapDOM to capture DOM nodes as images... by krebby on Sunday April 27, 2025 @01:36PM (#43813674)
How does this compare to something like the the Media Capture API? Looks like this uses `canvas.toDataURL()` which can be slow to serialize compared to `toBlob` or `canvas.captureStream(0).getVideoTracks()`

I've been using CropTarget.fromElement with a CaptureController: https://gist.github.com/akre54/e93ab2ce27999aecb109e38085f2e...

Re:Show HN: I created snapDOM to capture DOM nodes as images... by tinchox6 on Sunday April 27, 2025 @02:25PM (#43814029)
Hi! Insaw this API when I was starting to work on snapDOM but I thought it was not ok for my needs so I discarded it. Regarding other API it would be great if View Transitions API could exposed the old view in a future
Re:Show HN: I created snapDOM to capture DOM nodes as images... by krebby on Sunday April 27, 2025 @02:40PM (#43814136)
What were your needs that the API didn't meet?
Re:Show HN: I created snapDOM to capture DOM nodes as images... by tinchox6 on Sunday April 27, 2025 @03:58PM (#43814650)
It requires user intervention and doesn't work for me.
Re:Show HN: I created snapDOM to capture DOM nodes as images... by streptomycin on Sunday April 27, 2025 @01:58PM (#43813858)
Looks cool, somebody should make a package that puts a nice API around it. However seems that those APIs are only in Chrome now, not Firefox or Safari.
Re:Show HN: I created snapDOM to capture DOM nodes as images... by krebby on Sunday April 27, 2025 @02:49PM (#43814202)
IMO it's simple enough to just write directly without library code.

For most cases, the Element Capture or Region Capture API should be sufficient: https://developer.mozilla.org/en-US/docs/Web/API/Screen_Capt...

Demos: https://mdn.github.io/dom-examples/screen-capture-api/elemen... https://mdn.github.io/dom-examples/screen-capture-api/region...

If you need cross-browser compatibility (and can't just use a canvas) then yeah html2canvas is the way to go

Re:Show HN: I created snapDOM to capture DOM nodes as images... by streptomycin on Sunday April 27, 2025 @09:12PM (#43816586)
idk, your code is 170 lines, html2canvas and similar are 1 line to use.
Re:Show HN: I created snapDOM to capture DOM nodes as images... by tinchox6 on Sunday April 27, 2025 @04:36PM (#43814902)
I tested it on Chrome, Firefox and Safari
Re:Show HN: I created snapDOM to capture DOM nodes as images... by badmonster on Sunday April 27, 2025 @01:31PM (#43813622)
Does snapDOM support capturing elements with CSS animations in their current frame, or does it only capture static states?
Re:Show HN: I created snapDOM to capture DOM nodes as images... by tinchox6 on Sunday April 27, 2025 @01:49PM (#43813802)
Yes it capture elements with css animations in theirr current frame. It isnt't work for animted gifs. And I have to test js animations engines
Re:Show HN: I created snapDOM to capture DOM nodes as images... by nikeee on Sunday April 27, 2025 @02:26PM (#43814037)
Does it work with some DOM polyfill in Node.js?
Re:Show HN: I created snapDOM to capture DOM nodes as images... by tinchox6 on Sunday April 27, 2025 @02:28PM (#43814044)
Not tested yet
Re:Show HN: I created snapDOM to capture DOM nodes as images... by andrewstuart on Sunday April 27, 2025 @02:01PM (#43813871)
Screenshots on the GitHub would be great.
Re:Show HN: I created snapDOM to capture DOM nodes as images... by Eduard on Sunday April 27, 2025 @02:08PM (#43813910)
Not only screenshots, but also actual results. As I understand the short description, this tool allows to transform a website's current visual state into an SVG.
Re:Show HN: I created snapDOM to capture DOM nodes as images... by andrewstuart on Sunday April 27, 2025 @02:03PM (#43813890)
Why not do it at the back end where you can literally snapshot node to png.
Re:Show HN: I created snapDOM to capture DOM nodes as images... by simonw on Sunday April 27, 2025 @08:38PM (#43816417)
Tiny feature request: snapdom.toJpg(el) appears to return a JPEG image where the background color for any transparent areas is set to black - it would be useful to be able to set that to another color (I needed it to be white).
Re:Show HN: I created snapDOM to capture DOM nodes as images... by tinchox6 on Sunday April 27, 2025 @11:59PM (#43817396)
No problem! May you open an issue on the repo?
Re:Show HN: I created snapDOM to capture DOM nodes as images... by simonw on Monday April 28, 2025 @01:59AM (#43818082)
Re:Show HN: I created snapDOM to capture DOM nodes as images... by G_o_D on Sunday April 27, 2025 @10:28PM (#43816972)
It doesnt load, snapdom dont appear in my devtools had to copy paste script

Plus it just struck at pending promise dont work

Plus css is messed up, i cant call it a snapshot, if it dont look same

domtoimage lib works for me and is fast whole html body node captured within second

Re:Show HN: I created snapDOM to capture DOM nodes as images... by imvetri on Monday April 28, 2025 @07:05AM (#43819875)
How does it work
Re:Show HN: I created snapDOM to capture DOM nodes as images... by Sephr on Monday April 28, 2025 @10:57AM (#43822199)
Awesome! I was making exactly this library 13 years ago but only made it as far as creating CSS and other asset inlining+normalization tools before I ran into a few major roadblocks.

The most significant roadblock was that drawing SVG <foreignObject> with inline subresources (data: and blob: URIs) tainted canvases in Chrome and Safari, and this was partially resolved in 2019[1].

Notably, some issues still remain but apparently it's now workable. As is apparent from snapDOM, completely accurate CSS normalization is still an unsolved problem.

While I'm excited to see the HTML-in-foreignObject use case mature, it is also important to point out that the Media Capture API now has the ability to capture individual elements natively in Chrome[2].

1. https://issues.chromium.org/issues/41054640#comment49

2. https://developer.mozilla.org/en-US/docs/Web/API/CropTarget/...

Re:Show HN: I created snapDOM to capture DOM nodes as images... by tinchox6 on Thursday May 01, 2025 @06:21PM (#43864024)
Re:Show HN: I created snapDOM to capture DOM nodes as images... by matt-attack on Sunday May 04, 2025 @10:35AM (#43886960)
Can you explain what the use case is for this tool?