From: Ruben Beltran del Rio Date: Fri, 16 Feb 2024 21:57:18 +0000 (+0100) Subject: make js a bit smaller X-Git-Url: https://git.r.bdr.sh/rbdr/r.bdr.sh/commitdiff_plain/9b9f84dda63a725c97fe44f5308ea4dfe987fa7f?ds=sidebyside;hp=5d1f6d5f5143881ce3c41f6466dcca3e9714183d make js a bit smaller --- diff --git a/js/animation.js b/js/animation.js index f7642ee..b579fea 100644 --- a/js/animation.js +++ b/js/animation.js @@ -9,68 +9,68 @@ fps = 24, lastFrame = 0, speed = 1, - size = 5 + rand(10), + size = 5 + rnd(10), clear = !!(r() > 0.5), changeColor = !!(r() > 0.5), - items = Array(rand(10) + 6).fill(null).map(() => ({ - x: rand(64), - y: rand(64), + items = Array(rnd(10) + 6).fill(null).map(() => ({ + x: rnd(64), + y: rnd(64), angle: r() * 2 * pi, - color: Array(3).fill(rand(256)) + color: Array(3).fill(rnd(256)) })), - shapes = [square, circle, heart, chaos, lineChaos], - positions = [identity, spin(5), spin(15)], - draw = shapes[rand(shapes.length)], - position = positions[rand(positions.length)]; + shapes = [sqr, crc, hrt, chs, lch], + positions = [id, spn(5), spn(15)], + draw = shapes[rnd(shapes.length)], + pos = positions[rnd(positions.length)]; - function rand (x) { + function rnd (x) { return Math.floor(r() * x); } - function square(item) { + function sqr(i) { - var p = position(item); + var p = pos(i); context.fillRect(p.x, p.y, size, size); } - function circle(item) { + function crc(i) { - var p = position(item); + var p = pos(i); context.beginPath(); context.arc(p.x, p.y, size / 2, 0, 2 * pi); context.fill(); } - function chaos(item) { + function chs(i) { - var p = position(item); + var p = pos(i); context.beginPath(); context.moveTo(p.x, p.y); - context.lineTo(p.x * 10 * cos(item.angle), p.y * 10 * sin(item.angle)); - context.lineTo(rand(64), rand(64)); + context.lineTo(p.x * 10 * cos(i.angle), p.y * 10 * sin(i.angle)); + context.lineTo(rnd(64), rnd(64)); context.fill(); } - function lineChaos(item) { + function lch(i) { - var p = position(item); + var p = pos(i); context.beginPath(); context.lineWidth = 5; - context.strokeStyle = `rgb(${item.color.join(',')})`; + context.strokeStyle = `rgb(${i.color.join(',')})`; context.moveTo(p.x, p.y); - context.lineTo(p.x * 10 * cos(item.angle), p.y * 10 * sin(item.angle)); + context.lineTo(p.x * 10 * cos(i.angle), p.y * 10 * sin(i.angle)); context.stroke(); } - function square(item) { + function sqr(i) { - var p = position(item); + var p = pos(i); context.fillRect(p.x, p.y, size, size); } - function heart(item) { + function hrt(i) { - var p = position(item); + var p = pos(i); context.fillRect(p.x, p.y, size, size); context.beginPath(); @@ -84,38 +84,38 @@ context.closePath(); } - function identity(position) { + function id(p) { - return position; + return p; } - function spin(radius) { - return function (position) { + function spn(rad) { + return function (p) { return { - x: position.x + radius * cos(lastFrame), - y: position.y + radius * sin(lastFrame) + x: p.x + rad * cos(lastFrame), + y: p.y + rad * sin(lastFrame) } } } - function move(item) { + function mov(i) { - item.x = item.x + speed * cos(item.angle); - item.y = item.y + speed * sin(item.angle); + i.x = i.x + speed * cos(i.angle); + i.y = i.y + speed * sin(i.angle); - if (item.x < 0 || item.x > 64) { - item.angle = Math.atan2(sin(item.angle), -cos(item.angle)) + if (i.x < 0 || i.x > 64) { + i.angle = Math.atan2(sin(i.angle), -cos(i.angle)) } - if (item.y < 0 || item.y > 64) { - item.angle = Math.atan2(-sin(item.angle), cos(item.angle)) + if (i.y < 0 || i.y > 64) { + i.angle = Math.atan2(-sin(i.angle), cos(i.angle)) } } - function updateColor(item) { + function updateColor(i) { - item.color = item.color.map((c) => { + i.color = i.color.map((c) => { c = c + 5; return c > 255 ? 0 : c; }) @@ -129,11 +129,11 @@ if (delta > 1000 / fps) { clear && context.clearRect(0, 0, 64, 64); - for (var item of items) { - context.fillStyle = `rgb(${item.color.join(',')})`; - draw(item); - changeColor && updateColor(item); - move(item); + for (var i of items) { + context.fillStyle = `rgb(${i.color.join(',')})`; + draw(i); + changeColor && updateColor(i); + mov(i); } lastFrame = time;