troika-animation
Advanced tools
Comparing version 0.42.0 to 0.43.0
@@ -0,0 +0,0 @@ import {number, color} from '../src/Interpolators.js' |
@@ -0,0 +0,0 @@ import MultiTween from '../src/MultiTween.js' |
@@ -0,0 +0,0 @@ import SpringTween from '../src/SpringTween.js' |
@@ -0,0 +0,0 @@ import Tween from '../src/Tween.js' |
@@ -6,2 +6,11 @@ # Change Log | ||
# [0.43.0](https://github.com/protectwise/troika/compare/v0.42.0...v0.43.0) (2021-09-20) | ||
**Note:** Version bump only for package troika-animation | ||
# [0.42.0](https://github.com/protectwise/troika/compare/v0.41.2...v0.42.0) (2021-05-17) | ||
@@ -8,0 +17,0 @@ |
@@ -0,0 +0,0 @@ /* |
@@ -17,7 +17,5 @@ (function (global, factory) { | ||
var pow = Math.pow; | ||
var PI = Math.PI; | ||
var sqrt = Math.sqrt; | ||
var HALF_PI = PI / 2; | ||
var TWO_PI = PI * 2; | ||
const {pow, PI, sqrt} = Math; | ||
const HALF_PI = PI / 2; | ||
const TWO_PI = PI * 2; | ||
@@ -27,67 +25,76 @@ | ||
function makeInOut(inFn, outFn) { | ||
return function (t) { return t < 0.5 ? inFn(t * 2) * 0.5 : outFn(t * 2 - 1) * 0.5 + 0.5; } | ||
return t => t < 0.5 ? inFn(t * 2) * 0.5 : outFn(t * 2 - 1) * 0.5 + 0.5 | ||
} | ||
function makeExpIn(exp) { | ||
return function (t) { return pow(t, exp); } | ||
return t => pow(t, exp) | ||
} | ||
function makeExpOut(exp) { | ||
return function (t) { return 1 - pow(1 - t, exp); } | ||
return t => 1 - pow(1 - t, exp) | ||
} | ||
function makeExpInOut(exp) { | ||
return function (t) { return t < 0.5 ? | ||
return t => t < 0.5 ? | ||
pow(t * 2, exp) * 0.5 : | ||
(1 - pow(1 - (t * 2 - 1), exp)) * 0.5 + 0.5; } | ||
(1 - pow(1 - (t * 2 - 1), exp)) * 0.5 + 0.5 | ||
} | ||
var linear = function (t) { return t; }; | ||
const linear = t => t; | ||
var easeInQuad = makeExpIn(2); | ||
var easeOutQuad = makeExpOut(2); | ||
var easeInOutQuad = makeExpInOut(2); | ||
const easeInQuad = makeExpIn(2); | ||
const easeOutQuad = makeExpOut(2); | ||
const easeInOutQuad = makeExpInOut(2); | ||
var easeInCubic = makeExpIn(3); | ||
var easeOutCubic = makeExpOut(3); | ||
var easeInOutCubic = makeExpInOut(3); | ||
const easeInCubic = makeExpIn(3); | ||
const easeOutCubic = makeExpOut(3); | ||
const easeInOutCubic = makeExpInOut(3); | ||
var easeInQuart = makeExpIn(4); | ||
var easeOutQuart = makeExpOut(4); | ||
var easeInOutQuart = makeExpInOut(4); | ||
const easeInQuart = makeExpIn(4); | ||
const easeOutQuart = makeExpOut(4); | ||
const easeInOutQuart = makeExpInOut(4); | ||
var easeInQuint = makeExpIn(5); | ||
var easeOutQuint = makeExpOut(5); | ||
var easeInOutQuint = makeExpInOut(5); | ||
const easeInQuint = makeExpIn(5); | ||
const easeOutQuint = makeExpOut(5); | ||
const easeInOutQuint = makeExpInOut(5); | ||
var easeInSine = function (t) { return 1 - Math.cos(t * (HALF_PI)); }; | ||
var easeOutSine = function (t) { return Math.sin(t * (HALF_PI)); }; | ||
var easeInOutSine = function (t) { return -0.5 * (Math.cos(PI * t) - 1); }; | ||
const easeInSine = t => 1 - Math.cos(t * (HALF_PI)); | ||
const easeOutSine = t => Math.sin(t * (HALF_PI)); | ||
const easeInOutSine = t => -0.5 * (Math.cos(PI * t) - 1); | ||
var easeInExpo = function (t) { return (t === 0) ? 0 : pow(2, 10 * (t - 1)); }; | ||
const easeInExpo = t => | ||
(t === 0) ? 0 : pow(2, 10 * (t - 1)); | ||
var easeOutExpo = function (t) { return (t === 1) ? 1 : 1 - pow(2, -10 * t); }; | ||
const easeOutExpo = t => | ||
(t === 1) ? 1 : 1 - pow(2, -10 * t); | ||
var easeInOutExpo = function (t) { return (t === 0 || t === 1) ? t : | ||
const easeInOutExpo = t => | ||
(t === 0 || t === 1) ? t : | ||
t < 0.5 ? | ||
pow(2, 10 * (t * 2 - 1)) * 0.5 : | ||
(1 - pow(2, -10 * (t * 2 - 1))) * 0.5 + 0.5; }; | ||
(1 - pow(2, -10 * (t * 2 - 1))) * 0.5 + 0.5; | ||
var easeInCirc = function (t) { return 1 - sqrt(1 - t * t); }; | ||
const easeInCirc = t => | ||
1 - sqrt(1 - t * t); | ||
var easeOutCirc = function (t) { return sqrt(1 - pow(t - 1, 2)); }; | ||
const easeOutCirc = t => | ||
sqrt(1 - pow(t - 1, 2)); | ||
var easeInOutCirc = makeInOut(easeInCirc, easeOutCirc); | ||
const easeInOutCirc = makeInOut(easeInCirc, easeOutCirc); | ||
var easeInElastic = function (t) { return (t === 0 || t === 1) ? t : 1 - easeOutElastic(1 - t); }; | ||
const easeInElastic = t => | ||
(t === 0 || t === 1) ? t : 1 - easeOutElastic(1 - t); | ||
var easeOutElastic = function (t) { return (t === 0 || t === 1) ? t : | ||
Math.pow(2, -10 * t) * Math.sin((t - 0.075) * TWO_PI / 0.3) + 1; }; | ||
const easeOutElastic = t => | ||
(t === 0 || t === 1) ? t : | ||
Math.pow(2, -10 * t) * Math.sin((t - 0.075) * TWO_PI / 0.3) + 1; | ||
var easeInOutElastic = makeInOut(easeInElastic, easeOutElastic); | ||
const easeInOutElastic = makeInOut(easeInElastic, easeOutElastic); | ||
var easeInBack = function (t) { return t * t * (2.70158 * t - 1.70158); }; | ||
const easeInBack = t => | ||
t * t * (2.70158 * t - 1.70158); | ||
var easeOutBack = function (t) { return (t -= 1) * t * (2.70158 * t + 1.70158) + 1; }; | ||
const easeOutBack = t => | ||
(t -= 1) * t * (2.70158 * t + 1.70158) + 1; | ||
var easeInOutBack = function (t) { | ||
var s = 1.70158 * 1.525; | ||
const easeInOutBack = t => { | ||
const s = 1.70158 * 1.525; | ||
return (t *= 2) < 1 ? | ||
@@ -98,5 +105,7 @@ 0.5 * (t * t * ((s + 1) * t - s)) : | ||
var easeInBounce = function (t) { return 1 - easeOutBounce(1 - t); }; | ||
const easeInBounce = t => | ||
1 - easeOutBounce(1 - t); | ||
var easeOutBounce = function (t) { return t < (1 / 2.75) ? | ||
const easeOutBounce = t => | ||
t < (1 / 2.75) ? | ||
(7.5625 * t * t) : | ||
@@ -107,5 +116,5 @@ t < (2 / 2.75) ? | ||
(7.5625 * (t -= (2.25 / 2.75)) * t + .9375) : | ||
(7.5625 * (t -= (2.625 / 2.75)) * t + .984375); }; | ||
(7.5625 * (t -= (2.625 / 2.75)) * t + .984375); | ||
var easeInOutBounce = makeInOut(easeInBounce, easeOutBounce); | ||
const easeInOutBounce = makeInOut(easeInBounce, easeOutBounce); | ||
@@ -265,9 +274,9 @@ // Aliases...? | ||
*/ | ||
var colorValueToNumber = (function() { | ||
var colorCanvas, colorCanvasCtx; | ||
const colorValueToNumber = (function() { | ||
let colorCanvas, colorCanvasCtx; | ||
// Cache for evaluated string values | ||
var stringCache = Object.create(null); | ||
var stringCacheSize = 0; | ||
var stringCacheMaxSize = 2048; | ||
let stringCache = Object.create(null); | ||
let stringCacheSize = 0; | ||
const stringCacheMaxSize = 2048; | ||
@@ -292,4 +301,4 @@ return function(value) { | ||
colorCanvasCtx.fillRect(0, 0, 1, 1); | ||
var colorData = colorCanvasCtx.getImageData(0, 0, 1, 1).data; | ||
var result = rgbToNumber(colorData[0], colorData[1], colorData[2]); | ||
const colorData = colorCanvasCtx.getImageData(0, 0, 1, 1).data; | ||
const result = rgbToNumber(colorData[0], colorData[1], colorData[2]); | ||
@@ -333,23 +342,30 @@ // Enforce max cache size - for now this invalidates the entire cache when reaching | ||
*/ | ||
var AbstractTween = function AbstractTween () {}; | ||
class AbstractTween { | ||
/** | ||
* @abstract | ||
* For a given elapsed time relative to the start of the tween, calculates the value at that time and calls the | ||
* `callback` function with that value. If the given time is during the `delay` period, the callback will not be | ||
* invoked. | ||
* @param {number} time | ||
*/ | ||
gotoElapsedTime(time) {} | ||
AbstractTween.prototype.gotoElapsedTime = function gotoElapsedTime (time) {}; | ||
/** | ||
* @abstract | ||
* Like `gotoElapsedTime` but goes to the very end of the tween. | ||
*/ | ||
gotoEnd() {} | ||
/** | ||
* @abstract | ||
* Like `gotoElapsedTime` but goes to the very end of the tween. | ||
*/ | ||
AbstractTween.prototype.gotoEnd = function gotoEnd () {}; | ||
/** | ||
* @abstract | ||
* For a given elapsed time relative to the start of the tween, determines if the tween is in its completed end state. | ||
* @param {number} time | ||
* @return {boolean} | ||
*/ | ||
isDoneAtElapsedTime(time) {} | ||
} | ||
/** | ||
* @abstract | ||
* For a given elapsed time relative to the start of the tween, determines if the tween is in its completed end state. | ||
* @param {number} time | ||
* @return {boolean} | ||
*/ | ||
AbstractTween.prototype.isDoneAtElapsedTime = function isDoneAtElapsedTime (time) {}; | ||
const linear$1 = v => v; | ||
const maxSafeInteger = 0x1fffffffffffff; | ||
var linear$1 = function (v) { return v; }; | ||
var maxSafeInteger = 0x1fffffffffffff; | ||
/** | ||
@@ -379,12 +395,5 @@ * @class Tween | ||
*/ | ||
var Tween = /*@__PURE__*/(function (AbstractTween) { | ||
function Tween(callback, fromValue, toValue, duration, delay, easing, iterations, direction, interpolate) { | ||
if ( duration === void 0 ) duration=750; | ||
if ( delay === void 0 ) delay=0; | ||
if ( easing === void 0 ) easing=linear$1; | ||
if ( iterations === void 0 ) iterations=1; | ||
if ( direction === void 0 ) direction='forward'; | ||
if ( interpolate === void 0 ) interpolate='number'; | ||
AbstractTween.call(this); | ||
class Tween extends AbstractTween { | ||
constructor(callback, fromValue, toValue, duration=750, delay=0, easing=linear$1, iterations=1, direction='forward', interpolate='number') { | ||
super(); | ||
this.callback = callback; | ||
@@ -410,6 +419,2 @@ this.fromValue = fromValue; | ||
if ( AbstractTween ) Tween.__proto__ = AbstractTween; | ||
Tween.prototype = Object.create( AbstractTween && AbstractTween.prototype ); | ||
Tween.prototype.constructor = Tween; | ||
/** | ||
@@ -421,9 +426,9 @@ * For a given elapsed time relative to the start of the tween, calculates the value at that time and calls the | ||
*/ | ||
Tween.prototype.gotoElapsedTime = function gotoElapsedTime (time) { | ||
var duration = this.duration; | ||
var delay = this.delay; | ||
gotoElapsedTime(time) { | ||
let duration = this.duration; | ||
let delay = this.delay; | ||
if (time >= delay) { | ||
time = Math.min(time, this.totalElapsed) - delay; //never go past final value | ||
var progress = (time % duration) / duration; | ||
if (progress === 0 && time !== 0) { progress = 1; } | ||
let progress = (time % duration) / duration; | ||
if (progress === 0 && time !== 0) progress = 1; | ||
progress = this.easing(progress); | ||
@@ -435,3 +440,3 @@ if (this.direction === 'reverse' || (this.direction === 'alternate' && Math.ceil(time / duration) % 2 === 0)) { | ||
} | ||
}; | ||
} | ||
@@ -441,5 +446,5 @@ /** | ||
*/ | ||
Tween.prototype.gotoEnd = function gotoEnd () { | ||
gotoEnd() { | ||
this.gotoElapsedTime(this.totalElapsed); | ||
}; | ||
} | ||
@@ -451,9 +456,7 @@ /** | ||
*/ | ||
Tween.prototype.isDoneAtElapsedTime = function isDoneAtElapsedTime (time) { | ||
isDoneAtElapsedTime(time) { | ||
return time > this.totalElapsed | ||
}; | ||
} | ||
} | ||
return Tween; | ||
}(AbstractTween)); | ||
/** | ||
@@ -463,7 +466,7 @@ * A specialized Tween that controls one or more other tweens. The controlled tweens are treated as a | ||
*/ | ||
var MultiTween = /*@__PURE__*/(function (Tween) { | ||
function MultiTween(tweens, duration, delay, easing, iterations, direction) { | ||
class MultiTween extends Tween { | ||
constructor(tweens, duration, delay, easing, iterations, direction) { | ||
if (typeof duration !== 'number') { | ||
// Calculate duration based on the longest individual total duration | ||
duration = tweens.reduce(function (dur, tween) { return Math.max(dur, tween.totalElapsed); }, 0); | ||
duration = tweens.reduce((dur, tween) => Math.max(dur, tween.totalElapsed), 0); | ||
} | ||
@@ -476,3 +479,3 @@ if (duration === Infinity) { | ||
// Tween the total duration time | ||
Tween.call(this, null, 0, duration, duration, delay, easing, iterations, direction); | ||
super(null, 0, duration, duration, delay, easing, iterations, direction); | ||
if (tweens.length === 1) { | ||
@@ -487,7 +490,3 @@ this.callback = tweens[0].gotoElapsedTime.bind(tweens[0]); | ||
if ( Tween ) MultiTween.__proto__ = Tween; | ||
MultiTween.prototype = Object.create( Tween && Tween.prototype ); | ||
MultiTween.prototype.constructor = MultiTween; | ||
MultiTween.prototype._syncTweens = function _syncTweens (time) { | ||
_syncTweens(time) { | ||
// NOTE: forward iteration is important here so the tweens are evaluated in order | ||
@@ -498,10 +497,8 @@ // of when they end; that way later tweens will take precedence over earlier ones. | ||
// slightly prior to their end state. | ||
for (var i = 0, len = this.tweens.length; i < len; i++) { | ||
for (let i = 0, len = this.tweens.length; i < len; i++) { | ||
this.tweens[i].gotoElapsedTime(time); | ||
} | ||
}; | ||
} | ||
} | ||
return MultiTween; | ||
}(Tween)); | ||
function endTimeComparator(a, b) { | ||
@@ -511,5 +508,5 @@ return a.totalElapsed - b.totalElapsed | ||
var runners = []; | ||
var nextFrameTimer = null; | ||
var hasStoppedRunners = false; | ||
let runners = []; | ||
let nextFrameTimer = null; | ||
let hasStoppedRunners = false; | ||
@@ -522,3 +519,3 @@ function noop() {} | ||
function tick() { | ||
var now = Date.now(); | ||
let now = Date.now(); | ||
nextFrameTimer = null; | ||
@@ -534,3 +531,3 @@ | ||
// Sync each runner, filtering out empty ones as we go | ||
for (var i = runners.length; i-- > 0;) { | ||
for (let i = runners.length; i-- > 0;) { | ||
runners[i]._tick(now); | ||
@@ -543,3 +540,3 @@ } | ||
var _scheduler = window; | ||
let _scheduler = window; | ||
@@ -592,119 +589,121 @@ /** | ||
*/ | ||
var Runner = function Runner() { | ||
this.tweens = []; | ||
}; | ||
class Runner { | ||
constructor() { | ||
this.tweens = []; | ||
} | ||
Runner.prototype.destructor = function destructor () { | ||
this.tweens = null; | ||
stopRunner(this); | ||
this.start = this.stop = this.pause = this._tick = noop; | ||
}; | ||
/** | ||
* Add a tween to the runner. It will be invoked on the next frame, not immediately. | ||
* @param {Tween} tween | ||
*/ | ||
Runner.prototype.start = function start (tween) { | ||
// If previously paused, update start time to account for the duration of the pause | ||
if (tween.runner$paused && tween.runner$started) { | ||
tween.runner$started += (Date.now() - tween.runner$paused); | ||
} else { | ||
this.tweens.push(tween); | ||
destructor() { | ||
this.tweens = null; | ||
stopRunner(this); | ||
this.start = this.stop = this.pause = this._tick = noop; | ||
} | ||
tween.runner$paused = null; | ||
tween.runner$stopped = false; | ||
// add runner to running runners | ||
startRunner(this); | ||
}; | ||
/** | ||
* Add a tween to the runner. It will be invoked on the next frame, not immediately. | ||
* @param {Tween} tween | ||
*/ | ||
start(tween) { | ||
// If previously paused, update start time to account for the duration of the pause | ||
if (tween.runner$paused && tween.runner$started) { | ||
tween.runner$started += (Date.now() - tween.runner$paused); | ||
} else { | ||
this.tweens.push(tween); | ||
} | ||
tween.runner$paused = null; | ||
tween.runner$stopped = false; | ||
/** | ||
* Remove a tween from the runner. | ||
* @param tween | ||
*/ | ||
Runner.prototype.stop = function stop (tween) { | ||
// queue tween for removal from list on next tick | ||
tween.runner$stopped = true; | ||
tween.runner$paused = null; | ||
}; | ||
// add runner to running runners | ||
startRunner(this); | ||
} | ||
/** | ||
* Pause a tween; call `runner.start(tween)` to unpause it | ||
* @param tween | ||
*/ | ||
Runner.prototype.pause = function pause (tween) { | ||
if (!tween.runner$paused) { | ||
tween.runner$paused = Date.now(); | ||
/** | ||
* Remove a tween from the runner. | ||
* @param tween | ||
*/ | ||
stop(tween) { | ||
// queue tween for removal from list on next tick | ||
tween.runner$stopped = true; | ||
tween.runner$paused = null; | ||
} | ||
}; | ||
/** | ||
* Stop all running tweens. | ||
*/ | ||
Runner.prototype.stopAll = function stopAll () { | ||
if (this.tweens) { | ||
this.tweens.forEach(this.stop, this); | ||
/** | ||
* Pause a tween; call `runner.start(tween)` to unpause it | ||
* @param tween | ||
*/ | ||
pause(tween) { | ||
if (!tween.runner$paused) { | ||
tween.runner$paused = Date.now(); | ||
} | ||
} | ||
}; | ||
Runner.prototype._tick = function _tick (now) { | ||
var tweens = this.tweens; | ||
var hasStoppedTweens = false; | ||
var hasRunningTweens = false; | ||
/** | ||
* Stop all running tweens. | ||
*/ | ||
stopAll() { | ||
if (this.tweens) { | ||
this.tweens.forEach(this.stop, this); | ||
} | ||
} | ||
// Sync each tween, filtering out old finished ones as we go | ||
for (var i = 0, len = tweens.length; i < len; i++) { | ||
var tween = tweens[i]; | ||
if (!tween.runner$stopped && !tween.runner$paused) { | ||
// Sync the tween to current time | ||
var elapsed = now - (tween.runner$started || (tween.runner$started = now)); | ||
tween.gotoElapsedTime(elapsed); | ||
hasRunningTweens = true; | ||
_tick(now) { | ||
let tweens = this.tweens; | ||
let hasStoppedTweens = false; | ||
let hasRunningTweens = false; | ||
// Queue for removal if we're past its end time | ||
if (tween.isDoneAtElapsedTime(elapsed)) { | ||
this.stop(tween); | ||
if (tween.onDone) { | ||
tween.onDone(); | ||
// Sync each tween, filtering out old finished ones as we go | ||
for (let i = 0, len = tweens.length; i < len; i++) { | ||
let tween = tweens[i]; | ||
if (!tween.runner$stopped && !tween.runner$paused) { | ||
// Sync the tween to current time | ||
let elapsed = now - (tween.runner$started || (tween.runner$started = now)); | ||
tween.gotoElapsedTime(elapsed); | ||
hasRunningTweens = true; | ||
// Queue for removal if we're past its end time | ||
if (tween.isDoneAtElapsedTime(elapsed)) { | ||
this.stop(tween); | ||
if (tween.onDone) { | ||
tween.onDone(); | ||
} | ||
} | ||
} | ||
if (tween.runner$stopped) { | ||
hasStoppedTweens = true; | ||
} | ||
} | ||
if (tween.runner$stopped) { | ||
hasStoppedTweens = true; | ||
if (hasRunningTweens) { | ||
this.onTick(); | ||
} | ||
} | ||
if (hasRunningTweens) { | ||
this.onTick(); | ||
} | ||
// Prune list if needed | ||
// TODO perhaps batch this up so it happens less often | ||
if (hasStoppedTweens) { | ||
this.tweens = tweens.filter(isTweenNotStopped); | ||
// Prune list if needed | ||
// TODO perhaps batch this up so it happens less often | ||
if (hasStoppedTweens) { | ||
this.tweens = tweens.filter(isTweenNotStopped); | ||
// remove runner from running runners if it has no tweens left | ||
if (!this.tweens.length) { | ||
stopRunner(this); | ||
if (this.onDone) { | ||
this.onDone(); | ||
// remove runner from running runners if it has no tweens left | ||
if (!this.tweens.length) { | ||
stopRunner(this); | ||
if (this.onDone) { | ||
this.onDone(); | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
/** | ||
* Override to specify a function that will be called at the end of every frame, after all | ||
* tweens have been updated. | ||
*/ | ||
Runner.prototype.onTick = function onTick () { | ||
// abstract | ||
}; | ||
/** | ||
* Override to specify a function that will be called at the end of every frame, after all | ||
* tweens have been updated. | ||
*/ | ||
onTick() { | ||
// abstract | ||
} | ||
/** | ||
* Override to specify a function that will be called after all running tweens have completed. | ||
*/ | ||
Runner.prototype.onDone = function onDone () { | ||
// abstract | ||
}; | ||
/** | ||
* Override to specify a function that will be called after all running tweens have completed. | ||
*/ | ||
onDone() { | ||
// abstract | ||
} | ||
} | ||
@@ -727,6 +726,6 @@ /** | ||
// in react-spring. | ||
var tensionFactor = 0.000001; | ||
var frictionFactor = 0.001; | ||
const tensionFactor = 0.000001; | ||
const frictionFactor = 0.001; | ||
var DEFAULTS = PRESETS.default; | ||
const DEFAULTS = PRESETS.default; | ||
@@ -759,4 +758,4 @@ /** | ||
*/ | ||
var SpringTween = /*@__PURE__*/(function (AbstractTween) { | ||
function SpringTween ( | ||
class SpringTween extends AbstractTween { | ||
constructor ( | ||
callback, | ||
@@ -766,9 +765,6 @@ fromValue, | ||
springConfig, | ||
initialVelocity, | ||
delay | ||
initialVelocity = 0, | ||
delay = 0 | ||
) { | ||
if ( initialVelocity === void 0 ) initialVelocity = 0; | ||
if ( delay === void 0 ) delay = 0; | ||
AbstractTween.call(this); | ||
super(); | ||
this.isSpring = true; | ||
@@ -784,6 +780,4 @@ this.callback = callback; | ||
} | ||
if (!springConfig) { springConfig = DEFAULTS; } | ||
var mass = springConfig.mass; | ||
var tension = springConfig.tension; | ||
var friction = springConfig.friction; | ||
if (!springConfig) springConfig = DEFAULTS; | ||
const {mass, tension, friction} = springConfig; | ||
this.mass = typeof mass === 'number' ? mass : DEFAULTS.mass; | ||
@@ -798,20 +792,11 @@ this.tension = (typeof tension === 'number' ? tension : DEFAULTS.tension) * tensionFactor; | ||
if ( AbstractTween ) SpringTween.__proto__ = AbstractTween; | ||
SpringTween.prototype = Object.create( AbstractTween && AbstractTween.prototype ); | ||
SpringTween.prototype.constructor = SpringTween; | ||
SpringTween.prototype.gotoElapsedTime = function gotoElapsedTime (time) { | ||
gotoElapsedTime (time) { | ||
if (time >= this.delay) { | ||
var ref = this; | ||
var toValue = ref.toValue; | ||
var mass = ref.mass; | ||
var tension = ref.tension; | ||
var friction = ref.friction; | ||
var minAcceleration = ref.minAcceleration; | ||
var velocity = this.velocity || 0; | ||
var value = this.currentValue; | ||
let { toValue, mass, tension, friction, minAcceleration } = this; | ||
let velocity = this.velocity || 0; | ||
let value = this.currentValue; | ||
// Step simulation by 1ms | ||
for (var t = this.$lastTime; t < time; t++) { | ||
var acceleration = (tension * (toValue - value) - friction * velocity) / mass; | ||
for (let t = this.$lastTime; t < time; t++) { | ||
const acceleration = (tension * (toValue - value) - friction * velocity) / mass; | ||
// Acceleration converges to zero near end state | ||
@@ -832,17 +817,15 @@ if (Math.abs(acceleration) < minAcceleration) { | ||
} | ||
}; | ||
} | ||
SpringTween.prototype.gotoEnd = function gotoEnd () { | ||
gotoEnd () { | ||
this.velocity = 0; | ||
this.$lastTime = this.$endTime; | ||
this.callback(this.currentValue = this.toValue); | ||
}; | ||
} | ||
SpringTween.prototype.isDoneAtElapsedTime = function isDoneAtElapsedTime (time) { | ||
isDoneAtElapsedTime (time) { | ||
return time >= this.$endTime | ||
}; | ||
} | ||
} | ||
return SpringTween; | ||
}(AbstractTween)); | ||
exports.Easings = Easings; | ||
@@ -849,0 +832,0 @@ exports.Interpolators = Interpolators; |
@@ -1,15 +0,13 @@ | ||
'use strict';(function(e,p){"object"===typeof exports&&"undefined"!==typeof module?p(exports):"function"===typeof define&&define.amd?define(["exports"],p):(e="undefined"!==typeof globalThis?globalThis:e||self,p(e.troika_animation={}))})(this,function(e){function p(a,b){return function(c){return.5>c?.5*a(2*c):.5*b(2*c-1)+.5}}function u(a){return function(b){return k(b,a)}}function v(a){return function(b){return 1-k(1-b,a)}}function w(a){return function(b){return.5>b?.5*k(2*b,a):.5*(1-k(1-(2*b-1),a))+ | ||
.5}}function t(a,b,c){return a+(b-a)*c}function R(a,b){return a.totalElapsed-b.totalElapsed}function S(){}function T(a){return a.runner$running}function U(a){return!a.runner$stopped}function V(){var a=Date.now();q=null;x&&(r=r.filter(T),x=!1);if(r.length){for(var b=r.length;0<b--;)r[b]._tick(a);B()}}function B(){q||(q=y.requestAnimationFrame(V))}var k=Math.pow,C=Math.PI,G=Math.sqrt,H=C/2,W=2*C,z=u(2),D=v(2),l=w(2),n=u(3),X=v(3),Y=w(3),Z=u(4),aa=v(4),ba=w(4),ca=u(5),da=v(5),ea=w(5),I=function(a){return 1- | ||
G(1-a*a)},J=function(a){return G(1-k(a-1,2))},fa=p(I,J),K=function(a){return 0===a||1===a?a:1-E(1-a)},E=function(a){return 0===a||1===a?a:Math.pow(2,-10*a)*Math.sin((a-.075)*W/.3)+1},ha=p(K,E),L=function(a){return 1-F(1-a)},F=function(a){return a<1/2.75?7.5625*a*a:a<2/2.75?7.5625*(a-=1.5/2.75)*a+.75:a<2.5/2.75?7.5625*(a-=2.25/2.75)*a+.9375:7.5625*(a-=2.625/2.75)*a+.984375},ia=p(L,F),M=Object.freeze({__proto__:null,linear:function(a){return a},easeInQuad:z,easeOutQuad:D,easeInOutQuad:l,easeInCubic:n, | ||
easeOutCubic:X,easeInOutCubic:Y,easeInQuart:Z,easeOutQuart:aa,easeInOutQuart:ba,easeInQuint:ca,easeOutQuint:da,easeInOutQuint:ea,easeInSine:function(a){return 1-Math.cos(a*H)},easeOutSine:function(a){return Math.sin(a*H)},easeInOutSine:function(a){return-.5*(Math.cos(C*a)-1)},easeInExpo:function(a){return 0===a?0:k(2,10*(a-1))},easeOutExpo:function(a){return 1===a?1:1-k(2,-10*a)},easeInOutExpo:function(a){return 0===a||1===a?a:.5>a?.5*k(2,10*(2*a-1)):.5*(1-k(2,-10*(2*a-1)))+.5},easeInCirc:I,easeOutCirc:J, | ||
easeInOutCirc:fa,easeInElastic:K,easeOutElastic:E,easeInOutElastic:ha,easeInBack:function(a){return a*a*(2.70158*a-1.70158)},easeOutBack:function(a){return--a*a*(2.70158*a+1.70158)+1},easeInOutBack:function(a){return 1>(a*=2)?.5*a*a*(3.5949095*a-2.5949095):.5*((a-=2)*a*(3.5949095*a+2.5949095)+2)},easeInBounce:L,easeOutBounce:F,easeInOutBounce:ia}),N=function(){var a,b,c=Object.create(null),f=0;return function(g){if("number"===typeof g)return g;if("string"===typeof g){if(g in c)return c[g];a||(a=document.createElement("canvas"), | ||
b=a.getContext("2d"));a.width=a.height=1;b.fillStyle=g;b.fillRect(0,0,1,1);var d=b.getImageData(0,0,1,1).data;d=d[0]<<16^d[1]<<8^d[2];2048<f&&(c=Object.create(null),f=0);c[g]=d;f++;return d}return g&&g.isColor?g.getHex():0}}(),O=Object.freeze({__proto__:null,number:t,color:function(a,b,c){a=N(a);b=N(b);var f=t(a>>16&255,b>>16&255,c),g=t(a>>8&255,b>>8&255,c);a=t(a&255,b&255,c);return f<<16^g<<8^a}});n=function(){};n.prototype.gotoElapsedTime=function(a){};n.prototype.gotoEnd=function(){};n.prototype.isDoneAtElapsedTime= | ||
function(a){};var P=function(a){return a};z=function(a){function b(b,f,g,d,h,m,e,l,k){void 0===d&&(d=750);void 0===h&&(h=0);void 0===m&&(m=P);void 0===e&&(e=1);void 0===l&&(l="forward");void 0===k&&(k="number");a.call(this);this.callback=b;this.fromValue=f;this.toValue=g;this.duration=d;this.delay=h;this.easing="string"===typeof m?M[m]||P:m;this.iterations=e;this.direction=l;this.interpolate="function"===typeof k?k:O[k]||t;this.totalElapsed=9007199254740991>this.iterations?this.delay+this.duration* | ||
this.iterations:9007199254740991}a&&(b.__proto__=a);b.prototype=Object.create(a&&a.prototype);b.prototype.constructor=b;b.prototype.gotoElapsedTime=function(a){var b=this.duration,c=this.delay;if(a>=c){a=Math.min(a,this.totalElapsed)-c;c=a%b/b;0===c&&0!==a&&(c=1);c=this.easing(c);if("reverse"===this.direction||"alternate"===this.direction&&0===Math.ceil(a/b)%2)c=1-c;this.callback(this.interpolate(this.fromValue,this.toValue,c))}};b.prototype.gotoEnd=function(){this.gotoElapsedTime(this.totalElapsed)}; | ||
b.prototype.isDoneAtElapsedTime=function(a){return a>this.totalElapsed};return b}(n);D=function(a){function b(b,f,g,d,h,m){"number"!==typeof f&&(f=b.reduce(function(a,b){return Math.max(a,b.totalElapsed)},0));Infinity===f&&(f=Number.MAX_VALUE);a.call(this,null,0,f,f,g,d,h,m);1===b.length?this.callback=b[0].gotoElapsedTime.bind(b[0]):(b.sort(R),this.callback=this._syncTweens);this.tweens=b}a&&(b.__proto__=a);b.prototype=Object.create(a&&a.prototype);b.prototype.constructor=b;b.prototype._syncTweens= | ||
function(a){for(var b=0,c=this.tweens.length;b<c;b++)this.tweens[b].gotoElapsedTime(a)};return b}(z);var r=[],q=null,x=!1,y=window;l=function(){this.tweens=[]};l.prototype.destructor=function(){this.tweens=null;this.runner$running=!1;x=!0;this.start=this.stop=this.pause=this._tick=S};l.prototype.start=function(a){a.runner$paused&&a.runner$started?a.runner$started+=Date.now()-a.runner$paused:this.tweens.push(a);a.runner$paused=null;a.runner$stopped=!1;this.runner$running||(this.runner$running=!0,r.push(this), | ||
B())};l.prototype.stop=function(a){a.runner$stopped=!0;a.runner$paused=null};l.prototype.pause=function(a){a.runner$paused||(a.runner$paused=Date.now())};l.prototype.stopAll=function(){this.tweens&&this.tweens.forEach(this.stop,this)};l.prototype._tick=function(a){for(var b=this.tweens,c=!1,f=!1,g=0,d=b.length;g<d;g++){var h=b[g];if(!h.runner$stopped&&!h.runner$paused){var m=a-(h.runner$started||(h.runner$started=a));h.gotoElapsedTime(m);f=!0;if(h.isDoneAtElapsedTime(m)&&(this.stop(h),h.onDone))h.onDone()}h.runner$stopped&& | ||
(c=!0)}if(f)this.onTick();if(c&&(this.tweens=b.filter(U),!this.tweens.length&&(this.runner$running=!1,x=!0,this.onDone)))this.onDone()};l.prototype.onTick=function(){};l.prototype.onDone=function(){};var Q={default:{mass:1,tension:170,friction:26},gentle:{mass:1,tension:120,friction:14},wobbly:{mass:1,tension:180,friction:12},stiff:{mass:1,tension:210,friction:20},slow:{mass:1,tension:280,friction:60},molasses:{mass:1,tension:280,friction:120}},A=Q.default;n=function(a){function b(b,f,g,d,h,m){void 0=== | ||
h&&(h=0);void 0===m&&(m=0);a.call(this);this.isSpring=!0;this.callback=b;this.currentValue=f;this.toValue=g;this.velocity=h;this.delay=m;"string"===typeof d&&(d=Q[d]);d||(d=A);b=d.mass;f=d.tension;d=d.friction;this.mass="number"===typeof b?b:A.mass;this.tension=1E-6*("number"===typeof f?f:A.tension);this.friction=.001*("number"===typeof d?d:A.friction);this.minAcceleration=1E-10;this.$lastTime=m;this.$endTime=Infinity}a&&(b.__proto__=a);b.prototype=Object.create(a&&a.prototype);b.prototype.constructor= | ||
b;b.prototype.gotoElapsedTime=function(a){if(a>=this.delay){for(var b=this.toValue,c=this.mass,d=this.tension,h=this.friction,m=this.minAcceleration,e=this.velocity||0,k=this.currentValue,l=this.$lastTime;l<a;l++){var n=(d*(b-k)-h*e)/c;if(Math.abs(n)<m){e=0;k=b;this.$endTime=l;break}else e+=n,k+=e}this.velocity=e;this.$lastTime=a;this.callback(this.currentValue=k)}};b.prototype.gotoEnd=function(){this.velocity=0;this.$lastTime=this.$endTime;this.callback(this.currentValue=this.toValue)};b.prototype.isDoneAtElapsedTime= | ||
function(a){return a>=this.$endTime};return b}(n);e.Easings=M;e.Interpolators=O;e.MultiTween=D;e.Runner=l;e.SpringTween=n;e.Tween=z;e.setAnimationScheduler=function(a){a=a||window;a!==y&&(q&&(y.cancelAnimationFrame(q),q=null),y=a,B())};Object.defineProperty(e,"__esModule",{value:!0})}) | ||
'use strict';(function(e,l){"object"===typeof exports&&"undefined"!==typeof module?l(exports):"function"===typeof define&&define.amd?define(["exports"],l):(e="undefined"!==typeof globalThis?globalThis:e||self,l(e.troika_animation={}))})(this,function(e){function l(a,b){return c=>.5>c?.5*a(2*c):.5*b(2*c-1)+.5}function q(a){return b=>k(b,a)}function r(a){return b=>1-k(1-b,a)}function t(a){return b=>.5>b?.5*k(2*b,a):.5*(1-k(1-(2*b-1),a))+.5}function p(a,b,c){return a+(b-a)*c}function O(a,b){return a.totalElapsed- | ||
b.totalElapsed}function P(){}function Q(a){return a.runner$running}function R(a){return!a.runner$stopped}function S(){let a=Date.now();m=null;u&&(n=n.filter(Q),u=!1);if(n.length){for(let b=n.length;0<b--;)n[b]._tick(a);x()}}function x(){m||(m=v.requestAnimationFrame(S))}let {pow:k,PI:y,sqrt:B}=Math,C=y/2,T=2*y,U=q(2),V=r(2),W=t(2),X=q(3),Y=r(3),Z=t(3),aa=q(4),ba=r(4),ca=t(4),da=q(5),ea=r(5),fa=t(5),D=a=>1-B(1-a*a),E=a=>B(1-k(a-1,2)),ha=l(D,E),F=a=>0===a||1===a?a:1-z(1-a),z=a=>0===a||1===a?a:Math.pow(2, | ||
-10*a)*Math.sin((a-.075)*T/.3)+1,ia=l(F,z),G=a=>1-A(1-a),A=a=>a<1/2.75?7.5625*a*a:a<2/2.75?7.5625*(a-=1.5/2.75)*a+.75:a<2.5/2.75?7.5625*(a-=2.25/2.75)*a+.9375:7.5625*(a-=2.625/2.75)*a+.984375,ja=l(G,A);var H=Object.freeze({__proto__:null,linear:a=>a,easeInQuad:U,easeOutQuad:V,easeInOutQuad:W,easeInCubic:X,easeOutCubic:Y,easeInOutCubic:Z,easeInQuart:aa,easeOutQuart:ba,easeInOutQuart:ca,easeInQuint:da,easeOutQuint:ea,easeInOutQuint:fa,easeInSine:a=>1-Math.cos(a*C),easeOutSine:a=>Math.sin(a*C),easeInOutSine:a=> | ||
-.5*(Math.cos(y*a)-1),easeInExpo:a=>0===a?0:k(2,10*(a-1)),easeOutExpo:a=>1===a?1:1-k(2,-10*a),easeInOutExpo:a=>0===a||1===a?a:.5>a?.5*k(2,10*(2*a-1)):.5*(1-k(2,-10*(2*a-1)))+.5,easeInCirc:D,easeOutCirc:E,easeInOutCirc:ha,easeInElastic:F,easeOutElastic:z,easeInOutElastic:ia,easeInBack:a=>a*a*(2.70158*a-1.70158),easeOutBack:a=>--a*a*(2.70158*a+1.70158)+1,easeInOutBack:a=>1>(a*=2)?.5*a*a*(3.5949095*a-2.5949095):.5*((a-=2)*a*(3.5949095*a+2.5949095)+2),easeInBounce:G,easeOutBounce:A,easeInOutBounce:ja}); | ||
let I=function(){let a,b,c=Object.create(null),f=0;return function(d){if("number"===typeof d)return d;if("string"===typeof d){if(d in c)return c[d];a||(a=document.createElement("canvas"),b=a.getContext("2d"));a.width=a.height=1;b.fillStyle=d;b.fillRect(0,0,1,1);var g=b.getImageData(0,0,1,1).data;g=g[0]<<16^g[1]<<8^g[2];2048<f&&(c=Object.create(null),f=0);c[d]=g;f++;return g}return d&&d.isColor?d.getHex():0}}();var J=Object.freeze({__proto__:null,number:p,color:function(a,b,c){a=I(a);b=I(b);var f= | ||
p(a>>16&255,b>>16&255,c),d=p(a>>8&255,b>>8&255,c);a=p(a&255,b&255,c);return f<<16^d<<8^a}});class K{gotoElapsedTime(a){}gotoEnd(){}isDoneAtElapsedTime(a){}}let L=a=>a;class M extends K{constructor(a,b,c,f=750,d=0,g=L,h=1,ka="forward",e="number"){super();this.callback=a;this.fromValue=b;this.toValue=c;this.duration=f;this.delay=d;this.easing="string"===typeof g?H[g]||L:g;this.iterations=h;this.direction=ka;this.interpolate="function"===typeof e?e:J[e]||p;this.totalElapsed=9007199254740991>this.iterations? | ||
this.delay+this.duration*this.iterations:9007199254740991}gotoElapsedTime(a){let b=this.duration;var c=this.delay;if(a>=c){a=Math.min(a,this.totalElapsed)-c;c=a%b/b;0===c&&0!==a&&(c=1);c=this.easing(c);if("reverse"===this.direction||"alternate"===this.direction&&0===Math.ceil(a/b)%2)c=1-c;this.callback(this.interpolate(this.fromValue,this.toValue,c))}}gotoEnd(){this.gotoElapsedTime(this.totalElapsed)}isDoneAtElapsedTime(a){return a>this.totalElapsed}}class la extends M{constructor(a,b,c,f,d,g){"number"!== | ||
typeof b&&(b=a.reduce((a,b)=>Math.max(a,b.totalElapsed),0));Infinity===b&&(b=Number.MAX_VALUE);super(null,0,b,b,c,f,d,g);1===a.length?this.callback=a[0].gotoElapsedTime.bind(a[0]):(a.sort(O),this.callback=this._syncTweens);this.tweens=a}_syncTweens(a){for(let b=0,c=this.tweens.length;b<c;b++)this.tweens[b].gotoElapsedTime(a)}}let n=[],m=null,u=!1,v=window;class ma{constructor(){this.tweens=[]}destructor(){this.tweens=null;this.runner$running=!1;u=!0;this.start=this.stop=this.pause=this._tick=P}start(a){a.runner$paused&& | ||
a.runner$started?a.runner$started+=Date.now()-a.runner$paused:this.tweens.push(a);a.runner$paused=null;a.runner$stopped=!1;this.runner$running||(this.runner$running=!0,n.push(this),x())}stop(a){a.runner$stopped=!0;a.runner$paused=null}pause(a){a.runner$paused||(a.runner$paused=Date.now())}stopAll(){this.tweens&&this.tweens.forEach(this.stop,this)}_tick(a){let b=this.tweens,c=!1,f=!1;for(let d=0,g=b.length;d<g;d++){let h=b[d];if(!h.runner$stopped&&!h.runner$paused){let b=a-(h.runner$started||(h.runner$started= | ||
a));h.gotoElapsedTime(b);f=!0;if(h.isDoneAtElapsedTime(b)&&(this.stop(h),h.onDone))h.onDone()}h.runner$stopped&&(c=!0)}if(f)this.onTick();if(c&&(this.tweens=b.filter(R),!this.tweens.length&&(this.runner$running=!1,u=!0,this.onDone)))this.onDone()}onTick(){}onDone(){}}var N={default:{mass:1,tension:170,friction:26},gentle:{mass:1,tension:120,friction:14},wobbly:{mass:1,tension:180,friction:12},stiff:{mass:1,tension:210,friction:20},slow:{mass:1,tension:280,friction:60},molasses:{mass:1,tension:280, | ||
friction:120}};let w=N.default;class na extends K{constructor(a,b,c,f,d=0,g=0){super();this.isSpring=!0;this.callback=a;this.currentValue=b;this.toValue=c;this.velocity=d;this.delay=g;"string"===typeof f&&(f=N[f]);f||(f=w);let {mass:h,tension:e,friction:k}=f;this.mass="number"===typeof h?h:w.mass;this.tension=1E-6*("number"===typeof e?e:w.tension);this.friction=.001*("number"===typeof k?k:w.friction);this.minAcceleration=1E-10;this.$lastTime=g;this.$endTime=Infinity}gotoElapsedTime(a){if(a>=this.delay){let {toValue:b, | ||
mass:c,tension:f,friction:d,minAcceleration:e}=this,h=this.velocity||0,k=this.currentValue;for(let g=this.$lastTime;g<a;g++){let a=(f*(b-k)-d*h)/c;if(Math.abs(a)<e){h=0;k=b;this.$endTime=g;break}else h+=a,k+=h}this.velocity=h;this.$lastTime=a;this.callback(this.currentValue=k)}}gotoEnd(){this.velocity=0;this.$lastTime=this.$endTime;this.callback(this.currentValue=this.toValue)}isDoneAtElapsedTime(a){return a>=this.$endTime}}e.Easings=H;e.Interpolators=J;e.MultiTween=la;e.Runner=ma;e.SpringTween=na; | ||
e.Tween=M;e.setAnimationScheduler=function(a){a=a||window;a!==v&&(m&&(v.cancelAnimationFrame(m),m=null),v=a,x())};Object.defineProperty(e,"__esModule",{value:!0})}) |
{ | ||
"name": "troika-animation", | ||
"version": "0.42.0", | ||
"version": "0.43.0", | ||
"description": "Troika Animation Utilities", | ||
@@ -16,3 +16,3 @@ "author": "Jason Johnston <jason.johnston@protectwise.com>", | ||
"module:src": "src/index.js", | ||
"gitHead": "0f7129cd43fc58f5c6a5f6f20f2d0941f9f36510" | ||
"gitHead": "497fa534d015bc8f746c7d00ec7bfde1df92f491" | ||
} |
@@ -0,0 +0,0 @@ # Troika Animation |
@@ -0,0 +0,0 @@ /** |
@@ -0,0 +0,0 @@ /* |
@@ -0,0 +0,0 @@ // Animation |
@@ -0,0 +0,0 @@ /** |
@@ -0,0 +0,0 @@ import Tween from './Tween.js' |
@@ -0,0 +0,0 @@ let runners = [] |
@@ -0,0 +0,0 @@ /** |
@@ -0,0 +0,0 @@ import PRESETS from './SpringPresets.js' |
@@ -0,0 +0,0 @@ import * as Easings from './Easings.js' |
Sorry, the diff of this file is not supported yet
123050
2636