KineticJS - Scaling stage to viewport

余生颓废 提交于 2019-12-03 20:34:01
projeqht

On a separate problem I was trying to help solve, the user him/herself (user848039) figured out a solution that dealt with mouse position and KineticJS scaling.

This was the question: kineticjs stage.getAbsoluteMousePosition()?.

Credits go to him, but here was his formula that I modified to meet a more common KineticJS setup:

var mousePos = stage.getMousePosition();

mousePos.x = mousePos.x/stage.getScale().x-stage.getAbsolutePosition().x/stage.getScale().x+stage.getOffset().x;
mousePos.y = mousePos.y/stage.getScale().y-stage.getAbsolutePosition().y/stage.getScale().y+stage.getOffset().y;

Perhaps jsFiddle isn't the best example for this, since you want the stage to be innerWidth and innerHeight. Those measurements seem to play a bit weird in the jsFiddle frame, but if you look at the code, you'll see that I set the stage exactly the same as you mentioned above. With the new calculations for mousePos the mousePos.x and mousePos.y are returning the proper coordinates for your scale.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!