-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathether.html
More file actions
669 lines (584 loc) · 25.5 KB
/
ether.html
File metadata and controls
669 lines (584 loc) · 25.5 KB
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html class="js" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml" lang="en"><head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Yoann Babel Virtual Ether</title>
<body>
<div align="center">
Parabolic mirror generate planar wave (laser)
<br clear="all">
TimeStep (1-n): <input id="timestep" value="100" onchange="stop(); start();" size="5">
Damp (0-1.0): <input id="damp" value="0.95" onchange="change();" size="5">
PushForce (deformation when clicking 0.1-64): <input id="push" value="16.0" onchange="change();" size="5">
<br>Reaction Diffusion: <input id="grayscott" type="checkbox">
Kill (0-1.0): <input id="kill" value="0.062" onchange="change();" size="5">
Feed (0-1.0): <input id="feed" value="0.055" onchange="change();" size="5">
Diffusion rate A (0-1.0): <input id="dra" value="1.0" onchange="change();" size="5">
Diffusion rate B (0-1.0): <input id="drb" value="0.5" onchange="change();" size="5">
<br>Preselected :<select onchange="preselect(this.value);">
<option value="nothing"> Nothing </option>
<option value="magnets"> Tick : 2 Magnets </option>
<option value="bounding"> Tick : Bouding borders </option>
<option value="oscillator"> Tick : 2 Oscillators </option>
<option value="swamp"> Init : Ether damping + fluidity </option>
<option value="someclicks1"> Init : Some clicks 1 </option>
<option value="someclicks2"> Init : Some clicks 2 </option>
<option value="laser"> Tick : Laser generator </option>
<option value="closebox"> Tick : Close the ether box </option>
<option value="lens"> Init : parabolic lens emiter </option>
<option value="pmirror"> Tick : parabolic mirror </option>
<option value="plaser1"> Tick : Parabolic Laser generator 1</option>
<option value="plaser2"> Tick : Parabolic Laser generator 2 </option>
<option value="plaser3"> Tick : Parabolic Laser generator 3 </option>
<option value="ocean"> Init : Ocean with 4 regions </option>
</select>
Entropy (0-7.0): <input id="entropy" value="0.0" size="5">
<br/>Init :
<textarea id="swamp" type="hidden" style="display: none;">
simulator.initenv(
function (x,y) {
var fluid = 0.0; // 0 = fuild, 1 = visqueux
if (x > 35) fluid = 1.0;
return fluid;
},
function (x,y) {
var damp = document.getElementById('damp').value;
if (x < 10) { if (y < 10) { damp = 0; } else damp = 0.5; }
return damp;
});
</textarea>
<textarea id="init" cols="100" rows="5">
</textarea><br/>Each step :
<textarea id="tick" cols="100" rows="5">
p=24; d=16; pX=4; pY=25;
if (time < p*4/3)
simulator.push(p+pX, pY, Math.sin(time*2/3)*8);
if (time < p*3)
simulator.v_absorber(pY+d, pY-d, p+pX+1, -1);
for (y=-d; y<=d; y+=0.01) {
x = (y*y)/(4*p);
simulator.setether(Math.round(x)+pX, Math.round(y)+pY, 0);
}
simulator.v_absorber(gridY -1, 0, 0, 1);
simulator.v_absorber(gridY -1, 0, gridX-1, -1);
simulator.h_absorber(gridX-1, 0, 0, 1);
simulator.h_absorber(gridX-1, 0, gridY-1, -1);
</textarea><br/>
<input name="reset" value="Reset" onclick="reset();" type="button">
<input name="start" value="Start" onclick="start();" type="button">
<input name="stop" value="Stop" onclick="stop();" type="button">
<input name="step" value="Step" onclick="step();" type="button">
<input name="goback" value="Back" onclick="goback();" type="button">
<br clear="all">
<canvas id="canvas" width="501" height="261" style="background: transparent url(virtual-ether.png) repeat scroll 0% 0%; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; -moz-opaque:true;">
<div id="empty"></div>
</canvas>
<br>
<canvas id="chart" width="640" height="480"></canvas>
<br clear="all">
</div>
<script type="text/javascript">
// CANVAS CHART
var CanvasChart = function () {
var ctx;
var margin = { top: 40, left: 75, right: 0, bottom: 75 };
var chartHeight, chartWidth, yMax, xMax, data;
var maxYValue = 0;
var ratio = 0;
var renderType = { lines: 'lines', points: 'points' };
var render = function(canvasId, dataObj) {
data = dataObj;
getMaxDataYValue();
var canvas = document.getElementById(canvasId);
chartHeight = canvas.getAttribute('height');
chartWidth = canvas.getAttribute('width');
xMax = chartWidth - (margin.left + margin.right);
yMax = chartHeight - (margin.top + margin.bottom);
ratio = yMax / maxYValue;
ctx = canvas.getContext("2d");
ctx.clearRect ( 0 , 0 , canvas.width, canvas.height );
renderChart();
};
var renderChart = function () {
renderBackground();
renderText();
renderLinesAndLabels();
//render data based upon type of renderType(s) that client supplies
if (data.renderTypes == undefined || data.renderTypes == null) data.renderTypes = [renderType.lines];
for (var i = 0; i < data.renderTypes.length; i++) {
renderData(data.renderTypes[i]);
}
};
var getMaxDataYValue = function () {
for (var i = 0; i < data.dataPoints.length; i++) {
if (data.dataPoints[i].y > maxYValue) maxYValue = data.dataPoints[i].y;
}
};
var renderBackground = function() {
var lingrad = ctx.createLinearGradient(margin.left, margin.top, xMax - margin.right, yMax);
lingrad.addColorStop(0.0, '#D4D4D4');
lingrad.addColorStop(0.2, '#fff');
lingrad.addColorStop(0.8, '#fff');
lingrad.addColorStop(1, '#D4D4D4');
ctx.fillStyle = lingrad;
ctx.fillRect(margin.left, margin.top, xMax - margin.left, yMax - margin.top);
ctx.fillStyle = 'black';
};
var renderText = function() {
var labelFont = (data.labelFont != null) ? data.labelFont : '20pt Arial';
ctx.font = labelFont;
ctx.textAlign = "center";
//Title
var txtSize = ctx.measureText(data.title);
ctx.fillText(data.title, (chartWidth / 2), (margin.top / 2));
//X-axis text
txtSize = ctx.measureText(data.xLabel);
ctx.fillText(data.xLabel, margin.left + (xMax / 2) - (txtSize.width / 2), yMax + (margin.bottom / 1.2));
//Y-axis text
ctx.save();
ctx.rotate(-Math.PI / 2);
ctx.font = labelFont;
ctx.fillText(data.yLabel, (yMax / 2) * -1, margin.left / 4);
ctx.restore();
};
var renderLinesAndLabels = function () {
//Vertical guide lines
var yInc = yMax / data.dataPoints.length;
var yPos = 0;
var yLabelInc = (maxYValue * ratio) / data.dataPoints.length;
var step1 = Math.ceil(data.dataPoints.length / 10); step1 = step1==0?1:step1;
var xInc = getXInc();
var xPos = margin.left;
for (var i = 0; i < data.dataPoints.length; i+=1) {
yPos += (i == 0) ? margin.top : yInc;
//Draw horizontal lines
//drawLine(margin.left, yPos, xMax, yPos, '#E8E8E8');
//y axis labels
ctx.font = (data.dataPointFont != null) ? data.dataPointFont : '10pt Calibri';
var txt = Math.round( (maxYValue - ((i == 0) ? 0 : yPos / ratio))*10 )/10;
var txtSize = ctx.measureText(txt);
if (i % step1 == 0)
ctx.fillText(txt, margin.left - ((txtSize.width >= 14) ? txtSize.width : 10) - 7, yPos + 4);
//x axis labels
txt = data.dataPoints[i].x;
txtSize = ctx.measureText(txt);
if (i % step1 == 0)
ctx.fillText(txt, xPos, yMax + (margin.bottom / 3));
xPos += xInc;
}
//Vertical line
drawLine(margin.left, margin.top, margin.left, yMax, 'black');
//Horizontal Line
drawLine(margin.left, yMax, xMax, yMax, 'black');
};
var renderData = function(type) {
var xInc = getXInc();
var prevX = 0,
prevY = 0;
for (var i = 0; i < data.dataPoints.length; i++) {
var pt = data.dataPoints[i];
var ptY = (maxYValue - pt.y) * ratio;
if (ptY < margin.top) ptY = margin.top;
var ptX = (i * xInc) + margin.left;
if (i > 0 && type == renderType.lines) {
//Draw connecting lines
drawLine(ptX, ptY, prevX, prevY, 'black', 2);
}
if (type == renderType.points) {
var radgrad = ctx.createRadialGradient(ptX, ptY, 8, ptX - 5, ptY - 5, 0);
radgrad.addColorStop(0, 'Green');
radgrad.addColorStop(0.9, 'White');
ctx.beginPath();
ctx.fillStyle = radgrad;
//Render circle
ctx.arc(ptX, ptY, 8, 0, 2 * Math.PI, false)
ctx.fill();
ctx.lineWidth = 1;
ctx.strokeStyle = '#000';
ctx.stroke();
ctx.closePath();
}
prevX = ptX;
prevY = ptY;
}
};
var getXInc = function() {
return Math.round(xMax / data.dataPoints.length) - 1;
};
var drawLine = function(startX, startY, endX, endY, strokeStyle, lineWidth) {
if (strokeStyle != null) ctx.strokeStyle = strokeStyle;
if (lineWidth != null) ctx.lineWidth = lineWidth;
ctx.beginPath();
ctx.moveTo(startX, startY);
ctx.lineTo(endX, endY);
ctx.stroke();
ctx.closePath();
};
return {
renderType: renderType,
render: render
};
} ();
//Grayscott reaction diffusion, inspired from https://github.com/udion/ReactionDiffusion/
// http://www.algosome.com/articles/reaction-diffusion-gray-scott.html
// http://www.karlsims.com/rd.html
var timer_id = 0;
var timestep = 100;
var size=5;
var force = 0;
var time = 0; // time
//grayscott
var kill,feed,dra,drb = 0.0;
var grayscott=false;
var canvas = document.getElementById('canvas');
var entropyHist = [{ x: 0, y: 0 }];
canvas.mozOpaque = true;
var gridX = (canvas.width - 1) / size;
var gridY = (canvas.height - 1) / size;
var simulator = new Simulator(gridX, gridY);
simulator.setclick(canvas);
simulator.draw(canvas);
change(); // retrieve global value of force and damping
var dataDef = { title: "Entropy Chart", xLabel: 'Time',
yLabel: 'Entropy', labelFont: '19pt Arial', dataPointFont:
'10pt Arial', renderTypes: [CanvasChart.renderType.lines],
dataPoints: [{ x: 0, y: 0 }] };
function preselect(kind) {
var magnets = "if (time % 1==0) { simulator.setvalue(36,20, force); }\nif (time % 1==0) { simulator.setvalue(10+(Math.floor(time/16))%25,5, force); }";
var bounding = "simulator.h_absorber(gridX -1, 0, 0, 1); \nsimulator.v_absorber(gridY -1, 0, 0, 1);";
var oscillator = "if (time % 1==0) { simulator.simulclick(36,5, force); }\nif (time % 1==0) { simulator.simulclick(10,5, force); }";
var swamp = document.getElementById("swamp").value;
var someclicks1 = "simulator.simulclick(12,2, force);\nsimulator.simulclick(37,2, force);\nsimulator.simulclick(25,14, force);";
var someclicks2 = "simulator.simulclick(12,2, force);\nsimulator.simulclick(37,23, force);";
var laser = "for (var j=10; j<15; j++) if (time % 15==0) { simulator.setether(5,j, force); }\nfor (var i=0; i < 25; i++) { simulator.setether(i,9,0); simulator.setether(i, 15, 0);}";
var closebox = "simulator.v_absorber(gridY -1, 0, 0, 1);\nsimulator.v_absorber(gridY -1, 0, gridX-1, -1);\nsimulator.h_absorber(gridX-1, 0, 0, 1);\nsimulator.h_absorber(gridX-1, 0, gridY-1, -1);";
var lens = "for (y=0; y<16; y+=0.01) {\nx = (y-8) * (y-8)/8;\nsimulator.setether(Math.round(x)+2, Math.round(y)+10, 1);\n}";
var pmirror = "for (y=0; y<=16.0; y+=0.01) { x = (y-8) * (y-8)/8; simulator.setether(Math.round(x)+1, Math.round(y)+20, 0); }";
var plaser1 = "if (time % 7 == 0)\n for (y=-8; y<=8.0; y+=1) \n simulator.setether(11, Math.round(y)+12, 1);\nfor (y=0; y<=16.0; y+=0.01) {\n x = (y-8) * (y-8)/8;\n simulator.setether(Math.round(x)+2, Math.round(y)+4, 0);\n}";
var plaser2 = "if (time % 7 == 0)\n simulator.setether(4, 12, 1);\nfor (y=0; y<=16.0; y+=0.01) {\n x = (y-8) * (y-8)/8;\n simulator.setether(Math.round(x)+2, Math.round(y)+4, 0);\n}";
var plaser3 = "simulator.v_absorber(gridY -1, 0, 0, 1);\nsimulator.v_absorber(gridY -1, 0, gridX-1, -1);\nsimulator.h_absorber(gridX-1, 0, 0, 1);\nsimulator.h_absorber(gridX-1, 0, gridY-1, -1);\nYY = 20;\nif (time % 7 == 0)\n for (y=-8; y<=8.0; y+=1) \n simulator.setether(11, Math.round(y)+YY, 1);\nfor (y=-12; y<=12.0; y+=0.01) {\n x = (y) * (y)/8;\n simulator.setether(Math.round(x)+2, Math.round(y)+YY, 0);\n}";
var ocean = " simulator.initenv(\n function (x,y) {\n if (y<25) return 0;\n return 0.5;\n },\n function (x,y) {\n var damp = document.getElementById('damp').value;\n if (x<50) damp=0.95\n return damp;\n });";
switch (kind) {
case "nothing" : document.getElementById("init").value = ""; document.getElementById("tick").value = ""; break;
case "magnets" : document.getElementById("tick").value += "\n"+magnets; break;
case "bounding" : document.getElementById("tick").value += "\n"+bounding; break;
case "oscillator" : document.getElementById("tick").value += "\n"+oscillator; break;
case "swamp" : document.getElementById("init").value += "\n"+swamp; break;
case "someclicks1" : document.getElementById("init").value += "\n"+someclicks1; break;
case "someclicks2" : document.getElementById("init").value += "\n"+someclicks2; break;
case "laser" : document.getElementById("tick").value += "\n"+laser; break;
case "closebox" : document.getElementById("tick").value += "\n"+closebox; break;
case "lens" : document.getElementById("init").value += "\n"+lens; break;
case "pmirror" : document.getElementById("tick").value += "\n"+pmirror; break;
case "plaser1" : document.getElementById("tick").value += "\n"+plaser1; break;
case "plaser2" : document.getElementById("tick").value += "\n"+plaser2; break;
case "plaser3" : document.getElementById("tick").value += "\n"+plaser3; break;
case "ocean" : document.getElementById("init").value += "\n"+ocean; break;
}
}
function init() {
eval(document.getElementById("init").value);
}
function change() {
force = document.getElementById('push').value - 0.0;
//grayscott
kill = document.getElementById('kill').value - 0.0;
feed = document.getElementById('feed').value - 0.0;
dra = document.getElementById('dra').value - 0.0;
drb = document.getElementById('drb').value - 0.0;
simulator.initenv(null,
function (x,y) {
return document.getElementById('damp').value;
});
}
function simulate() {
if (! document.getElementById('grayscott').checked)
simulator.compute_O1();
else
simulator.grayscott(kill, feed, dra, drb);
eval(document.getElementById("tick").value);
simulator.swap();
e = simulator.entropy();
document.getElementById('entropy').value = e;
time += 1;
entropyHist.push({ x: time, y: e});
//simulator.draw_O1(canvas);
simulator.draw(canvas);
dataDef.dataPoints.push({ x: time, y: e});
if (time % timestep == 0)
CanvasChart.render('chart', dataDef);
}
function reset() {
time=0;
dataDef.dataPoints = [{ x: 0, y: 0 }];
simulator.clear();
change();
init();
simulate()
}
function start() {
if (timer_id == 0) {
simulate();
timestep = document.getElementById('timestep').value;
timer_id = window.setInterval('simulate()', timestep);
}
}
function stop() {
if (timer_id != 0) {
window.clearInterval(timer_id);
timer_id = 0;
}
}
function step() {
simulate();
}
function goback() {
simulator.inverse();
}
reset();
function Simulator(width, height) {
this.damp = new Array(width * height);
this.fluid = new Array(width * height);
this.ether = new Array(width * height);
this.buffer = new Array(width * height);
//grayscott
this.etherA = new Array(width * height);
this.bufferA = new Array(width * height);
this.etherB = new Array(width * height);
this.bufferB = new Array(width * height);
this.getdamp = function (x, y) {
return (x >= 0 && x < width && y >= 0 && y < height)
? this.damp[y * width + x] : 0.0;
}
this.getfluid = function (x, y) {
return (x >= 0 && x < width && y >= 0 && y < height)
? this.fluid[y * width + x] : 0.0;
}
this.getbuffer = function (x, y) { return (x >= 0 && x < width && y >= 0 && y < height) ? this.buffer[y * width + x] : 0.0; }
this.getether = function (x, y) { return (x >= 0 && x < width && y >= 0 && y < height) ? this.ether[y * width + x] : 0.0; }
this.setether = function (x, y, value) { return this.ether[y * width + x] = value; }
this.setbuffer = function (x, y, value) { return this.buffer[y * width + x] = value; }
this.setdamp = function (x, y, value) { return this.damp[y * width + x] = value; }
this.setfluid = function (x, y, value) { return this.fluid[y * width + x] = value; }
//grayscott
this.getbufferA = function (x, y) { return (x >= 0 && x < width && y >= 0 && y < height) ? this.bufferA[y * width + x] : 0.0; }
this.getetherA = function (x, y) { return (x >= 0 && x < width && y >= 0 && y < height) ? this.etherA[y * width + x] : 0.0; }
this.setetherA = function (x, y, value) { return this.etherA[y * width + x] = value; }
this.setbufferA = function (x, y, value) { return this.bufferA[y * width + x] = value; }
this.getbufferB = function (x, y) { return (x >= 0 && x < width && y >= 0 && y < height) ? this.bufferB[y * width + x] : 0.0; }
this.getetherB = function (x, y) { return (x >= 0 && x < width && y >= 0 && y < height) ? this.etherB[y * width + x] : 0.0; }
this.setetherB = function (x, y, value) { return this.etherB[y * width + x] = value; }
this.setbufferB = function (x, y, value) { return this.bufferB[y * width + x] = value; }
this.initenv = function (getfluid, getdamp) {
for (var y = height - 1; y >= 0; y--) {
for (var x = width - 1; x >= 0; x--) {
if (getfluid != null) this.setfluid(x, y, getfluid(x,y));
if (getdamp != null) this.setdamp(x, y, getdamp(x,y));
}
}
}
this.clear = function () {
for (var offset = this.ether.length - 1; offset >= 0; offset--) {
this.ether[offset] = 0.0;
this.etherA[offset] = Math.random();
this.etherB[offset] = 0.0; //Math.random();
this.fluid[offset] = 0.0;
this.damp[offset] = 1.0;
this.buffer[offset] = 0.0;
this.bufferA[offset] = 0.0;
this.bufferB[offset] = 0.0;
}
}
this.push = function (x,y,v) {
this.setether(x,x,v);
this.setetherB(x,y,v/16);
}
this.grayscott = function(kill, feed, dra, drb) {
for (var y = height - 1; y >= 0; y--) {
for (var x = width - 1; x >= 0; x--) {
A = this.getetherA(x,y); B = this.getetherB(x,y);
laplacianA = (this.ether_neighboorA(x,y)-A);
laplacianB = (this.ether_neighboorB(x,y)-B);
ABB = A*B*B;
dA = dra*laplacianA - ABB + feed*(1.0-A);
dB = drb*laplacianB + ABB - (kill+feed)*B;
this.setbufferA(x,y, A+dA); this.setbufferB(x,y,B+dB);
}
}
}
this.compute = function() {
for (var y = height - 1; y >= 0; y--) {
for (var x = width - 1; x >= 0; x--) {
var f = this.getfluid(x,y);
var d = this.getdamp(x,y);
var v = ( this.buffer_neighboor(x, y) * (2.0-f) - (1.0-f)*this.getether(x,y) ) *d;
this.setether(x,y, v)
}
}
}
this.compute_O1 = function() {
for (var y = height - 1; y >= 0; y--) {
for (var x = width - 1; x >= 0; x--) {
var ofs = x+y*width;
var f = this.fluid[ofs];
var d = this.damp[ofs];
var v = ( this.buffer_neighboor_O1(ofs, x, y) * (2.0-f) - (1.0-f)*this.ether[ofs] ) *d;
this.ether[ofs] = v;
}
}
}
this.h_absorber = function (a, b, y, dir) { // dir is 1 or -1
for (var x = a; x >= b; x--) {
var d = this.getdamp(x,y+dir);
var v = this.getbuffer(x,y+dir);
this.setether(x,y,v*d);
if (y> 0 && y < height-1) this.setether(x,y-dir,0);
}
}
this.v_absorber = function (a, b, x, dir) { // dir is 1 or -1
for (var y = a; y >= b; y--) {
var d = this.getdamp(x+dir,y);
var v = this.getbuffer(x+dir,y);
this.setether(x,y,v*d);
if (x> 0 && x < width-1) this.setether(x-dir,y,0);
}
}
this.swap = function() {
var tmp = this.buffer;
this.buffer = this.ether;
this.ether = tmp;
var tmpA = this.bufferA;
this.bufferA = this.etherA;
this.etherA = tmpA;
var tmpB = this.bufferB;
this.bufferB = this.etherB;
this.etherB = tmpB;
}
this.cellwidth = function (canvas) { return Math.floor(canvas.width / width); }
this.cellheight = function (canvas) { return Math.floor(canvas.height / height); }
this.getcolor = function (x,y) {
var v = this.getether(x,y);
return v>0 ? "rgb("+Math.floor(v*255)+",0,0)" : "rgb(0,0,"+Math.floor(v*-255)+")";
}
this.getcolorGrayscott = function (x,y) {
var a = this.getetherA(x,y);
var b = this.getetherB(x,y);
//return "rgb("+Math.floor(Math.abs(a)*255)+","+Math.floor(Math.abs(b)*255)+",0)";
return "rgb("+Math.floor(Math.abs(a-b)*255)+",0,0)";
}
this.draw = function (canvas, draw_grid) {
var ctx = canvas.getContext('2d');
var cwidth = this.cellwidth(canvas);
var cheight = this.cellheight(canvas);
ctx.fillStyle = 'white';
ctx.fillRect(0, 0, cwidth * width, cheight * height);
for (var y = height - 1; y >= 0; y--) {
var offset = y * width;
for (var x = width - 1; x >= 0; x--) {
if (! document.getElementById('grayscott').checked) {
if (Math.abs(this.getether(x,y)) < 0.1)
ctx.fillStyle = "rgb(0,"+Math.floor(this.getdamp(x,y)*64)+","+Math.floor(this.getfluid(x,y)*64)+")";
else
ctx.fillStyle = this.getcolor(x,y);
} else
ctx.fillStyle = this.getcolorGrayscott(x,y);
ctx.fillRect(x * cwidth, y * cheight, cwidth, cheight);
}
}
if (draw_grid) {
ctx.fillStyle = 'grey';
for (var y = 0; y <= height; y++) {
ctx.fillRect(0, y * cheight, cwidth * width, 1);
for (var x = 0; x <= width; x++) {
ctx.fillRect(x * cwidth, 0, 1, cheight * height);
}
}
}
}
this.draw_O1 = function (canvas, draw_grid) {
var ctx = canvas.getContext('2d');
var cwidth = this.cellwidth(canvas);
var cheight = this.cellheight(canvas);
for (var y = height - 1; y >= 0; y--) {
for (var x = width - 1; x >= 0; x--) {
var ofs = x+y * width;
var v = this.ether[ofs];
if (Math.abs(v) < 0.05)
ctx.fillStyle = "rgb(0,"+Math.floor(this.damp[ofs]*64)+","+Math.floor(this.fluid[ofs]*64)+")";
else
ctx.fillStyle = v>0 ? "rgb("+Math.floor(v*255)+",0,0)" : "rgb(0,0,"+Math.floor(v*-255)+")";;
ctx.fillRect(x * cwidth, y * cheight, cwidth, cheight);
}
}
}
this.buffer_neighboor = function(x, y) {
return (this.getbuffer(x+1, y) + this.getbuffer(x, y+1) + this.getbuffer(x-1, y) + this.getbuffer(x, y-1))/4.0;
}
//do not work with only 4 neighboor !!
this.ether_neighboorA = function(x, y) {
return (this.getetherA(x+1, y) + this.getetherA(x, y+1) + this.getetherA(x-1, y) + this.getetherA(x, y-1))*0.20 +
(this.getetherA(x+1, y+1) + this.getetherA(x+1, y-1) + this.getetherA(x-1, y+1) + this.getetherA(x-1, y-1))*0.05;
}
this.ether_neighboorB = function(x, y) {
return (this.getetherB(x+1, y) + this.getetherB(x, y+1) + this.getetherB(x-1, y) + this.getetherB(x, y-1))*0.20 +
(this.getetherB(x+1, y+1) + this.getetherB(x+1, y-1) + this.getetherB(x-1, y+1) + this.getetherB(x-1, y-1))*0.05;
}
this.buffer_neighboor_O1 = function(ofs, x, y) {
if (x==0 || y==0 || x==width-1 || y==height-1) return this.buffer_neighboor(x, y);
return (this.buffer[ofs+1] + this.buffer[ofs+width] + this.buffer[ofs-1] + this.buffer[ofs-width])/4.0;
}
this.simulclick = function(x, y, c) {
var v = c-this.getether(x,y);
this.setether(x,y, v);
var v = c-this.getbuffer(x,y);
this.setbuffer(x,y, v);
//grayscott
this.setetherB(x,y, 1.0);
}
this.setvalue = function(x, y, v) {
this.setether(x,y, v);
this.setbuffer(x,y, v);
}
this.click = function(e, canvas) {
var x = Math.floor((e.clientX-canvas.offsetLeft)/this.cellwidth(canvas));
var y = Math.floor((e.clientY-canvas.offsetTop)/this.cellheight(canvas));
this.simulclick(x,y, force);
}
this.setclick= function(canvas) {
var self=this;
var drag = false;
canvas.addEventListener("mousedown", function(e) { drag = true; }, false);
canvas.addEventListener("mouseup", function(e) { drag = false; self.click(e, canvas); }, false);
canvas.addEventListener("mousemove",
function(e) {
if (! drag) return;
self.click(e, canvas);
}, false);
}
this.inverse = function() {
for (var offset = this.ether.length - 1; offset >= 0; offset--) {
var tmp = this.ether[offset];
this.ether[offset] = this.buffer[offset];
this.buffer[offset] = tmp;
}
}
this.entropy = function() {
var bytes = new Array(256); var sum = 0; var plogp=0;
for (var offset = this.ether.length - 1; offset >= 0; offset--) {
var value = Math.floor(255*(this.ether[offset]+1)/2);
sum += value;
if (bytes[value]) bytes[value]++; else bytes[value]=1;
}
for (i=0; i<256; i++) {
if (bytes[i]) {
var p = bytes[i] / this.ether.length;
plogp -= p * Math.log(p);
}
}
plogp /= Math.log(2);
return plogp;
}
this.clear();
}
//-->
</script>
</body></html>