-
Notifications
You must be signed in to change notification settings - Fork 2
/
worker.js
247 lines (238 loc) · 8.61 KB
/
worker.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
let canvas = null;
const maxDepth = 50;
const particleAmount = 700;
let maxDistributionX;
let maxDistributionY;
const particles = new Array(particleAmount);
const placeParticles = () => {
for (let i = 0; i < particles.length; i += 1) {
particles[i] = {
x: random(-maxDistributionX, maxDistributionX),
y: random(-maxDistributionY, maxDistributionY),
z: random(1, maxDepth),
};
}
};
// const displayType = 0 // Can be any integer from 0 - 3
// let color="#222"
const normalize = (val, threshold=200) => ((val > threshold) ? val - threshold : 0);
const normalize1 = (val, max, min) => ((val-min)/(max-min))
const changeRange =(OldValue,NewRange, OldRange, OldMin, NewMin)=>((((OldValue - OldMin) * NewRange) / OldRange) + NewMin)
const random = (min, max) => Math.floor(Math.random() * (max - min)) + min;
const drawVisualizer = ({ bufferLength, dataArray, config }) => {
// let radius = 128
// const barWidth = canvas.width / bufferLength;
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height); // clears the canvas
let max = Math.max(...dataArray.slice(0,bufferLength))
let min = Math.min(...dataArray.slice(0,bufferLength))
let threshold = min + (max - min) * 0.68;
let radius =config.radius
// ctx.translate(250, 250)
// ctx.translate(canvas.width / 2, canvas.height / 2)
const heightsArr = dataArray.map(el=>{
if(config.beatDetection) return normalize(el, threshold)*(radius/80)
else return (el *0.4)*(radius/80)
})
for(let j=1;j<=heightsArr.length;j++){
heightsArr[j] = (heightsArr[(j-1)%(heightsArr.length)]+heightsArr[j%(heightsArr.length)]+heightsArr[(j+1)%(heightsArr.length)]+heightsArr[(j+2)%(heightsArr.length)]) /4
}
for (let i = 0; i < bufferLength; i++) {
// const height =normalize(dataArray[i],100,0)
// -i>0?(dataArray[i] *0.4)-i:(dataArray[i] *0.4)
drawLine(
{
i,
bufferLength,
heightsArr,
radius,
config
},
ctx
);
}
};
const drawLine = (opts, ctx) => {
const { i, radius, bufferLength, heightsArr, config } = opts;
const height = heightsArr[i]
const centerX = canvas.width / 2;
const centerY = canvas.height / 2;
const lineWidth = 2 * Math.PI * radius / bufferLength;
const rads = (Math.PI * 2) / bufferLength;
let rot = Math.ceil(changeRange(i,bufferLength, bufferLength , 0, -bufferLength/4))
const x = centerX + Math.cos(rads * rot) * (radius);
const y = centerY + Math.sin(rads * rot) * (radius);
const endX = centerX + Math.cos(rads * rot) * (radius + height);
const endY = centerY + Math.sin(rads * rot) * (radius + height);
// Mirror points
// rot = changeRange(i,bufferLength, bufferLength , 0, -bufferLength/2)
let width = canvas.width / bufferLength;
ctx.strokeStyle = config.color;
ctx.fillStyle = config.color
ctx.lineWidth = lineWidth;
ctx.lineCap = "round";
switch (config.displayType) {
case 1:
if (i == 0) {
ctx.beginPath()
ctx.moveTo(endX,endY)
}
ctx.lineTo(endX,endY)
if (i == bufferLength - 1) {
ctx.fill()
}
break; case 2:
// let width = canvas.width / bufferLength;
ctx.fillRect(i * width, 0, width, height)
break; case 3:
// let width = canvas.width / bufferLength;
if (i == 0) {
ctx.beginPath()
ctx.moveTo(0, 0)
}
ctx.lineTo(i * width, height)
if (i == bufferLength - 1) {
ctx.lineTo(canvas.width,0)
ctx.fill()
}
break; case 4:
ctx.fillRect(i * width, centerY - height, width, height * 2)
break; case 5:
let color = ctx.fillStyle.slice(1)
ctx.fillStyle = `rgba(${parseInt(color.slice(0,2),16)},${parseInt(color.slice(2,4),16)},${parseInt(color.slice(4,6),16)},${height / 64})`
ctx.fillRect(i * width, 0, width, canvas.height)
break; case 6:
ctx.beginPath()
ctx.arc((Math.sin(rads * i) * radius * 1.5) + centerX,(Math.cos(rads * i) * radius * 1.5)+centerY,height / 2,0,Math.PI * 2)
ctx.fill()
break; case 7:
ctx.beginPath()
ctx.arc(endX,endY,5,0,10)
ctx.fill()
break; case 8:
for (let j = 1; j < 6; j++) {
ctx.beginPath()
ctx.arc(
centerX + (Math.sin(rads * i) * (height + radius) * (j / 3)),
centerY + (Math.cos(rads * i) * (height + radius) * (j / 3)),
5,
0,
10
)
ctx.fill()
}
break; case 9:
ctx.beginPath()
ctx.arc(i * width,centerY,height,0,10)
ctx.fill()
break; case 10:
ctx.beginPath()
ctx.moveTo(
centerX + Math.cos(rads * i) * (radius - height),
centerY + Math.sin(rads * i) * (radius - height)
)
ctx.lineTo(
centerX + Math.cos(rads * i) * (radius + height),
centerY + Math.sin(rads * i) * (radius + height)
)
ctx.stroke()
break; case 11:
if(i<=bufferLength/2) {
const negx = centerX - Math.cos(rads * rot) * (radius);
const negy = centerY + Math.sin(rads * rot) * (radius);
const negendX = centerX - Math.cos(rads * rot) * (radius + heightsArr[i]);
const negendY = centerY + Math.sin(rads * rot) * (radius + heightsArr[i]);
ctx.beginPath();
ctx.moveTo(x, y);
ctx.lineTo(endX, endY);
ctx.moveTo(negx, negy);
ctx.lineTo(negendX, negendY);
ctx.stroke();
}
break; case 12:
const negendX = centerX + Math.cos(rads * rot) * (radius + heightsArr[bufferLength-i]);
const negendY = centerY + Math.sin(rads * rot) * (radius + heightsArr[bufferLength-i]);
if(i<=bufferLength/2) {
if (i == 0) {
ctx.beginPath()
ctx.moveTo(endX,endY)
}
ctx.lineTo(endX,endY)
if (i == bufferLength/2) {
ctx.moveTo(endX,endY)
}
} else {
ctx.lineTo(negendX,negendY)
if (i == bufferLength-1) {
rot = Math.ceil(changeRange(0,bufferLength, bufferLength , 0, -bufferLength/4))
const negendX = centerX + Math.cos(rads * rot) * (radius + heightsArr[0]);
const negendY = centerY + Math.sin(rads * rot) * (radius + heightsArr[0]);
ctx.lineTo(negendX,negendY)
ctx.fill()
}
}
break; default:
ctx.beginPath();
ctx.moveTo(x, y);
ctx.lineTo(endX, endY);
ctx.stroke();
}
};
const drawStars = ({ bufferLength, dataArray, config })=>{
if(!config.showParticles) return
const ctx = canvas.getContext("2d");
const r = parseInt(config.color.substr(1,2), 16)
const g = parseInt(config.color.substr(3,2), 16)
const b = parseInt(config.color.substr(5,2), 16)
const centerX = canvas.width / 2;
const centerY = canvas.height / 2;
let max = Math.max(...dataArray.slice(0,bufferLength/3))
let min = Math.min(...dataArray.slice(0,bufferLength/3))
threshold = min + (max - min) * 0.68;
let speed = config.bounce;
speed = speed>0.05?speed:0.05
// .3 or .5 looks good, 1 for no shade
// context.fillStyle = 'rgba(0, 34, 34, .5)';
// context.fillRect(0, 0, canvas.width, canvas.height);
// ctx.clearRect(0, 0, canvas.width, canvas.height);
for (let i = 0; i < particles.length; i += 1) {
particles[i].z -= speed;
if (particles[i].z <= 0) {
particles[i].x = random(-maxDistributionX, maxDistributionX);
particles[i].y = random(-maxDistributionY, maxDistributionY);
particles[i].z = maxDepth;
}
const k = 100 / particles[i].z;
const newX = particles[i].x * k + centerX;
const newY = particles[i].y * k + centerY;
if (newX >= 0 && newX <= canvas.width && newY >= 0 && newY <= canvas.height) {
const size = (1 - particles[i].z / maxDepth) * 12;
var radgrad = ctx.createRadialGradient(newX,newY,0,newX,newY,size/2);
radgrad.addColorStop(0, `rgba(${r},${g},${b},1)`);
radgrad.addColorStop(0.5, `rgba(${r},${g},${b},.5)`);
radgrad.addColorStop(1, `rgba(${r},${g},${b},0)`);
// draw shape
ctx.fillStyle = radgrad;
// ctx.fillStyle = config.color;
ctx.beginPath();
ctx.arc(newX, newY, size / 2, 0, Math.PI * 2, false);
ctx.closePath();
ctx.fill();
}
}
}
onmessage = function (e) {
console.log("Worker: Message received from main script");
const { bufferLength, dataArray, config, canvas: canvasMessage, resize_canvas } = e.data;
if (canvasMessage) {
canvas = canvasMessage;
maxDistributionX = canvas.width / 8
maxDistributionY = canvas.height / 4
placeParticles()
} else if (resize_canvas) {
[canvas.width, canvas.height] = resize_canvas
} else {
drawVisualizer({ bufferLength, dataArray, config });
drawStars({ bufferLength, dataArray, config })
}
};