Saif Taher
Saif Taher

Reputation: 343

How to animate multiple objects on the html5-canvas using classes and functions?

I want to animate multiple rectangles moving downwards on an HTML5-canvas. There's a class through which I can easily create any rectangle I want by simply passing arguments to the class. The problem is with classes and the built-in method "requestAnimationFrame", I can only animate a single object: Here's the code:

function resize(canvasElement) {
    window.addEventListener("resize", function () {
        canvasElement.width = window.innerWidth / 3;
        canvasElement.height = window.innerHeight * 0.9975;
    })
}
var radius = 5;
class roundRect {
    constructor(rectX, rectY, rectHeight, rectWidth, stroke, fill, c) {
        c.clearRect(rectX-100, rectY-100, rectX + 100, rectY + 100);
        c.beginPath();
        c.strokeStyle = stroke;
        c.fillStyle = fill;
        c.moveTo(rectX + rectWidth/2, rectY);
        c.arcTo(rectX + rectWidth, rectY, rectX + rectWidth, rectY + rectHeight, radius);
        c.arcTo(rectX + rectWidth, rectY + rectHeight, rectX, rectY + rectHeight, radius);
        c.arcTo(rectX, rectY + rectHeight, rectX, rectY, radius);
        c.arcTo(rectX, rectY, rectX + rectWidth / 2, rectY, radius);
        c.closePath();
        c.fill();
        c.stroke();
    }
}
var rectY = 10;
function animate() {
    new roundRect(10, rectY, 50, 7, "red", "red", ctx);
    requestAnimationFrame(animate);
    rectY++;
}
{
    var canvas = document.body.querySelector("#canvasOne");
    canvas.width = window.innerWidth / 3;
    canvas.height = window.innerHeight * 0.9975;
    resize(canvas);
    var ctx = canvas.getContext("2d");
    animate();
}
canvasOne {
    border:1px solid black;
}
<!DOCTYPE html>
<html>
<head>
    <link href="homeComing.css" rel="stylesheet" />
    <meta charset="utf-8" />
    <title></title>
</head>
<body>
    <canvas id="canvasOne"></canvas>
    <script src="homeComing.js"></script>
</body>
</html>

If someone knows a way to animate multiple objects while writing only a few lines of code, it would be appreciated. Thanks in advance!

Upvotes: 1

Views: 1595

Answers (1)

Helder Sepulveda
Helder Sepulveda

Reputation: 17594

In your class you are using the clearRect in a way that could be deleting other objects.
Just do the clear outside and then you can get multiple rectangles just fine.

Here is a very simple example:

class roundRect {
  constructor(rectX, rectY, rectHeight, rectWidth, stroke, fill, c) {
    var radius = 5;
    c.beginPath();
    c.strokeStyle = stroke;
    c.fillStyle = fill;
    c.moveTo(rectX + rectWidth / 2, rectY);
    c.arcTo(rectX + rectWidth, rectY, rectX + rectWidth, rectY + rectHeight, radius);
    c.arcTo(rectX + rectWidth, rectY + rectHeight, rectX, rectY + rectHeight, radius);
    c.arcTo(rectX, rectY + rectHeight, rectX, rectY, radius);
    c.arcTo(rectX, rectY, rectX + rectWidth / 2, rectY, radius);
    c.closePath();
    c.fill();
    c.stroke();
  }
}

var objects = [
  {x:10, y:10, speed:{x:0, y:1},    w:50, h:7,  stroke:"red",   fill:"red"},
  {x:50, y:10, speed:{x:0.5, y:1},  w:25, h:20, stroke:"black", fill:"blue"},
  {x:190, y:10, speed:{x:-0.2, y:1}, w:20, h:18, stroke:"black", fill:"lime"}
]

function animate() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  for (i = 0; i < objects.length; i++) { 
    var o = objects[i]
    new roundRect(o.x, o.y, o.w, o.h, o.stroke, o.fill, ctx);
    o.x += o.speed.x
    o.y += o.speed.y    
  }
  requestAnimationFrame(animate);  
}

var canvas = document.body.querySelector("#canvasOne");
canvas.width = window.innerWidth / 3;
canvas.height = window.innerHeight * 0.9975;
var ctx = canvas.getContext("2d");
animate();
<canvas id="canvasOne"></canvas>

This is example is based on your code, but if you truly want to do all that a more with less lines of code you should be using a game engine.

Upvotes: 1

Related Questions