mirror of
https://github.com/konvajs/konva.git
synced 2025-10-15 12:34:52 +08:00
lots of refactoring, and code hardening. Also fixed bug in which dragend events were being fired before mouseup/touchend events
This commit is contained in:
@@ -204,11 +204,7 @@
|
||||
|
||||
if (!canvas && layer) {
|
||||
canvas = layer.getCanvas();
|
||||
}
|
||||
|
||||
if(layer && layer.getClearBeforeDraw()) {
|
||||
canvas.clear();
|
||||
}
|
||||
}
|
||||
|
||||
if(this.isVisible()) {
|
||||
if (clip) {
|
||||
@@ -229,20 +225,21 @@
|
||||
},
|
||||
drawHit: function() {
|
||||
var clip = !!this.getClipFunc() && this.nodeType !== 'Stage',
|
||||
dd = Kinetic.DD,
|
||||
hitCanvas;
|
||||
|
||||
if (clip) {
|
||||
hitCanvas = this.getLayer().hitCanvas;
|
||||
hitCanvas._clip(this);
|
||||
}
|
||||
if(this.isVisible() && this.isListening()) {
|
||||
if(this.shouldDrawHit()) {
|
||||
if (clip) {
|
||||
hitCanvas = this.getLayer().hitCanvas;
|
||||
hitCanvas._clip(this);
|
||||
}
|
||||
var children = this.children, len = children.length;
|
||||
for(var n = 0; n < len; n++) {
|
||||
children[n].drawHit();
|
||||
}
|
||||
}
|
||||
if (clip) {
|
||||
hitCanvas.getContext().restore();
|
||||
if (clip) {
|
||||
hitCanvas.getContext().restore();
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@@ -1,7 +1,7 @@
|
||||
(function() {
|
||||
Kinetic.DD = {
|
||||
anim: new Kinetic.Animation(),
|
||||
moving: false,
|
||||
started: false,
|
||||
offset: {
|
||||
x: 0,
|
||||
y: 0
|
||||
@@ -35,9 +35,6 @@
|
||||
|
||||
if(!dd.moving) {
|
||||
dd.moving = true;
|
||||
node.setListening(false);
|
||||
|
||||
// execute dragstart events if defined
|
||||
node._handleEvent('dragstart', evt);
|
||||
}
|
||||
|
||||
@@ -45,37 +42,53 @@
|
||||
node._handleEvent('dragmove', evt);
|
||||
}
|
||||
};
|
||||
Kinetic.DD._endDrag = function(evt) {
|
||||
var dd = Kinetic.DD, node = dd.node;
|
||||
Kinetic.DD._endDragBefore = function(evt) {
|
||||
var dd = Kinetic.DD,
|
||||
evt = evt || {},
|
||||
node = dd.node,
|
||||
nodeType, layer;
|
||||
|
||||
if(node) {
|
||||
var nodeType = node.nodeType, stage = node.getStage();
|
||||
node.setListening(true);
|
||||
if(nodeType === 'Stage') {
|
||||
node.draw();
|
||||
nodeType = node.nodeType,
|
||||
layer = node.getLayer();
|
||||
|
||||
if((nodeType === 'Group' || nodeType === 'Shape') && node.getDragOnTop()) {
|
||||
node.getStage().dragLayer.remove();
|
||||
}
|
||||
// else if group, shape, or layer
|
||||
else {
|
||||
if((nodeType === 'Group' || nodeType === 'Shape') && node.getDragOnTop()) {
|
||||
node.getStage().dragLayer.remove();
|
||||
}
|
||||
|
||||
node.moveToTop();
|
||||
node.getLayer().draw();
|
||||
}
|
||||
|
||||
delete dd.node;
|
||||
dd.anim.stop();
|
||||
|
||||
// only fire dragend event if the drag and drop
|
||||
// operation actually started. This can be detected by
|
||||
// checking dd.moving
|
||||
// operation actually started.
|
||||
if(dd.moving) {
|
||||
dd.moving = false;
|
||||
node._handleEvent('dragend', evt);
|
||||
evt.dragEndNode = node;
|
||||
}
|
||||
|
||||
delete dd.node;
|
||||
|
||||
node.moveToTop();
|
||||
|
||||
if (layer) {
|
||||
layer.draw();
|
||||
}
|
||||
else {
|
||||
node.draw();
|
||||
}
|
||||
}
|
||||
};
|
||||
Kinetic.DD._endDragAfter = function(evt) {
|
||||
var evt = evt || {},
|
||||
dragEndNode = evt.dragEndNode;
|
||||
|
||||
if (evt && dragEndNode) {
|
||||
dragEndNode._handleEvent('dragend', evt);
|
||||
}
|
||||
};
|
||||
Kinetic.DD._endDrag = function() {
|
||||
this._endDragBefore();
|
||||
this._endDragAfter();
|
||||
};
|
||||
Kinetic.Node.prototype._startDrag = function(evt) {
|
||||
var dd = Kinetic.DD,
|
||||
that = this,
|
||||
@@ -99,22 +112,12 @@
|
||||
// Group or Shape node types
|
||||
else {
|
||||
if(this.getDragOnTop()) {
|
||||
|
||||
|
||||
// WARNING: it's important to delay the moveTo operation,
|
||||
// layer redraws, and anim.start() until after the method execution
|
||||
// has completed or else there will be a flicker on mobile devices
|
||||
// due to the time it takes to append the dd canvas to the DOM
|
||||
//setTimeout(function() {
|
||||
//if(dd.node) {
|
||||
// clear shape from layer canvas
|
||||
that.setVisible(false);
|
||||
layer.draw();
|
||||
that.setVisible(true);
|
||||
stage.add(stage.dragLayer);
|
||||
dd.anim.start();
|
||||
//}
|
||||
//}, 0);
|
||||
// clear shape from layer canvas
|
||||
that.setVisible(false);
|
||||
layer.draw();
|
||||
that.setVisible(true);
|
||||
stage.add(stage.dragLayer);
|
||||
dd.anim.start();
|
||||
}
|
||||
else {
|
||||
dd.anim.start();
|
||||
@@ -173,6 +176,7 @@
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Kinetic.Node.prototype._dragCleanup = function() {
|
||||
this.off('mousedown.kinetic');
|
||||
this.off('touchstart.kinetic');
|
||||
@@ -224,11 +228,15 @@
|
||||
* @methodOf Kinetic.Node.prototype
|
||||
*/
|
||||
Kinetic.Node.prototype.isDraggable = Kinetic.Node.prototype.getDraggable;
|
||||
|
||||
|
||||
// listen for capturing phase so that the _endDrag method is
|
||||
// called before the stage mouseup event is triggered in order
|
||||
// to render the hit graph just in time to pick up the event
|
||||
var html = document.getElementsByTagName('html')[0];
|
||||
html.addEventListener('mouseup', Kinetic.DD._endDrag, true);
|
||||
html.addEventListener('touchend', Kinetic.DD._endDrag, true);
|
||||
html.addEventListener('mouseup', Kinetic.DD._endDragBefore, true);
|
||||
html.addEventListener('touchend', Kinetic.DD._endDragBefore, true);
|
||||
|
||||
html.addEventListener('mouseup', Kinetic.DD._endDragAfter, false);
|
||||
html.addEventListener('touchend', Kinetic.DD._endDragAfter, false);
|
||||
|
||||
})();
|
||||
|
32
src/Layer.js
32
src/Layer.js
@@ -46,30 +46,6 @@
|
||||
this.afterDrawFunc.call(this);
|
||||
}
|
||||
},
|
||||
/**
|
||||
* draw children nodes on hit. this includes any groups
|
||||
* or shapes
|
||||
* @name drawHit
|
||||
* @methodOf Kinetic.Layer.prototype
|
||||
*/
|
||||
drawHit: function() {
|
||||
this.hitCanvas.clear();
|
||||
Kinetic.Container.prototype.drawHit.call(this);
|
||||
},
|
||||
/**
|
||||
* draw children nodes on scene. this includes any groups
|
||||
* or shapes
|
||||
* @name drawScene
|
||||
* @methodOf Kinetic.Layer.prototype
|
||||
* @param {Kinetic.Canvas} [canvas]
|
||||
*/
|
||||
drawScene: function(canvas) {
|
||||
canvas = canvas || this.getCanvas();
|
||||
if(this.getClearBeforeDraw()) {
|
||||
canvas.clear();
|
||||
}
|
||||
Kinetic.Container.prototype.drawScene.call(this, canvas);
|
||||
},
|
||||
toDataURL: function(config) {
|
||||
config = config || {};
|
||||
var mimeType = config.mimeType || null,
|
||||
@@ -117,6 +93,14 @@
|
||||
var stage = this.getStage();
|
||||
return (stage && stage._isTempDDLayerActive()) ? stage.dragLayer.canvas : this.canvas;
|
||||
},
|
||||
/**
|
||||
* get layer hit canvas
|
||||
* @name getHitCanvas
|
||||
* @methodOf Kinetic.Layer.prototype
|
||||
*/
|
||||
getHitCanvas: function() {
|
||||
return this.hitCanvas;
|
||||
},
|
||||
/**
|
||||
* get layer canvas context
|
||||
* @name getContext
|
||||
|
@@ -916,7 +916,7 @@
|
||||
}
|
||||
},
|
||||
/*
|
||||
* draw both scene and hit graphs.
|
||||
* draw both scene and hit graphs. If the node being drawn is the stage, all of the layers will be cleared and redra
|
||||
* @name draw
|
||||
* @methodOf Kinetic.Node.prototype
|
||||
* the scene renderer
|
||||
@@ -926,10 +926,15 @@
|
||||
|
||||
if(layer && layer.getClearBeforeDraw()) {
|
||||
layer.getCanvas().clear();
|
||||
layer.getHitCanvas().clear();
|
||||
}
|
||||
|
||||
this.drawScene();
|
||||
this.drawHit();
|
||||
},
|
||||
shouldDrawHit: function() {
|
||||
var dd = Kinetic.DD;
|
||||
return this.isVisible() && this.isListening() && (!dd || !dd.moving);
|
||||
}
|
||||
};
|
||||
|
||||
|
@@ -188,7 +188,7 @@
|
||||
canvas = this.getLayer().hitCanvas,
|
||||
context = canvas.getContext();
|
||||
|
||||
if(drawFunc && this.isVisible() && this.isListening()) {
|
||||
if(drawFunc && this.shouldDrawHit()) {
|
||||
context.save();
|
||||
canvas._applyLineJoin(this);
|
||||
canvas._applyAncestorTransforms(this);
|
||||
|
22
src/Stage.js
22
src/Stage.js
@@ -51,6 +51,22 @@
|
||||
}
|
||||
this.setAttr('container', container);
|
||||
},
|
||||
draw: function() {
|
||||
// clear children layers
|
||||
var children = this.getChildren(),
|
||||
len = children.length,
|
||||
n, layer;
|
||||
|
||||
for(n = 0; n < len; n++) {
|
||||
layer = children[n];
|
||||
if (layer.getClearBeforeDraw()) {
|
||||
layer.getCanvas().clear();
|
||||
layer.getHitCanvas().clear();
|
||||
}
|
||||
}
|
||||
|
||||
Kinetic.Node.prototype.draw.call(this);
|
||||
},
|
||||
/**
|
||||
* draw layer scene graphs
|
||||
* @name draw
|
||||
@@ -396,8 +412,10 @@
|
||||
},
|
||||
_mouseup: function(evt) {
|
||||
this._setUserPosition(evt);
|
||||
var that = this, dd = Kinetic.DD, obj = this.getIntersection(this.getUserPosition());
|
||||
|
||||
var that = this,
|
||||
dd = Kinetic.DD,
|
||||
obj = this.getIntersection(this.getUserPosition());
|
||||
|
||||
if(obj && obj.shape) {
|
||||
var shape = obj.shape;
|
||||
shape._handleEvent('mouseup', evt);
|
||||
|
Reference in New Issue
Block a user