mirror of
https://github.com/konvajs/konva.git
synced 2025-06-27 19:43:28 +08:00
cleaned up animation and transition interaction
This commit is contained in:
parent
84e7e71461
commit
70fe63b2b6
34
dist/kinetic-core.js
vendored
34
dist/kinetic-core.js
vendored
@ -41,7 +41,6 @@ var Kinetic = {};
|
|||||||
Kinetic.GlobalObject = {
|
Kinetic.GlobalObject = {
|
||||||
stages: [],
|
stages: [],
|
||||||
idCounter: 0,
|
idCounter: 0,
|
||||||
isAnimating: false,
|
|
||||||
frame: {
|
frame: {
|
||||||
time: 0,
|
time: 0,
|
||||||
timeDiff: 0,
|
timeDiff: 0,
|
||||||
@ -64,9 +63,17 @@ Kinetic.GlobalObject = {
|
|||||||
},
|
},
|
||||||
_isaCanvasAnimating: function() {
|
_isaCanvasAnimating: function() {
|
||||||
for(var n = 0; n < this.stages.length; n++) {
|
for(var n = 0; n < this.stages.length; n++) {
|
||||||
if(this.stages[n].isAnimating) {
|
var stage = this.stages[n];
|
||||||
|
if(stage.isAnimating) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for(var i = 0; i < stage.children.length; i++) {
|
||||||
|
var layer = stage.children[i];
|
||||||
|
if(layer.isTransitioning) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
},
|
},
|
||||||
@ -111,13 +118,17 @@ Kinetic.GlobalObject = {
|
|||||||
_removeTransition: function(transition) {
|
_removeTransition: function(transition) {
|
||||||
var layer = transition.node.getLayer();
|
var layer = transition.node.getLayer();
|
||||||
var id = transition.id;
|
var id = transition.id;
|
||||||
|
|
||||||
for(var n = 0; n < layer.transitions.length; n++) {
|
for(var n = 0; n < layer.transitions.length; n++) {
|
||||||
if(layer.transitions[n].id === id) {
|
if(layer.transitions[n].id === id) {
|
||||||
layer.transitions.splice(0, 1);
|
layer.transitions.splice(0, 1);
|
||||||
return false;
|
// exit loop
|
||||||
|
n = layer.transitions.length;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(layer.transitions.length === 0) {
|
||||||
|
layer.isTransitioning = false;
|
||||||
|
}
|
||||||
},
|
},
|
||||||
_runFrames: function() {
|
_runFrames: function() {
|
||||||
for(var n = 0; n < this.stages.length; n++) {
|
for(var n = 0; n < this.stages.length; n++) {
|
||||||
@ -171,7 +182,7 @@ Kinetic.GlobalObject = {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
_animationLoop: function() {
|
_animationLoop: function() {
|
||||||
if(this.isAnimating) {
|
if(this._isaCanvasAnimating()) {
|
||||||
this._updateFrameObject();
|
this._updateFrameObject();
|
||||||
this._runFrames();
|
this._runFrames();
|
||||||
var that = this;
|
var that = this;
|
||||||
@ -182,12 +193,10 @@ Kinetic.GlobalObject = {
|
|||||||
},
|
},
|
||||||
_handleAnimation: function() {
|
_handleAnimation: function() {
|
||||||
var that = this;
|
var that = this;
|
||||||
if(!this.isAnimating && this._isaCanvasAnimating()) {
|
if(this._isaCanvasAnimating()) {
|
||||||
this.isAnimating = true;
|
|
||||||
that._animationLoop();
|
that._animationLoop();
|
||||||
}
|
}
|
||||||
else if(this.isAnimating && !this._isaCanvasAnimating()) {
|
else {
|
||||||
this.isAnimating = false;
|
|
||||||
this.frame.lastTime = 0;
|
this.frame.lastTime = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -672,6 +681,9 @@ Kinetic.Node.prototype = {
|
|||||||
node: this,
|
node: this,
|
||||||
changes: changes
|
changes: changes
|
||||||
});
|
});
|
||||||
|
|
||||||
|
layer.isTransitioning = true;
|
||||||
|
Kinetic.GlobalObject._handleAnimation();
|
||||||
},
|
},
|
||||||
/**
|
/**
|
||||||
* set drag constraint
|
* set drag constraint
|
||||||
@ -885,6 +897,7 @@ Kinetic.Container.prototype = {
|
|||||||
* Stage constructor. A stage is used to contain multiple layers and handle
|
* Stage constructor. A stage is used to contain multiple layers and handle
|
||||||
* animations
|
* animations
|
||||||
* @constructor
|
* @constructor
|
||||||
|
* @augments Kinetic.Container
|
||||||
* @param {String|DomElement} cont Container id or DOM element
|
* @param {String|DomElement} cont Container id or DOM element
|
||||||
* @param {int} width
|
* @param {int} width
|
||||||
* @param {int} height
|
* @param {int} height
|
||||||
@ -1587,7 +1600,8 @@ Kinetic.Layer = function(config) {
|
|||||||
this.canvas.style.position = 'absolute';
|
this.canvas.style.position = 'absolute';
|
||||||
this.transitions = [];
|
this.transitions = [];
|
||||||
this.transitionIdCounter = 0;
|
this.transitionIdCounter = 0;
|
||||||
|
this.isTransitioning = false;
|
||||||
|
|
||||||
// call super constructors
|
// call super constructors
|
||||||
Kinetic.Container.apply(this, []);
|
Kinetic.Container.apply(this, []);
|
||||||
Kinetic.Node.apply(this, [config]);
|
Kinetic.Node.apply(this, [config]);
|
||||||
|
2
dist/kinetic-core.min.js
vendored
2
dist/kinetic-core.min.js
vendored
File diff suppressed because one or more lines are too long
@ -13,7 +13,6 @@ var Kinetic = {};
|
|||||||
Kinetic.GlobalObject = {
|
Kinetic.GlobalObject = {
|
||||||
stages: [],
|
stages: [],
|
||||||
idCounter: 0,
|
idCounter: 0,
|
||||||
isAnimating: false,
|
|
||||||
frame: {
|
frame: {
|
||||||
time: 0,
|
time: 0,
|
||||||
timeDiff: 0,
|
timeDiff: 0,
|
||||||
@ -36,9 +35,17 @@ Kinetic.GlobalObject = {
|
|||||||
},
|
},
|
||||||
_isaCanvasAnimating: function() {
|
_isaCanvasAnimating: function() {
|
||||||
for(var n = 0; n < this.stages.length; n++) {
|
for(var n = 0; n < this.stages.length; n++) {
|
||||||
if(this.stages[n].isAnimating) {
|
var stage = this.stages[n];
|
||||||
|
if(stage.isAnimating) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for(var i = 0; i < stage.children.length; i++) {
|
||||||
|
var layer = stage.children[i];
|
||||||
|
if(layer.isTransitioning) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
},
|
},
|
||||||
@ -83,13 +90,17 @@ Kinetic.GlobalObject = {
|
|||||||
_removeTransition: function(transition) {
|
_removeTransition: function(transition) {
|
||||||
var layer = transition.node.getLayer();
|
var layer = transition.node.getLayer();
|
||||||
var id = transition.id;
|
var id = transition.id;
|
||||||
|
|
||||||
for(var n = 0; n < layer.transitions.length; n++) {
|
for(var n = 0; n < layer.transitions.length; n++) {
|
||||||
if(layer.transitions[n].id === id) {
|
if(layer.transitions[n].id === id) {
|
||||||
layer.transitions.splice(0, 1);
|
layer.transitions.splice(0, 1);
|
||||||
return false;
|
// exit loop
|
||||||
|
n = layer.transitions.length;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(layer.transitions.length === 0) {
|
||||||
|
layer.isTransitioning = false;
|
||||||
|
}
|
||||||
},
|
},
|
||||||
_runFrames: function() {
|
_runFrames: function() {
|
||||||
for(var n = 0; n < this.stages.length; n++) {
|
for(var n = 0; n < this.stages.length; n++) {
|
||||||
@ -143,7 +154,7 @@ Kinetic.GlobalObject = {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
_animationLoop: function() {
|
_animationLoop: function() {
|
||||||
if(this.isAnimating) {
|
if(this._isaCanvasAnimating()) {
|
||||||
this._updateFrameObject();
|
this._updateFrameObject();
|
||||||
this._runFrames();
|
this._runFrames();
|
||||||
var that = this;
|
var that = this;
|
||||||
@ -154,12 +165,10 @@ Kinetic.GlobalObject = {
|
|||||||
},
|
},
|
||||||
_handleAnimation: function() {
|
_handleAnimation: function() {
|
||||||
var that = this;
|
var that = this;
|
||||||
if(!this.isAnimating && this._isaCanvasAnimating()) {
|
if(this._isaCanvasAnimating()) {
|
||||||
this.isAnimating = true;
|
|
||||||
that._animationLoop();
|
that._animationLoop();
|
||||||
}
|
}
|
||||||
else if(this.isAnimating && !this._isaCanvasAnimating()) {
|
else {
|
||||||
this.isAnimating = false;
|
|
||||||
this.frame.lastTime = 0;
|
this.frame.lastTime = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,8 @@ Kinetic.Layer = function(config) {
|
|||||||
this.canvas.style.position = 'absolute';
|
this.canvas.style.position = 'absolute';
|
||||||
this.transitions = [];
|
this.transitions = [];
|
||||||
this.transitionIdCounter = 0;
|
this.transitionIdCounter = 0;
|
||||||
|
this.isTransitioning = false;
|
||||||
|
|
||||||
// call super constructors
|
// call super constructors
|
||||||
Kinetic.Container.apply(this, []);
|
Kinetic.Container.apply(this, []);
|
||||||
Kinetic.Node.apply(this, [config]);
|
Kinetic.Node.apply(this, [config]);
|
||||||
|
@ -470,6 +470,9 @@ Kinetic.Node.prototype = {
|
|||||||
node: this,
|
node: this,
|
||||||
changes: changes
|
changes: changes
|
||||||
});
|
});
|
||||||
|
|
||||||
|
layer.isTransitioning = true;
|
||||||
|
Kinetic.GlobalObject._handleAnimation();
|
||||||
},
|
},
|
||||||
/**
|
/**
|
||||||
* set drag constraint
|
* set drag constraint
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
* Stage constructor. A stage is used to contain multiple layers and handle
|
* Stage constructor. A stage is used to contain multiple layers and handle
|
||||||
* animations
|
* animations
|
||||||
* @constructor
|
* @constructor
|
||||||
|
* @augments Kinetic.Container
|
||||||
* @param {String|DomElement} cont Container id or DOM element
|
* @param {String|DomElement} cont Container id or DOM element
|
||||||
* @param {int} width
|
* @param {int} width
|
||||||
* @param {int} height
|
* @param {int} height
|
||||||
|
@ -10,7 +10,7 @@ function log(message) {
|
|||||||
* Test constructor
|
* Test constructor
|
||||||
*/
|
*/
|
||||||
function Test() {
|
function Test() {
|
||||||
this.testOnly = "";
|
this.testOnly = '';
|
||||||
this.counter = 0;
|
this.counter = 0;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
|
@ -76,6 +76,10 @@ Test.prototype.tests = {
|
|||||||
});
|
});
|
||||||
|
|
||||||
stage.start();
|
stage.start();
|
||||||
|
|
||||||
|
setTimeout(function() {
|
||||||
|
stage.stop();
|
||||||
|
}, 1000);
|
||||||
},
|
},
|
||||||
'EVENTS - mousedown mouseup mouseover mouseout click dblclick / touchstart touchend dbltap': function(containerId) {
|
'EVENTS - mousedown mouseup mouseover mouseout click dblclick / touchstart touchend dbltap': function(containerId) {
|
||||||
var stage = new Kinetic.Stage(containerId, 578, 200);
|
var stage = new Kinetic.Stage(containerId, 578, 200);
|
||||||
|
@ -200,6 +200,76 @@ Test.prototype.tests = {
|
|||||||
layer.add(group);
|
layer.add(group);
|
||||||
stage.add(layer);
|
stage.add(layer);
|
||||||
},
|
},
|
||||||
|
'GROUPS - hide group': function(containerId) {
|
||||||
|
var stage = new Kinetic.Stage(containerId, 578, 200);
|
||||||
|
var layer = new Kinetic.Layer();
|
||||||
|
var group = new Kinetic.Group();
|
||||||
|
var circle = new Kinetic.Circle({
|
||||||
|
x: stage.width / 2,
|
||||||
|
y: stage.height / 2,
|
||||||
|
radius: 70,
|
||||||
|
fill: 'green',
|
||||||
|
stroke: 'black',
|
||||||
|
strokeWidth: 4
|
||||||
|
});
|
||||||
|
|
||||||
|
group.add(circle);
|
||||||
|
layer.add(group);
|
||||||
|
stage.add(layer);
|
||||||
|
|
||||||
|
group.hide();
|
||||||
|
layer.draw();
|
||||||
|
},
|
||||||
|
'GROUPS - create two groups, move first group': function(containerId) {
|
||||||
|
var stage = new Kinetic.Stage(containerId, 578, 200);
|
||||||
|
var greenLayer = new Kinetic.Layer();
|
||||||
|
var blueLayer = new Kinetic.Layer();
|
||||||
|
var greenGroup = new Kinetic.Group();
|
||||||
|
var blueGroup = new Kinetic.Group();
|
||||||
|
|
||||||
|
var greenCircle = new Kinetic.Circle({
|
||||||
|
x: stage.width / 2 - 100,
|
||||||
|
y: stage.height / 2,
|
||||||
|
radius: 70,
|
||||||
|
fill: 'green',
|
||||||
|
stroke: 'black',
|
||||||
|
strokeWidth: 4,
|
||||||
|
draggable: true
|
||||||
|
});
|
||||||
|
|
||||||
|
var blueCircle = new Kinetic.Circle({
|
||||||
|
x: stage.width / 2 + 100,
|
||||||
|
y: stage.height / 2,
|
||||||
|
radius: 70,
|
||||||
|
fill: 'blue',
|
||||||
|
stroke: 'black',
|
||||||
|
strokeWidth: 4
|
||||||
|
});
|
||||||
|
|
||||||
|
greenGroup.add(greenCircle);
|
||||||
|
blueGroup.add(blueCircle);
|
||||||
|
greenLayer.add(greenGroup);
|
||||||
|
blueLayer.add(blueGroup);
|
||||||
|
stage.add(greenLayer);
|
||||||
|
stage.add(blueLayer);
|
||||||
|
|
||||||
|
blueLayer.removeChildren();
|
||||||
|
var blueGroup2 = new Kinetic.Group();
|
||||||
|
var blueCircle2 = new Kinetic.Circle({
|
||||||
|
x: stage.width / 2,
|
||||||
|
y: stage.height / 2,
|
||||||
|
radius: 70,
|
||||||
|
fill: 'blue',
|
||||||
|
stroke: 'black',
|
||||||
|
strokeWidth: 4
|
||||||
|
});
|
||||||
|
blueGroup2.add(blueCircle2);
|
||||||
|
blueLayer.add(blueGroup2);
|
||||||
|
blueLayer.draw();
|
||||||
|
|
||||||
|
blueGroup2.setPosition(100, 0);
|
||||||
|
blueLayer.draw();
|
||||||
|
},
|
||||||
////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////
|
||||||
// SHAPES tests
|
// SHAPES tests
|
||||||
////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////
|
||||||
@ -850,26 +920,6 @@ Test.prototype.tests = {
|
|||||||
circle.show();
|
circle.show();
|
||||||
layer.draw();
|
layer.draw();
|
||||||
},
|
},
|
||||||
'GROUPS - hide group': function(containerId) {
|
|
||||||
var stage = new Kinetic.Stage(containerId, 578, 200);
|
|
||||||
var layer = new Kinetic.Layer();
|
|
||||||
var group = new Kinetic.Group();
|
|
||||||
var circle = new Kinetic.Circle({
|
|
||||||
x: stage.width / 2,
|
|
||||||
y: stage.height / 2,
|
|
||||||
radius: 70,
|
|
||||||
fill: 'green',
|
|
||||||
stroke: 'black',
|
|
||||||
strokeWidth: 4
|
|
||||||
});
|
|
||||||
|
|
||||||
group.add(circle);
|
|
||||||
layer.add(group);
|
|
||||||
stage.add(layer);
|
|
||||||
|
|
||||||
group.hide();
|
|
||||||
layer.draw();
|
|
||||||
},
|
|
||||||
'SHAPES - set shape alpha to 0.5': function(containerId) {
|
'SHAPES - set shape alpha to 0.5': function(containerId) {
|
||||||
var stage = new Kinetic.Stage(containerId, 578, 200);
|
var stage = new Kinetic.Stage(containerId, 578, 200);
|
||||||
var layer = new Kinetic.Layer();
|
var layer = new Kinetic.Layer();
|
||||||
@ -1212,16 +1262,16 @@ Test.prototype.tests = {
|
|||||||
layer.draw();
|
layer.draw();
|
||||||
});
|
});
|
||||||
test(stage.isAnimating === false, 'stage should not be animating');
|
test(stage.isAnimating === false, 'stage should not be animating');
|
||||||
test(Kinetic.GlobalObject.isAnimating === false, 'global object should not be animating');
|
test(Kinetic.GlobalObject._isaCanvasAnimating() === false, 'global object should not be animating');
|
||||||
|
|
||||||
stage.start();
|
stage.start();
|
||||||
|
|
||||||
test(stage.isAnimating === true, 'stage should be animating');
|
test(stage.isAnimating === true, 'stage should be animating');
|
||||||
test(Kinetic.GlobalObject.isAnimating === true, 'global object should be animating');
|
test(Kinetic.GlobalObject._isaCanvasAnimating() === true, 'global object should be animating');
|
||||||
|
|
||||||
stage.stop();
|
stage.stop();
|
||||||
|
|
||||||
test(stage.isAnimating === false, 'stage should not be animating');
|
test(stage.isAnimating === false, 'stage should not be animating');
|
||||||
test(Kinetic.GlobalObject.isAnimating === false, 'global object should not be animating');
|
test(Kinetic.GlobalObject._isaCanvasAnimating() === false, 'global object should not be animating');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user