first pass at integrating filters into new caching mechanism. added group filter test. Removed blurX and blurY filters because I don't think they'll be needed in their current state. Commented out half filter blur test because it's not a common use case. other filter tests have been disabled for now. Working on enabling them with future commits

This commit is contained in:
Eric Rowell
2013-12-31 13:04:05 -08:00
parent 1166154c1b
commit 12e7b06978
19 changed files with 331 additions and 619 deletions

View File

@@ -43,6 +43,7 @@
this.eventListeners = {};
this.attrs = {};
this._cache = {};
this._filterUpToDate = false;
this.setAttrs(config);
// event bindings for cache handling
@@ -112,18 +113,25 @@
* @example
* node.cache();
*/
cache: function(box) {
var x = box.x || 0,
cache: function(b) {
var box = b || {},
x = box.x || 0,
y = box.y || 0,
width = box.width || this.width(),
height = box.height || this.height(),
sceneCanvasCache = new Kinetic.SceneCanvas({
pixelRatio: 1,
width: box.width,
height: box.height
width: width,
height: height
}),
filterCanvasCache = new Kinetic.SceneCanvas({
pixelRatio: 1,
width: width,
height: height
}),
hitCanvasCache = new Kinetic.HitCanvas({
pixelRatio: 1,
width: box.width,
height: box.height
width: width,
height: height
}),
origTransEnabled = this.transformsEnabled(),
origX = this.x(),
@@ -142,6 +150,7 @@
this._cache.canvas = {
scene: sceneCanvasCache,
filter: filterCanvasCache,
hit: hitCanvasCache,
x: x,
y: y
@@ -149,6 +158,48 @@
return this;
},
_drawCachedSceneCanvas: function(context) {
var filters = this.filters(),
cachedCanvas = this._cache.canvas,
sceneCanvas = cachedCanvas.scene,
filterCanvas = cachedCanvas.filter,
filterContext = filterCanvas.getContext(),
len, imageData, n, filter;
context.save();
context._applyTransform(this);
if (filters) {
if (!this._filterUpToDate) {
try {
len = filters.length;
filterContext.clear();
// copy cached canvas onto filter context
filterContext.drawImage(sceneCanvas._canvas, 0, 0);
imageData = filterContext.getImageData(0, 0, filterCanvas.getWidth(), filterCanvas.getHeight());
// apply filters to filter context
for (n=0; n<len; n++) {
filter = filters[n];
filter.call(this, imageData);
filterContext.putImageData(imageData, 0, 0);
}
}
catch(e) {
Kinetic.Util.warn('Unable to apply filter. ' + e.message);
}
this._filterUpToDate = true;
}
context.drawImage(filterCanvas._canvas, 0, 0);
}
else {
context.drawImage(sceneCanvas._canvas, 0, 0);
}
context.restore();
},
/*
* the default isDraggable method returns false.
* if the DragAndDrop module is included, this is overwritten
@@ -1753,6 +1804,23 @@
* @returns {Boolean|String}
*/
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'filters');
/**
* get/set filters
* @name filters
* @method
* @memberof Kinetic.Node.prototype
* @param {Array} filters array of filters
* @returns {Array}
* @example
* // set a single filter<br>
* node.filters([Kinetic.Filters.Blur]);<br><br>
*
* // get filters<br>
* var filters = node.filters();
*/
Kinetic.Factory.addGetterSetter(Kinetic.Node, 'visible', 'inherit');
/**