niklasvh / html2canvas

Screenshots with JavaScript
https://html2canvas.hertzen.com/
MIT License
30.09k stars 4.75k forks source link

How to remove an image from the canvas #3125

Open drieJAC opened 8 months ago

drieJAC commented 8 months ago

I can draw an image to the canvas but if i want to remove it and draw another one instead how can I do that? Currently I store the base64 encoded image in a variable. If I unset the variable then the image disappears but if i click to redraw it again i get the following warning:

"Multiple readback operations using getImageData are faster with the willReadFrequently attribute set to true."

As you can see in the code below I am already setting 'willReadFrequently' to true which should prevent this warning but I think that somehow the canvas is hanging around in memory somewhere.

const elementToCapture = document.body; const canvas = await html2canvas(elementToCapture); canvas.getContext('2d', { willReadFrequently: true, // => faster multiple readback operations }); const thumbnail = canvas.toDataURL('image/png');