-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathhypercube.js
167 lines (148 loc) Β· 3.7 KB
/
hypercube.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
const canvasSketch = require('canvas-sketch');
const chroma = require('chroma-js');
const Random = require('canvas-sketch-util/random');
const { point, line } = require('./geometry');
const { matrixMultiply } = require('./matrix');
const { hueCycle } = require('./clrs');
const settings = {
dimensions: [1600, 1600],
animate: true,
duration: 4,
scaleToView: true,
};
const sketch = () => {
console.clear();
let angle = Math.PI / 4;
Random.setSeed(Random.getRandomSeed());
// Choose a new starting hue
let hueStart = Random.value();
// prettier-ignore
const hyperCube = [
[-1, -1, -1, 1],
[ 1, -1, -1, 1],
[ 1, 1, -1, 1],
[-1, 1, -1, 1],
[-1, -1, 1, 1],
[ 1, -1, 1, 1],
[ 1, 1, 1, 1],
[-1, 1, 1, 1],
[-1, -1, -1, -1],
[ 1, -1, -1, -1],
[ 1, 1, -1, -1],
[-1, 1, -1, -1],
[-1, -1, 1, -1],
[ 1, -1, 1, -1],
[ 1, 1, 1, -1],
[-1, 1, 1, -1],
];
const edges = [
[0, 1],
[1, 2],
[2, 3],
[3, 0],
//
[4, 5],
[5, 6],
[6, 7],
[7, 4],
//
[8, 9],
[9, 10],
[10, 11],
[11, 8],
//
[12, 13],
[13, 14],
[14, 15],
[15, 12],
//
[0, 4],
[1, 5],
[2, 6],
[3, 7],
//
[0, 8],
[1, 9],
[2, 10],
[3, 11],
//
[8, 12],
[9, 13],
[10, 14],
[11, 15],
//
[4, 12],
[5, 13],
[6, 14],
[7, 15],
];
return ({ context, width, height, playhead }) => {
// const angle = Math.sin(playhead * Math.PI) * 2 * Math.PI;
const angle = playhead * 2 * Math.PI;
// prettier-ignore
const rotationXY = [
[Math.cos(angle), -Math.sin(angle), 0, 0],
[Math.sin(angle), Math.cos(angle), 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1],
];
// prettier-ignore
const rotationZW = [
[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, Math.cos(angle), -Math.sin(angle)],
[0, 0, Math.sin(angle), Math.cos(angle)],
];
// prettier-ignore
const rotationX = [
[1, 0, 0],
[0, Math.cos(-Math.PI / 2), -Math.sin(-Math.PI / 2)],
[0, Math.sin(-Math.PI / 2), Math.cos(-Math.PI / 2)],
];
const distance = 2.25;
const projected = hyperCube.map((vertex) => {
const v = vertex.map((v) => [v]);
// Rotate around planes
let rotated4d = matrixMultiply(rotationXY, v);
rotated4d = matrixMultiply(rotationZW, rotated4d);
// Project from 4D to 3D
const w4d = 1 / (distance - rotated4d[3]);
// prettier-ignore
const projection3d = [
[w4d, 0, 0, 0],
[0, w4d, 0, 0],
[0, 0, w4d, 0],
];
const projected3d = matrixMultiply(projection3d, rotated4d);
const rotated3d = matrixMultiply(rotationX, projected3d);
// Project from 3D to 2D
const w3d = 1 / (distance - rotated3d[2]);
// prettier-ignore
const projection2d = [
[w3d, 0, 0],
[0, w3d, 0],
];
const projected2d = matrixMultiply(projection2d, rotated3d);
return projected2d.map((v) => (v * width) / 3);
});
context.fillStyle = '#0D0308';
context.clearRect(0, 0, width, height);
context.fillRect(0, 0, width, height);
context.translate(width / 2, height / 2);
const color = hueCycle(hueStart, playhead);
context.shadowBlur = 25;
context.shadowColor = chroma(color).brighten(2);
edges.forEach(([a, b]) => {
line(context, projected[a], projected[b], {
lineWidth: 4,
stroke: color,
});
});
// angle += 0.01;
};
};
canvasSketch(sketch, settings);
function beat(value, intensity = 2, frequency = 2) {
const v = Math.atan(Math.sin(value * Math.PI * frequency) * intensity);
return (v + Math.PI / 2) / Math.PI;
}