-
Notifications
You must be signed in to change notification settings - Fork 15
/
noise-rings.js
109 lines (98 loc) Β· 2.53 KB
/
noise-rings.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
const canvasSketch = require('canvas-sketch');
const SimplexNoise = require('simplex-noise');
const getCurvePoints = require('cardinal-spline-js/').getCurvePoints;
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: 24,
timeLoops: 2,
loop: true,
displacement: 0.125 / 4,
curves: true,
radius: {
start: 0.0625,
delta: 0.05,
},
};
return ({ context, frame, width, height, playhead }) => {
// clear
context.clearRect(0, 0, width, height);
context.fillStyle = '#222';
context.fillRect(0, 0, width, height);
// Setup
const time = 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.01;
context.strokeStyle = '#fff';
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(displacePts(displacement, loop, time, playhead)),
R.map(pts => translateAll([width / 2, height / 2], pts)),
);
}
function displacePts(displacement, loop, time, playhead) {
return pts =>
pts.map(({ r, theta }) => ({
r:
r +
r *
displacement *
simplex.noise3D(
Math.cos(theta),
Math.sin(theta),
loop ? time : playhead,
),
theta,
}));
}