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

7.69% Statements 1/13
0% Branches 0/12
50% Functions 1/2
8.33% Lines 1/12

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                                                4x                                                                    
import extend from "./extend";
 
/**
 * Perform a zoom into a camera, with or without animation, to the
 * coordinates indicated using a specified ratio.
 *
 * Recognized parameters:
 * **********************
 * Here is the exhaustive list of every accepted parameters in the animation
 * object:
 *
 *   {?number} duration     An amount of time that means the duration of the
 *                          animation. If this parameter doesn't exist the
 *                          zoom will be performed without animation.
 *   {?function} onComplete A function to perform it after the animation. It
 *                          will be performed even if there is no duration.
 *
 * @param {camera}     The camera where perform the zoom.
 * @param {x}          The X coordiantion where the zoom goes.
 * @param {y}          The Y coordiantion where the zoom goes.
 * @param {ratio}      The ratio to apply it to the current camera ratio.
 * @param {?animation} A dictionary with options for a possible animation.
 */
export default function zoomTo(sigma) {
  return (camera, x, y, ratio, animation) => {
    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 = extend(animation, {
          easing: count ? "quadraticOut" : "quadraticInOut"
        });
 
        sigma.misc.animation.camera(camera, coordinates, animation);
      } else {
        camera.goTo(coordinates);
        if (animation && animation.onComplete) animation.onComplete();
      }
    }
  };
}