mercredi 9 novembre 2016

save previous and current mouse click coordinates

I have this code that tracks mouse (x,y) coordinates and clicked on circle

coordinates as well . How the (x,y ) coordinates for the previous click and

current click can be saved and displayed ?

var xPos, yPos, circX, circY, saveLabel; // global
function clickIt(evt)
{ var i, xDiff, yDiff, dist, result, cX, cY; 
xPos=null; yPos=null; circX=null; circY=null; saveLabel=""  
evt= evt || event;
xPos=evt.offsetX || evt.pageX;
yPos=evt.offsetY || evt.pageY;
// check posn against centres
  cirx=0; ciry=0;   
for( i=0;i<circles.length;i++)
 { cX=circles[i].x; cY=circles[i].y;
   xDiff=Math.abs(cX-xPos);
   yDiff=Math.abs(cY-yPos);
   dist=Math.sqrt(Math.pow(xDiff,2)+Math.pow(yDiff,2)); 
  // info on clicked circle

   if(dist <=30)
    { saveLabel=circles[i].label;  
      circX=circles[i].x;
      circY=circles[i].y;
    }
 }
   result=(saveLabel.length >0)? "You hit circle "+saveLabel+"" : "Try to click on a circle"; 
   document.getElementById("msg").innerHTML=result;  
   alert("Label= "+saveLabel+": xPos= "+xPos+": yPos= "+yPos+": circX= "+circX+": circY= "+circY+":");    
}

JSfiddle : https://jsfiddle.net/sarmadm/wyzo5rrb/




Aucun commentaire:

Enregistrer un commentaire