Html5 Canvas Draw Multiple Rectangles That Move In The Canvas
In this code i have a rectangle moving from the middle-left to the right at 30ticks, however, my brain hurts when i try to think how im going to add another square about the same s
Solution 1:
Use JavaScript objects to represent multiple rectangles
Here's an outline of how to do it:
- Use a javascript object to describe each of your rectangles
- Put each rect object in a rects[] array
- Inside an animation loop:
- Change each rect's xvalue
- Redraw the canvas with the rects in their new positions
- Request another loop in the animation
 
- Change each rect's 
Annotated code and a Demo:
var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");
var cw=canvas.width;
var ch=canvas.height;
// define a rect using a javascript objectvar rect1={
  x:20,
  y:20,
  width:40,
  height:40,
  directionX:1
}
// define another rect using a javascript objectvar rect2={
  x:250,
  y:90,
  width:40,
  height:40,
  directionX:-1
}
// put each rect in a rects[] arrayvar rects=[rect1,rect2];
// start the animation looprequestAnimationFrame(animate);
functionanimate(time){
  // move each rect in the rects[] array by its // own directionxfor(var i=0;i<rects.length;i++){
    rects[i].x+=rects[i].directionX;
  }
  // draw all the rects in their new positionsdraw();
  // request another frame in the animation looprequestAnimationFrame(animate);
}
functiondraw(){
  ctx.clearRect(0,0,cw,ch);
  for(var i=0;i<rects.length;i++){
    var r=rects[i]
    ctx.strokeRect(r.x,r.y,r.width,r.height);
  }
}body{ background-color: ivory; }
#canvas{border:1px solid red; margin:0 auto; }<canvasid="canvas"width=300height=300></canvas>An improvement I leave for you to do is to stop the animation when all rectangles have left the visible canvas.
Solution 2:
This is the point of OOP (Object Orientated Programming), where each item in your program is represented as an object. In your case we can have a Square object that has a: x, y, width, and color. Along with a function to draw itself anim():
function Square(x, y, w, color) 
{
    this.x = x;
    this.y = y;
    this.w = w;
    this.color = color;
    this.anim = function() 
    {
        if (this.x < context.canvas.width) {
            this.x += 5;
            context.fillStyle = this.color;
            context.strokeStyle = "red";
            context.lineWidth = 3;
            context.fillRect(this.x,this.y,this.w,this.w);
            context.strokeRect(this.x,this.y,this.w,this.w);
        }
        elsethis.x=-this.w;
    }
}
Then you can easily create objects, to animate each one place them inside an array and call anim() for each square in objects:
var rect1 = newSquare(-40, 200, 40, "yellow");
var rect2 = newSquare(0, 100, 40, "blue");
var objects = [ rect1, rect2 ];
setInterval(function(){
    blank();
    for(rect in objects){
        objects[rect].anim();
    }
}, 30);
Post a Comment for "Html5 Canvas Draw Multiple Rectangles That Move In The Canvas"