iwmlib/lib/3rdparty/gsap/src/esm/RoundPropsPlugin.js

101 lines
2.8 KiB
JavaScript
Raw Normal View History

/*!
* VERSION: 1.6.0
* DATE: 2018-08-27
* UPDATES AND DOCS AT: http://greensock.com
*
* @license Copyright (c) 2008-2019, GreenSock. All rights reserved.
* This work is subject to the terms at http://greensock.com/standard-license or for
* Club GreenSock members, the software agreement that was issued with your membership.
*
* @author: Jack Doyle, jack@greensock.com
**/
/* eslint-disable */
import { _gsScope } from "./TweenLite.js";
export var RoundPropsPlugin = _gsScope._gsDefine.plugin({
propName: "roundProps",
version: "1.7.0",
priority: -1,
API: 2,
//called when the tween renders for the first time. This is where initial values should be recorded and any setup routines should run.
init: function(target, value, tween) {
this._tween = tween;
return true;
}
}),
_getRoundFunc = function(v) { //pass in 0.1 get a function that'll round to the nearest tenth, or 5 to round to the closest 5, or 0.001 to the closest 1000th, etc.
var p = v < 1 ? Math.pow(10, (v + "").length - 2) : 1; //to avoid floating point math errors (like 24 * 0.1 == 2.4000000000000004), we chop off at a specific number of decimal places (much faster than toFixed()
return function(n) {
return ((Math.round(n / v) * v * p) | 0) / p;
};
},
_roundLinkedList = function(node, mod) {
while (node) {
if (!node.f && !node.blob) {
node.m = mod || Math.round;
}
node = node._next;
}
},
p = RoundPropsPlugin.prototype;
p._onInitAllProps = function() {
var tween = this._tween,
rp = tween.vars.roundProps,
lookup = {},
rpt = tween._propLookup.roundProps,
pt, next, i, p;
if (typeof(rp) === "object" && !rp.push) {
for (p in rp) {
lookup[p] = _getRoundFunc(rp[p]);
}
} else {
if (typeof(rp) === "string") {
rp = rp.split(",");
}
i = rp.length;
while (--i > -1) {
lookup[rp[i]] = Math.round;
}
}
for (p in lookup) {
pt = tween._firstPT;
while (pt) {
next = pt._next; //record here, because it may get removed
if (pt.pg) {
pt.t._mod(lookup);
} else if (pt.n === p) {
if (pt.f === 2 && pt.t) { //a blob (text containing multiple numeric values)
_roundLinkedList(pt.t._firstPT, lookup[p]);
} else {
this._add(pt.t, p, pt.s, pt.c, lookup[p]);
//remove from linked list
if (next) {
next._prev = pt._prev;
}
if (pt._prev) {
pt._prev._next = next;
} else if (tween._firstPT === pt) {
tween._firstPT = next;
}
pt._next = pt._prev = null;
tween._propLookup[p] = rpt;
}
}
pt = next;
}
}
return false;
};
p._add = function(target, p, s, c, mod) {
this._addTween(target, p, s, s + c, p, mod || Math.round);
this._overwriteProps.push(p);
};
export { RoundPropsPlugin as default };