-
Notifications
You must be signed in to change notification settings - Fork 63
/
waves.js
84 lines (64 loc) · 1.76 KB
/
waves.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
importScripts('helpers.js')
postMessage(['sliders', defaultControls.concat([
{label: 'Angle', value: 0, min: 0, max: 360},
{label: 'Step size', value: 5, min: 1, max: 20, step: 0.1},
])]);
onmessage = function(e) {
const [ config, pixData ] = e.data;
const getPixel = pixelProcessor(config, pixData)
const pi=Math.PI
const cos = Math.cos(config.Angle/180*pi)
const sin = Math.sin(config.Angle/180*pi)
const a = config['Step size']
const w = config.width
const h = config.height
const L = Math.sqrt(w*w+h*h)
let left = [], right=[];
let lastline, line = [];
function inside(x,y){ return (x>=0 && y>=0 && x<w && y<h) }
function pix(x,y) { return (inside(x,y)) ? (255-getPixel(Math.floor(x),Math.floor(y)))*a/255 : 0 }
// initial straight line
let x=(w - L*cos)/2, y=(h - L*sin)/2
for (let i=0;i<L;i++) {
x+=cos
y+=sin
// if (inside(x,y))
line.push([x,y])
}
left.push(line)
for (let j=0;j<L/2/a;j++){
lastline=line, line=[]
for (let i=0;i<L;i++) {
x=lastline[i][0] + sin*a
y=lastline[i][1] - cos*a
let z = pix(x,y)
x+= sin*z
y-= cos*z
line.push([x,y])
}
left.push(line)
}
line=left[0];
for (let j=0;j<L/2/a;j++){
lastline=line, line=[]
for (let i=0;i<L;i++) {
x=lastline[i][0] - sin*a
y=lastline[i][1] + cos*a
let z = pix(x,y)
x-= sin*z
y+= cos*z
line.push([x,y])
}
right.push(line)
}
right.reverse()
let temp= right.concat(left), output=[]
for (let i=0;i<temp.length;i++) {
let line=temp[i], newline=[]
for (let j=0;j<line.length;j++){
if (inside(line[j][0], line[j][1])) newline.push(line[j])
}
if (newline.length>1) output.push(newline);
}
postLines(output);
}