added batchDraw which enables you to call batchDraw() as many times as you want, and let Kinetic automatically combine layer draws together asynchronously

This commit is contained in:
Eric Rowell
2013-05-05 22:09:32 -07:00
parent e66e147274
commit 11f269868f
3 changed files with 94 additions and 4 deletions

View File

@@ -50,6 +50,33 @@
getLayers: function() {
return this.layers;
},
/**
* add layer. Returns true if the layer was added, and false if it was not
* @name addLayer
* @methodOf Kinetic.Animation.prototype
* @param {Kinetic.Layer} layer
*/
addLayer: function(layer) {
var layers = this.layers,
len, n;
if (layers) {
len = layers.length;
// don't add the layer if it already exists
for (n = 0; n < len; n++) {
if (layers[n]._id === layer._id) {
return false;
}
}
}
else {
this.layers = [];
}
this.layers.push(layer);
return true;
},
/**
* determine if animation is running or not. returns true or false
* @name isRunning
@@ -143,7 +170,7 @@
}
// if animation object has a function, execute it
if(func) {
func(anim.frame);
func.call(anim, anim.frame);
}
}
}

View File

@@ -14,6 +14,13 @@
this._initLayer(config);
};
Kinetic.Layer.batchAnim = new Kinetic.Animation(function() {
if (this.getLayers().length === 0) {
this.stop();
}
this.setLayers([]);
});
Kinetic.Layer.prototype = {
_initLayer: function(config) {
this.nodeType = 'Layer';
@@ -28,8 +35,7 @@
/**
* get intersection object that contains shape and pixel data
* @name getIntersection
* @methodOf Kinetic.Stage.prototype
* @param {Object} pos point object
* @methodOf Kinetic.Layer.prototype
*/
getIntersection: function() {
var pos = Kinetic.Type._getXY(Array.prototype.slice.call(arguments)),
@@ -56,6 +62,19 @@
return null;
},
/**
* get batch draw
* @name batchDraw
* @methodOf Kinetic.Layer.prototype
*/
batchDraw: function() {
var batchAnim = Kinetic.Layer.batchAnim;
batchAnim.addLayer(this);
if (!batchAnim.isRunning()) {
batchAnim.start();
}
},
drawScene: function(canvas) {
var canvas = canvas || this.getCanvas();