All files / core/domain/utils/misc zoomTo.ts

18.18% Statements 2/11
0% Branches 0/8
100% Functions 1/1
20% Lines 2/10

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      4x 4x                                                                                  
import Camera from "../../classes/Camera";
import { SigmaLibrary } from "../../../interfaces";
 
export default function zoomTo(sigma: SigmaLibrary) {
  return (
    camera: Camera,
    x: number,
    y: number,
    ratio: number,
    animation: any // TODO: animation type
  ) => {
    const { settings } = camera;
 
    // Create the newRatio dealing with min / max:
    const newRatio = Math.max(
      settings("zoomMin"),
      Math.min(settings("zoomMax"), camera.ratio * ratio)
    );
 
    // Check that the new ratio is different from the initial one:
    if (newRatio !== camera.ratio) {
      // Create the coordinates variable:
      ratio = newRatio / camera.ratio;
      const coordinates = {
        x: x * (1 - ratio) + camera.x,
        y: y * (1 - ratio) + camera.y,
        ratio: newRatio
      };
 
      if (animation && animation.duration) {
        // Complete the animation setings:
        const count = sigma.misc.animation.killAll(camera);
        animation = {
          ...animation,
          easing: count ? "quadraticOut" : "quadraticInOut"
        };
 
        sigma.misc.animation.camera(camera, coordinates, animation);
      } else {
        camera.goTo(coordinates);
        if (animation && animation.onComplete) animation.onComplete();
      }
    }
  };
}