-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
129 lines (108 loc) · 3.26 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
import * as tome from 'chromotome';
import SimplexNoise from 'simplex-noise';
import { draw_line, draw_poly, draw_grid } from './display';
let sketch = function(p) {
let THE_SEED;
let simplex;
let noise_grid;
const palette = tome.get('tsu_akasaka');
const grid_dim = 800;
const padding = 80;
const canvas_dim = grid_dim + 2 * padding;
const cell_dim = 2;
const n = grid_dim / cell_dim;
const noise_dim = 0.0025;
const persistence = 0.45;
p.setup = function() {
p.createCanvas(canvas_dim, canvas_dim);
THE_SEED = p.floor(p.random(9999999));
simplex = new SimplexNoise(THE_SEED);
p.randomSeed(THE_SEED);
noise_grid = build_noise_grid();
p.background(palette.background);
p.translate(padding, padding);
draw_grid(p, grid_dim, 12);
process_grid(0.3, 10, 0.7 / 10, ['#d4a710']);
process_grid(-1, 120, 1.3 / 120, []);
};
function process_grid(init, steps, delta, fill_palette) {
const thresholds = build_threshold_list(init, steps, delta, fill_palette);
const filled = fill_palette.length !== 0;
p.push();
for (let y = 0; y < n; y++) {
p.push();
for (let x = 0; x < n; x++) {
process_cell(x, y, filled, thresholds, delta);
p.translate(cell_dim, 0);
}
p.pop();
p.translate(0, cell_dim);
}
p.pop();
}
function process_cell(x, y, filled, thresholds, delta) {
const v1 = get_noise(x, y);
const v2 = get_noise(x + 1, y);
const v3 = get_noise(x + 1, y + 1);
const v4 = get_noise(x, y + 1);
// Some optimization
const min = p.min([v1, v2, v3, v4]);
const max = p.max([v1, v2, v3, v4]);
const relevant_thresholds = thresholds.filter(
t => t.val >= min - delta && t.val <= max
);
for (const t of relevant_thresholds) {
const b1 = v1 > t.val ? 8 : 0;
const b2 = v2 > t.val ? 4 : 0;
const b3 = v3 > t.val ? 2 : 0;
const b4 = v4 > t.val ? 1 : 0;
const id = b1 + b2 + b3 + b4;
if (filled) {
p.fill(t.col);
draw_poly(p, id, v1, v2, v3, v4, t.val, cell_dim);
} else {
p.stroke(palette.stroke ? palette.stroke : '#111');
draw_line(p, id, v1, v2, v3, v4, t.val, cell_dim);
}
}
}
function get_noise(x, y) {
return noise_grid[y][x];
}
function build_noise_grid() {
let grid = [];
for (let y = 0; y < n + 1; y++) {
let row = [];
for (let x = 0; x < n + 1; x++) {
row.push(sum_octave(16, x, y));
}
grid.push(row);
}
return grid;
}
function build_threshold_list(init, steps, delta, colors) {
let thresholds = [];
for (let t = 0; t <= steps; t++) {
let col = colors.length === 0 ? '#fff' : colors[p.floor(p.random(colors.length))];
thresholds.push({ val: init + t * delta, col: col });
}
return thresholds;
}
function sum_octave(num_iterations, x, y) {
let noise = 0;
let maxAmp = 0;
let amp = 1;
let freq = noise_dim;
for (let i = 0; i < num_iterations; i++) {
noise += simplex.noise2D(14.3 + x * freq, 5.71 + y * freq) * amp;
maxAmp += amp;
amp *= persistence;
freq *= 2;
}
return noise / maxAmp;
}
p.keyPressed = function() {
if (p.keyCode === 80) p.saveCanvas('sketch_' + THE_SEED, 'jpeg');
};
};
new p5(sketch);