All files / Sources/Interaction/Misc/DeviceOrientationToCamera index.js

7.5% Statements 3/40
9.09% Branches 2/22
0% Functions 0/13
7.5% Lines 3/40

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 1201x 1x             1x                                                                                                                                                                                                                              
const listeners = [];
const orientation = {
  device: {},
  screen: window.orientation || 0,
  supported: !!window.DeviceMotionEvent,
  update: false,
};
 
const SCREEN_ORIENTATION_MAP = {
  'landscape-primary': 90,
  'landscape-secondary': -90,
  'portrait-secondary': 180,
  'portrait-primary': 0,
};
 
function isEventValid(evt) {
  return Number.isFinite(evt.alpha);
}
 
function onDeviceOrientationChangeEvent(evt) {
  orientation.device = evt;
  if (!Number.isFinite(evt.alpha)) {
    orientation.supported = false;
  }
}
 
function onScreenOrientationChangeEvent() {
  orientation.screen =
    SCREEN_ORIENTATION_MAP[
      window.screen.orientation || window.screen.mozOrientation
    ] ||
    window.orientation ||
    0;
}
 
function addWindowListeners() {
  window.addEventListener(
    'orientationchange',
    onScreenOrientationChangeEvent,
    false
  );
  window.addEventListener(
    'deviceorientation',
    onDeviceOrientationChangeEvent,
    false
  );
  orientation.update = true;
  listeners
    .filter((i) => !!i)
    .forEach((i) => i.renderWindowInteractor.requestAnimation(i));
}
 
function removeWindowListeners() {
  window.removeEventListener(
    'orientationchange',
    onScreenOrientationChangeEvent,
    false
  );
  window.removeEventListener(
    'deviceorientation',
    onDeviceOrientationChangeEvent,
    false
  );
  orientation.update = false;
  listeners
    .filter((i) => !!i)
    .forEach((i) => i.renderWindowInteractor.cancelAnimation(i));
}
 
function addCameraToSynchronize(
  renderWindowInteractor,
  camera,
  onCameraUpdate
) {
  function onAnimation() {
    if (orientation.update && isEventValid(orientation.device)) {
      const { alpha, beta, gamma } = orientation.device;
      const { screen } = orientation;
      camera.setDeviceAngles(alpha, beta, gamma, screen);
      if (onCameraUpdate) {
        onCameraUpdate();
      }
    }
  }
 
  const subscription = renderWindowInteractor.onAnimation(onAnimation);
  const listener = { subscription, renderWindowInteractor };
  const listenerId = listeners.length;
  listeners.push(listener);
 
  if (orientation.update) {
    listener.renderWindowInteractor.requestAnimation(listener);
  }
 
  return listenerId;
}
 
function removeCameraToSynchronize(id, cancelAnimation = true) {
  const listener = listeners[id];
  if (listener) {
    listener.subscription.unsubscribe();
    if (cancelAnimation) {
      listener.renderWindowInteractor.cancelAnimation(listener);
    }
  }
  listeners[id] = null;
}
 
function isDeviceOrientationSupported() {
  return orientation.supported;
}
 
export default {
  addCameraToSynchronize,
  addWindowListeners,
  isDeviceOrientationSupported,
  removeCameraToSynchronize,
  removeWindowListeners,
};