0

How do I get the x and y pixel value of the mouse click on the image after a canvas translate & scale? What's the needed calculation from the event x and y?

I am using panning & zooming code from this answer:

var zoom = function(clicks) {
    var pt = ctx.transformedPoint(lastX,lastY);
    ctx.translate(pt.x,pt.y);
    var factor = Math.pow(scaleFactor,clicks);
    ctx.scale(factor,factor);
    ctx.translate(-pt.x,-pt.y);
    redraw();
}

var handleScroll = function(evt) {
    var delta = evt.wheelDelta ? evt.wheelDelta/40 : evt.detail ? -evt.detail : 0;
    if (delta) zoom(delta);
    return evt.preventDefault() && false;
};

Example website

Community
  • 1
  • 1
Mona
  • 21
  • 4
  • Can you show us some code you've tried? – cereallarceny Oct 23 '12 at 23:15
  • @cereallarceny - thanks for your patience and help! I deleted my previous useless postings. So given the above code, what calculations do I need to do in order to get the image x and y pixel in a mousedown event after the user has zoomed? – Mona Oct 25 '12 at 22:54

1 Answers1

-3

If you're just trying to get the mouse X and Y position after a click, then you should attach a function to the mousedown event in Javascript. This is called event binding. In short, while in the DOM you can bind events to elements within the page. Here's an example I made which shows an event bound to the #box element.

document.getElementById("box").onmousedown = function() {
    var posx = 0;
    var posy = 0;
    if (!e) var e = window.event;
    if (e.pageX || e.pageY)     {
        posx = e.pageX;
        posy = e.pageY;
    }
    else if (e.clientX || e.clientY)     {
        posx = e.clientX + document.body.scrollLeft
            + document.documentElement.scrollLeft;
        posy = e.clientY + document.body.scrollTop
            + document.documentElement.scrollTop;
    }

    alert("Mouse x: " + posx + "\nMouse y: " + posy);
};​

This code finds an element with an ID of box and tells the code in the function to run every time after the mouse is depressed. In this example we also fall back for older system to ensure that it works cross-platform and cross-browser. If you wanted to bind this event to the whole webpage rather than just an element then you can replace document.getElementById("box") with window.

This however does not compute the relative position within a DOM element. There's multiple ways to do that which I won't go into detail on here, but I'll include a few methods below. Best of luck!

RESOURCES

Community
  • 1
  • 1
cereallarceny
  • 4,913
  • 4
  • 39
  • 74
  • 1
    thanks for the answer @cereallarceny. I do know how to attach the event and your code is good for unzoomed canvas. My question is more how to calculate the image x and y pixel AFTER the user zooms (zoom code is in the first posted code sectione). Notice that the zoom code moves toward the mouse spot the user is zooming in on so it does translation along the way. I have not been able to figure out the calculation needed to get the image x and y of the mouse click AFTER the zoom.... – Mona Oct 26 '12 at 01:23
  • I don't see why it should be any different. I mean, if you were to click, then zoom in on that spot, then click again, wouldn't the x and y be slightly more accurate than before you zoomed in? In this case, I'd suggest storing the x and y **before** you zoom in and then use those values on the completion of the zoom. – cereallarceny Oct 26 '12 at 01:45
  • Yes, I thought it should be the same too and I am storing the x & y before the zoom. From the zoom function above, I store the pt and factor variables and use it below (so pt from zoom() is same pt below, etc). Here is my simplified code which is not working (it is close but correct... – Mona Oct 26 '12 at 22:46
  • function mousedown_listener ( event ) { var x = event.pageX; var y = event.pageY; var point = { 'x' : 0, 'y' : 0 }; if ( factor == 1.0 ) { point.x = x - pt.x; point.y = y - pt.y; } else { point.x = ( pt.x - x ) / factor + x; point.y = ( pt.y - y ) / factor + y; } [...] – Mona Oct 26 '12 at 22:47
  • Alright, I feel like an idiot, why can't I get the code to format properly? I've read the help but am still having problems...sigh – Mona Oct 26 '12 at 22:48
  • You can't format your code very well in comments. Perhaps try editing your question. If you scroll back up to your question and look for the box with your name in it, look on the left side and you'll see `edit`. Try that by making an update to your original question. – cereallarceny Oct 27 '12 at 04:40