Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #785: Cache in scaled scaled canvas was scaled twice. #787

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Container.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@
clipY = this.getClipY();

context.save();
context._applyTransform(this);
context._applyTransform(this, canvas.isCache);
context.beginPath();
context.rect(clipX, clipY, clipWidth, clipHeight);
context.clip();
Expand Down
21 changes: 15 additions & 6 deletions src/Context.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,15 @@
this.setAttr('lineCap', lineCap);
}
},
_applyOpacity: function(shape) {
var absOpacity = shape.getAbsoluteOpacity();
if(absOpacity !== 1) {
this.setAttr('globalAlpha', absOpacity);
_applyOpacity: function(shape, isCacheCanvas) {
var opacity;
if (isCacheCanvas) {
opacity = shape.getRelativeOpacity();
} else {
opacity = shape.getAbsoluteOpacity();
}
if(opacity !== 1) {
this.setAttr('globalAlpha', opacity);
}
},
_applyLineJoin: function(shape) {
Expand All @@ -223,12 +228,16 @@
this.setAttr('lineJoin', lineJoin);
}
},
_applyTransform: function(shape) {
_applyTransform: function(shape, isCacheCanvas) {
var transformsEnabled = shape.getTransformsEnabled(),
m;

if (transformsEnabled === 'all') {
m = shape.getAbsoluteTransform().getMatrix();
if (isCacheCanvas) {
m = shape.getRelativeTransform().getMatrix();
} else {
m = shape.getAbsoluteTransform().getMatrix();
}
this.transform(m[0], m[1], m[2], m[3], m[4], m[5]);
}
else if (transformsEnabled === 'position') {
Expand Down
68 changes: 64 additions & 4 deletions src/Node.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
(function() {
// CONSTANTS
var ABSOLUTE_OPACITY = 'absoluteOpacity',
RELATIVE_OPACITY = 'relativeOpacity',
ABSOLUTE_TRANSFORM = 'absoluteTransform',
RELATIVE_TRANSFORM = 'relativeTransform',
BEFORE = 'before',
CHANGE = 'Change',
CHILDREN = 'children',
Expand Down Expand Up @@ -50,6 +52,7 @@
this.on(TRANSFORM_CHANGE_STR, function() {
this._clearCache(TRANSFORM);
that._clearSelfAndDescendantCache(ABSOLUTE_TRANSFORM);
that._clearSelfAndDescendantCache(RELATIVE_TRANSFORM);
});
this.on('visibleChange.kinetic', function() {
that._clearSelfAndDescendantCache(VISIBLE);
Expand All @@ -59,6 +62,7 @@
});
this.on('opacityChange.kinetic', function() {
that._clearSelfAndDescendantCache(ABSOLUTE_OPACITY);
that._clearSelfAndDescendantCache(RELATIVE_OPACITY);
});
},
_clearCache: function(attr){
Expand Down Expand Up @@ -103,6 +107,7 @@
clearCache: function() {
delete this._cache.canvas;
this._filterUpToDate = false;
this.cacheBegin = false;
return this;
},
/**
Expand Down Expand Up @@ -170,6 +175,10 @@

this.clearCache();

cachedSceneCanvas.isCache = true;
cachedHitCanvas.isCache = true;
this.cacheBegin = true;

this.transformsEnabled('position');
this.x(x * -1);
this.y(y * -1);
Expand Down Expand Up @@ -205,7 +214,8 @@
},
_drawCachedSceneCanvas: function(context) {
context.save();
context._applyTransform(this);
context._applyTransform(this, context.canvas.isCache);
context._applyOpacity(this, context.canvas.isCache);
context.drawImage(this._getCachedSceneCanvas()._canvas, 0, 0);
context.restore();
},
Expand Down Expand Up @@ -251,7 +261,7 @@
hitCanvas = cachedCanvas.hit;

context.save();
context._applyTransform(this);
context._applyTransform(this, context.canvas.isCache);
context.drawImage(hitCanvas._canvas, 0, 0);
context.restore();
},
Expand Down Expand Up @@ -401,9 +411,11 @@
// traversal must be cleared when removing a node
this._clearSelfAndDescendantCache(STAGE);
this._clearSelfAndDescendantCache(ABSOLUTE_TRANSFORM);
this._clearSelfAndDescendantCache(RELATIVE_TRANSFORM);
this._clearSelfAndDescendantCache(VISIBLE);
this._clearSelfAndDescendantCache(LISTENING);
this._clearSelfAndDescendantCache(ABSOLUTE_OPACITY);
this._clearSelfAndDescendantCache(RELATIVE_OPACITY);

return this;
},
Expand Down Expand Up @@ -753,6 +765,7 @@

this._clearCache(TRANSFORM);
this._clearSelfAndDescendantCache(ABSOLUTE_TRANSFORM);
this._clearSelfAndDescendantCache(RELATIVE_TRANSFORM);
},
_clearTransform: function() {
var trans = {
Expand All @@ -779,6 +792,7 @@

this._clearCache(TRANSFORM);
this._clearSelfAndDescendantCache(ABSOLUTE_TRANSFORM);
this._clearSelfAndDescendantCache(RELATIVE_TRANSFORM);

// return original transform
return trans;
Expand Down Expand Up @@ -815,7 +829,7 @@
this.setPosition({x:x, y:y});
return this;
},
_eachAncestorReverse: function(func, includeSelf) {
_eachAncestorReverse: function(func, includeSelf, stopAtCacheBegin) {
var family = [],
parent = this.getParent(),
len, n;
Expand All @@ -824,7 +838,7 @@
if(includeSelf) {
family.unshift(this);
}
while(parent) {
while(parent && (!stopAtCacheBegin || !parent.cacheBegin)) {
family.unshift(parent);
parent = parent.parent;
}
Expand Down Expand Up @@ -937,6 +951,22 @@
}
return absOpacity;
},
/**
* get relative opacity
* @method
* @memberof Kinetic.Node.prototype
* @returns {Number}
*/
getRelativeOpacity: function() {
return this._getCache(RELATIVE_OPACITY, this._getRelativeOpacity);
},
_getRelativeOpacity: function() {
var opacity = 1;
this._eachAncestorReverse(function(node) {
opacity *= node.getOpacity();
}, true, true);
return opacity;
},
/**
* move node to another container
* @method
Expand Down Expand Up @@ -1090,6 +1120,36 @@
}, true);
return at;
},

/**
* get relative transform of the node which takes into
* account its ancestor transforms
* @method
* @memberof Kinetic.Node.prototype
* @returns {Kinetic.Transform}
*/
getRelativeTransform: function() {
return this._getCache(RELATIVE_TRANSFORM, this._getRelativeTransform);
},
_getRelativeTransform: function() {
var at = new Kinetic.Transform(),
transformsEnabled, trans;

// start with stage and traverse downwards to self
this._eachAncestorReverse(function(node) {
transformsEnabled = node.transformsEnabled();
trans = node.getTransform();

if (transformsEnabled === 'all') {
at.multiply(trans);
}
else if (transformsEnabled === 'position') {
at.translate(node.x(), node.y());
}
}, true, true);
return at;
},

/**
* get transform of the node
* @method
Expand Down
10 changes: 5 additions & 5 deletions src/Shape.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@
bufferContext.clear();
bufferContext.save();
bufferContext._applyLineJoin(this);
bufferContext._applyTransform(this);
bufferContext._applyTransform(this, canvas.isCache);

drawFunc.call(this, bufferContext);
bufferContext.restore();
Expand All @@ -166,13 +166,13 @@
context.restore();
}

context._applyOpacity(this);
context._applyOpacity(this, canvas.isCache);
context.drawImage(bufferCanvas._canvas, 0, 0);
}
// if buffer canvas is not needed
else {
context._applyLineJoin(this);
context._applyTransform(this);
context._applyTransform(this, canvas.isCache);

if (hasShadow) {
context.save();
Expand All @@ -181,7 +181,7 @@
context.restore();
}

context._applyOpacity(this);
context._applyOpacity(this, canvas.isCache);
drawFunc.call(this, context);
}
context.restore();
Expand All @@ -205,7 +205,7 @@
else if (drawFunc) {
context.save();
context._applyLineJoin(this);
context._applyTransform(this);
context._applyTransform(this, canvas.isCache);

drawFunc.call(this, context);
context.restore();
Expand Down