-
Notifications
You must be signed in to change notification settings - Fork 16
/
noisy-grid.js
73 lines (63 loc) Β· 2.27 KB
/
noisy-grid.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
const canvasSketch = require('canvas-sketch');
const SimplexNoise = require('simplex-noise');
const simplex = new SimplexNoise();
const settings = {
animate: true,
duration: 8,
dimensions: [1080, 1080],
scaleToView: true,
};
// const glyphs = ['β', 'β', 'β', 'β', 'β', 'β', 'β', 'β', 'β³'];
const glyphs = ['β§', 'β¨', 'β©', 'βͺ', 'β ', 'β‘', 'β¬', 'β¬', 'β¬'];
// const glyphs = ['Β€', 'β³', 'β', 'β', 'β', 'β', 'β', 'β', 'β'];
canvasSketch(() => {
const gridSize = 24;
const frequency = 1 / (gridSize * 2);
return ({ context, width, height, playhead }) => {
context.clearRect(0, 0, width, height);
context.fillStyle = '#001';
context.fillRect(0, 0, width, height);
const time = Math.sin(playhead * 2 * Math.PI);
const padding = width * 0.15;
const tileSize = (width - padding * 2) / gridSize;
for (let x = 0; x <= gridSize; x++) {
for (let y = 0; y <= gridSize; y++) {
// Get the noise value
const n = simplex.noise3D(x * frequency, y * frequency, time);
// Map the noise to 0 - 360 degree angle
const angle = mapRange(n, -1, 1, 0, 360);
// Convert angle to a direction
const dir = Math.round(angle / 45);
const position = [
mapRange(x, 0, gridSize, padding, width - padding),
mapRange(y, 0, gridSize, padding, height - padding),
];
// Visualize the grid
context.font = `400 ${tileSize * 0.6}px 'monospace'`;
context.textAlign = 'center';
context.textBaseline = 'middle';
context.fillStyle = '#fff';
context.fillText(glyphs[dir], position[0], position[1]);
}
}
};
}, settings);
function mapRange(value, inputMin, inputMax, outputMin, outputMax, clamp) {
if (Math.abs(inputMin - inputMax) < Number.EPSILON) {
return outputMin;
} else {
var outVal =
((value - inputMin) / (inputMax - inputMin)) * (outputMax - outputMin) +
outputMin;
if (clamp) {
if (outputMax < outputMin) {
if (outVal < outputMax) outVal = outputMax;
else if (outVal > outputMin) outVal = outputMin;
} else {
if (outVal > outputMax) outVal = outputMax;
else if (outVal < outputMin) outVal = outputMin;
}
}
return outVal;
}
}