-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
319 lines (276 loc) · 7.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
// globals: general
var FDS;
var CanvasElement;
var CanvasContext;
var canvas_width;
var canvas_height;
var canvas_pos_in_doc;
var canvas_pos_in_doc_x;
var canvas_pos_in_doc_y;
var inv_canvas_width;
var inv_canvas_height;
var CanvasElementCopy; // second canvas element used for double buffering
var CanvasContextCopy;
var CanvasImageDataCopy;
// globals: buttons
var start_stop_b;
var color_p_c;
var fps_b;
// FPS
var lastDate;
var fps;
$(document).ready(function(e)
{
var grid_width = N_;
var grid_height = N_;
var jQueryCanvasElement = $('#my_canvas');
// get the canvas element and it's context
CanvasElement = jQueryCanvasElement.get(0);
CanvasContext = CanvasElement.getContext('2d');
canvas_width = parseInt(CanvasElement.getAttribute('width'));
canvas_height = parseInt(CanvasElement.getAttribute('height'));
// get the position of the canvas element inside the document;
// used to calculate the position of the mouse when the cursor is moving
// inside the canvas element
canvas_pos_in_doc = jQueryCanvasElement.position();
canvas_pos_in_doc_x = canvas_pos_in_doc.left;
canvas_pos_in_doc_y = canvas_pos_in_doc.top;
// create inverted width and heigt for normalized positions inside the
// element where we going to draw(our canvas)
inv_canvas_width = 1 / canvas_width;
inv_canvas_height = 1 / canvas_height;
// copy the canvas element and it's context;
// used for double buffering
CanvasElementCopy = document.createElement('canvas'); // with jQuery: slower
CanvasElementCopy.width = grid_width;
CanvasElementCopy.height = grid_height;
CanvasContextCopy = CanvasElementCopy.getContext('2d');
CanvasImageDataCopy = CanvasContextCopy.createImageData(grid_width, grid_height);
// create a FDS object and initialise it
FDS = new STJ.FluidDynamicsSolver();
FDS.initFDS();
// take care of buttons and register events to them
// if mouse is moved, values are added to density and velocity
registerUserInput();
// start the main loop here
setInterval(mainCallback, 30);
});
function registerUserInput()
{
start_stop_b = false;
color_p_c = 0;
fps_b = false;
// attach callback to mouse
$(document).mousemove(mouseCallback);
// attach callback to buttons
$('#b0_start_stop').click(startStopButtonCallback);
$('#b1_reset').click(resetButtonCallback);
$('#b2_color').click(colorButtonCallback);
$('#b3_fps').click(fpsButtonCallback);
}
// just some variables are set here, the fps calculation happens in calcFPS()
function fpsButtonCallback()
{
lastDate = new Date;
fps = 0;
$('#b3_fps').children('#b3_text').fadeOut(200, fadeOutFadeIn);
fps_b = !fps_b;
function fadeOutFadeIn()
{
if(fps_b)
{
$(this).text(parseInt(fps)).fadeIn(200);
}
else
{
$(this).text('--').fadeIn(200);
}
}
}
// just the button text is changed, the color change happens in drawDensity()
function colorButtonCallback()
{
$('#b2_color').children('#b2_text').fadeOut(200, fadeOutFadeIn);
color_p_c++;
function fadeOutFadeIn()
{
switch(color_p_c % 4)
{
case 0:
{
$(this).text('white').fadeIn(200);
break;
}
case 1:
{
$(this).text('red').fadeIn(200);
break;
}
case 2:
{
$(this).text('green').fadeIn(200);
break;
}
case 3:
{
$(this).text('blue').fadeIn(200);
break;
}
}
}
}
// reset calculated values to the initial state
function resetButtonCallback()
{
FDS.initFDS();
}
// just the button text is changed, the pause is happening in mainCallback()
function startStopButtonCallback()
{
$('#b0_start_stop').children('#b0_text').fadeOut(200, fadeOutFadeIn);
start_stop_b = !start_stop_b;
function fadeOutFadeIn()
{
if(start_stop_b)
{
// initialize FDS values
$(this).text('stop').fadeIn(200);
}
else
{
$(this).text('start').fadeIn(200);
}
}
}
// values resulting from mouse movement pushed to FDS
function mouseCallback(event)
{
// capture mouse movement only if the start button was pressed
if(start_stop_b)
{
var x = event.pageX - canvas_pos_in_doc.left;
var y = event.pageY - canvas_pos_in_doc.top;
prepareSource(x, y);
}
}
function prepareSource(_x, _y)
{
var x = _x * inv_canvas_width;
var y = _y * inv_canvas_height;
var dx = (_x - canvas_pos_in_doc_x) * inv_canvas_width;
var dy = (_y - canvas_pos_in_doc_y) * inv_canvas_height;
addSource(x, y, dx, dy);
canvas_pos_in_doc_x = _x;
canvas_pos_in_doc_y = _y;
}
// x, y: normalized mouse position inside the canvas element
// dx, dy: normalized direction of mouse movement
function addSource(x, y, dx, dy)
{
var movement = dx * dx + dy * dy;
var index = 0;
var multiplicator = 30;
// check if mouse was moved
if(movement > 0)
{
// check if mouse is outside the canvas element
if(x < 0)
{
x = 0;
}
else if(x > 1)
{
x = 1;
}
if(y < 0)
{
y = 0;
}
else if(y > 1)
{
y = 1;
}
index = FDS.NORM_IX(x, y);
// push values to FDS globals
dens_prev_[index] = 1000;
u_prev_[index] = dx * multiplicator;
v_prev_[index] = dy * multiplicator;
}
}
// draw fluid in the canvas element
function drawDensity()
{
var img_data_i;
var dens_index;
// i: column, j: row
for(var j = 1; j < (N_ + 1); j++)
{
for(var i = 1; i < (N_ + 1); i++)
{
// in case you need to know what that is:
// https://developer.mozilla.org/En/HTML/Canvas/Pixel_manipulation_with_canvas
img_data_i = ((j - 1) * N_ * 4) + ((i - 1) * 4);
dens_index = FDS.REG_IX(i, j);
// write RGB value to canvas
// color button changes the color of the fluid
switch(color_p_c % 4)
{
case 0:
{
CanvasImageDataCopy.data[img_data_i] = dens_[dens_index];
CanvasImageDataCopy.data[img_data_i + 1] = dens_[dens_index];
CanvasImageDataCopy.data[img_data_i + 2] = dens_[dens_index];
break;
}
case 1:
{
CanvasImageDataCopy.data[img_data_i] = dens_[dens_index];
CanvasImageDataCopy.data[img_data_i + 1] = 0;
CanvasImageDataCopy.data[img_data_i + 2] = 0;
break;
}
case 2:
{
CanvasImageDataCopy.data[img_data_i] = 0;
CanvasImageDataCopy.data[img_data_i + 1] = dens_[dens_index];
CanvasImageDataCopy.data[img_data_i + 2] = 0;
break;
}
case 3:
{
CanvasImageDataCopy.data[img_data_i] = 0;
CanvasImageDataCopy.data[img_data_i + 1] = 0;
CanvasImageDataCopy.data[img_data_i + 2] = dens_[dens_index];
break;
}
}
CanvasImageDataCopy.data[img_data_i + 3] = 255;
}
}
// write new data back to the copy
CanvasContextCopy.putImageData(CanvasImageDataCopy, 0, 0);
// draw image on screen using the canvas copy
CanvasContext.drawImage(CanvasElementCopy, 0, 0, canvas_width, canvas_height);
}
function calcFPS()
{
var presentDate = new Date;
fps = 1000 / (presentDate - lastDate);
lastDate = presentDate;
$('#b3_fps').children('#b3_text').text(parseInt(fps));
}
function mainCallback()
{
// update fluid solver if start button was pressed
if(start_stop_b)
{
FDS.update();
}
// calculate frames per second if the fps button was pressed
if(fps_b)
{
calcFPS();
}
// draw density to canvas element
drawDensity();
}