-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflow-field-tings.js
206 lines (175 loc) · 5.59 KB
/
flow-field-tings.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
const canvasSketch = require("canvas-sketch");
const Random = require("canvas-sketch-util/random");
const SimplexNoise = require("simplex-noise");
const { mapRange } = require("canvas-sketch-util/math");
const { clipPolylinesToBox } = require("canvas-sketch-util/geometry");
const chroma = require("chroma-js");
const { heading, calcVec, normalize } = require("./math");
const scale = 1;
const debug = true;
const trace = true;
const settings = {
dimensions: [800 * scale, 600 * scale],
// scaletToFit: true,
animate: true,
duration: 20,
// playbackRate: "throttle",
fps: 24,
};
const FREQUENCY = 0.01 / scale;
const AMPLITUDE = 5;
const PARTICLE_COUNT = debug ? 100 : 200;
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)]];
// debug
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 = 2;
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();
});
}
// Draw the flow field
if (trace) {
for (let y = 0; y < height; y += 20) {
for (let x = 0; x < width; x += 20) {
const angle = Random.noise2D(x * FREQUENCY, y * FREQUENCY);
const length = 20;
const x2 = x + Math.cos(angle) * length;
const y2 = y + Math.sin(angle) * length;
context.strokeStyle = "rgba(255, 255, 255, 0.1)";
context.beginPath();
context.moveTo(x, y);
context.lineTo(x2, y2);
context.stroke();
}
}
}
},
};
};
canvasSketch(sketch, settings);
/**
* Moves the provided particle by:
* - Calculating the angle/direction based on Perlin noise
* - Updating the particle's velocity based on that angle and the step size
* - Moving the particle by its velocity
* - Applying damping to the velocity
* - Saving the particle's position to its line array
*/
function moveParticle(particle) {
// Calculate direction from noise
// const angle = Random.noise2D(particle.x, particle.y, FREQUENCY, AMPLITUDE);
const angle = Random.noise2D(
particle.x * FREQUENCY,
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]);
}
/**
* Draws a vector field visualization using Perlin noise.
*
* For a grid of points, calculates a noise-based angle at each point, draws a rotated
* line segment with that angle.
*
* @param {CanvasRenderingContext2D} context - The canvas context
* @param {number} width - Width of the canvas
* @param {number} height - Height of the canvas
*/
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();
}
}
}