clearCanvas()
Its simple, It just Clears the canvas. Nothing fancy
Although, it has some cool utility.
Syntax
function draw(){
clearCanvas();
//code
requestAnimationFrame(draw);
}
Example
Not using clearCanvas() can be used to make some nice things, like...
setCanvas(elem);
pos={
x:10,
y:10,
r:10
}
vel={
x:1,
y:1
}
w=WIDTH;
h=HEIGHT;
function velOnCollision(pos,vel){
if(pos.x+pos.r>w || pos.x-pos.r<0){
vel.x*=-1
}
if(pos.y+pos.r>h || pos.y-pos.r<0){
vel.y*=-1;
}
}
t=0;
function draw(){
new circle(pos.x,pos.y,pos.r,`hsl(${t},100%,50%)`,1,'#ff0000',0);
pos.x+=vel.x;
pos.y+=vel.y;
velOnCollision(pos,vel);
t++;
requestAnimationFrame(draw);
}
draw();
Note: If you dont use clearCanvas(), over longer time, the illustration will start getting slower because of tons of elements.