简体   繁体   English

获取相对于旋转图像中心的鼠标位置

[英]Getting mouse position relative to center of rotated image

I'm trying to write some code that allows a user to arbitrarily drag an image around a canvas, resize the image, and rotate the image using their mouse. 我正在尝试编写一些代码,允许用户在画布周围任意拖动图像,调整图像大小,并使用鼠标旋转图像。 I am having some trouble with my trigonometry. 我的三角函数有问题。

All points of interest are relative to the center of the image. 所有兴趣点都相对于图像的中心。 In the illustration below the mouse is at the absolute position 850, 400 (relative to the canvas' upper left). 在下图中,鼠标位于绝对位置850,400 (相对于画布的左上角)。 Since there is no rotation I can easily figure out that the mouse is in the relative position 250, 0 (relative to the center of the image). 由于没有旋转,我可以很容易地发现鼠标处于相对位置250,0 (相对于图像的中心)。

在此输入图像描述

After I rotate the image 270° and put the mouse in the same position, the absolute is 600, 150 . 将图像旋转270°并将鼠标放在相同位置后,绝对值为600,150 The relative position is exactly the same as it was before the rotation though ( 250, 0 ). 相对位置与旋转前的相对位置完全相同( 250,0 )。

在此输入图像描述

Given the absolute coordinates of the image's center point, it's rotation, and the absolute position of the mouse, how can I determine the transformed, rotated coordinates of the mouse relative to the center of the image? 给定图像中心点的绝对坐标,它的旋转以及鼠标的绝对位置, 如何确定鼠标相对于图像中心的变换旋转坐标?

I've tried many things and attempted to adapt answers from similar StackOverflow questions. 我尝试了很多东西,并试图从类似的StackOverflow问题中调整答案。 My current attempt uses a Matrix class someone published in a Gist. 我当前的尝试使用了某人在Gist中发布的类。

layerRelativePoint(x, y, layer){
    var radians = layer.rotation * (Math.PI/180);
    var matrix = new Matrix();
    matrix.translate(-layer.x, -layer.y);
    matrix.rotate(radians);
    var [x, y] = matrix.transformPoint(x, y);
    return {x, y};
}

Demo: https://plnkr.co/edit/T9XCfpZVlMWLMY67bOvW?p=preview 演示: https//plnkr.co/edit/T9XCfpZVlMWLMY67bOvW?p = preview

I've found an answer on Math.se . 我在Math.se上找到了答案。

Here is the implementation I came up with: 这是我提出的实现:

/**
 * Translate a point's absolute coordinates to it's coordinates 
 * relative to a possibly rotated center point
 * -------------------------------------------------------------
 * @param {number} absPointX - The absolute x ordinate of the point to translate
 * @param {number} absPointY - The absolute y ordinate of the point to translate
 * @param {number} centerX - The absolute x ordinate of the center point of rotation
 * @param {number} centerY - The absolute y ordinate of the center point of rotation
 * @param {number} rotationDegrees - The angle of rotation in degrees
 * @returns {x, y} - The translated point's coordinates
 */
function translatePoint(absPointX, absPointY, centerX, centerY, rotationDegrees=0) {
    // Get coordinates relative to center point
    absPointX -= centerX;
    absPointY -= centerY;

    // Convert degrees to radians
    var radians = rotationDegrees * (Math.PI / 180);

    // Translate rotation
    var cos = Math.cos(radians);
    var sin = Math.sin(radians);
    var x = (absPointX * cos) + (absPointY * sin);
    var y = (-absPointX * sin) + (absPointY * cos);

    // Round to nearest hundredths place
    x = Math.floor(x * 100) / 100;
    y = Math.floor(y * 100) / 100;

    return {x, y};
}

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

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