All files / lib/anim Tween.ts

21.1% Statements 42/199
100% Branches 0/0
0% Functions 0/25
21.1% Lines 42/199

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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400                              1x 1x 1x                               1x 1x 1x 1x   1x 1x   1x         1x         1x         1x         1x         1x         1x         1x         1x         1x         1x         1x         1x     1x 1x                           1x                                                                                     1x                                                 1x                                                 1x           1x                                                                                                                                                                     1x 1x 1x   1x                 1x                                                     1x                                           1x       1x                           1x               1x           1x 1x   1x  
/*
 * Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
 * http://paperjs.org/
 *
 * Copyright (c) 2011 - 2020, Jürg Lehni & Jonathan Puckey
 * http://juerglehni.com/ & https://puckey.studio/
 *
 * Distributed under the MIT license. See LICENSE file for details.
 *
 * All rights reserved.
 */
 
// TODO: remove eslint-disable comment and deal with errors over time
/* eslint-disable */
 
import { ref } from '~/globals';
import { Base } from '~/straps';
import { Emitter } from '~/core/Emitter';
 
/**
 * @name Tween
 *
 * @class Allows tweening `Object` properties between two states for a given
 * duration. To tween properties on Paper.js {@link Item} instances,
 * {@link Item#tween(from, to, options)} can be used, which returns created
 * tween instance.
 *
 * @see Item#tween(from, to, options)
 * @see Item#tween(to, options)
 * @see Item#tween(options)
 * @see Item#tweenTo(to, options)
 * @see Item#tweenFrom(from, options)
 */
