X-Git-Url: https://git.r.bdr.sh/rbdr/r.bdr.sh/blobdiff_plain/8f2ed424421fc94e18fefe0e6a49de651b58a3ec..9b9f84dda63a725c97fe44f5308ea4dfe987fa7f:/js/animation.js diff --git a/js/animation.js b/js/animation.js index 907df5d..b579fea 100644 --- a/js/animation.js +++ b/js/animation.js @@ -1,75 +1,76 @@ (function () { var canvas = document.querySelector('canvas'), + r = Math.random, + pi = Math.PI, + cos = Math.cos, + sin = Math.sin, context = canvas.getContext('2d'), - fps = 30, + fps = 24, lastFrame = 0, speed = 1, - size = 5 + rand(10), - r = Math.random, - pi = Math.PI; - - 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: r() * 2 * pi, - color: Array(3).fill(rand(256)) - })); - var shapes = [square, circle, heart, chaos, lineChaos]; - var positions = [identity, spin(5), spin(15)]; - var draw = shapes[rand(shapes.length)]; - var position = positions[rand(positions.length)]; - - function rand (x) { + size = 5 + rnd(10), + clear = !!(r() > 0.5), + changeColor = !!(r() > 0.5), + items = Array(rnd(10) + 6).fill(null).map(() => ({ + x: rnd(64), + y: rnd(64), + angle: r() * 2 * pi, + color: Array(3).fill(rnd(256)) + })), + shapes = [sqr, crc, hrt, chs, lch], + positions = [id, spn(5), spn(15)], + draw = shapes[rnd(shapes.length)], + pos = positions[rnd(positions.length)]; + + 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 * Math.cos(item.angle), p.y * 10 * Math.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 * Math.cos(item.angle), p.y * 10 * Math.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(); @@ -83,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 * Math.cos(lastFrame), - y: position.y + radius * Math.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 * Math.cos(item.angle); - item.y = item.y + speed * Math.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(Math.sin(item.angle), -Math.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(-Math.sin(item.angle), Math.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; }) @@ -128,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;