-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
74 lines (57 loc) · 1.56 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { Gradient } from './gradient.js';
import { StaticTesseract } from './cube.js';
const canvas = document.getElementById("cube");
const ctx = canvas.getContext("2d");
const SPEED = 1;
const BASE_ANGLE = [192, 105];
const PULSE = 500;
const size = Math.min(canvas.width, canvas.height);
const tesseract = new StaticTesseract(size);
tesseract.rotate(...BASE_ANGLE)
const rotate = createRandomRotator(tesseract);
function loop() {
draw(tesseract);
rotate();
requestAnimationFrame(loop);
}
function resize() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
}
resize()
window.addEventListener('resize', resize, false);
requestAnimationFrame(loop);
const gradient = new Gradient('#7665ff', '#ff6565', PULSE / 2);
function draw(projection) {
ctx.save();
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.translate(canvas.width / 2, canvas.height / 2);
const color = gradient.next()
ctx.strokeStyle = color;
ctx.shadowColor = color;
ctx.shadowBlur = 16;
ctx.lineWidth = 0.3;
ctx.beginPath();
for (const edges of projection.edges) {
const p1 = projection.nodes[edges[0]];
const p2 = projection.nodes[edges[1]];
ctx.moveTo(p1[0], p1[1]);
ctx.lineTo(p2[0], p2[1]);
}
ctx.closePath();
ctx.stroke();
ctx.restore();
}
function createRandomRotator(projection) {
let i = 1;
let x = 1 / (180 / SPEED);
let y = 1 / (180 / SPEED);
return () => {
if (i % PULSE === 0) {
x = Math.random() / (180 / SPEED);
y = Math.random() / (180 / SPEED);
}
i++;
projection.rotate(x, y);
}
}