-
Notifications
You must be signed in to change notification settings - Fork 63
/
longwave.js
91 lines (73 loc) · 2.72 KB
/
longwave.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
importScripts('helpers.js')
postMessage(['sliders', defaultControls.concat([
{label: 'Wave Speed', value: 20, min: 1, max: 100},
{label: 'Wave Amplitude', value: 10, min: 0, max: 50, step: 0.1},
{label: 'Step size', value: 5, min: 1, max: 20, step: 0.1},
{label: 'Simplify', value: 10, min: 1, max: 50, step: 0.1},
{label: 'Depth', value: 1, min: 1, max: 8},
{label: 'Direction', type:'select', value:'Vertical', options:['Vertical', 'Horizontal', 'Both']},
{label: 'Optimize route', type:'checkbox', checked:false}
])]);
onmessage = function(e) {
const [ config, pixData ] = e.data;
const getPixel = pixelProcessor(config, pixData)
const lineSpacing = 2*config['Step size'];
const waveFreq = Math.pow(config['Wave Speed'],0.8)/config.width;
const waveAmplitude = config['Wave Amplitude']/50/waveFreq;
const simplify = config.Simplify;
let lines = [];
const t = [];
for (let i=0;i<config.Depth;i++) t.push( 128+i*128/config.Depth )
const thresholds = t.concat(t.slice(0,t.length-1).reverse())
let ln = 0;
if (config.Direction != 'Horizontal') {
for (let sx = -waveAmplitude; sx <= config.width + waveAmplitude; sx += lineSpacing) {
let line = [];
let threshold = thresholds[ln++ % thresholds.length];
let hysteresis = 0;
for (let y =0; y < config.height; y ++) {
let nx = sx + Math.sin( y*waveFreq )*waveAmplitude;
hysteresis += getPixel(nx, y)>threshold ? 1:-1;
if (hysteresis<= -simplify) hysteresis= -simplify;
if (hysteresis> simplify) hysteresis= simplify;
if (nx>0 && nx<config.width && hysteresis>0) {
line.push([nx, y])
} else {
if (line.length) {
lines.push(line);
line=[]
}
}
}
if (line.length) lines.push(line);
}
}
if (config.Direction != 'Vertical') {
for (let sy = -waveAmplitude; sy <= config.height + waveAmplitude; sy += lineSpacing) {
let line = [];
let threshold = thresholds[ln++ % thresholds.length];
let hysteresis = 0;
for (let x =0; x < config.width; x ++) {
let ny = sy + Math.sin( x*waveFreq )*waveAmplitude;
hysteresis += getPixel(x, ny)>threshold ? 1:-1;
if (hysteresis<= -simplify) hysteresis= -simplify;
if (hysteresis> simplify) hysteresis= simplify;
if (ny>0 && ny<config.height && hysteresis>0) {
line.push([x, ny])
} else {
if (line.length) {
lines.push(line);
line=[]
}
}
}
if (line.length) lines.push(line);
}
}
if (config['Optimize route']) {
postMessage(['msg', "Optimizing..."]);
lines = sortlines(lines)
postMessage(['msg', ""]);
}
postLines(lines);
}