Я разрабатываю полноэкранную анимацию дождя, используя HTML, CSS и JavaScript. Я использую один элемент с циклом requestAnimationFrame, чтобы оживить 500 дождей. Несмотря на это, использование памяти моего графического процессора остается высоким, а время нагрузки увеличивается при высоких FPS. Ниже приведен минимальный фрагмент кода, который воспроизводит проблему: < /p>
const canvas = document.getElementById('rainCanvas');
const ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
const numDrops = 500;
const drops = [];
for (let i = 0; i < numDrops; i++) {
drops.push({
x: Math.random() * canvas.width,
y: Math.random() * canvas.height,
speed: 2 + Math.random() * 4,
length: 10 + Math.random() * 10
});
}
function drawRain() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.strokeStyle = "rgba(255, 255, 255, 0.5)";
ctx.lineWidth = 2;
drops.forEach(drop => {
ctx.beginPath();
ctx.moveTo(drop.x, drop.y);
ctx.lineTo(drop.x, drop.y + drop.length);
ctx.stroke();
drop.y += drop.speed;
if (drop.y > canvas.height) {
drop.y = -drop.length;
}
});
requestAnimationFrame(drawRain);
}
drawRain();< /code>
body,
html {
margin: 0;
padding: 0;
overflow: hidden;
background-color: black;
}
canvas {
display: block;
}< /code>
< /code>
< /div>
< /div>
< /p>
Как я могу оптимизировать эту анимацию дождя на основе холста, чтобы уменьшить нагрузку графического процессора, пока Поддержание плавной производительности?
Подробнее здесь: https://stackoverflow.com/questions/794 ... ad-and-mai