code: function rotateEuler(euler, result) {
    var heading, bank, attitude,
      ch = Math.cos(euler.yaw),
      sh = Math.sin(euler.yaw),
      ca = Math.cos(euler.pitch),
      sa = Math.sin(euler.pitch),
      cb = Math.cos(euler.roll),
      sb = Math.sin(euler.roll),
  
      matrix = [
        sh*sb - ch*sa*cb,   -ch*ca,    ch*sa*sb + sh*cb,
        ca*cb,              -sa,      -ca*sb,
        sh*sa*cb + ch*sb,    sh*ca,   -sh*sa*sb + ch*cb
      ]; // Includes 90-degree rotation around z axis
  
    /* [m00 m01 m02] 0 1 2
     * [m10 m11 m12] 3 4 5
     * [m20 m21 m22] 6 7 8 */
  
    if (matrix[3] > 0.9999)
    {
      // Deal with singularity at north pole
      heading = Math.atan2(matrix[2],matrix[8]);
      attitude = Math.PI/2;
      bank = 0;
    }
    else if (matrix[3] < -0.9999)
    {
      // Deal with singularity at south pole
      heading = Math.atan2(matrix[2],matrix[8]);
      attitude = -Math.PI/2;
      bank = 0;
    }
    else
    {
      heading = Math.atan2(-matrix[6],matrix[0]);
      bank = Math.atan2(-matrix[5],matrix[4]);
      attitude = Math.asin(matrix[3]);
    }
  
    result.yaw = heading;
    result.pitch = attitude;
    result.roll = bank;
  }
docs: Taken from krpano's gyro plugin by Aldo Hoeben: https://github.com/fieldOfView/krpano_fovplugins/tree/master/gyro/ For the math, see references: http://www.euclideanspace.com/maths/geometry/rotations/conversions/eulerToMatrix/index.htm http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToEuler/index.htm