B2G/gecko/browser/devtools/tilt/TiltVisualizer.jsm
/**
   * Maps the 2d coordinates of the mouse location to a 3d point on a sphere.
   *
   * @param {Number} x
   *                 the current horizontal coordinate of the mouse
   * @param {Number} y
   *                 the current vertical coordinate of the mouse
   * @param {Number} aWidth
   *                 the width of canvas
   * @param {Number} aHeight
   *                 the height of canvas
   * @param {Number} aRadius
   *                 optional, the radius of the arcball
   * @param {Array} aSphereVec
   *                a 3d vector to store the sphere coordinates
   */
Maps the 2d coordinates of the mouse location to a 3d point on a sphere.
Maps the 2d coordinates of the mouse location to a 3d point on a sphere .
function TVA__pointToSphere(
    x, y, aWidth, aHeight, aRadius, aSphereVec)
  {
    x = (x - aWidth * 0.5) / aRadius;
    y = (y - aHeight * 0.5) / aRadius;
    let normal = 0;
    let sqlength = x * x + y * y;
    if (sqlength > 1) {
      normal = 1 / Math.sqrt(sqlength);
      aSphereVec[0] = x * normal;
      aSphereVec[1] = y * normal;
      aSphereVec[2] = 0;
    } else {
      aSphereVec[0] = x;
      aSphereVec[1] = y;
      aSphereVec[2] = Math.sqrt(1 - sqlength);
    }
  }
function TVA__pointToSphere ( x , y , aWidth , aHeight , aRadius , aSphereVec ) { x = ( x - aWidth * 0.5 ) / aRadius ; y = ( y - aHeight * 0.5 ) / aRadius ; let normal = 0 ; let sqlength = x * x + y * y ; if ( sqlength > 1 ) { normal = 1 / Math . sqrt ( sqlength ) ; aSphereVec [ 0 ] = x * normal ; aSphereVec [ 1 ] = y * normal ; aSphereVec [ 2 ] = 0 ; } else { aSphereVec [ 0 ] = x ; aSphereVec [ 1 ] = y ; aSphereVec [ 2 ] = Math . sqrt ( 1 - sqlength ) ; } }
Maps the 2d coordinates of the mouse location to a 3d point on a sphere.
Maps the 2d coordinates of the mouse location to a 3d point on a sphere .
// adjust point coords and scale down to range of [-1..1] // compute the square length of the vector to the point from the center // if the point is mapped outside of the sphere // calculate the normalization factor // set the normalized vector (a point on the sphere) // set the vector to a point mapped inside the sphere