JS Canvas – Save Transparent Image

I am using the following code for downloading (taking a snapshot) of a canvas:

    var strMime = "image/png";
    var canvas = document.getElementsByTagName('canvas')[0];
    var link = document.createElement("a");
    link.href = canvas.toDataURL(strMime);
    link.download = 'test1.png';

the thing is that it creates the “test1.png” image with a white background
(even if i set the canvas style background color to be transparent)

any idea how to generate transparent png files from canvas?

Refer to


If you put a 24-bit PNG with alpha on the canvas it understands it.