javascript - Named methods and mouse event handlers -


i've been going around in circles this. know there's didn't understand it, because made respond while, never worked properly.

these conditionals inside loop updates animation. want listen mouse motion events when option equals value, in other case, remove event listener.

the method handling mouse motion event updatemouse , it's called according state of variable opt.

if (opt == 7) {     canvas2.addeventlistener("onmousemove", updatemouse(canvas2), false); } else {     canvas2.removeeventlistener("onmousemove", updatemouse, false); } 

the problem when running script is: 'cannot read property clientx of undefined'.

function updatemouse(c) {     if (!e) var e = window.event;     var rect = c.getboundingclientrect();     m_pos = {         x: event.clientx - rect.left,         y: event.clienty - rect.top     };     console.log("salida: " + m_pos.x + " " + m_pos.y); } 

isn't correct way handle mouse events named methods?

first, should "mousemove" not "onmousemove"

second, mousemove event takes event argument, achieve want, can use:

function updatemouse(c) {     return function(e) {         var e = e | window.event;  //some cases event doesn't passed function         var rect = c.getboundingclientrect();         m_pos = {             x: event.clientx - rect.left,             y: event.clienty - rect.top         };         console.log("salida: " + m_pos.x + " " + m_pos.y);     }; } 

long story short, returns function has correct argument (accepting event), still has access canvas object passed in.

also, need keep track of result of call updatemouse(canvas2) , remove canvas, not updatemouse


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -