Made getAbsoluteOpacity work recursively. Also added support for stage opacity (included test).

This commit is contained in:
David Johansson 2012-10-01 00:55:12 +02:00
parent 759ec116ce
commit 73bf805483
4 changed files with 37 additions and 17 deletions

11
dist/kinetic-core.js vendored
View File

@ -3,7 +3,7 @@
* http://www.kineticjs.com/
* Copyright 2012, Eric Rowell
* Licensed under the MIT or GPL Version 2 licenses.
* Date: Sep 26 2012
* Date: Oct 01 2012
*
* Copyright (C) 2011 - 2012 by Eric Rowell
*
@ -1742,12 +1742,9 @@ Kinetic.Node.prototype = {
* @methodOf Kinetic.Node.prototype
*/
getAbsoluteOpacity: function() {
var absOpacity = 1;
var node = this;
// traverse upwards
while(node.nodeType !== 'Stage') {
absOpacity *= node.attrs.opacity;
node = node.parent;
var absOpacity = this.getOpacity();
if (this.getParent()) {
absOpacity *= this.getParent().getAbsoluteOpacity();
}
return absOpacity;
},

File diff suppressed because one or more lines are too long

View File

@ -519,12 +519,9 @@ Kinetic.Node.prototype = {
* @methodOf Kinetic.Node.prototype
*/
getAbsoluteOpacity: function() {
var absOpacity = 1;
var node = this;
// traverse upwards
while(node.nodeType !== 'Stage') {
absOpacity *= node.attrs.opacity;
node = node.parent;
var absOpacity = this.getOpacity();
if (this.getParent()) {
absOpacity *= this.getParent().getAbsoluteOpacity();
}
return absOpacity;
},

View File

@ -696,6 +696,32 @@ Test.prototype.tests = {
test(circle.getAbsoluteOpacity() === 0.25, 'abs opacity should be 0.25');
test(layer.getAbsoluteOpacity() === 0.5, 'abs opacity should be 0.5');
},
'STAGE - set shape, layer and stage opacity to 0.5': function(containerId) {
var stage = new Kinetic.Stage({
container: containerId,
width: 578,
height: 200
});
var layer = new Kinetic.Layer();
var circle = new Kinetic.Circle({
x: stage.getWidth() / 2,
y: stage.getHeight() / 2,
radius: 70,
fill: 'green',
stroke: 'black',
strokeWidth: 4
});
circle.setOpacity(0.5);
layer.setOpacity(0.5);
stage.setOpacity(0.5);
layer.add(circle);
stage.add(layer);
test(circle.getAbsoluteOpacity() === 0.125, 'abs opacity should be 0.125');
test(layer.getAbsoluteOpacity() === 0.25, 'abs opacity should be 0.25');
test(stage.getAbsoluteOpacity() === 0.5, 'abs opacity should be 0.5');
},
'STAGE - remove shape without adding its parent to stage': function(containerId) {
var stage = new Kinetic.Stage({
container: containerId,