-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathchromatic-noise.js
158 lines (129 loc) Β· 3.71 KB
/
chromatic-noise.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
const canvasSketch = require('canvas-sketch');
const lerp = require('lerp');
const SimplexNoise = require('simplex-noise');
const chroma = require('chroma-js');
const { mapRange, linspace } = require('canvas-sketch-util/math');
const { clipPolylinesToBox } = require('canvas-sketch-util/geometry');
const MarchingSquaresJS = require('marchingsquares');
const simplex = new SimplexNoise();
// 'chromatic';
const settings = {
animate: true,
duration: 4,
// dimensions: [1200, 630],
dimensions: [1920, 1080],
// dimensions: [800, 400],
scaleToView: true,
// playbackRate: 'throttle',
// fps: 24,
};
const colourScale = chroma.scale().domain([0, 1]);
const gridSize = [128 * 2, 128 * 2];
canvasSketch(() => {
return ({ context, width, height, playhead }) => {
context.clearRect(0, 0, width, height);
// context.fillStyle = '#2a0481';
context.fillStyle = '#131217'; // '#001';
context.fillRect(0, 0, width, height);
const padding = height * 0.2;
const off = 0.005;
const config = [
{
time: Math.sin(playhead * Math.PI - Math.PI * off * 3),
color: '#FF0000',
},
{
time: Math.sin(playhead * Math.PI - Math.PI * off * 2),
color: '#00FF00',
},
{
time: Math.sin(playhead * Math.PI - Math.PI * off),
color: '#0000FF',
},
{ time: Math.sin(playhead * Math.PI), color: '#fff' },
];
let data = config.map((c) => ({
color: c.color,
data: noiseData(c.time, padding, width, height),
}));
const bands = data.map((c) => ({
color: c.color,
lines: drawIsoLines(c.data, [width, height]),
}));
context.lineWidth = 24;
bands.forEach((l) => {
drawLines(l.lines, l.color, context);
});
};
}, settings);
function noiseData(time, padding, width, height) {
const data = [];
for (let y = 0; y < gridSize[1]; y++) {
data[y] = [];
for (let x = 0; x < gridSize[0]; x++) {
// get a 0..1 UV coordinate
const u = gridSize[0] <= 1 ? 0.5 : x / (gridSize[0] - 1);
const v = gridSize[1] <= 1 ? 0.5 : y / (gridSize[1] - 1);
// scale to dimensions with a border padding
const t = {
x: lerp(padding, width - padding, u),
y: lerp(padding, height - padding, v),
};
const _n = simplex.noise3D(
x / (gridSize[0] * 0.75),
y / (gridSize[1] * 0.75),
time * 0.5
);
const n = mapRange(_n, -1, 1, 0, 1);
data[y].push(n);
}
}
return data;
}
function drawIsoLines(noiseData, [sizeX, sizeY]) {
const intervals = linspace(12);
const lines = [];
intervals.forEach((_, idx) => {
if (idx > 0) {
const lowerBand = intervals[idx - 1];
const upperBand = intervals[idx];
MarchingSquaresJS.isoBands(noiseData, lowerBand, upperBand - lowerBand, {
successCallback(bands) {
bands.forEach((band) => {
const scaledBand = band.map(([x, y]) => {
return [
mapRange(x, 0, gridSize[0] - 1, 0, sizeX),
mapRange(y, 0, gridSize[1] - 1, 0, sizeY),
];
});
lines.push(drawShape(scaledBand));
});
},
noQuadTree: true,
noFrame: true,
});
}
});
const margin = sizeY * 0.04;
return clipPolylinesToBox(lines, [
margin,
margin,
sizeX - margin,
sizeY - margin,
]);
}
function drawLines(lines, color, context) {
context.strokeStyle = color;
lines.forEach((line) => {
const [start, ...pts] = line;
context.beginPath();
context.moveTo(...start);
pts.forEach((pt) => {
context.lineTo(...pt);
});
context.stroke();
});
}
function drawShape([start, ...pts]) {
return [start, ...pts, start];
}