Skip to content

Latest commit

 

History

History
78 lines (53 loc) · 2.39 KB

18_translating_mouse_coordinates.md

File metadata and controls

78 lines (53 loc) · 2.39 KB

Example step

Find the mouse position and translate it to scene's coordinates

Goal

Understand the difference between page, canvas and scene coordinates and the related formulas

Instructions

  • start from our default scene

  • open the javascript console, we'll use it for debug

  • note that we are going to use jQuery, as trying to manage different browsers' events is not our aim and would require some boilerplate

  • define a new function and name it onMouseMove

    function onMouseMove(event) {
    
       // code here
       
    }
  • now we need to listen for mousemove event on canvas element, passing it the event handler function onMouseMove

    $(canvas).on('mousemove', onMouseMove);
  • our onMouseEvent function will receive event as argument, which is normalized by jQuery in order to provide consistent data across different environments

     $(canvas).on('mousemove', evenHandler);
  • with event we can get the current mouse position related to the page

    var pageX = event.pageX;
    var pageY = event.pageY;
  • then we will get the current canvas' offsets: the distance from the left and the top borders

    var offset = $(renderer.domElement).offset();
  • first, let's calculate the mouse position related to the canvas: just subtract offset.left from pageX and offset.top from event.pageY

    var canvasX = pageX - offset.left;
    var canvasY = pageY - offset.top;
  • print it to the console or write it into an HTML element in order to monitor it and try to run the page just to see it

  • now we must translate local coordinates to WebGL context's coordinates: we know that the context is just a cartesian graph with (0,0) in the center of canvas, horizontal x axis and vertical y axis. The limits are (-1,1) for all axis

  • so just divide the local coordinates per canvas width, multiply it for 2 and subtract 1 to the result: these are the real canvas coordinates!

    var mouseX = (localX / renderer.domElement.width) * 2 - 1;
    var mouseY = (1 - (localY / renderer.domElement.height)) * 2 - 1;
  • print or write the canvas coordinates in order to see and compare them with local and page coordinates.

Explanation