I'm drawing an image to a scaled and translated HTML5 canvas. It's zoomed to a certain point on the image to be more specific, so only a smaller partition is visible. In this smaller part, I want to select a point, and calculate the point relative to the original image. Here is an illustration:
There is a similar answer on SO, which is explaining the canvas transformation nicely, but only shows how to get the point relative to the scaled image. What is the function, which gives the correct point described above?
Update:
There are three type of coordinates in this problem.
- Image point: point on the image.
- Transformed point: point on the transformed image, where the transformation can be scaling and translating.
- Viewport point: a point from the visible area.
The transformation represented by the following matrix:
$$ \begin{bmatrix} x' \\ y' \\ 1 \\ \end{bmatrix} = \begin{bmatrix} a & c & e \\ b & d & f \\ 0 & 0 & 1 \\ \end{bmatrix} \begin{bmatrix} x \\ y \\ 1 \\ \end{bmatrix} $$
The only known data about the transformation (for me, at my current knowledge) is the transformation matrix, the original image size and the viewport size.
So, if I'm right, what needed here is the way, how can a viewport point converted to an image point.
The StackOverflow answer tells you how to change a viewport point to a transformed point. What you want to do is change a viewport point to an image point. To do this, you need to take the inverse of the matrix. Basically, the inverse of a matrix does the exact opposite of what the original matrix did. For example, if the original matrix zooms in by a factor of $2$, then the inverse zooms out by a factor of $2$. This allows us to go from a viewport point to an image point given the transformation matrix.
However, the matrix of HTML5 canvas transformations can not be inversed because it is $2 \times 3$ and not $3 \times 3$ or some other square matrix. However, to fix this, we add the row $0 \ 0 \ 1$ to the bottom of the matrix. This works because the way
translateandscaleactually works in terms of regular matrices and vectors is that it uses a $3 \times 3$ matrix, except the last row is always $0 \ 0 \ 1$, and it uses a 3D point, except the $z$ coordinate is always $1$. Thus, if we add $0 \ 0 \ 1$ to the end of the matrix, we'll be able to take the inverse of it and find the image point given the viewport point. To take the inverse of a matrix in JavaScript, use math.js: