Getting the touched position in local coordinates of a transformed element

后端 未结 1 1472
别跟我提以往
别跟我提以往 2020-12-16 06:33

I have an element which has been transformed with a matrix3d to give it perspective. It represents the screen of a handheld device.

There\'s a backgroun

相关标签:
1条回答
  • 2020-12-16 06:57

    I am not entirely sure about the theory, I read about this while I was working on a similar problem, but here is what I found. The multiplication of the matrices (the matrix of the 3d transformation - homogeneous coordinates, and the position of the cursor) produces two more values apart from x and y. The first is z, and the other one is w which is used in order to project the 3d object on a 2d plane.

    If you divide the x and y values of the vector by the w coordinate, you get the correct position/mapping of the cursor on the Cartesian plane.

    So, I would replace the code in your fiddle, lines 69-70, with these:

    var x = result.get([0]);
    var y = result.get([1]);
    var w = result.get([3]);
    screenCrosshair.style.left = x / w + 'px';
    screenCrosshair.style.top = y / w + 'px';
    

    Here is the update fiddle: https://jsfiddle.net/x3ruc3tL/1/

    And then you don't need the offsetX and offsetY values of the browser in order to find the correct position.

    Please read the following articles for more information:

    https://en.wikipedia.org/wiki/3D_projection#Perspective_projection http://deltaorange.com/2012/03/08/the-truth-behind-homogenous-coordinates/

    0 讨论(0)
提交回复
热议问题