When canvas is converted to png, sometimes you get an all-black picture.

when I use toDataUrl () to convert canvas to png, sometimes I get an all-black picture, but the amazing thing is that sometimes the picture is normal, does anyone know what"s going on?

// "stage"  canvas 
const canvas = document.querySelector(".stage");
const image = canvas.toDataUrl("image/png");
const link = document.createElement("a");
link.href = image;
link.download = "balabala.png";
const event = document.createEvent("MouseEvents");
event.initMouseEvent("click", true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
link.dispatchEvent(event);
Jan.21,2022

Click events should be placed in image.onload. Click black

before the picture is loaded.
Menu