-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
149 lines (115 loc) · 2.97 KB
/
index.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
import { SVG } from '@svgdotjs/svg.js';
import hull from 'hull.js';
import { Noise } from 'noisejs';
import simplify from 'simplify-js';
let draw;
let output;
let seedOutput;
let noise;
let config = {};
document.addEventListener('DOMContentLoaded', () => {
draw = new SVG()
.addTo('#svg-holder')
.size(304, 304);
output = document.querySelector('#output');
seedOutput = document.querySelector('#seed');
document.querySelector('#regen').addEventListener('click', e => {
e.preventDefault();
reseed();
generatePoints();
drawPoly();
});
[...document.querySelectorAll('input')].forEach(input => {
const val = getValue(input);
config[input.dataset.controls] = val;
updateLabel(input, val);
input.addEventListener('input', e => {
const key = e.target.dataset.controls;
const val = getValue(e.target);
config[key] = val;
updateLabel(e.target, val);
if (e.target.dataset.shouldRegenerate) {
if (key === "seed") {
console.log(val);
reseed(val);
}
generatePoints();
}
drawPoly();
});
});
reseed();
generatePoints();
drawPoly();
});
const reseed = (seed) => {
config.seed = seed || Math.floor(Math.random() * 65536);
noise = new Noise(config.seed);
seedOutput.value = config.seed;
}
const generatePoints = () => {
config.noise = [];
for (let x = 0; x < 100; x++) {
for (let y = 0; y < 100; y++) {
let val = Math.abs(noise.simplex2(
x / config.resolution,
y / config.resolution
));
let vIntensity = 1000 - config.vignette;
if (vIntensity) {
const vignette = 1 - (Math.hypot(50 - x, 50 - y) / vIntensity);
val *= vignette;
}
config.noise.push(val);
}
}
}
const drawPoly = () => {
draw.clear();
const points = [];
for (let i = 0; i < config.noise.length; i++) {
if (config.noise[i] >= config.threshold) {
const x = i % 100;
const y = Math.floor(i / 100);
points.push([2 + x * 3, 2 + y * 3]);
}
}
if (!points.length) return;
let outline = hull(points, config.concavity);
if (config.simplify) {
outline = simplify(
outline.map(p => ({x: p[0], y: p[1]})),
config.simplify
).map(p => [p.x, p.y]);
}
if (config.rectilinear) {
outline = outline.reduce((acc, curr, idx, src) => {
acc.push(curr);
if (idx < src.length - 1) {
const next = src[idx + 1];
acc.push([curr[0], next[1]]);
}
return acc;
}, []);
}
draw
.polyline(outline)
.fill('none')
.stroke({
color: '#000',
width: 2,
linecap: 'round',
linejoin: 'round'
});
output.value = JSON.stringify(outline);
};
const getValue = elem => {
return elem.type === 'checkbox'
? elem.checked
: parseFloat(elem.value);
}
const updateLabel = (elem, val) => {
if (elem.type !== 'range') return;
const label = elem.parentElement;
label.querySelector('.val').innerText = val;
}