I am trying to reset my rectangle animation from the starting point when I resize the browser, the code is like this:
/*from basic setup*/
canvas = document.getElementById("myCanvas");
canvas.height = window.innerHeight;
canvas.width = window.innerWidth;
ctx = canvas.getContext("2d");
/*the "resize" event and the reset function should be triggered */
window.addEventListener("resize",function(){
canvas.height = window.innerHeight;
canvas.width = window.innerWidth;
//cancelAnimationFrame(timeID)
//theRect();
});
/*drawing the rectangle*/
var x = 0,y = canvas.height - 100,w = 100, h = 100,dx=1;
function theRect(){
ctx.fillStyle = "lightblue";
ctx.fillRect(x,y,w,h)
x+=dx;
if(x+w>canvas.width/2 || x<0){
dx=-dx;
}
}
/*start animation*/
function animate(){
ctx.clearRect(0,0,canvas.width,canvas.height);
theRect();
requestAnimationFrame(animate);
}
//var timeID = requestAnimationFrame(animate)
animate();
I was thinking if each time the browser resized, the rectangle has to be reset to the starting point asctx.fillRect(0,canvas.height-100,100,100)
and continue its animation.
So my original approach to solve this is using cancelAnimationFrame()
this method to kill the animation first and calling this function again within my eventlistener
so each time the event was fired, the animation will stop and run again.
but cancellAnimationFrame()
function requires the target animation timeID, and there is no way to track this timeID inside my animate()
function since if I am calling var timeID = requestionAnimationFrame(animate)
it will come to huge lag spikes on my browser
just the more I want to modify to more I become to loss with this, so my questions are:
1, is this wise to use cancelAnimationFrame
here as the first step to reset my animation?
2, is there any other good way to achieve this "reset" purpose?
Could someone please help me with this? thanks in advance.
There is no need to use cancelAnimationFrame()
function at all in this case scenario.
You could just simply change / reset the value (position) of x
and y
variable accordingly on window resize and that will reset the rectangle's starting position / point.
Since, the animate
function is running in a recursive manner hence, changes to any variable's (used inside animate function) value will take effect immediately.
/*from basic setup*/
canvas = document.getElementById("myCanvas");
canvas.height = window.innerHeight;
canvas.width = window.innerWidth;
ctx = canvas.getContext("2d");
/*drawing the rectangle*/
var x = 0,
y = canvas.height - 100,
w = 100,
h = 100,
dx = 1;
/*the "resize" event and the reset function should be triggered */
window.addEventListener("resize", function() {
canvas.height = window.innerHeight;
canvas.width = window.innerWidth;
x = 0; //reset starting point (x-axis)
y = canvas.height - 100; //reset starting point (y-axis)
});
function theRect() {
ctx.fillStyle = "lightblue";
ctx.fillRect(x, y, w, h);
x += dx;
if (x + w > canvas.width / 2 || x < 0) {
dx = -dx;
}
}
/*start animation*/
function animate() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
theRect();
requestAnimationFrame(animate);
}
animate();
body{margin:0;overflow:hidden}
<canvas id="myCanvas"></canvas>
also, see a demo on JSBin