-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
485 lines (420 loc) · 10.6 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
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
<!DOCTYPE html>
<html>
<head>
<link rel="icon" href="favicon.ico">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Osmosis simulator</title>
<style>
body {
margin: 0;
font-family: Arial, Helvetica, sans-serif;
color:rgb(255, 255, 255);
overflow: hidden;
background: #222;
}
#container {
position: relative;
}
#help {
position: absolute;
bottom: 0;
left: 0;
margin: 5px;
}
/* unvisited link */
a:link {
color: #bada55;
text-decoration: none;
}
/* visited link */
a:visited {
color: #bada55;
text-decoration: none;
}
/* mouse over link */
a:hover {
color: #bada55;
text-decoration: underline;
}
/* selected link */
a:active {
color: #bada55;
text-decoration: underline;
}
/* Dropdown controls */
/* Remove default bullets */
ul, #myUL {
list-style-type: none;
padding-left: 10px;
}
/* Remove margins and padding from the parent ul,
and set it floating top left over canvas */
#myUL {
margin: 5px;
padding: 10px;
position: absolute;
top: 0;
left: 0;
background-color: rgba(0,0,0,0.5);
border-radius: 10px;
}
/* Style the caret/arrow */
.caret {
cursor: pointer;
user-select: none; /* Prevent text selection */
}
/* Create the caret/arrow with a unicode, and style it */
.caret::before {
content: "\25B6";
color: rgb(255, 255, 255);
display: inline-block;
margin-right: 6px;
}
/* Rotate the caret/arrow icon when clicked on (using JavaScript) */
.caret-down::before {
transform: rotate(90deg);
}
/* Hide the nested list */
.nested {
display: none;
}
/* Show the nested list when the user clicks on the caret/arrow (with JavaScript) */
.active {
display: block;
}
/* Checkboxes */
/* from https://codepen.io/JAGATHISH1123/embed/PvayMB?default-tab=&theme-id= */
.switch {
position: relative;
display: inline-block;
width: 32px;
height: 16px;
background-color: rgba(255, 255, 255, 0.25);
border-radius: 16px;
transition: all 0.3s;
}
.switch::after {
content: '';
position: absolute;
width: 12px;
height: 12px;
border-radius:50%;
background-color: white;
top: 2px;
left: 2px;
transition: all 0.3s;
}
.checkbox:checked + .switch::after {
left : 16px;
}
.checkbox:checked + .switch {
background-color: #bada55;
}
.checkbox {
display : none;
}
/* SLIDERS */
/* The slider itself */
.slider {
-webkit-appearance: none; /* Override default CSS styles */
appearance: none;
width: 100px;
height: 16px;
border-radius: 8px;
background: rgba(255, 255, 255, 0.25);
}
/* The slider handle (use -webkit- (Chrome, Opera, Safari, Edge) and -moz- (Firefox) to override default look) */
.slider::-webkit-slider-thumb {
-webkit-appearance: none; /* Override default look */
appearance: none;
width: 12px;
height: 12px;
border-radius: 6px;
background: #bada55;
cursor: pointer;
}
.slider::-moz-range-thumb {
width: 12px;
height: 12px;
border-radius: 6px;
background: #bada55;
cursor: pointer;
}
output {
color: #bada55;
}
</style>
</head>
<body>
<div id="container">
<canvas id="myCanvas">
<p>This browser doesn't support canvas.</p>
</canvas>
<ul id="myUL">
<li><span class="caret">Controlli</span>
<ul class="nested">
<li>
<input type="checkbox" id="start" class="checkbox" />
<label for="start" class="switch"></label>
Avvia
</li>
<li>
Molecole d'acqua: <br />
<input type="range" min="0" max="50" value="25" class="slider" id="water" />
</li>
<li>
Ioni sodio: <br />
<input type="range" min="0" max="20" value="10" class="slider" id="sodium">
</li>
</ul>
</li>
</ul>
<div id="help">
<a href="help.html" target="_blank">Help & Credits</a>
</div>
</div>
</body>
<script>
// Dropdown controls
const toggler = document.getElementsByClassName("caret");
for (let i = 0; i < toggler.length; i++) {
toggler[i].addEventListener("click", function() {
this.parentElement.querySelector(".nested").classList.toggle("active");
this.classList.toggle("caret-down");
});
}
</script>
<script>
// Parameters
const size = 10;
const frictionThreshold = 20;
// Links to page elements
const canvas = document.getElementById("myCanvas");
const startBtn = document.getElementById("start");
const waterSlider = document.getElementById("water");
const sodiumSlider = document.getElementById("sodium");
// Set up canvas and context
let width = canvas.width = window.innerWidth;
let height = canvas.height = window.innerHeight;
const ctx = canvas.getContext('2d');
class Particle{
constructor(x, y, velX, velY) {
this.x = x;
this.y = y;
this.velX = velX;
this.velY = velY;
}
updateVel() {
if((this.x) >= width) {
this.velX += - 0.1 * (this.x - width);
}
if((this.x) <= 0) {
this.velX += - 0.1 * this.x;
}
if((this.y) >= height) {
this.velY += - 0.1 * ( this.y - height );
}
if((this.y) <= 0) {
this.velY += - 0.1 * this.y;
}
if(this.velX * this.velX + this.velY * this.velY >= frictionThreshold) {
this.velX += - this.velX * 0.1;
this.velY += - this.velY * 0.1;
}
}
update() {
this.updateVel();
this.x += this.velX;
this.y += this.velY;
}
}
class Water extends Particle{
constructor(x, y, velX, velY, theta) {
super(x, y, velX, velY);
this.theta = theta;
}
draw() {
ctx.beginPath();
ctx.fillStyle = "white";
ctx.arc(
this.x + 0.9584 * size * Math.cos(this.theta + 2.2252948),
this.y + 0.9584 * size * Math.sin(this.theta + 2.2252948),
size * 1.2,
0,
2 * Math.PI);
ctx.fill();
ctx.beginPath();
ctx.fillStyle = "white";
ctx.arc(
this.x + 0.9584 * size * Math.cos(this.theta - 2.2252948),
this.y + 0.9584 * size * Math.sin(this.theta - 2.2252948),
size * 1.2,
0,
2 * Math.PI);
ctx.fill();
ctx.beginPath();
ctx.fillStyle = "red";
ctx.arc(this.x, this.y, size * 1.52, 0, 2 * Math.PI);
ctx.fill();
}
updateVel() {
let d = width;
for(let j = 0; j < sodiums.length; j++) {
const dx = this.x - sodiums[j].x;
const dy = this.y - sodiums[j].y;
const distance = Math.sqrt(dx * dx + dy * dy);
if(distance < d) {
d = distance;
if(dx < 0) {
this.theta = Math.atan( dy / dx );
} else {
this.theta = Math.PI + Math.atan( dy / dx );
}
}
if(distance <= 100) {
const F = 400 / ( distance * distance );
this.velX += - F * ( dx / distance );
this.velY += - F * ( dy / distance );
const F2 = 12000 / ( distance * distance * distance );
this.velX += F2 * ( dx / distance );
this.velY += F2 * ( dy / distance );
this.velX += - ( this.velX - sodiums[j].velX ) * 0.1;
this.velY += - ( this.velY - sodiums[j].velY ) * 0.1;
}
}
for(let j = 0; j < waters.length; j++) {
if(!(this === waters[j])) {
const dx = this.x - waters[j].x;
const dy = this.y - waters[j].y;
const distance = Math.sqrt(dx * dx + dy * dy);
const F = 1000 / ( distance * distance * distance );
this.velX += F * ( dx / distance );
this.velY += F * ( dy / distance );
}
}
super.updateVel();
}
}
class Sodium extends Particle{
draw() {
ctx.beginPath();
ctx.fillStyle = "#ab5cf2";
ctx.arc(this.x, this.y, size * 2.27, 0, 2 * Math.PI);
ctx.fill();
}
updateVel() {
if((this.x) >= width / 2 - 20) {
this.velX += - 0.1 * (this.x - width / 2 + 20);
}
super.updateVel();
}
}
addWater = function() {
let water = new Water(
Math.random() * width,
Math.random() * height,
Math.random() * 5 - 2.5,
Math.random() * 5 - 2.5,
Math.random() * Math.PI * 2
);
waters.push(water);
}
addSodium = function() {
let sodium = new Sodium(
Math.random() * width / 2,
Math.random() * height,
Math.random() * 5 - 2.5,
Math.random() * 5 - 2.5
);
sodiums.push(sodium);
}
init = function() {
while(waters.length < waterSlider.value) {
addWater();
}
while(sodiums.length < sodiumSlider.value) {
addSodium();
}
drawCanvas();
}
// Controls
waterSlider.oninput = function() {
while(waters.length < this.value) {
addWater();
drawCanvas();
}
while(waters.length > this.value) {
waters.pop();
drawCanvas();
}
}
sodiumSlider.oninput = function() {
while(sodiums.length < this.value) {
addSodium();
drawCanvas();
}
while(sodiums.length > this.value) {
sodiums.pop();
drawCanvas();
}
}
// Resizer
function resizer() {
const prevWidth = width;
const prevHeight = height;
width = canvas.width = window.innerWidth;
height = canvas.height = window.innerHeight;
const rx = width / prevWidth;
const ry = height / prevHeight;
for(let i = 0; i < waters.length; i++) {
waters[i].x = waters[i].x * rx;
waters[i].y = waters[i].y * ry;
}
for(let i = 0; i < sodiums.length; i++) {
sodiums[i].x = sodiums[i].x * rx;
sodiums[i].y = sodiums[i].y * ry;
}
drawCanvas();
}
window.addEventListener('resize', resizer);
drawCanvas = function() {
ctx.fillStyle = 'rgb(0,0,0)';
ctx.fillRect(0,0,width,height);
ctx.fillStyle = 'rgb(100,100,100)';
ctx.fillRect(width / 2 - 5,0,10,height);
ctx.fillStyle = 'rgb(255,255,100)';
ctx.fillRect(width / 2 - 7,0,2,height);
ctx.fillRect(width / 2 + 5,0,2,height);
let w1 = 0;
let w2 = 0;
for(let i = 0; i < waters.length; i++) {
waters[i].draw();
if(waters[i].x <= width / 2) {w1++;} else {w2++;}
}
for(let i = 0; i < sodiums.length; i++) {
sodiums[i].draw();
}
ctx.fillStyle = 'white';
ctx.font = '18px arial';
ctx.fillText('Water = ' + w1, width / 2 - 130, 30);
ctx.fillText('Water = ' + w2, width / 2 + 30, 30);
}
// define loop that keeps drawing the scene constantly
function loop() {
if(startBtn.checked) {
for(let i = 0; i < waters.length; i++) {
waters[i].update();
}
for(let i = 0; i < sodiums.length; i++) {
sodiums[i].update();
}
drawCanvas();
}
requestAnimationFrame(loop);
}
let waters = [];
let sodiums = [];
init();
loop();
</script>
</html>