Я делаю пусковую установку для конфетти, которая создает квадраты и размещает их в верхней части экрана, каждый из которых имеет определенную силу тяжести и направление в сторону. У меня есть кнопка, которая делает 100 из них одновременно. Однако каждый раз, когда я нажимаю кнопку, она создает новые, но также ускоряет движение существующих квадратов.
var width1 = window.innerWidth || document.documentElement.clientWidth ||
document.body.clientWidth;
var height1 = window.innerHeight || document.documentElement.clientHeight ||
document.body.clientHeight;
let canvas = document.getElementById('confetti');
let ctx = canvas.getContext('2d');
canvas.width = width1;
canvas.height = height1;
let pieces = [];
let numberOfPieces = 100;
let lastUpdateTime = Date.now();
var a = 0;
var intervalID;
function randomColor() {
let colors = ['#999999ff', '#b7b7b7ff', ' #D3D3D3', '#ffff00 ', '#d9d9d9ff'];
return colors[Math.floor(Math.random() * colors.length)];
}
function update() {
let now = Date.now(),
dt = now - lastUpdateTime;
for (let i = pieces.length - 1; i >= 0; i--) {
let p = pieces[i];
if (p.y > canvas.height) {
pieces.splice(i, 1);
continue;
}
p.y += p.gravity * dt;
p.rotation += p.rotationSpeed * dt;
p.x += p.direction;
}
if (pieces.length < numberOfPieces) {
for (var b = pieces.length; b < numberOfPieces; b++) {
pieces.push(new Piece(Math.random() * canvas.width, -20));
b--;
numberOfPieces--;
}
}
lastUpdateTime = now;
a++;
if (a >= 1) {
numberOfPieces = 0;
//console.log("number of pieces: " + numberOfPieces + " pieces.length: " + pieces.length);
}
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
pieces.forEach(function(p) {
ctx.save();
ctx.fillStyle = p.color;
ctx.translate(p.x + p.size / 2, p.y + p.size / 2);
ctx.rotate(p.rotation);
ctx.fillRect(-p.size / 2, -p.size / 2, p.size, p.size);
ctx.restore();
});
requestAnimationFrame(draw);
}
function Piece(x, y) {
this.x = x;
this.y = y;
this.size = (Math.random() * 0.5 + 0.75) * 15;
this.gravity = (Math.random() * 0.5 + 0.75) * 0.15;
var c = Math.random()
if (c > 0.5) {
this.direction = -(Math.random() * 0.6);
} else {
this.direction = (Math.random() * 0.6);
}
this.rotation = (Math.PI * 2) * Math.random();
this.rotationSpeed = (Math.PI * 2) * (Math.random() - 0.5) * 0.0005;
this.color = randomColor();
}
while (pieces.length < numberOfPieces) {
pieces.push(new Piece(Math.random() * canvas.width, Math.random() * canvas.height));
}
var bye = 0;
function myfunction() {
var hello = Date.now();
var difference = hello - bye;
if (difference > 1000) {
a = 0;
numberOfPieces = pieces.length + 100;
intervalID = setInterval(update, 30);
draw();
bye = Date.now();
}
}
<canvas id="confetti"></canvas>
<button style="float: right; border: 1px blue solid; width: 100px; height: 100px;" onclick="myfunction()">Click me</button>