News for hackers. Stuff that matters.
Re:Stanford CS248: Implement an SVG Rasterizer by rayshan on Friday March 20, 2020 @04:35PM (#22641400)
What's a good JavaScript-based solution to render SVG into an image? html2canvas doesn't work well with SVG, especially those heavily styled with CSS.
Re:Stanford CS248: Implement an SVG Rasterizer by hypothete on Friday March 20, 2020 @05:29PM (#22641841)
If you're not in a Node.js context, the Canvas API does a decent job of rasterizing SVGs nowadays. Once rasterized, you can call canvas.toDataURL() to get a download link. Here's a demo:

https://codepen.io/hypothete/pen/WNvKLEE

Re:Stanford CS248: Implement an SVG Rasterizer by zakjan on Saturday March 21, 2020 @03:19AM (#22645141)
I used to have troubles with retina displays, I always got a blurred image, no matter to what trick I tried. Is there a way to fix the rendering pixel ratio to 1?
Re:Stanford CS248: Implement an SVG Rasterizer by graup on Saturday March 21, 2020 @03:37AM (#22645194)
The usual approach with canvas for high dpi is to increase the size of the canvas and then scale it back down afterwards: https://codepen.io/graup/pen/jOPpopR
Re:Stanford CS248: Implement an SVG Rasterizer by zakjan on Saturday March 21, 2020 @04:14AM (#22645265)
This downloads 200x200. How to scale it back to 100x100? Ideally so that the image is the same no matter the display where it was exported on. Also I would like to not need to include any image processing libs. The usecase is to export a HTML element (e.g. a chart) and send to another API (e.g. attach it to a report), without downloading.
Re:Stanford CS248: Implement an SVG Rasterizer by beansie on Friday March 20, 2020 @05:44PM (#22641961)
Does the css need to be inlined?
Re:Stanford CS248: Implement an SVG Rasterizer by yzmtf2008 on Friday March 20, 2020 @06:05PM (#22642131)
Re:Stanford CS248: Implement an SVG Rasterizer by rayshan on Friday March 20, 2020 @06:58PM (#22642594)
Yup this is the problem. Workable but painful.
Re:Stanford CS248: Implement an SVG Rasterizer by wolfspider on Friday March 20, 2020 @07:42PM (#22642882)
I’m surprised no one mentioned canvg: https://canvg.github.io/canvg/demo/index.html

If using CSS with SVG is important then it’s actually better to use a backend with a library like librsvg (e.g. Cairo) then run node-canvas on top of it like automattic does. Even Inkscape has problems with CSS in SVG files. CSS in browsers is subjective because nowadays they might be using an style that doesn’t have a straightforward approach that can be rasterized. I haven’t had much luck with rasterizing SVG styles past CSS2 spec.

Re:Stanford CS248: Implement an SVG Rasterizer by lucideer on Friday March 20, 2020 @04:57PM (#22641578)
Re:Stanford CS248: Implement an SVG Rasterizer by rayshan on Friday March 20, 2020 @06:54PM (#22642564)
This looks great, will test it out, thx!
Re:Stanford CS248: Implement an SVG Rasterizer by restingrobot on Friday March 20, 2020 @05:32PM (#22641869)
Shameless Plug for a library I helped write: https://github.com/SVG-Edit/svgedit
Re:Stanford CS248: Implement an SVG Rasterizer by rayshan on Friday March 20, 2020 @06:57PM (#22642586)
This looks so cool! But your demo link is broken :(
Re:Stanford CS248: Implement an SVG Rasterizer by FigmentEngine on Saturday March 21, 2020 @04:47AM (#22645365)
not javascript, but screenshots work best - other approaches suffer from css and rendering bugs. best way is to just use your browser in headless mode from the command line. you can do a screenshot, and specify the window size.

https://developer.mozilla.org/en-US/docs/Mozilla/Firefox/Hea...

https://developers.google.com/web/updates/2017/04/headless-c...

and in dev panel https://developer.mozilla.org/en-US/docs/Tools/Taking_screen...

Re:Stanford CS248: Implement an SVG Rasterizer by Groxx on Friday March 20, 2020 @04:38PM (#22641430)
Does Path2D cover it? If it does, seems like you could just draw to a canvas and save the result. https://developer.mozilla.org/en-US/docs/Web/API/Path2D/Path...
Re:Stanford CS248: Implement an SVG Rasterizer by coreyoconnor on Friday March 20, 2020 @04:39PM (#22641434)
Did not know this. Is that due to browser issues or a specification issue?
Re:Stanford CS248: Implement an SVG Rasterizer by kylebarron on Friday March 20, 2020 @04:57PM (#22641582)
https://github.com/shakiba/svgexport

Is that what you’re looking for?

Re:Stanford CS248: Implement an SVG Rasterizer by rayshan on Friday March 20, 2020 @06:56PM (#22642575)
No. It does a perfect job because Puppeteer is just Chrome, not JavaScript :) Thanks though!
Re:Stanford CS248: Implement an SVG Rasterizer by dublin on Friday March 20, 2020 @05:38PM (#22641923)
Kinda defeats the whole purpose of SVG. Only photos should be bitmaps. Graphics should be vectors. This is the 21st century, folks...
Re:Stanford CS248: Implement an SVG Rasterizer by avmich on Friday March 20, 2020 @06:40PM (#22642436)
True, but if you have digital display - that is, which have separate, immovable pixels, working with pixels and not with vector-based analog primitives - you'll eventually have to convert "vector" picture into "pixel" picture.
Re:Stanford CS248: Implement an SVG Rasterizer by chrisseaton on Saturday March 21, 2020 @08:04AM (#22645955)
Do you have a vector display of some kind?

No?

So someone somewhere is going to need to rasterise your vectors if you want to see them on a screen, aren't they?

Re:Stanford CS248: Implement an SVG Rasterizer by rayshan on Friday March 20, 2020 @06:56PM (#22642581)
I wish for that world to exist, but rasterized images are much easier to share and render nicely in iMessages etc.
Re:Stanford CS248: Implement an SVG Rasterizer by Sephr on Friday March 20, 2020 @06:04PM (#22642116)
One reason is to generate screenshots of HTML by combining this technique with SVG <foreignObject>.