[英]Relative Pointer Position
I need some help figuring out how to calculate the relative pointer position on a KineticJS stage. 我需要一些帮助来弄清楚如何在KineticJS平台上计算相对指针位置。
In my case my stage changes position , offset , and scale . 就我而言,舞台改变位置 , 偏移量和比例 。
Here is the demo: http://jsfiddle.net/pCZzv/ 这是演示: http : //jsfiddle.net/pCZzv/
function getRelativePointerPosition() {
var pointer = stage.getPointerPosition();
var pos = stage.getPosition();
var offset = stage.getOffset();
var scale = stage.getScale();
return {
x : ((pointer.x - pos.x + offset.x) / scale.x),
y : ((pointer.y - pos.y + offset.y) / scale.y)
};
}
I want the red circles to appear where the mouse is clicked. 我希望红色圆圈出现在单击鼠标的位置。 I'm running into problems when the stage has changed it's offset and scale. 当舞台更改了偏移量和比例时,我遇到了问题。
Your calculation of the x
and y
coordinates are slightly off. 您对x
和y
坐标的计算略有偏离。 This is a working demo of your example: http://jsfiddle.net/pCZzv/1/ 这是您的示例的有效演示: http : //jsfiddle.net/pCZzv/1/
Here is the relevant changed code: 这是相关的更改代码:
function getRelativePointerPosition() {
var pointer = stage.getPointerPosition();
var pos = stage.getPosition();
var offset = stage.getOffset();
var scale = stage.getScale();
return {
x : ((pointer.x / scale.x) - (pos.x / scale.x) + offset.x),
y : ((pointer.y / scale.y) - (pos.y / scale.y) + offset.y)
};
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.