-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
133 lines (110 loc) · 3.16 KB
/
script.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
const playButton = document.querySelector(".button-wrapper")
let maxi = maximilian();
let maxiEngine = new maxi.maxiAudio();
let osc1 = new maxi.maxiOsc();
let osc2 = new maxi.maxiOsc();
let osc3 = new maxi.maxiOsc();
let drawOutput = 0;
let audioOut;
let playAudio = () => {
playButton.removeEventListener("click", playAudio)
playButton.classList.add("hide")
maxiEngine.init();
maxiEngine.play = () => {
audioOut = ((osc1.sinewave(120) * 2) * osc3.sinewave(0.5)) * osc2.sinewave(0.2)
return audioOut
}
// CANVAS -------------------------------------
let fov = 500;
let canvas = document.querySelector("canvas");
let width = window.innerWidth;
let height = window.innerHeight;
let context = canvas.getContext("2d");
canvas.setAttribute("width", width);
canvas.setAttribute("height", height);
let point = [];
let points = [];
let point3d = [];
let angleX = 0;
let angleY = 0;
let HALF_WIDTH = width / 2;
let HALF_HEIGHT = height / 2;
let x3d = 0;
let y3d = 0;
let z3d = 0;
let lastScale = 0;
let lastx2d = 0;
let lasty2d = 0;
let dim = 50;
let spacing = ((Math.PI * 2) / dim);
let numPoints = dim * dim;
let size = 250;
for (let i = 0; i < dim; i++) {
let z = size * Math.cos(spacing / 2 * i);
let s = size * Math.sin(spacing / 2 * i);
for (let j = 0; j < dim; j++) {
let point = [Math.cos(spacing * j) * s, Math.sin(spacing * j) * s, z];
points.push(point);
}
}
function draw() {
// fetch audio in
let audioIn = audioOut * 10 || 0
if (audioIn === null || audioIn === undefined) {
audioIn = 0
}
fov = 500 + audioIn
console.log(audioIn)
context.fillStyle = "rgb(0,0,0)";
context.fillRect(0, 0, width, height);
angleX = 0.0001 + (audioIn * 0.00055);
angleY = 0.0001 + (audioIn * 0.00055);
for (let i = 0; i < numPoints; i++) {
point3d = points[i];
z3d = point3d[2];
if (z3d < -fov) z3d += 0;
point3d[2] = z3d;
rotateX(point3d, angleX);
rotateY(point3d, angleY);
x3d = point3d[0];
y3d = point3d[1];
z3d = point3d[2];
let scale = (fov / (fov + z3d));
let x2d = (x3d * scale) + HALF_WIDTH;
let y2d = (y3d * scale) + HALF_HEIGHT;
context.lineWidth = 1;
context.strokeStyle = "rgb(255,255,255)";
context.beginPath();
context.moveTo(x2d, y2d);
context.lineTo(x2d + scale, y2d);
context.stroke();
}
requestAnimationFrame(draw);
}
requestAnimationFrame(draw);
function rotateX(point3d, angleX) {
let x = point3d[0];
let z = point3d[2];
let cosRY = Math.cos(angleX);
let sinRY = Math.sin(angleX);
let tempz = z;
let tempx = x;
x = (tempx * cosRY) + (tempz * sinRY);
z = (tempx * -sinRY) + (tempz * cosRY);
point3d[0] = x;
point3d[2] = z;
}
function rotateY(point3d, angleY) {
let y = point3d[1];
let z = point3d[2];
let cosRX = Math.cos(angleY);
let sinRX = Math.sin(angleY);
let tempz = z;
let tempy = y;
y = (tempy * cosRX) + (tempz * sinRX);
z = (tempy * -sinRX) + (tempz * cosRX);
point3d[1] = y;
point3d[2] = z;
}
}
playButton.addEventListener("click", playAudio)