I am developing an online painter and I need to know in which part of the canvas * -a * (and it is not full screen) to be the cursor (and I can't find the size of the canvas :( ).

Ps Here is an illustration to make it clearer:

alt text

    2 answers 2

    1. Get the absolute cursor coordinates window.event.x; window.event.y;
    2. Get the absolute coordinates of the upper left corner of the drawing area. Probably they are in css or written directly in the <div>
    3. Subtract from (1) (2)

    In general, Flash for such things is much better than Javascript. Pruflnik

      So:

      function position(event) { var x = y = 0; if (document.attachEvent != null) { x = event.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft); y = event.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop); } else if (!document.attachEvent && document.addEventListener) { x = event.clientX + scrollX; y = event.clientY + scrollY; } else {} return {x: x,y: y}; }