mirror of
https://github.com/konvajs/konva.git
synced 2025-12-21 19:27:08 +08:00
exposed absolute positioning logic inside the _prepareDrag method so that developers have access to it via the setAbsolutePosition() method
This commit is contained in:
49
src/Node.js
49
src/Node.js
@@ -33,7 +33,7 @@ Kinetic.Node = function(config) {
|
||||
this.attrs.draggable = false;
|
||||
this.eventListeners = {};
|
||||
|
||||
// set attrs
|
||||
// set attrs
|
||||
this.setAttrs(config);
|
||||
};
|
||||
/*
|
||||
@@ -298,6 +298,53 @@ Kinetic.Node.prototype = {
|
||||
getAbsolutePosition: function() {
|
||||
return this.getAbsoluteTransform().getTranslation();
|
||||
},
|
||||
/**
|
||||
* set absolute position relative to stage
|
||||
*/
|
||||
setAbsolutePosition: function(pos, override) {
|
||||
/*
|
||||
* save rotation and scale and then
|
||||
* remove them from the transform
|
||||
*/
|
||||
var rot = this.attrs.rotation;
|
||||
var scale = {
|
||||
x: this.attrs.scale.x,
|
||||
y: this.attrs.scale.y
|
||||
};
|
||||
this.attrs.rotation = 0;
|
||||
this.attrs.scale = {
|
||||
x: 1,
|
||||
y: 1
|
||||
};
|
||||
|
||||
// unravel transform
|
||||
var it = this.getAbsoluteTransform();
|
||||
it.invert();
|
||||
it.translate(pos.x, pos.y);
|
||||
pos = {
|
||||
x: this.attrs.x + it.getTranslation().x,
|
||||
y: this.attrs.y + it.getTranslation().y
|
||||
};
|
||||
|
||||
// handle override
|
||||
if(override !== undefined) {
|
||||
if(override.x !== undefined) {
|
||||
pos.x = override.x;
|
||||
}
|
||||
if(override.y !== undefined) {
|
||||
pos.y = override.y;
|
||||
}
|
||||
}
|
||||
|
||||
this.setPosition(pos.x, pos.y);
|
||||
|
||||
// restore rotation and scale
|
||||
this.rotate(rot);
|
||||
this.attrs.scale = {
|
||||
x: scale.x,
|
||||
y: scale.y
|
||||
};
|
||||
},
|
||||
/**
|
||||
* move node by an amount
|
||||
* @param {Number} x
|
||||
|
||||
Reference in New Issue
Block a user