2 ways to get a mouse click relative to the Canva position

Source: Internet
Author: User

If you add the onmousedown event to the canvas, the mouse position obtained is relative to the position of the current document (x, y):

The first type of conversion:

(x-x1,y-y1)

X, y is the mouse click position, the Getboundingclientrect method is the canvas comes with a function to get the location information of the paint area

function Windowtocanvas (x, y) {   var bbox = Canvas.getboundingclientrect ();   return {x:x-Bbox.left * (canvas.width  /bbox.width),            y:y-bbox.top  * (Canvas.height/bbox.height)};}

The second type is more concise:

Canvas.onmousedown = function (e) {   //var loc = Windowtocanvas (e.clientx| | e.x, e.clienty| | E.Y);   var x=e.layerx| | E.offsetx;   var y=e.layery| | e.offsety;   E.preventdefault (); Prevent cursor Change   savedrawingsurface ();   mousedown.x = loc.x;   MOUSEDOWN.Y = loc.y;   dragging = true;};

Only Firefox supports Layerx, and other browsers support the standard OFFSETX

2 ways to get a mouse click relative to the Canva position

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.