简体   繁体   English

在画布中获取特定位置

[英]Getting the certain positions within a canvas

I am using Kinetic.js for this, but I figure this is not Kinetic specific. 我为此使用Kinetic.js,但我认为这不是Kinetic专用的。 The problem is as follows: 问题如下:

I am loading an image in a canvas, and I then use Kinetic to rotate this image. 我正在画布中加载图像,然后使用Kinetic旋转该图像。 How do I get the x and y of, for example, leftmost point of this image? 如何获得该图像最左端的x和y?

在此处输入图片说明

Try doing the calculations manually: 尝试手动进行计算:

 var theta = image.getRotation*Math.PI/180.;

 // Find the middle rotating point
 var midX = image.getX() + image.getWidth()/2;
 var midY = image.getY() + image.getHeight()/2;

 // Find all the corners relative to the center
 var cornersX = [image.getX()-midX, image.getX()-midX, image.getX()+image.getWidth()-midX, image.getX()+image.getWidth()-midX];
 var cornersY = [image.getY()-midY, image.getY()+image.getHeight()-midY, midY-image.getY(), image.getY()+image.getHeight()-midY];

 // Find new the minimum corner X and Y by taking the minimum of the bounding box
 var newX = 1e10;
 var newY = 1e10;
 for (var i=0; i<4; i=i+1) {
     newX = min(newX, cornersX[i]*Math.cos(theta) - cornersY[i]*Math.sin(theta) + midX);
     newY = min(newY, cornersX[i]*Math.sin(theta) + cornersY[i]*Math.cos(theta) + midY);
 }

 // new width and height
 newWidth = midX - newX;
 newHeight = midY - newY;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM