(function () {
- var canvas = document.querySelector('canvas'),
- r = Math.random,
- pi = Math.PI,
- cos = Math.cos,
- sin = Math.sin,
- context = canvas.getContext('2d'),
- fps = 30,
- lastFrame = 0,
- speed = 1,
- size = 5 + rand(10),
- clear = !!(r() > 0.5),
- changeColor = !!(r() > 0.5),
- 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 K = document.querySelector('canvas'),
+ { random: R, PI, cos: c, sin: s, floor, atan2 } = Math,
+ ctx = K.getContext('2d'),
+ fps = 24,
+ lst = 0,
+ spd = 1,
+ siz = 5 + r(10),
+ clr = R() > 0.5,
+ ccl = R() > 0.5,
+ I = Array(r(10) + 6).fill().map(() => ({
+ x: r(64),
+ y: r(64),
+ a: R() * 2 * PI,
+ c: Array(3).fill(r(256))
})),
- shapes = [square, circle, heart, chaos, lineChaos],
- positions = [identity, spin(5), spin(15)],
- draw = shapes[rand(shapes.length)],
- position = positions[rand(positions.length)];
+ S = [sqr, crc, hrt, chs, lch],
+ P = [id, spn(5), spn(15)],
+ draw = S[r(S.length)],
+ pos = P[r(P.length)];
- function rand (x) {
- return Math.floor(r() * x);
+ function r(x) {
+ return floor(R() * x);
}
- function square(item) {
+ function sqr(i) {
- var p = position(item);
- context.fillRect(p.x, p.y, size, size);
+ var p = pos(i);
+ ctx.fillRect(p.x, p.y, siz, siz);
}
- function circle(item) {
+ function crc(i) {
- var p = position(item);
- context.beginPath();
- context.arc(p.x, p.y, size / 2, 0, 2 * pi);
- context.fill();
+ var p = pos(i);
+ ctx.beginPath();
+ ctx.arc(p.x, p.y, siz / 2, 0, 2 * PI);
+ ctx.fill();
}
- function chaos(item) {
+ function chs(i) {
- var p = position(item);
- 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.fill();
+ var p = pos(i);
+ ctx.beginPath();
+ ctx.moveTo(p.x, p.y);
+ ctx.lineTo(p.x * 10 * c(i.a), p.y * 10 * s(i.a));
+ ctx.lineTo(r(64), r(64));
+ ctx.fill();
}
- function lineChaos(item) {
+ function lch(i) {
- var p = position(item);
- context.beginPath();
- context.lineWidth = 5;
- context.strokeStyle = `rgb(${item.color.join(',')})`;
- context.moveTo(p.x, p.y);
- context.lineTo(p.x * 10 * cos(item.angle), p.y * 10 * sin(item.angle));
- context.stroke();
+ var p = pos(i);
+ ctx.beginPath();
+ ctx.lineWidth = 5;
+ ctx.strokeStyle = `rgb(${i.c.join(',')})`;
+ ctx.moveTo(p.x, p.y);
+ ctx.lineTo(p.x * 10 * c(i.a), p.y * 10 * s(i.a));
+ ctx.stroke();
}
- function square(item) {
+ function hrt(i) {
- var p = position(item);
- context.fillRect(p.x, p.y, size, size);
- }
-
- function heart(item) {
+ var p = pos(i);
+ ctx.fillRect(p.x, p.y, siz, siz);
- var p = position(item);
- context.fillRect(p.x, p.y, size, size);
+ ctx.beginPath();
+ ctx.arc(p.x + siz / 2, p.y, siz / 2, 0, 2 * PI, false);
+ ctx.fill();
+ ctx.closePath();
- context.beginPath();
- 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 * pi, false);
- context.fill();
- context.closePath();
+ ctx.beginPath();
+ ctx.arc(p.x + siz, p.y + siz / 2, siz / 2, 0, 2 * PI, false);
+ ctx.fill();
+ ctx.closePath();
}
- function identity(position) {
+ function id(p) {
- return position;
+ return p;
}
- function spin(radius) {
- return function (position) {
-
- return {
- x: position.x + radius * cos(lastFrame),
- y: position.y + radius * sin(lastFrame)
- }
- }
+ function spn(rad) {
+ return (p) => ({
+ x: p.x + rad * c(lst),
+ y: p.y + rad * s(lst)
+ })
}
- 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 + spd * c(i.a);
+ i.y = i.y + spd * s(i.a);
- 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.a = atan2(s(i.a), -c(i.a))
}
- 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.a = atan2(-s(i.a), c(i.a))
}
}
- function updateColor(item) {
+ function ucl(i) {
- item.color = item.color.map((c) => {
+ i.c = i.c.map((c) => {
c = c + 5;
return c > 255 ? 0 : c;
})
}
- function frame(time) {
+ function frm(t) {
- window.requestAnimationFrame(frame);
+ window.requestAnimationFrame(frm);
- var delta = time - lastFrame;
+ var dt = t - lst;
- 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);
+ if (dt > 1000 / fps) {
+ clr && ctx.clearRect(0, 0, 64, 64);
+ for (var i of I) {
+ ctx.fillStyle = `rgb(${i.c.join(',')})`;
+ draw(i);
+ ccl && ucl(i);
+ mov(i);
}
- lastFrame = time;
+ lst = t;
}
}
- frame();
+ frm();
}
)();