-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.html
228 lines (225 loc) · 9.44 KB
/
index.html
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
<!doctype HTML>
<html>
<head>
<meta charset="utf-8">
<style>
body {
background: #333333;
color: white;
font-family: Helvetica;
}
</style>
<title>Simple fluid dynamics simulator</title> <!-- Based on Oliver's -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script src="pressure.js"></script>
<script src="pressure-display.js"></script>
<script>
onmouseup = function() { mouseIsDown = false; }
function log(message) {
document.getElementById("log").innerHTML = message;
}
var frames = 0;
var force = 5;
var source = 100;
var sources = [];
var omx, omy;
var mx, my;
var mouseIsDown = false;
var res;
var displaySize = 512;
var fieldRes;
var canvas;
function prepareFrame(field)
{
var canvas = document.getElementById("canvas");
var vel_factor = $("#mouse_velocity").val();
if ((omx >= 0 && omx < displaySize && omy >= 0 && omy < displaySize) && mouseIsDown) {
var dx = mx - omx;
var dy = my - omy;
var length = (Math.sqrt(dx * dx + dy * dy) + 0.5) | 0;
if (length < 1) length = 1;
for (var i = 0; i < length; i++) {
var x = (((omx + dx * (i / length)) / displaySize) * field.width()) | 0
var y = (((omy + dy * (i / length)) / displaySize) * field.height()) | 0;
field.setVelocity(x, y, dx * vel_factor, dy * vel_factor);
field.setDensity(x, y, $("#mouse_density").val());
}
omx = mx;
omy = my;
}
var vel_factor = $("#source_velocity").val();
for (var i = 0; i < sources.length; i++) {
var x = ((sources[i][0] / displaySize) * field.width()) | 0;
var y = ((sources[i][1] / displaySize) * field.height()) | 0;
field.setDensity(x, y, $("#source_density").val());
// For now, direction is right and slightly down
field.setVelocity(x, y, vel_factor, vel_factor * 0.2);
}
}
var running = false;
function stopAnimation() {
running = false;
clearTimeout(interval);
}
function startAnimation() {
if (running)
return;
running=true;
interval = setTimeout(updateFrame, 10);
}
var start = new Date;
var frames = 0;
function updateFrame() {
field.update();
var end = new Date;
frames++;
if ((end - start) > 1000) {
log("FPS: " + ((1000*frames/(end - start) + 0.5)|0));
start = end;
frames=0;
}
if (running)
interval = setTimeout(updateFrame, 10);
}
var options = {};
try {
options = JSON.parse(location.search.substring(1));
} catch(e) {
}
window.onload=function(){
canvas = document.getElementById("canvas");
field = new FluidField(canvas);
document.getElementById("iterations").value = 10;
res = document.getElementById("resolution");
res.value = options.res || "128";
document.getElementById("decay").value = 2;
document.getElementById("diffusion").value = 0;
document.getElementById("viscosity").value = 5;
document.getElementById("mouse_velocity").value = 1.0;
document.getElementById("mouse_density").value = 30;
document.getElementById("source_velocity").value = 0.0;
document.getElementById("source_density").value = 15;
field.setUICallback(prepareFrame);
updateRes = function() {
var r = parseInt(res.value);
canvas.width = r;
canvas.height = r;
fieldRes = r;
field.setResolution(r, r);
}
updateRes();
function getTopLeftOfElement(element) {
var top = 0;
var left = 0;
do {
top += element.offsetTop;
left += element.offsetLeft;
} while(element = element.offsetParent);
return {left: left, top: top};
}
canvas.onmousedown = function(event) {
var o = getTopLeftOfElement(canvas);
omx = mx = event.clientX - o.left;
omy = my = event.clientY - o.top;
if (!event.altKey && event.button == 0)
mouseIsDown = true;
else
sources.push([mx, my]);
event.preventDefault();
return false;
}
canvas.onmousemove = function(event) {
var o = getTopLeftOfElement(canvas);
mx = event.clientX - o.left;
my = event.clientY - o.top;
}
field.setDisplayFunction(toggleDisplayFunction(canvas));
startAnimation();
}
</script>
</head>
<body>
<table style="width: 100%;">
<tr>
<td>
<canvas id=canvas style="width: 512px; height: 512px;" width=512 height=512></canvas>
</td>
<td>
Simple fluid dynamics simulator based on the navier-stokes equations, implemented in JavaScript.
<ul>
<li>Click and drag to add density and velocity</li>
<li>Add density source with alt-click or right-button</li>
</ul>
<div id = log></div>
<button onclick="startAnimation()">start</button>
<button onclick="stopAnimation()">stop</button>
<button onclick="field.reset(); frames = 0; sources = [];">reset</button><br />
<button onclick="field.setDisplayFunction(toggleDisplayFunction(canvas))">Toggle drawing mode</button><br />
<table>
<tr>
<td>Decay</td>
<td><input id="decay" type="range" onchange="field.setDecay(1.0 - 0.001 * event.target.value)" min='0' max='10' step='1'></td>
</tr>
<tr>
<td>Diffusion Speed</td>
<td><input id="diffusion" type="range" onchange="field.setDiffusion(event.target.value)" min='0' max='8' step='1'></td>
</tr>
<tr>
<td>Viscosity</td>
<td><input id="viscosity" type="range" onchange="field.setViscosity(event.target.value)" min='0' max='10.0' step='0.5'></td>
</tr>
<tr>
<td>Solver<br/>Iterations</td>
<td><input id="iterations" type="range" onchange="field.setIterations(event.target.value)" min=1 max=75></td>
</tr>
<tr>
<td>Resolution</td>
<td><select id=resolution onchange="updateRes()">
<option>16</option>
<option>24</option>
<option>32</option>
<option>48</option>
<option>64</option>
<option>96</option>
<option>128</option>
<option>256</option>
<option>512</option>
</select></td>
</tr>
</table>
</td>
</tr>
</table>
<table>
<tr>
<td>
Mouse Cursor
<table>
<tr>
<td>Velocity</td>
<td><input type="range" id="mouse_velocity" min="0.0" max="10.0" step="0.5" /> </td>
</tr>
<tr>
<td>Density</td>
<td><input type="range" id="mouse_density" min="0" max="100" step="5" /> </td>
</tr>
</table>
</td>
<td style="width: 50px"></td>
<td>
Density Sources
<table>
<tr>
<td>Velocity</td>
<td><input type="range" id="source_velocity" min="0.0" max="10.0" step="0.5" /></td>
</tr>
<tr>
<td>Density</td>
<td><input type="range" id="source_density" min="0" max="100" step="5" /></td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>