Okay, this is getting complicated…
Given situation:
I have a canvas with the dimensions of 800×600.
My mouse is at canvas position 100×200 (for example).
I save my canvas state.
Now I rotate and translate the canvas, I draw a square.
I restore my canvas state.
Is there any way to determine if my mouse is over the square?
I think I would have to translate/rotate my mouse position as well – in the opposite direction, but how would I do this?
You can get hold of an objects world position/rotation by recursively applying this formula:
A javascript implementation would be:
Try it out with these objects:
It won’t be long before you want to calculate the shortest angle between two objects, if you get the deltaX (x2-x1) and deltaY (y2-y1) between the two objects you can get the angle with this function:
In the long run it’s better to learn using matrices instead. The equivalence of getting the world pos/rot is a world matrix. Here’s some good info about matrices (in the SVG doc, but it’s not relevant): http://www.w3.org/TR/SVG/coords.html#NestedTransformations
This is how you would do it with matrices (and the gl-matrix lib https://github.com/toji/gl-matrix):
Oh, i forgot, now to finally register a click you just get the screen coordinates of the mouse and compare them to the objects position + the canvas viewport offset.