Search code examples
callbackcanvg

Does canvg still have a way to trigger code when the render is finished?


There used to be a way to add a callback function renderCallback.

I've searched high and low but can't seem to find any examples of how to replicate that in canvg 3.x.

Is there anyone who can point me in the right direction?


Solution

  • So I'm not sure why the canvg docs make no mention of this that I can tell (maybe they thought it was just self evident?) but in version 3.x and above the render() method returns a promise.

    In my case I was trying to write an SVG to a canvas so I could then pull a PNG image from it.

    So something like this was the solution I was looking for:

    let canvas = document.getElementById( 'canvas' );
    
    let ctx = canvasgetContext( '2d' );
    
    let v = canvg.Canvg.fromString( ctx, svg );
    
    let render = v.render();
    
    render.then(function() {
        let img = canvas.toDataURL( 'image/png' );
    });