-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathcolor-noise.js
More file actions
184 lines (153 loc) Β· 4.61 KB
/
Copy pathcolor-noise.js
File metadata and controls
184 lines (153 loc) Β· 4.61 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
const canvasSketch = require('canvas-sketch');
const lerp = require('lerp');
const SimplexNoise = require('simplex-noise');
const { mapRange, linspace } = require('canvas-sketch-util/math');
const Random = require('canvas-sketch-util/random');
const { clipPolylinesToBox } = require('canvas-sketch-util/geometry');
const MarchingSquaresJS = require('marchingsquares');
const { generateRandomColorRamp } = require('fettepalette/dist/index.umd');
const simplex = new SimplexNoise('chromatic');
const config = { intervals: 12, lineWidth: 20, offset: 0.005 };
const settings = {
animate: true,
duration: 8,
dimensions: [1080, 1080],
scaleToView: true,
};
const gridSize = [128, 128];
const centerHue = Random.range(0, 300);
const clrs = generateRandomColorRamp({
total: config.intervals,
centerHue, //: 289.2,
hueCycle: 0.5,
// centerHue: Random.range(240, 300),
// hueCycle: Random.range(0.5, 1),
curveMethod: 'lamΓ©',
curveAccent: 0.2,
offsetTint: 0.251,
offsetShade: 0.01,
tintShadeHueShift: 0.0,
offsetCurveModTint: 0.03,
offsetCurveModShade: 0.03,
minSaturationLight: [0, 0],
maxSaturationLight: [1, 1],
});
const hsl = (c) => `hsla(${c[0]}, ${c[1] * 100}%, ${c[2] * 100}%, 1)`;
const bg = hsl(Random.pick(clrs.light));
canvasSketch(() => {
const levels = [
{ offset: Math.PI * config.offset * 3, colorScale: createColorScale(0.3) },
{ offset: Math.PI * config.offset * 2, colorScale: createColorScale(0.2) },
{ offset: Math.PI * config.offset, colorScale: createColorScale(0.1) },
{ offset: 0, colorScale: createColorScale(0) },
];
return ({ context, width, height, playhead }) => {
context.clearRect(0, 0, width, height);
context.fillStyle = bg;
context.fillRect(0, 0, width, height);
levels.forEach(({ offset, colorScale }) => {
const time = loopNoise(
{ cx: 0, cy: 0, radius: 1, offset, range: [0, 1] },
playhead
);
let data = noiseData(time);
const lines = drawIsoLines(data, [width, height], colorScale);
drawLines(lines, context);
});
};
}, settings);
function noiseData(time) {
const data = [];
for (let y = 0; y < gridSize[1]; y++) {
data[y] = [];
for (let x = 0; x < gridSize[0]; x++) {
const rawN = simplex.noise3D(
x / (gridSize[0] * 0.8),
y / (gridSize[1] * 0.8),
time
);
const n = mapRange(rawN, -1, 1, 0, 1);
data[y].push(n);
}
}
return data;
}
function drawIsoLines(noiseData, [sizeX, sizeY], colourScale) {
const intervals = linspace(config.intervals);
const lines = [];
MarchingSquaresJS.isoLines(noiseData, intervals, {
polygons: false,
linearRing: false,
}).forEach((isoLines, idx) => {
isoLines.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({
line: scaledBand,
color: colourScale(idx),
});
});
});
const margin = sizeY * 0.04;
return lines
.map(({ line, color }) => {
const clippedLines = clipPolylinesToBox(
[line],
[margin, margin, sizeX - margin, sizeY - margin],
false,
false
).flat();
return {
color,
line: clippedLines,
};
})
.filter(({ line }) => line.length > 0);
}
function drawLines(lines, context) {
context.lineJoin = 'round';
context.lineCap = 'round';
context.lineWidth = config.lineWidth;
lines.forEach(({ line, color }) => {
const [start, ...pts] = line;
context.strokeStyle = color;
context.beginPath();
context.moveTo(...start);
pts.forEach((pt, idx) => {
context.lineTo(...pt);
});
context.stroke();
});
}
function loopNoise({ cx, cy, radius, offset, range }, playhead) {
const v = Random.noise2D(
(cx + radius * Math.cos(Math.PI * 2 * playhead) + offset) / 10,
(cy + radius * Math.sin(Math.PI * 2 * playhead) + offset) / 10,
1,
1
);
return mapRange(v, -1, 1, range[0], range[1]);
}
function createColorScale(shift) {
const clrs = generateRandomColorRamp({
total: config.intervals,
centerHue: centerHue, //289.2,
hueCycle: 0.5 + shift,
curveMethod: 'lamΓ©',
curveAccent: 0.2,
offsetTint: 0.251,
offsetShade: 0.01,
tintShadeHueShift: 0.0,
offsetCurveModTint: 0.03,
offsetCurveModShade: 0.03,
minSaturationLight: [0, 0],
maxSaturationLight: [1, 1],
});
const hsl = (c) => `hsla(${c[0]}, ${c[1] * 100}%, ${c[2] * 100}%, 1)`;
const colors = clrs.all.map(hsl).filter((c) => c !== bg);
return (idx) => colors[idx];
}