export const Tween = Base.extend(
  Emitter,
  /** @lends Tween# */ {
    _class: 'Tween',
 
    statics: {
      easings: new Base({
        // no easing, no acceleration
        linear: function (t) {
          return t;
        },
 
        // accelerating from zero velocity
        easeInQuad: function (t) {
          return t * t;
        },
 
        // decelerating to zero velocity
        easeOutQuad: function (t) {
          return t * (2 - t);
        },
 
        // acceleration until halfway, then deceleration
        easeInOutQuad: function (t) {
          return t < 0.5 ? 2 * t * t : -1 + 2 * (2 - t) * t;
        },
 
        // accelerating from zero velocity
        easeInCubic: function (t) {
          return t * t * t;
        },
 
        // decelerating to zero velocity
        easeOutCubic: function (t) {
          return --t * t * t + 1;
        },
 
        // acceleration until halfway, then deceleration
        easeInOutCubic: function (t) {
          return t < 0.5 ? 4 * t * t * t : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1;
        },
 
        // accelerating from zero velocity
        easeInQuart: function (t) {
          return t * t * t * t;
        },
 
        // decelerating to zero velocity
        easeOutQuart: function (t) {
          return 1 - --t * t * t * t;
        },
 
        // acceleration until halfway, then deceleration
        easeInOutQuart: function (t) {
          return t < 0.5 ? 8 * t * t * t * t : 1 - 8 * --t * t * t * t;
        },
 
        // accelerating from zero velocity
        easeInQuint: function (t) {
          return t * t * t * t * t;
        },
 
        // decelerating to zero velocity
        easeOutQuint: function (t) {
          return 1 + --t * t * t * t * t;
        },
 
        // acceleration until halfway, then deceleration
        easeInOutQuint: function (t) {
          return t < 0.5 ? 16 * t * t * t * t * t : 1 + 16 * --t * t * t * t * t;
        },
      }),
    },
 
    /**
     * Creates a new tween.
     *
     * @param {Object} object the object to tween the properties on
     * @param {Object} from the state at the start of the tweening
     * @param {Object} to the state at the end of the tweening
     * @param {Number} duration the duration of the tweening
     * @param {String|Function} [easing='linear'] the type of the easing
     *     function or the easing function
     * @param {Boolean} [start=true] whether to start tweening automatically
     * @return {Tween} the newly created tween
     */
    initialize: function Tween(object, from, to, duration, easing, start) {
      this.object = object;
      var type = typeof easing;
      var isFunction = type === 'function';
      this.type = isFunction ? type : type === 'string' ? easing : 'linear';
      // @ts-expect-error = 'easings' does not exist on type
      this.easing = isFunction ? easing : Tween.easings[this.type];
      this.duration = duration;
      this.running = false;
 
      this._then = null;
      this._startTime = null;
      var state = from || to;
      this._keys = state ? Object.keys(state) : [];
      this._parsedKeys = this._parseKeys(this._keys);
      this._from = state && this._getState(from);
      this._to = state && this._getState(to);
      if (start !== false) {
        this.start();
      }
    },
 
    /**
     * Set a function that will be executed when the tween completes.
     * @param {Function} function the function to execute when the tween
     *     completes
     * @return {Tween}
     *
     * @example {@paperscript}
     * // Tweens chaining:
     * var circle = new Path.Circle({
     *     center: view.center,
     *     radius: 40,
     *     fillColor: 'blue'
     * });
     * // Tween color from blue to red.
     * var tween = circle.tweenTo({ fillColor: 'red' }, 2000);
     * // When the first tween completes...
     * tween.then(function() {
     *     // ...tween color back to blue.
     *     circle.tweenTo({ fillColor: 'blue' }, 2000);
     * });
     */
    then: function (then) {
      // TODO: This only supports one callback for `then()`. Figure out how
      // to handle multiple calls to then in an decent way, without relying
      // on Promises (or should we?).
      this._then = then;
      return this;
    },
 
    /**
     * Start tweening.
     * @return {Tween}
     *
     * @example {@paperscript}
     * // Manually start tweening.
     * var circle = new Path.Circle({
     *     center: view.center,
     *     radius: 40,
     *     fillColor: 'blue'
     * });
     * var tween = circle.tweenTo(
     *     { fillColor: 'red' },
     *     { duration: 2000, start: false }
     * );
     * tween.start();
     */
    start: function () {
      this._startTime = null;
      this.running = true;
      return this;
    },
 
    /**
     * Stop tweening.
     * @return {Tween}
     *
     * @example {@paperscript}
     * // Stop a tween before it completes.
     * var circle = new Path.Circle({
     *     center: view.center,
     *     radius: 40,
     *     fillColor: 'blue'
     * });
     * // Start tweening from blue to red for 2 seconds.
     * var tween = circle.tweenTo({ fillColor: 'red' }, 2000);
     * // After 1 second...
     * setTimeout(function(){
     *     // ...stop tweening.
     *     tween.stop();
     * }, 1000);
     */
    stop: function () {
      this.running = false;
      return this;
    },
 
    // DOCS: Document Tween#update(progress)
    update: function (progress) {
      if (this.running) {
        if (progress >= 1) {
          // Always finish the animation.
          progress = 1;
          this.running = false;
        }
 
        var factor = this.easing(progress),
          keys = this._keys,
          getValue = function (value) {
            return typeof value === 'function' ? value(factor, progress) : value;
          };
        for (var i = 0, l = keys && keys.length; i < l; i++) {
          var key = keys[i],
            from = getValue(this._from[key]),
            to = getValue(this._to[key]),
            // Some paper objects have math functions (e.g.: Point,
            // Color) which can directly be used to do the tweening.
            value =
              from && to && from.__add && to.__add
                ? to.__subtract(from).__multiply(factor).__add(from)
                : (to - from) * factor + from;
          this._setProperty(this._parsedKeys[key], value);
        }
 
        // TODO: Set `progress` and `factor` on Tween object also, so they
        // can be used witout events.
        if (this.responds('update')) {
          this.emit(
            'update',
            new Base({
              progress: progress,
              factor: factor,
            })
          );
        }
        if (!this.running && this._then) {
          // TODO: Look into what should be returned.
          this._then(this.object);
        }
      }
      return this;
    },
 
    /**
     * {@grouptitle Event Handlers}
     *
     * The function to be called when the tween is updated. It receives an
     * object as its sole argument, containing the current progress of the
     * tweening and the factor calculated by the easing function.
     *
     * @name Tween#onUpdate
     * @property
     * @type ?Function
     *
     * @example {@paperscript}
     * // Display tween progression values:
     * var circle = new Path.Circle({
     *     center: view.center,
     *     radius: 40,
     *     fillColor: 'blue'
     * });
     * var tween = circle.tweenTo(
     *     { fillColor: 'red' },
     *     {
     *         duration: 2000,
     *         easing: 'easeInCubic'
     *     }
     * );
     * var progressText = new PointText(view.center + [60, -10]);
     * var factorText = new PointText(view.center + [60, 10]);
     *
     * // Install event using onUpdate() property:
     * tween.onUpdate = function(event) {
     *     progressText.content = 'progress: ' + event.progress.toFixed(2);
     * };
     *
     * // Install event using on('update') method:
     * tween.on('update', function(event) {
     *     factorText.content = 'factor: ' + event.factor.toFixed(2);
     * });
     */
    _events: {
      onUpdate: {},
    },
 
    _handleFrame: function (time) {
      var startTime = this._startTime,
        progress = startTime ? (time - startTime) / this.duration : 0;
      if (!startTime) {
        this._startTime = time;
      }
      this.update(progress);
    },
 
    _getState: function (state) {
      var keys = this._keys,
        result = {};
      for (var i = 0, l = keys.length; i < l; i++) {
        var key = keys[i],
          path = this._parsedKeys[key],
          current = this._getProperty(path),
          value;
        if (state) {
          var resolved = this._resolveValue(current, state[key]);
          // Temporarily set the resolved value, so we can retrieve the
          // coerced value from paper's internal magic.
          this._setProperty(path, resolved);
          value = this._getProperty(path);
          // Clone the value if possible to prevent future changes.
          value = value && value.clone ? value.clone() : value;
          this._setProperty(path, current);
        } else {
          // We want to get the current state at the time of the call, so
          // we have to clone if possible to prevent future changes.
          value = current && current.clone ? current.clone() : current;
        }
        result[key] = value;
      }
      return result;
    },
 
    _resolveValue: function (current, value) {
      if (value) {
        if (Array.isArray(value) && value.length === 2) {
          var operator = value[0];
          return operator &&
            operator.match &&
            // We're (unnecessarily) escaping '*/' here to not confuse
            // the ol' JSDoc parser...
            operator.match(/^[+\-\*\/]=/)
            ? this._calculate(current, operator[0], value[1])
            : value;
        } else if (typeof value === 'string') {
          var match = value.match(/^[+\-*/]=(.*)/);
          if (match) {
            var parsed = JSON.parse(match[1].replace(/(['"])?([a-zA-Z0-9_]+)(['"])?:/g, '"$2": '));
            return this._calculate(current, value[0], parsed);
          }
        }
      }
      return value;
    },
 
    _calculate: function (left, operator, right) {
      return ref.paper.PaperScript.calculateBinary(left, operator, right);
    },
 
    _parseKeys: function (keys) {
      var parsed = {};
      for (var i = 0, l = keys.length; i < l; i++) {
        var key = keys[i],
          path = key
            // Convert from JS property access notation to JSON pointer:
            .replace(/\.([^.]*)/g, '/$1')
            // Expand array property access notation ([])
            .replace(/\[['"]?([^'"\]]*)['"]?\]/g, '/$1');
        parsed[key] = path.split('/');
      }
      return parsed;
    },
 
    _getProperty: function (path, offset) {
      var obj = this.object;
      for (var i = 0, l = path.length - (offset || 0); i < l && obj; i++) {
        obj = obj[path[i]];
      }
      return obj;
    },
 
    _setProperty: function (path, value) {
      var dest = this._getProperty(path, 1);
      if (dest) {
        dest[path[path.length - 1]] = value;
      }
    },
  }
);
 
ref.Tween = Tween;