Published
Edited
Dec 20, 2020
4 stars
Insert cell
Insert cell
currsvg = renderSvg({ x: currAngle, y: currAngle })
Insert cell
Insert cell
Insert cell
Insert cell
totalframes = 50
Insert cell
Insert cell
rasterizedSvgs = {
const step = (Math.PI * 4) / (totalframes - 1);
const imgs = [];
for (let angle = 0; angle < Math.PI * 4; angle += step) {
imgs.push(await rasterize(renderSvg({ x: angle, y: angle })));
}
return imgs;
}
Insert cell
gif = magickGif(rasterizedSvgs, 9)
Insert cell
Insert cell
async function magickGif(frames, delay = 10) {
const files = [];

for (const [i, file] of frames.entries()) {
files.push({
name: `frame${String(i).padStart(6, '0')}.png`,
content: await file.arrayBuffer()
});
}
const commands = `convert -delay ${delay} -loop 0 frame*.png out.gif`;

const { outputFiles, exitCode } = await Magick.executeOne({
inputFiles: files,
commands
});

const imgout = new Image();
imgout.src = URL.createObjectURL(outputFiles[0].blob);
imgout.blob = outputFiles[0].blob;
return imgout;
}
Insert cell
Magick = import("https://cdn.jsdelivr.net/npm/wasm-imagemagick@1.2.8/dist/magickApi.js")
Insert cell
function rasterize(svg) {
let resolve, reject;
const promise = new Promise((y, n) => ((resolve = y), (reject = n)));
const image = new Image();
image.onerror = reject;
image.onload = () => {
const width = svg.getAttribute('width');
const height = svg.getAttribute('height');
const canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
const context = canvas.getContext('2d');
context.drawImage(image, 0, 0, width, height);
context.canvas.toBlob(resolve);
};
image.src = URL.createObjectURL(serialize(svg));
return promise;
}
Insert cell
Zdog = require("zdog@1/dist/zdog.dist.min.js")
Insert cell
import { serialize } with { rasterize } from "@mbostock/saving-svg"
Insert cell
import { slider } from "@jashkenas/inputs"
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more