-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathflow-field.js
169 lines (135 loc) Β· 4.13 KB
/
flow-field.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
159
160
161
162
163
164
165
166
167
168
169
const canvasSketch = require('canvas-sketch');
const Random = require('canvas-sketch-util/random');
const { mapRange } = require('canvas-sketch-util/math');
const { clipPolylinesToBox } = require('canvas-sketch-util/geometry');
const scale = 2;
const debug = false;
const trace = true;
const settings = {
dimensions: [800 * scale, 600 * scale],
scaleToView: true,
animate: true,
duration: 2,
playbackRate: 'throttle',
fps: 24,
};
const FREQUENCY = 0.001 / scale;
const AMPLITUDE = 5;
const PARTICLE_COUNT = debug ? 100 : 900;
const DAMPING = 0.1;
const STEP = 5 * scale;
const PARTICLE_STEPS = 30 * scale;
const sketch = () => {
const seed = 'noise-flow-field';
Random.setSeed(seed);
let particles = [];
let STEPS_TAKEN = 0;
return {
begin({ width, height }) {
STEPS_TAKEN = 0;
particles = [];
// Generate some particles with a random position
for (let i = 0; i < PARTICLE_COUNT; i++) {
particles.push({
x: Random.range(0, width),
y: Random.range(0, height),
vx: 0,
vy: 0,
line: [],
color: debug
? '#da3900'
: Random.pick(['#fcfaf1', '#aaa', '#cacaca', '#e6b31e']),
});
}
},
render({ context, width, height, playhead }) {
context.clearRect(0, 0, width, height);
context.fillStyle = '#343434';
context.fillRect(0, 0, width, height);
const margin = 0.03 * width;
const clipBox = [
[margin, margin],
[width - margin, height - margin],
];
if (debug) {
drawVectorField(context, width, height);
}
STEPS_TAKEN = Math.floor(mapRange(playhead, 0, 1, 0, PARTICLE_STEPS));
if (particles[0].line.length < PARTICLE_STEPS) {
particles.forEach((particle) => {
moveParticle(particle);
});
}
const lines = particles.map((particle) => particle.line);
const clippedLines = clipPolylinesToBox(lines, clipBox, false, false);
context.lineWidth = 4;
context.lineJoin = 'round';
context.lineCap = 'round';
if (trace) {
clippedLines.forEach((line, index) => {
const [start, ...pts] = line;
context.beginPath();
context.moveTo(...start);
pts.forEach((pt) => {
context.lineTo(...pt);
});
context.strokeStyle = particles[index].color;
context.stroke();
});
} else {
clippedLines.forEach((line, index) => {
const tail = line[line.length - 1];
context.fillStyle = particles[index].color;
context.beginPath();
context.arc(...tail, 10, 0, 2 * Math.PI);
context.fill();
});
}
},
};
};
canvasSketch(sketch, settings);
function moveParticle(particle) {
// Calculate direction from noise
const angle = Random.noise2D(particle.x, particle.y, FREQUENCY, AMPLITUDE);
// Update the velocity of the particle
// based on the direction
particle.vx += Math.cos(angle) * STEP;
particle.vy += Math.sin(angle) * STEP;
// Move the particle
particle.x += particle.vx;
particle.y += particle.vy;
// Use damping to slow down the particle (think friction)
particle.vx *= DAMPING;
particle.vy *= DAMPING;
particle.line.push([particle.x, particle.y]);
}
function drawVectorField(context, width, height) {
// const angle = Random.noise2D(particle.x, particle.y, FREQUENCY, AMPLITUDE);
const length = 20;
const thickness = 4;
const padding = 0; // 0.1 * height;
for (let x = 0; x < 32; x++) {
for (let y = 0; y < 32; y++) {
context.save();
context.fillStyle = 'rgba(255, 255, 255, 0.5)';
const t = {
x: mapRange(x, 0, 31, padding, width - padding),
y: mapRange(y, 0, 31, padding, height - padding),
};
const angle = Random.noise2D(t.x, t.y, FREQUENCY, AMPLITUDE);
// Rotate in place
context.translate(t.x, t.y);
context.rotate(angle);
context.translate(-t.x, -t.y);
// Draw the line
context.fillRect(
t.x - length / 2,
t.y - thickness / 2,
length,
thickness
);
context.restore();
}
}
}