1

I am trying to clear the contents of WebGL canvas.

Is it fine if I just delete the canvas element from DOM and create a new one, and most importantly, will it clear up all the resources and not cause any memory leak.

My scenario is that, I am getting images continuously via AJAX polling from the server every 20 seconds and using a hidden WebGL canvas to draw it and further using another canvas to show it perfectly.

Suggestions?

Yves M.
  • 29,855
  • 23
  • 108
  • 144
Subhasish Dash
  • 219
  • 5
  • 13
  • Why are you using a canvas over, let's say, an `img` tag? – Aurelia Aug 18 '16 at 07:19
  • its necessary for me , requirement of the project , I have to do all the processing on the hidden canvas and then copy that canvas to the main canvas using Fabric.js – Subhasish Dash Aug 18 '16 at 07:21

1 Answers1

1

Basically no, if you just delete the canvas from the DOM there is no guarantee it will get freed in time to create a new one, after 8 or 16 of them you'll possibly get an error.

see How do I clean up and unload a WebGL canvas context from GPU after use?

Why can't you just keep reusing the same canvas or canvases?

Community
  • 1
  • 1
gman
  • 100,619
  • 31
  • 269
  • 393
  • I want to reuse the canvas ,but the images will be of different length . and I need to update the hidden canvas width and height in order to send it to fabricjs. I have also applied the code that is coming along with the above link that you have provided but I didn't get it working somehow – Subhasish Dash Aug 18 '16 at 09:22