Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Canvas offsetTop and offsetLeft

In HTML5 Canvas what is offsetTop and offsetLeft ?

I'm trying to get the X and Y of a a mouse click event. I know I can get that through:

mycanvas.onclick = function (evt) {
    var offX = evt.layerX - mycanvas.offsetLeft;
    var offY = evt.layerY - mycanvas.offsetTop;
}

but what is offsetLeft and offsetTop? and what is LayerX and LayerY ?

like image 933
Stacker Avatar asked Oct 19 '11 13:10

Stacker


People also ask

What is canvas offsetLeft?

Definition and Usage The offsetLeft property returns the left position (in pixels) relative to the parent. The returned value includes: the left position, and margin of the element. the left padding, scrollbar and border of the parent.

What is element offsetTop?

The offsetTop property returns the top position (in pixels) relative to the parent. The returned value includes: the top position, and margin of the element. the top padding, scrollbar and border of the parent.

How do you get the offset of a div?

You can call the method getBoundingClientRect() on a reference to the element. Then you can examine the top , left , right and/or bottom properties... var offsets = document. getElementById('11a').

What is Offsetparent?

The offset parent is the nearest ancestor that has a position other than static. If no offset parent exists, the offset parent is the document body.


1 Answers

The offsetLeft properties are specific to elements and is described in this documentation as:

Returns the number of pixels that the upper left corner of the current element is offset to the left within the offsetParent node.

LayerX specific to events and is described in this documentation as:

Returns the horizontal coordinate of the event relative to the current layer.

Hope that helps!

like image 88
Chris Ching Avatar answered Sep 19 '22 04:09

Chris Ching