DigM
DigM

Reputation: 519

Clear WebGL2RenderingContext from canvas

I have a WebGL canvas which I want to clear at a certain point which uses a WebGL2RenderingContext and I can't seem to find a way to either:

I have tried something like this, and while it allows me to get the canvas and it's context, I'm now not sure where to go from here

var canvas = document.getElementById('#canvas');
var gl = canvas.getContext('webgl2');
gl.clear();

I have also tried something like this to overwrite the canvas with a new context but it didn't seem to work.

var canvas = document.getElementById('#canvas');
var gl = canvas.getContext('2d');
gl.clearRect(0, 0, canvas.width, canvas.height);

I would like it to be so that the canvas itself is still on the page but just not have anything on it.

Upvotes: 1

Views: 938

Answers (1)

user128511
user128511

Reputation:

You can not change the context of a canvas.

You can clear a WebGL or WebGL2 canvas with

 const red = 1;
 const green = 0.5;
 const blue = 0.7;
 const alpha = 1;
 gl.clearColor(red, green, blue, alpha);
 gl.clear(gl.COLOR_BUFFER_BIT);

Upvotes: 1

Related Questions