initialises layers JIT, instead of in the constructor

This commit is contained in:
Francisco Dans 2015-02-25 12:42:21 +01:00
parent 43e6cb7a37
commit 9479b214fa

View File

@ -10,8 +10,7 @@ function BallRenderer(thecanvas){
this.width = this.canvas.width; this.width = this.canvas.width;
this.height = this.canvas.height; this.height = this.canvas.height;
this.size = this.width * this.height; this.size = this.width * this.height;
this.pointList = new Uint8ClampedArray(this.size * 4); this.pointLayer = new Uint8ClampedArray(this.size * 4);
this.contourLayer = new Uint8ClampedArray(this.size * 4);
this.radius = 30; this.radius = 30;
this.drawnTemp = []; this.drawnTemp = [];
this.prof = 0; this.prof = 0;
@ -72,24 +71,28 @@ BallRenderer.prototype = {
} }
a[a.length-1] = 255; a[a.length-1] = 255;
var l = -step/2; var l = -step/2;
var gradient = new Uint8ClampedArray(1024);
for(var i = 0; i<a.length; i++){ for(var i = 0; i<a.length; i++){
var thisAlpha = a[i]/255; var y = Math.round(i*step);
this.ctx.fillStyle = "rgba(0, 0, 0, " + thisAlpha + ")"; var thisAlpha = a[i];
this.ctx.fillRect(0, l, 1, step); while(y<step*(i+1)){
l+=step; gradient[y*4+3] = thisAlpha;
y++;
}
} }
var _grad = this.ctx.getImageData(0, 0, 1, 255).data; if(!this.contourLayer) this.contourLayer = new Uint8ClampedArray(this.size * 4);
this.ctx.clearRect(0, 0, 1, 256); for (var i = 0; i< this.pointLayer.length; i+=4){
if(this.pointLayer[i+3] === 0) continue;
for (var i = 0; i< this.pointList.length; i+=4){ var currentAlpha = this.pointLayer[i+3];
if(this.pointList[i+3] === 0) continue; this.contourLayer[i+3] = gradient[currentAlpha*4+3];
var currentAlpha = this.pointList[i+3];
this.contourLayer[i+3] = this.pointList[i+3] === 255? 255: _grad[currentAlpha*4+3];
} }
}, },
draw: function(dataArray){ draw: function(dataArray){
if(!dataArray) dataArray = this.pointList; if (!dataArray){
else this.contourLayer = dataArray; if (this.isoplethLayer) dataArray = this.isoplethLayer;
else if (this.contourLayer) dataArray = this.contourLayer;
else dataArray = this.pointLayer;
}
if(!this.imageData) this.imageData = this.ctx.createImageData(this.width, this.height); if(!this.imageData) this.imageData = this.ctx.createImageData(this.width, this.height);
this.imageData.data.set(dataArray); this.imageData.data.set(dataArray);
this.ctx.putImageData(this.imageData, 0, 0); this.ctx.putImageData(this.imageData, 0, 0);