-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathnoise-rects.js
134 lines (121 loc) Β· 3.12 KB
/
noise-rects.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
const canvasSketch = require('canvas-sketch');
const SimplexNoise = require('simplex-noise');
const lerp = require('lerp');
const { curve } = require('cardinal-spline-js/curve_func.min');
const R = require('ramda');
const { normalize, noiseGrid, randomNumber, range } = require('./math');
const { regularPolygon, translateAll, drawShape, arcs } = require('./geometry');
const simplex = new SimplexNoise('1234567890abcdefghijklmnopqrstuvwxyz');
const settings = {
animate: true,
duration: 8,
dimensions: [800, 800],
scaleToView: true,
// playbackRate: 'throttle',
// fps: 8,
};
canvasSketch(() => {
const opts = {
circleCount: 16,
segmentCount: 32,
timeLoops: 2,
loop: true,
displacement: 0.125,
curves: false,
radius: {
start: 0.0625,
delta: 0.05,
},
};
let time = 0;
return ({ context, frame, width, height, playhead }) => {
// clear
context.clearRect(0, 0, width, height);
context.fillStyle = '#fff';
context.fillRect(0, 0, width, height);
// Setup
time += 0.01; // Math.sin(playhead * opts.timeLoops * Math.PI);
const circles = R.pipe(
concentricRadii(width, opts.radius),
generateCircles(width, height, time, playhead, opts),
)(opts.circleCount);
// Draw
context.lineWidth = width * 0.003;
context.strokeStyle = '#222';
if (opts.curves) {
circles.forEach(drawCircle(context));
} else {
circles.forEach(pts => {
drawShape(context, pts);
context.stroke();
});
}
};
}, settings);
function drawCircle(context) {
return R.pipe(
R.flatten,
pts => {
context.beginPath();
curve(context, pts, 0.4, 48, true);
context.stroke();
context.closePath();
},
);
}
function concentricRadii(width, radius) {
return circleCount =>
range(circleCount).map(
idx => width * radius.start + width * radius.delta * idx,
);
}
function generateCircles(
width,
height,
time,
playhead,
{ segmentCount, displacement, loop },
) {
return R.pipe(
R.map(radius => arcs(segmentCount, radius)),
R.map(pts =>
pts.map(({ r, theta }) => ({
r:
r *
Math.min(
1 / Math.abs(Math.cos(theta)),
1 / Math.abs(Math.sin(theta)),
),
theta,
})),
),
R.map(displacePts(displacement, loop, time, playhead)),
R.map(pts => translateAll([width / 2, height / 2], pts)),
);
}
function rectPath(segmentCount, s) {
return [[-s, -s], [s, -s], [s, s], [-s, s]];
}
function displacePts(displacement, loop, time, playhead) {
return pts =>
pts.map(({ r, theta }) => ({
r:
r +
r *
displacement *
simplex.noise3D(
(Math.cos(theta) * r) / 1000,
(Math.sin(theta) * r) / 1000,
// Math.min(
// 1 / Math.abs(Math.cos(theta)),
// 1 / Math.abs(Math.sin(theta)),
// ) / 2,
// Math.min(
// 1 / Math.abs(Math.cos(theta)),
// 1 / Math.abs(Math.sin(theta)),
// ) / 2,
loop ? time : playhead,
),
theta,
}));
}