mirror of
https://github.com/konvajs/konva.git
synced 2025-09-18 18:27:58 +08:00
stage drag and drop works again. continued refactoring DragAndDrop.js
This commit is contained in:
@@ -11,11 +11,12 @@
|
|||||||
|
|
||||||
// methods
|
// methods
|
||||||
_drag: function(evt) {
|
_drag: function(evt) {
|
||||||
var dd = Kinetic.DD, node = dd.node;
|
var dd = Kinetic.DD,
|
||||||
|
node = dd.node;
|
||||||
|
|
||||||
if(node) {
|
if(node) {
|
||||||
var pos = node.getStage().getPointerPosition();
|
var pos = node.getStage().getPointerPosition();
|
||||||
var dbf = node.attrs.dragBoundFunc;
|
var dbf = node.getDragBoundFunc();
|
||||||
|
|
||||||
var newNodePos = {
|
var newNodePos = {
|
||||||
x: pos.x - dd.offset.x,
|
x: pos.x - dd.offset.x,
|
||||||
@@ -72,34 +73,46 @@
|
|||||||
if (evt && dragEndNode) {
|
if (evt && dragEndNode) {
|
||||||
dragEndNode._handleEvent('dragend', evt);
|
dragEndNode._handleEvent('dragend', evt);
|
||||||
}
|
}
|
||||||
},
|
|
||||||
_endDrag: function() {
|
|
||||||
this._endDragBefore();
|
|
||||||
this._endDragAfter();
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Node extenders
|
// Node extenders
|
||||||
|
|
||||||
Kinetic.Node.prototype._startDrag = function(evt) {
|
/**
|
||||||
|
* initiate drag and drop
|
||||||
|
* @name startDrag
|
||||||
|
* @methodOf Kinetic.Node.prototype
|
||||||
|
*/
|
||||||
|
Kinetic.Node.prototype.startDrag = function() {
|
||||||
var dd = Kinetic.DD,
|
var dd = Kinetic.DD,
|
||||||
that = this,
|
that = this,
|
||||||
stage = this.getStage(),
|
stage = this.getStage(),
|
||||||
layer = this.getLayer(),
|
layer = this.getLayer(),
|
||||||
pos = stage.getPointerPosition();
|
pos = stage.getPointerPosition(),
|
||||||
|
m = this.getTransform().getTranslation(),
|
||||||
|
ap = this.getAbsolutePosition(),
|
||||||
|
animNode = layer || this;
|
||||||
|
|
||||||
if(pos) {
|
if(pos) {
|
||||||
var m = this.getTransform().getTranslation(),
|
|
||||||
ap = this.getAbsolutePosition(),
|
|
||||||
layer = this.getLayer();
|
|
||||||
|
|
||||||
dd.node = this;
|
dd.node = this;
|
||||||
dd.offset.x = pos.x - ap.x;
|
dd.offset.x = pos.x - ap.x;
|
||||||
dd.offset.y = pos.y - ap.y;
|
dd.offset.y = pos.y - ap.y;
|
||||||
dd.anim.node = layer;
|
dd.anim.node = animNode;
|
||||||
dd.anim.start();
|
dd.anim.start();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* stop drag and drop
|
||||||
|
* @name stopDrag
|
||||||
|
* @methodOf Kinetic.Node.prototype
|
||||||
|
*/
|
||||||
|
Kinetic.Node.prototype.stopDrag = function() {
|
||||||
|
var dd = Kinetic.DD;
|
||||||
|
dd._endDragBefore();
|
||||||
|
dd._endDragAfter();
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* set draggable
|
* set draggable
|
||||||
* @name setDraggable
|
* @name setDraggable
|
||||||
@@ -126,7 +139,7 @@
|
|||||||
var that = this;
|
var that = this;
|
||||||
this.on('mousedown.kinetic touchstart.kinetic', function(evt) {
|
this.on('mousedown.kinetic touchstart.kinetic', function(evt) {
|
||||||
if(!Kinetic.DD.node) {
|
if(!Kinetic.DD.node) {
|
||||||
that._startDrag(evt);
|
that.startDrag(evt);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
@@ -147,7 +160,7 @@
|
|||||||
var stage = this.getStage();
|
var stage = this.getStage();
|
||||||
var dd = Kinetic.DD;
|
var dd = Kinetic.DD;
|
||||||
if(stage && dd.node && dd.node._id === this._id) {
|
if(stage && dd.node && dd.node._id === this._id) {
|
||||||
dd._endDrag();
|
dd.node.stopDrag();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -204,7 +217,7 @@
|
|||||||
*/
|
*/
|
||||||
Kinetic.Node.prototype.isDraggable = Kinetic.Node.prototype.getDraggable;
|
Kinetic.Node.prototype.isDraggable = Kinetic.Node.prototype.getDraggable;
|
||||||
|
|
||||||
// listen for capturing phase so that the _endDrag method is
|
// listen for capturing phase so that the _endDrag* methods are
|
||||||
// called before the stage mouseup event is triggered in order
|
// called before the stage mouseup event is triggered in order
|
||||||
// to render the hit graph just in time to pick up the event
|
// to render the hit graph just in time to pick up the event
|
||||||
var html = document.getElementsByTagName('html')[0];
|
var html = document.getElementsByTagName('html')[0];
|
||||||
|
@@ -406,7 +406,6 @@
|
|||||||
this.targetShape = null;
|
this.targetShape = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// start drag and drop
|
|
||||||
if(dd) {
|
if(dd) {
|
||||||
dd._drag(evt);
|
dd._drag(evt);
|
||||||
}
|
}
|
||||||
@@ -414,6 +413,7 @@
|
|||||||
_mousedown: function(evt) {
|
_mousedown: function(evt) {
|
||||||
this._setPointerPosition(evt);
|
this._setPointerPosition(evt);
|
||||||
var dd = Kinetic.DD,
|
var dd = Kinetic.DD,
|
||||||
|
go = Kinetic.Global,
|
||||||
obj = this.getIntersection(this.getPointerPosition()),
|
obj = this.getIntersection(this.getPointerPosition()),
|
||||||
shape;
|
shape;
|
||||||
|
|
||||||
@@ -425,8 +425,8 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
//init stage drag and drop
|
//init stage drag and drop
|
||||||
if(dd && this.isDraggable() && !dd.node) {
|
if(dd && !go.isDragging() && this.isDraggable()) {
|
||||||
this._startDrag(evt);
|
this.startDrag(evt);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
_mouseup: function(evt) {
|
_mouseup: function(evt) {
|
||||||
@@ -480,7 +480,7 @@
|
|||||||
|
|
||||||
// init stage drag and drop
|
// init stage drag and drop
|
||||||
if(dd && !go.isDragging() && this.isDraggable()) {
|
if(dd && !go.isDragging() && this.isDraggable()) {
|
||||||
this._startDrag(evt);
|
this.startDrag(evt);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
_touchend: function(evt) {
|
_touchend: function(evt) {
|
||||||
|
@@ -893,7 +893,7 @@ Test.Modules.DRAG_AND_DROP = {
|
|||||||
|
|
||||||
//Circle.savePixels();
|
//Circle.savePixels();
|
||||||
},
|
},
|
||||||
'drag and drop stage': function(containerId) {
|
'*drag and drop stage': function(containerId) {
|
||||||
var stage = new Kinetic.Stage({
|
var stage = new Kinetic.Stage({
|
||||||
container: containerId,
|
container: containerId,
|
||||||
width: 578,
|
width: 578,
|
||||||
|
Reference in New Issue
Block a user