-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.js
368 lines (290 loc) · 10.5 KB
/
main.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/**
* Demo usage of the FluidSolver class.
*
* @author Topaz Bar <[email protected]>
*/
import { GUI } from 'https://cdn.jsdelivr.net/npm/[email protected]/+esm';
import { Particle } from './particle.js';
import { FluidSolver } from './fluidsolver.js';
import { AppGUI } from './gui.js';
const NUM_OF_CELLS = 128, // Number of cells (not including the boundary)
VIEW_SIZE = 640; // View size (square)
const CELL_SIZE = VIEW_SIZE / NUM_OF_CELLS,// Size of each cell in pixels
CELL_SIZE_CEIL = Math.ceil(CELL_SIZE); // Size of each cell in pixels (ceiling)
// noinspection JSUnresolvedVariable
const isMobile = window.navigator.userAgentData.mobile;
console.log(`isMobile: ${isMobile}`);
// Globals
const canvas = document.getElementById('main-canvas'),
context = canvas.getContext('2d');
// Create the fluid solver
const fs = new FluidSolver(NUM_OF_CELLS);
fs.resetVelocity();
// We draw the density on a bitmap for performance reasons
const fdBuffer = context.createImageData(VIEW_SIZE, VIEW_SIZE);
// Demo app variables
let lastTime = Date.now(),
isMouseDown = true,
oldMouseX = 0,
oldMouseY = 0,
particles = [];
// App options object for the gui
const appOptions = {
fluidSolver: fs,
drawVelocityField: false,
drawDensityField: true,
drawParticles: false,
grayscale: false,
resetParticles: () => {
particles.length = 0;
}
};
// Set up the gui
const gui = new AppGUI(GUI, { width: 400, autoPlace: false }, appOptions);
gui.init();
// Set render states
canvas.width = canvas.height = VIEW_SIZE; // View size
context.lineWidth = 1; // Velocity field line width
context.strokeStyle = 'rgb(192, 0, 0)'; // Velocity field color
// Disable smoothing when using floating point pixel values
context.imageSmoothingEnabled = false;
//<editor-fold desc="Mouse and touch event listeners registration">
document.addEventListener('mouseup', () => { isMouseDown = false; }, false);
document.addEventListener('mousedown', () => { isMouseDown = true; }, false);
// Mouse move listener (on the canvas element)
canvas.addEventListener('mousemove', onMouseMove, false);
// Touch listeners (on the canvas element)
if (isMobile) {
canvas.addEventListener('touchstart', onTouchStart, false);
canvas.addEventListener('touchend', onTouchEnd, false);
canvas.addEventListener('touchleave', onTouchEnd, false);
canvas.addEventListener('touchcancel', onTouchEnd, false);
canvas.addEventListener('touchmove', onTouchMove, false);
// Dom stuff for mobile.
// FIXME: should probably replace this with media queries
const mainContainer = document.querySelector('#main-grid');
mainContainer.classList.remove('t1008-grid');
mainContainer.classList.add('container-fluid');
const canvasContainer = document.querySelector('#canvas-container');
canvasContainer.classList.add('mobile');
}
//</editor-fold>
/**
* Main mouse move listener
*
* @param e {MouseEvent|Object}
*/
function onMouseMove(e) {
const mouseX = e.offsetX,
mouseY = e.offsetY;
// Find the cell below the mouse
const i = (mouseX / VIEW_SIZE) * NUM_OF_CELLS + 1,
j = (mouseY / VIEW_SIZE) * NUM_OF_CELLS + 1;
// Don't overflow grid bounds
if (i > NUM_OF_CELLS || i < 1 || j > NUM_OF_CELLS || j < 1) return;
// Mouse velocity
const du = (mouseX - oldMouseX) * 0.5,
dv = (mouseY - oldMouseY) * 0.5;
// Add the mouse velocity to cells above, below, to the left, and to the right.
fs.uOld[fs.I(i, j)] = du;
fs.vOld[fs.I(i, j)] = dv;
fs.uOld[fs.I(i + 1, j)] = du;
fs.vOld[fs.I(i + 1, j)] = dv;
fs.uOld[fs.I(i - 1, j)] = du;
fs.vOld[fs.I(i - 1, j)] = dv;
fs.uOld[fs.I(i, j + 1)] = du;
fs.vOld[fs.I(i, j + 1)] = dv;
fs.uOld[fs.I(i, j - 1)] = du;
fs.vOld[fs.I(i, j - 1)] = dv;
if (isMouseDown) {
// If holding down the mouse, add density to the cell below the mouse
fs.dOld[fs.I(i, j)] = 150;
}
if (isMouseDown && appOptions.drawParticles) {
// Add particles
for (let k = 0; k < 5; k++) {
const p = new Particle(mouseX + getRandom(-50, 50), mouseY + getRandom(-50, 50));
p.vx = du;
p.vy = dv;
particles.push(p);
}
}
// Save current mouse position for next frame
oldMouseX = mouseX;
oldMouseY = mouseY;
} // End onMouseMove()
/**
* Touch listeners.
*
* preventDefault() prevents the mouse events from being dispatched.
*/
function onTouchStart(e) { e.preventDefault(); isMouseDown = true; }
function onTouchEnd(e) { e.preventDefault(); isMouseDown = false; }
/**
* Touch move listener.
* just passes the call to onMouseMove with the correct coordinates.
*
* @param e {*} The TouchEvent
*/
function onTouchMove(e) {
e.preventDefault();
const touches = e.touches;
if (touches && touches.length > 0) {
// console.log(touches[0]);
const x = touches[0].clientX - 100;
const y = touches[0].clientY - VIEW_SIZE - 50;
onMouseMove({ offsetX: x, offsetY: y });
}
}
//// End event listeners
/**
* Update loop
*/
function update(/*time*/) {
const invMaxColor = 1.0 / 255;
const deltaTime = (Date.now() - lastTime) / 1000;
// Step the fluid simulation
fs.velocityStep();
fs.densityStep();
// Clear the canvas
context.clearRect(0, 0, VIEW_SIZE, VIEW_SIZE);
// Draw the last frame's buffer and clear for drawing the current.
context.putImageData(fdBuffer, 0, 0);
clearImageData(fdBuffer);
// drawGrid();
if (appOptions.drawVelocityField) {
// Call once per frame
context.lineWidth = 1;
context.strokeStyle = 'rgb(192, 0, 0)';
context.beginPath();
}
// Render fluid
for (let i = 1; i <= NUM_OF_CELLS; i++) {
// The x position of current cell
const dx = (i - 0.5) * CELL_SIZE;
for (let j = 1; j <= NUM_OF_CELLS; j++) {
// The y position of current cell
const dy = (j - 0.5) * CELL_SIZE;
const cellIndex = i + (NUM_OF_CELLS + 2) * j;
// Draw density
const density = fs.d[cellIndex];
if (appOptions.drawDensityField && density > 0) {
const color = density * 255;
// fdBuffer.data is actually a Uint8ClampedArray so there is no need to manually clamp color values
//if (color < 0) color = 0;
//if (color > 255) color = 255;
const r = color;
const g = ((appOptions.grayscale) ? color : color * dx * invMaxColor);
const b = ((appOptions.grayscale) ? color : color * dy * invMaxColor);
// Draw the cell on an image for performance reasons
for (let l = 0; l < CELL_SIZE_CEIL; l++) {
for (let m = 0; m < CELL_SIZE_CEIL; m++) {
const pxX = (i - 1) * CELL_SIZE + l;
const pxY = (j - 1) * CELL_SIZE + m;
const pxIdx = ((pxX | pxX) + (pxY | pxY) * VIEW_SIZE) * 4;
fdBuffer.data[pxIdx ] = r;
fdBuffer.data[pxIdx + 1] = g;
fdBuffer.data[pxIdx + 2] = b;
fdBuffer.data[pxIdx + 3] = 255;
}
}
}
// Draw velocity field ?
if (appOptions.drawVelocityField && (i % 2) === 0 && (j % 2) === 0) {
const u = fs.u[cellIndex] * 150;
const v = fs.v[cellIndex] * 150;
context.moveTo(dx, dy);
context.lineTo(dx + u, dy + v);
}
} // End for all cells in the y direction
} // End for all cells in the x direction
if (appOptions.drawVelocityField) {
// Call once per frame
context.stroke();
}
// Update and render particles
let lastAlpha = 0,
particlesLength = particles.length;
if (appOptions.drawParticles) {
context.lineWidth = 2;
context.strokeStyle = 'rgb(255, 255, 255)';
context.beginPath();
for (let k = 0; k < particlesLength; k++) {
const p = particles[k];
p.age += deltaTime;
const alpha = (1 - (p.age / Particle.TIME_TO_LIVE));
if ((alpha < 0.01) ||
(p.age >= Particle.TIME_TO_LIVE) ||
(p.x <= 0 || p.x >= VIEW_SIZE || p.y <= 0 || p.y >= VIEW_SIZE)) {
p.dead = true;
} else {
const x0 = (p.x / VIEW_SIZE) * NUM_OF_CELLS + 2;
const y0 = (p.y / VIEW_SIZE) * NUM_OF_CELLS + 2;
const cellIndex = fs.I(x0, y0);
p.vx = fs.u[cellIndex] * 50;
p.vy = fs.v[cellIndex] * 50;
p.x += p.vx;
p.y += p.vy;
if (Math.abs(alpha - lastAlpha) > 0.01) {
// Only change stroke style if the alpha changed to save on render state changes.
context.strokeStyle = `rgba(255, 255, 255, ${alpha})`;
lastAlpha = alpha;
}
context.moveTo(p.x, p.y);
context.lineTo(p.x + p.vx, p.y + p.vy);
}
if (p.dead) {
// Remove dead particles, and update the length manually
particles.splice(k, 1);
particlesLength = particles.length;
}
} // End for all particles
context.stroke();
} // End if drawParticles
// lastTime is now
lastTime = Date.now();
requestAnimationFrame(update);
} // End update()
// Start app
update();
/**
* @param min {Number}
* @param max {Number}
* @returns {Number}
*/
function getRandom(min, max) {
return min + Math.random() * (max - min);
}
/**
* Clears all the pixels on the image data.
*
* @param image {ImageData}
*/
function clearImageData(image) {
for (let i = 0; i < image.data.length; i++) {
if ((i % 4) === 0) {
image.data[i] = 255;
} else {
image.data[i] = 0;
}
}
}
/**
* Draw the simulation grid. (for debugging and development purposes)
*/
function drawGrid() {
context.lineWidth = 1;
context.strokeStyle = 'rgb(255, 255, 255)';
context.beginPath();
// Vertical
for (let i = 0; i <= VIEW_SIZE; i += CELL_SIZE_CEIL) {
context.moveTo(i, 0);
context.lineTo(i, VIEW_SIZE);
}
// Horizontal
for (let i = 0; i <= VIEW_SIZE; i += CELL_SIZE_CEIL) {
context.moveTo(0, i);
context.lineTo(VIEW_SIZE, i);
}
context.stroke();
}