I have a node.js process with a canvas that is being exported to a frame buffer as a bitmap (RGBA values in Uint8Array
).
Now I want to draw SVG to this canvas, and found canvg
which works well for static SVG images so far.
But I also need to show animated SVGs. Currently I can only get the first frame of the SVG rendered on the canvas by using v.start() or v.render().
I understand that in a node.js environment we cannot call requestAnimationFrame()
so that's probably why canvg
does only render the first frame. It seems to work out of box on the basic example in a browser.
But I'm looking for a way to trigger the rendering again in a node.js process, based on timer or external event, with awareness for the time that has passed since last render, in respect for the animation progress.
const preset = presets.node({
DOMParser,
canvas: Canvas,
fetch,
})
const canvas = preset.createCanvas(width, height)
const ctx = canvas.getContext('2d') // TODO: switch to RGB24
const svgStr = getSVG(t, 'Thank you')
const v = Canvg.fromString(ctx, svgStr, preset)
v.start()
async function drawFrame() {
// I need something here to request the next frame of
// the animation so it can be drawn on the canvas before
// the canvas is exported as bitmap.
// Example of what I need:
await v.nextFrame(new Date().getMilliseconds())
const renderedImage = ctx.getImageData(0, 0, width, height)
const buffer = Buffer.from(renderedImage.data)
em.emit('newFrame', buffer)
}
setInterval(drawFrame, 200)