The situation:
The canvas contains an image, which can be rotated, scaled and translated (moved). The image is an SVG with many paths inside, thus the original coordinates must be retrieved to check which path is encapsulating the mouse.
The SVG is rendered on a canvas.
//calculates the distance between two points
var distance = function(x1, y1, x2, y2) {
return Math.sqrt(Math.pow(Math.abs(x1 - x2), 2),
Math.pow(Math.abs(y1 - y2), 2));
};
//defines the center of the image
//this is also the rotational point
var center = {
x : target.getWidth() / 2,
y : target.getHeight() / 2
};
//defines the click location
var click = {
x : event.layerX / target.getScaleX() - target.getLeft(),
y : event.layerY / target.getScaleY() - target.getTop()
};
Above code makes sure the x
and y
are the correct values used within the paths, provided there is no rotation.
So basically: provided the user has rotated the image 45 degrees and clicks on the image: the click coordinates need to be rotated back 45 degrees to get the needed coordinates. But it's been a few years since I've done maths like that...
The origin (rotational point) of the image is its center.