I can usually produce high quality PNG export via the rasterize function in my notebooks. (see this one for example, I can create 11000 pixels wide images)
It works with import { serialize } from "@mbostock/saving-svg"
But in my latest notebook, I can only produce 4400 pixels wide images. I have tried different computer on Chrome and I dont see the problem.
With the help of ChatGPT, I found the error, here is a resume of the problem and its solution
Issue:
I was trying to rasterize an SVG chart in an Observable notebook using a function that converts it to a PNG. When setting PNGprecision = 5, the function stopped working. Debugging logs showed that the canvas size was unexpectedly doubling, leading to excessive memory usage (~81MB) and possible crashes.
Cause:
The issue was due to automatic canvas scaling on high-DPI screens. Browsers adjust context2d dimensions based on window.devicePixelRatio, resulting in a canvas size twice as large as expected.
Solution:
Explicitly set the canvas width and height after creating the context to prevent unwanted scaling:
This fixed the issue, ensuring the canvas size matched the expected resolution. Now, the rasterization function works smoothly, even at high precision!
DOM.context2d has a third argument that sets the scaling factor (both by scaling the canvas dimensions and setting a transform), which defaults to devicePixelRatio. For exports (or when calling drawImage) you’ll want to set that to 1.
Browsers also have varying constraints for canvas dimensions (in the range of 16K-32K side length).
Thank you @mootari , I have add the third agument, but stiil hit limits. I have seen on this different computer the following error in my consol when trying higher resolution:
WebGL: CONTEXT_LOST_WEBGL: loseContext: context lost
and
POST https://o299802.ingest.sentry.io/api/5593183/envelope/?sentry_key=ca9eac14b9164458a334243bb3c08c45&sentry_version=7&sentry_client=sentry.javascript.nextjs%2F8.32.0 net::ERR_BLOCKED_BY_CLIENT
I actually realize now the the .style("mix-blend-mode", "multiply") create a lower limit on the size export as well. If I remove this line (but it does not look so good) I can scale to a higher degree, but not sufficiant enough.
I have tried to do the scaling via inkscape or image magic and I had some result, but the text on the curved path and the font are broken…
Then ChatGPT suggested using the print feature of the browser when seeing the SVG file with looks just perfect. Setting the A1 size in the print as file, it gives a perfect PDF!
I can then use Ghostscript to create a png image gs -sDEVICE=png16m -r600 -o output.png input.pdf at the resolution needed.
Also, it looks like you can custom the print output of chrome via Virtual Printer (CUPS-PDF) or export to postscript