Skip to content

Commit a40bc71

Browse files
committed
added checkbox to toggle fading of previous generations, for #2
1 parent 7af1cf2 commit a40bc71

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

index.html

+3-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ <h3>Evolution</h3-->
9898
<br />
9999
<div style="text-align: right">
100100
<label for="drawTree">Update tree</label>
101-
<div id="drawTree"></div>
101+
<div id="drawTree"></div><br />
102+
<label for="drawFade">Fade previous</label>
103+
<div id="drawFade"></div>
102104
</div>
103105
<br /><br />
104106
<svg id="svg"></svg>

uilib.js

+12-5
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ var UI = function() {
5252

5353
// make checkbox
5454
$('#drawTree').replaceWith('<input type="checkbox" id="drawTree" checked="checked" />');
55+
$('#drawFade').replaceWith('<input type="checkbox" id="drawFade" checked="checked" />');
5556

5657
// collect all controls in an accordion
5758
$("#controls").accordion({ collapsible: true, active: false });
@@ -278,12 +279,18 @@ UI.prototype.colorArea = function(ctx,startX,startY,rgb,imageData){
278279
* Fades the previous generation of individuals on the canvas
279280
*/
280281
UI.prototype.fade = function() {
281-
var imgData = this.evoCtx.getImageData(0,0,window.innerWidth,window.innerHeight);
282-
var pix = imgData.data;
283-
for ( var i = 0; i < pix.length; i += 4 ) {
284-
pix[i+3] *= 0.9;
282+
var fade = this.getBoolean('drawFade');
283+
if ( fade ) {
284+
var imgData = this.evoCtx.getImageData(0, 0, window.innerWidth, window.innerHeight);
285+
var pix = imgData.data;
286+
for (var i = 0; i < pix.length; i += 4) {
287+
pix[i + 3] *= 0.9;
288+
}
289+
this.evoCtx.putImageData(imgData, 0, 0);
290+
}
291+
else {
292+
this.evoCtx.clearRect(0,0, window.innerWidth, window.innerHeight);
285293
}
286-
this.evoCtx.putImageData(imgData,0,0);
287294
};
288295

289296
/**

0 commit comments

Comments
 (0)