All files / sigma.js/src/core/misc sigma.misc.animation.ts

51.61% Statements 32/62
26.76% Branches 19/71
50% Functions 5/10
51.72% Lines 30/58

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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183      4x           4x 4x 4x 1x         1x             1x                       1x 1x                     1x             3x       1x 1x             1x 1x     1x       2x 2x 2x                                       2x   2x       1x 1x             1x   1x                                                                                                                             4x 4x 4x 4x 4x    
import { SigmaLibrary, AnimationReference } from "../interfaces";
import Camera, { CameraLocation } from "../domain/classes/Camera";
 
export default function configure(sigma: SigmaLibrary) {
  /**
   * Generates a unique ID for the animation.
   *
   * @return {string} Returns the new ID.
   */
  const _getID = (function getId() {
    let id = 0;
    return function next(): number {
      return ++id;
    };
  })();
 
  function animationCamera(camera: Camera, val: CameraLocation, options?: any) {
    Iif (
      !(camera instanceof sigma.classes.camera) ||
      typeof val !== "object" ||
      !val
    )
      throw new Error("animation.camera: Wrong arguments.");
 
    Iif (
      typeof val.x !== "number" &&
      typeof val.y !== "number" &&
      typeof val.ratio !== "number" &&
      typeof val.angle !== "number"
    )
      throw new Error(
        "There must be at least one valid coordinate in the given val."
      );
 
    let id: number;
    let anim: AnimationReference;
    const o = options || {};
    const start = sigma.utils.dateNow();
 
    // Store initial values:
    const initialVal = {
      x: camera.x,
      y: camera.y,
      ratio: camera.ratio,
      angle: camera.angle
    };
 
    const easing =
      typeof o.easing !== "function"
        ? sigma.utils.easings[o.easing || "quadraticInOut"]
        : o.easing;
 
    function tick() {
      let coef;
 
      const t = o.duration ? (sigma.utils.dateNow() - start) / o.duration : 1;
 
      // If the animation is over:
      if (t >= 1) {
        camera.isAnimated = false;
        camera.goTo({
          x: val.x !== undefined ? val.x : initialVal.x,
          y: val.y !== undefined ? val.y : initialVal.y,
          ratio: val.ratio !== undefined ? val.ratio : initialVal.ratio,
          angle: val.angle !== undefined ? val.angle : initialVal.angle
        });
 
        cancelAnimationFrame(id);
        delete sigma.misc.animation.running[id];
 
        // Check callbacks:
        Eif (typeof o.onComplete === "function") o.onComplete();
 
        // Else, let's keep going:
      } else {
        coef = easing(t);
        camera.isAnimated = true;
        camera.goTo({
          x:
            val.x !== undefined
              ? initialVal.x + (val.x - initialVal.x) * coef
              : initialVal.x,
          y:
            val.y !== undefined
              ? initialVal.y + (val.y - initialVal.y) * coef
              : initialVal.y,
          ratio:
            val.ratio !== undefined
              ? initialVal.ratio + (val.ratio - initialVal.ratio) * coef
              : initialVal.ratio,
          angle:
            val.angle !== undefined
              ? initialVal.angle + (val.angle - initialVal.angle) * coef
              : initialVal.angle
        });
 
        // Check callbacks:
        Eif (typeof o.onNewFrame === "function") o.onNewFrame();
 
        anim.frameId = requestAnimationFrame(tick);
      }
    }
 
    id = _getID();
    anim = {
      frameId: requestAnimationFrame(tick),
      target: camera,
      type: "camera",
      options: o,
      fn: tick
    };
    sigma.misc.animation.running[id] = anim;
 
    return id;
  }
 
  function kill(id: number) {
    if (arguments.length !== 1 || typeof id !== "number")
      throw new Error("animation.kill: Wrong arguments.");
 
    const o = sigma.misc.animation.running[id];
 
    if (o) {
      cancelAnimationFrame(id);
      delete sigma.misc.animation.running[o.frameId];
 
      if (o.type === "camera") o.target.isAnimated = false;
 
      // Check callbacks:
      if (typeof (o.options || {}).onComplete === "function")
        o.options.onComplete();
    }
  }
 
  function killAll(filter?: string | object) {
    let o;
    let count = 0;
    const type = typeof filter === "string" ? filter : null;
    const target = typeof filter === "object" ? filter : null;
    const { running } = sigma.misc.animation;
    Object.keys(running).forEach(id => {
      if (
        (!type || running[id].type === type) &&
        (!target || running[id].target === target)
      ) {
        o = sigma.misc.animation.running[id];
        cancelAnimationFrame(o.frameId);
        delete sigma.misc.animation.running[id];
 
        if (o.type === "camera") o.target.isAnimated = false;
 
        // Increment counter:
        count++;
 
        // Check callbacks:
        if (typeof (o.options || {}).onComplete === "function")
          o.options.onComplete();
      }
    });
 
    return count;
  }
 
  function has(filter: string | object) {
    const type = typeof filter === "string" ? filter : null;
    const target = typeof filter === "object" ? filter : null;
    const { running } = sigma.misc.animation;
 
    return Object.keys(running).some(id => {
      return (
        (!type || running[id].type === type) &&
        (!target || running[id].target === target)
      );
    });
  }
 
  sigma.register("sigma.misc.animation.running", Object.create(null));
  sigma.register("sigma.misc.animation.kill", kill);
  sigma.register("sigma.misc.animation.killAll", killAll);
  sigma.register("sigma.misc.animation.has", has);
  sigma.register("sigma.misc.animation.camera", animationCamera);
}