From: Ruben Beltran del Rio Date: Tue, 13 Feb 2024 22:12:18 +0000 (+0100) Subject: Don't read Math all the time X-Git-Url: https://git.r.bdr.sh/rbdr/r.bdr.sh/commitdiff_plain/8f2ed424421fc94e18fefe0e6a49de651b58a3ec?hp=--cc Don't read Math all the time --- 8f2ed424421fc94e18fefe0e6a49de651b58a3ec diff --git a/js/animation.js b/js/animation.js index fc6246e..907df5d 100644 --- a/js/animation.js +++ b/js/animation.js @@ -5,14 +5,16 @@ fps = 30, lastFrame = 0, speed = 1, - size = 5 + rand(10); + size = 5 + rand(10), + r = Math.random, + pi = Math.PI; - var clear = !!(Math.random() > 0.5); - var changeColor = !!(Math.random() > 0.5); + var clear = !!(r() > 0.5); + var changeColor = !!(r() > 0.5); var items = Array(rand(10) + 6).fill(null).map(() => ({ x: rand(64), y: rand(64), - angle: Math.random() * 2 * Math.PI, + angle: r() * 2 * pi, color: Array(3).fill(rand(256)) })); var shapes = [square, circle, heart, chaos, lineChaos]; @@ -21,7 +23,7 @@ var position = positions[rand(positions.length)]; function rand (x) { - return Math.floor(Math.random() * x); + return Math.floor(r() * x); } function square(item) { @@ -34,7 +36,7 @@ var p = position(item); context.beginPath(); - context.arc(p.x, p.y, size / 2, 0, 2 * Math.PI); + context.arc(p.x, p.y, size / 2, 0, 2 * pi); context.fill(); } @@ -71,12 +73,12 @@ context.fillRect(p.x, p.y, size, size); context.beginPath(); - context.arc(p.x + size / 2, p.y, size / 2, 0, 2 * Math.PI, false); + context.arc(p.x + size / 2, p.y, size / 2, 0, 2 * pi, false); context.fill(); context.closePath(); context.beginPath(); - context.arc(p.x + size, p.y + size / 2, size / 2, 0, 2 * Math.PI, false); + context.arc(p.x + size, p.y + size / 2, size / 2, 0, 2 * pi, false); context.fill(); context.closePath(); }