0

Is it possible to take a "display coordinate" such as the location of the mouse over the canvas and get back a "scene coordinate", i.e. the coordinate before transformations would be applied? For instance, fill in the blank in the following code:

context.scale(500.0 / sizeMax, 500.0 / sizeMax);
context.translate(-minY, -minX);

mouse_x = someval;
mouse_y = someval2;

$('#xy_location').val(/* do something using mouse_x and mouse_y to get
                     back untransformed coordinate */);

I'm aware of how to get the mouse xy coordinate into canvas display space (or w/e you call it) using a technique like this one. I'm not sure how to take said display coordinate and "un-transform" it back into a coordinate in the pre-transformed space. I could track all the transforms and then manually put in their opposites, but I'd prefer some way that's more clean then that. Another option would be if someone could show me how to get the transformation matrix values I could just invert that on my own.

Is this possible in HTML5/Canvas and if so, how?

CrazyCasta
  • 26,917
  • 4
  • 45
  • 72
  • http://stackoverflow.com/questions/21717001/html5-canvas-get-coordinates-after-zoom-and-translate As for the current matrix,not all browsers implement that feature so you need to keep track of that yourself. – Sebastian Nette Jul 17 '15 at 11:50
  • https://github.com/simonsarris/Canvas-tutorials/blob/master/transform.js – markE Jul 17 '15 at 17:31
  • 1
    @markE Thanks for the link. That's what I'm trying to avoid, but it's nice to know there's some nice pre-written code assuming the browsers don't support this (which it sounds like the don't). – CrazyCasta Jul 18 '15 at 19:48

0 Answers0