Anybody got any good ideas?
- Turn the SVG markup into a data URL
- Assign URL to image src
- Draw image onto canvas
- Fetch image data and pass to library of your choice (e.g. gif.js)
Expect this to be slow, though. Example:
const img = new Image;
img.src = `data:image/svg+xml;utf8,${encodeURIComponent(svg.outerHTML)}`;
await img.decode();
If you donāt care about dropped frames, thereās also the MediaStream API:
observable-prerender
might be an option here, if youāre alright with generating it from outside observable (and if youāre comfortable with the command line). This notebook has an example of making PNG screenshots of a cell for every frame of an animation, then using ffmpeg to convert those frames to MP4 (though you can do something very similar with SVGs and GIFs). Thereās also a CLI that makes it slightly easier
If youāre talking about an animated GIF, you could take a look at what I did in https://observablehq.com/@sanderevers/swirled-series (cells ārasterizeā and āmakezipā).
Each frame is a separate SVG that I convert to PNG using the approach from https://observablehq.com/@mbostock/saving-svg. Then I zip the frames using npm package jszip, in order to download them all at once. On my local machine I use convert (imagemagick) to create the animated gif:
convert -delay 2 -loop 0 frames/frame*.png out.gif
Thanks for the ideas. Excellent technical approaches but I might try a different form of distribution.
How about this?
I used @mootari suggestion of gif.js. I tried to make this a āgenericā notebook which can be used against any public svg timeseries. So you donāt have to āimportā anything to use it.
Iāve been recently playing with some WASM libs, and here is one experiment using WASM-ImageMagick to generate a GIF.
Iāve used rasterize function from @sanderevers post and originally from Mikeās notebook.
Itās cool that this WASM lib works on mobile as well. Unfortunately I couldnāt find a way to invalidate, abort the calls to the WASM lib as noted here https://observablehq.com/@radames/wasm-imagemagick-helper so you have to be careful I guess.