Initial commit xmarticaims
This commit is contained in:
+82
@@ -0,0 +1,82 @@
|
||||
/**
|
||||
* Anime.js - animation - CJS
|
||||
* @version v4.5.0
|
||||
* @license MIT
|
||||
* @copyright 2026 - Julian Garnier
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
var consts = require('../core/consts.cjs');
|
||||
var helpers = require('../core/helpers.cjs');
|
||||
var render = require('../core/render.cjs');
|
||||
|
||||
const additive = {
|
||||
animation: null,
|
||||
update: consts.noop,
|
||||
};
|
||||
|
||||
/**
|
||||
* @import {
|
||||
* Tween,
|
||||
* TweenAdditiveLookups,
|
||||
* } from '../types/index.js'
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef AdditiveAnimation
|
||||
* @property {Number} duration
|
||||
* @property {Number} _offset
|
||||
* @property {Number} _delay
|
||||
* @property {Tween} _head
|
||||
* @property {Tween} _tail
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {TweenAdditiveLookups} lookups
|
||||
* @return {AdditiveAnimation}
|
||||
*/
|
||||
const addAdditiveAnimation = lookups => {
|
||||
let animation = additive.animation;
|
||||
if (!animation) {
|
||||
animation = {
|
||||
duration: consts.minValue,
|
||||
computeDeltaTime: consts.noop,
|
||||
_offset: 0,
|
||||
_delay: 0,
|
||||
_head: null,
|
||||
_tail: null,
|
||||
};
|
||||
additive.animation = animation;
|
||||
additive.update = () => {
|
||||
lookups.forEach(propertyAnimation => {
|
||||
for (let propertyName in propertyAnimation) {
|
||||
const tweens = propertyAnimation[propertyName];
|
||||
const lookupTween = tweens._head;
|
||||
if (lookupTween) {
|
||||
const valueType = lookupTween._valueType;
|
||||
const additiveValues = valueType === consts.valueTypes.COMPLEX || valueType === consts.valueTypes.COLOR ? helpers.cloneArray(lookupTween._fromNumbers) : null;
|
||||
let additiveValue = lookupTween._fromNumber;
|
||||
let tween = tweens._tail;
|
||||
while (tween && tween !== lookupTween) {
|
||||
if (additiveValues) {
|
||||
for (let i = 0, l = tween._numbers.length; i < l; i++) additiveValues[i] += tween._numbers[i];
|
||||
} else {
|
||||
additiveValue += tween._number;
|
||||
}
|
||||
tween = tween._prevAdd;
|
||||
}
|
||||
lookupTween._toNumber = additiveValue;
|
||||
lookupTween._toNumbers = additiveValues;
|
||||
}
|
||||
}
|
||||
});
|
||||
// TODO: Avoid polymorphism here, idealy the additive animation should be a regular animation with a higher priority in the render loop
|
||||
render.render(animation, 1, 1, 0, consts.tickModes.FORCE);
|
||||
};
|
||||
}
|
||||
return animation;
|
||||
};
|
||||
|
||||
exports.addAdditiveAnimation = addAdditiveAnimation;
|
||||
exports.additive = additive;
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
export namespace additive {
|
||||
export let animation: any;
|
||||
export { noop as update };
|
||||
}
|
||||
export function addAdditiveAnimation(lookups: TweenAdditiveLookups): AdditiveAnimation;
|
||||
export type AdditiveAnimation = {
|
||||
duration: number;
|
||||
_offset: number;
|
||||
_delay: number;
|
||||
_head: Tween;
|
||||
_tail: Tween;
|
||||
};
|
||||
import { noop } from '../core/consts.js';
|
||||
import type { TweenAdditiveLookups } from '../types/index.js';
|
||||
import type { Tween } from '../types/index.js';
|
||||
+79
@@ -0,0 +1,79 @@
|
||||
/**
|
||||
* Anime.js - animation - ESM
|
||||
* @version v4.5.0
|
||||
* @license MIT
|
||||
* @copyright 2026 - Julian Garnier
|
||||
*/
|
||||
|
||||
import { noop, minValue, valueTypes, tickModes } from '../core/consts.js';
|
||||
import { cloneArray } from '../core/helpers.js';
|
||||
import { render } from '../core/render.js';
|
||||
|
||||
const additive = {
|
||||
animation: null,
|
||||
update: noop,
|
||||
};
|
||||
|
||||
/**
|
||||
* @import {
|
||||
* Tween,
|
||||
* TweenAdditiveLookups,
|
||||
* } from '../types/index.js'
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef AdditiveAnimation
|
||||
* @property {Number} duration
|
||||
* @property {Number} _offset
|
||||
* @property {Number} _delay
|
||||
* @property {Tween} _head
|
||||
* @property {Tween} _tail
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {TweenAdditiveLookups} lookups
|
||||
* @return {AdditiveAnimation}
|
||||
*/
|
||||
const addAdditiveAnimation = lookups => {
|
||||
let animation = additive.animation;
|
||||
if (!animation) {
|
||||
animation = {
|
||||
duration: minValue,
|
||||
computeDeltaTime: noop,
|
||||
_offset: 0,
|
||||
_delay: 0,
|
||||
_head: null,
|
||||
_tail: null,
|
||||
};
|
||||
additive.animation = animation;
|
||||
additive.update = () => {
|
||||
lookups.forEach(propertyAnimation => {
|
||||
for (let propertyName in propertyAnimation) {
|
||||
const tweens = propertyAnimation[propertyName];
|
||||
const lookupTween = tweens._head;
|
||||
if (lookupTween) {
|
||||
const valueType = lookupTween._valueType;
|
||||
const additiveValues = valueType === valueTypes.COMPLEX || valueType === valueTypes.COLOR ? cloneArray(lookupTween._fromNumbers) : null;
|
||||
let additiveValue = lookupTween._fromNumber;
|
||||
let tween = tweens._tail;
|
||||
while (tween && tween !== lookupTween) {
|
||||
if (additiveValues) {
|
||||
for (let i = 0, l = tween._numbers.length; i < l; i++) additiveValues[i] += tween._numbers[i];
|
||||
} else {
|
||||
additiveValue += tween._number;
|
||||
}
|
||||
tween = tween._prevAdd;
|
||||
}
|
||||
lookupTween._toNumber = additiveValue;
|
||||
lookupTween._toNumbers = additiveValues;
|
||||
}
|
||||
}
|
||||
});
|
||||
// TODO: Avoid polymorphism here, idealy the additive animation should be a regular animation with a higher priority in the render loop
|
||||
render(animation, 1, 1, 0, tickModes.FORCE);
|
||||
};
|
||||
}
|
||||
return animation;
|
||||
};
|
||||
|
||||
export { addAdditiveAnimation, additive };
|
||||
+746
@@ -0,0 +1,746 @@
|
||||
/**
|
||||
* Anime.js - animation - CJS
|
||||
* @version v4.5.0
|
||||
* @license MIT
|
||||
* @copyright 2026 - Julian Garnier
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
var consts = require('../core/consts.cjs');
|
||||
var helpers = require('../core/helpers.cjs');
|
||||
var globals = require('../core/globals.cjs');
|
||||
var targets = require('../core/targets.cjs');
|
||||
var registry = require('../adapters/registry.cjs');
|
||||
var values = require('../core/values.cjs');
|
||||
var styles = require('../core/styles.cjs');
|
||||
var units = require('../core/units.cjs');
|
||||
var parser = require('../easings/eases/parser.cjs');
|
||||
var timer = require('../timer/timer.cjs');
|
||||
var composition = require('./composition.cjs');
|
||||
var additive = require('./additive.cjs');
|
||||
|
||||
/**
|
||||
* @import {
|
||||
* Tween,
|
||||
* TweenKeyValue,
|
||||
* TweenParamsOptions,
|
||||
* TweenValues,
|
||||
* DurationKeyframes,
|
||||
* PercentageKeyframes,
|
||||
* AnimationParams,
|
||||
* TweenPropValue,
|
||||
* ArraySyntaxValue,
|
||||
* TargetsParam,
|
||||
* TimerParams,
|
||||
* TweenParamValue,
|
||||
* DOMTarget,
|
||||
* TargetsArray,
|
||||
* Callback,
|
||||
* EasingFunction,
|
||||
* } from '../types/index.js'
|
||||
*
|
||||
* @import {
|
||||
* Timeline,
|
||||
* } from '../timeline/timeline.js'
|
||||
*
|
||||
* @import {
|
||||
* Spring,
|
||||
* } from '../easings/spring/index.js'
|
||||
*/
|
||||
|
||||
// Defines decomposed values target objects only once and mutate their properties later to avoid GC
|
||||
// TODO: Maybe move the objects creation to values.js and use the decompose function to create the base object
|
||||
const fromTargetObject = values.createDecomposedValueTargetObject();
|
||||
const toTargetObject = values.createDecomposedValueTargetObject();
|
||||
const inlineStylesStore = {};
|
||||
const toFunctionStore = { func: null };
|
||||
const fromFunctionStore = { func: null };
|
||||
const keyframesTargetArray = [null];
|
||||
const fastSetValuesArray = [null, null];
|
||||
/** @type {TweenKeyValue} */
|
||||
const keyObjectTarget = { to: null };
|
||||
|
||||
let tweenId = 0;
|
||||
let JSAnimationId = 0;
|
||||
let keyframes;
|
||||
/** @type {TweenParamsOptions & TweenValues} */
|
||||
let key;
|
||||
|
||||
/**
|
||||
* @param {DurationKeyframes | PercentageKeyframes} keyframes
|
||||
* @param {AnimationParams} parameters
|
||||
* @return {AnimationParams}
|
||||
*/
|
||||
const generateKeyframes = (keyframes, parameters) => {
|
||||
/** @type {AnimationParams} */
|
||||
const properties = {};
|
||||
if (helpers.isArr(keyframes)) {
|
||||
const propertyNames = [].concat(.../** @type {DurationKeyframes} */(keyframes).map(key => Object.keys(key))).filter(helpers.isKey);
|
||||
for (let i = 0, l = propertyNames.length; i < l; i++) {
|
||||
const propName = propertyNames[i];
|
||||
const propArray = /** @type {DurationKeyframes} */(keyframes).map(key => {
|
||||
/** @type {TweenKeyValue} */
|
||||
const newKey = {};
|
||||
for (let p in key) {
|
||||
const keyValue = /** @type {TweenPropValue} */(key[p]);
|
||||
if (helpers.isKey(p)) {
|
||||
if (p === propName) {
|
||||
newKey.to = keyValue;
|
||||
}
|
||||
} else {
|
||||
newKey[p] = keyValue;
|
||||
}
|
||||
}
|
||||
return newKey;
|
||||
});
|
||||
properties[propName] = /** @type {ArraySyntaxValue} */(propArray);
|
||||
}
|
||||
|
||||
} else {
|
||||
const totalDuration = /** @type {Number} */(values.setValue(parameters.duration, globals.globals.defaults.duration));
|
||||
const keys = Object.keys(keyframes)
|
||||
.map(key => { return {o: parseFloat(key) / 100, p: keyframes[key]} })
|
||||
.sort((a, b) => a.o - b.o);
|
||||
keys.forEach(key => {
|
||||
const offset = key.o;
|
||||
const prop = key.p;
|
||||
for (let name in prop) {
|
||||
if (helpers.isKey(name)) {
|
||||
let propArray = /** @type {Array} */(properties[name]);
|
||||
if (!propArray) propArray = properties[name] = [];
|
||||
const duration = offset * totalDuration;
|
||||
let length = propArray.length;
|
||||
let prevKey = propArray[length - 1];
|
||||
const keyObj = { to: prop[name] };
|
||||
let durProgress = 0;
|
||||
for (let i = 0; i < length; i++) {
|
||||
durProgress += propArray[i].duration;
|
||||
}
|
||||
if (length === 1) {
|
||||
keyObj.from = prevKey.to;
|
||||
}
|
||||
if (prop.ease) {
|
||||
keyObj.ease = prop.ease;
|
||||
}
|
||||
keyObj.duration = duration - (length ? durProgress : 0);
|
||||
propArray.push(keyObj);
|
||||
}
|
||||
}
|
||||
return key;
|
||||
});
|
||||
|
||||
for (let name in properties) {
|
||||
const propArray = /** @type {Array} */(properties[name]);
|
||||
let prevEase;
|
||||
// let durProgress = 0
|
||||
for (let i = 0, l = propArray.length; i < l; i++) {
|
||||
const prop = propArray[i];
|
||||
// Emulate WAPPI easing parameter position
|
||||
const currentEase = prop.ease;
|
||||
prop.ease = prevEase ? prevEase : undefined;
|
||||
prevEase = currentEase;
|
||||
// durProgress += prop.duration;
|
||||
// if (i === l - 1 && durProgress !== totalDuration) {
|
||||
// propArray.push({ from: prop.to, ease: prop.ease, duration: totalDuration - durProgress })
|
||||
// }
|
||||
}
|
||||
if (!propArray[0].duration) {
|
||||
propArray.shift();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return properties;
|
||||
};
|
||||
|
||||
class JSAnimation extends timer.Timer {
|
||||
/**
|
||||
* @param {TargetsParam} targets
|
||||
* @param {AnimationParams} parameters
|
||||
* @param {Timeline} [parent]
|
||||
* @param {Number} [parentPosition]
|
||||
* @param {Boolean} [fastSet=false]
|
||||
* @param {Number} [index=0]
|
||||
* @param {TargetsArray} [allTargets]
|
||||
*/
|
||||
constructor(
|
||||
targets$1,
|
||||
parameters,
|
||||
parent,
|
||||
parentPosition,
|
||||
fastSet = false,
|
||||
index = 0,
|
||||
allTargets
|
||||
) {
|
||||
|
||||
super(/** @type {TimerParams & AnimationParams} */(parameters), parent, parentPosition);
|
||||
|
||||
/** @type {Tween} */
|
||||
this._head;
|
||||
/** @type {Tween} */
|
||||
this._tail;
|
||||
|
||||
++JSAnimationId;
|
||||
|
||||
const parsedTargets = targets.registerTargets(targets$1);
|
||||
const targetsLength = parsedTargets.length;
|
||||
|
||||
// If the parameters object contains a "keyframes" property, convert all the keyframes values to regular properties
|
||||
|
||||
const kfParams = /** @type {AnimationParams} */(parameters).keyframes;
|
||||
const params = /** @type {AnimationParams} */(kfParams ? helpers.mergeObjects(generateKeyframes(/** @type {DurationKeyframes} */(kfParams), parameters), parameters) : parameters);
|
||||
|
||||
const {
|
||||
id,
|
||||
delay,
|
||||
duration,
|
||||
ease,
|
||||
playbackEase,
|
||||
modifier,
|
||||
composition: composition$1,
|
||||
onRender,
|
||||
} = params;
|
||||
|
||||
const animDefaults = parent ? parent.defaults : globals.globals.defaults;
|
||||
const animEase = values.setValue(ease, animDefaults.ease);
|
||||
const animPlaybackEase = values.setValue(playbackEase, animDefaults.playbackEase);
|
||||
const parsedAnimPlaybackEase = animPlaybackEase ? parser.parseEase(animPlaybackEase) : null;
|
||||
const hasSpring = !helpers.isUnd(/** @type {Spring} */(animEase).ease);
|
||||
const tEasing = hasSpring ? /** @type {Spring} */(animEase).ease : values.setValue(ease, parsedAnimPlaybackEase ? 'linear' : animDefaults.ease);
|
||||
const tDuration = hasSpring ? /** @type {Spring} */(animEase).settlingDuration : values.setValue(duration, animDefaults.duration);
|
||||
const tDelay = values.setValue(delay, animDefaults.delay);
|
||||
const tModifier = modifier || animDefaults.modifier;
|
||||
// If no composition is defined and the targets length is high (>= 1000) set the composition to 'none' (0) for faster tween creation
|
||||
const tComposition = helpers.isUnd(composition$1) && targetsLength >= consts.K ? consts.compositionTypes.none : !helpers.isUnd(composition$1) ? composition$1 : animDefaults.composition;
|
||||
// const absoluteOffsetTime = this._offset;
|
||||
const absoluteOffsetTime = this._offset + (parent ? parent._offset : 0);
|
||||
// This allows targeting the current animation in the spring onComplete callback
|
||||
if (hasSpring) /** @type {Spring} */(animEase).parent = this;
|
||||
|
||||
let iterationDuration = NaN;
|
||||
let iterationDelay = NaN;
|
||||
let animationAnimationLength = 0;
|
||||
let shouldTriggerRender = 0;
|
||||
|
||||
for (let targetIndex = 0; targetIndex < targetsLength; targetIndex++) {
|
||||
|
||||
const target = parsedTargets[targetIndex];
|
||||
const ti = index || targetIndex;
|
||||
const tl = allTargets || parsedTargets;
|
||||
|
||||
let lastTransformGroupIndex = NaN;
|
||||
let lastTransformGroupLength = NaN;
|
||||
|
||||
for (let p in params) {
|
||||
|
||||
if (helpers.isKey(p)) {
|
||||
|
||||
const tweenType = values.getTweenType(target, p);
|
||||
const adapterProp = registry.resolveAdapterEntry(target, p);
|
||||
|
||||
const propName = styles.sanitizePropertyName(p, target, tweenType);
|
||||
|
||||
let propValue = params[p];
|
||||
|
||||
const isPropValueArray = helpers.isArr(propValue);
|
||||
|
||||
if (fastSet && !isPropValueArray) {
|
||||
fastSetValuesArray[0] = propValue;
|
||||
fastSetValuesArray[1] = propValue;
|
||||
propValue = fastSetValuesArray;
|
||||
}
|
||||
|
||||
// TODO: Allow nested keyframes inside ObjectValue value (prop: { to: [.5, 1, .75, 2, 3] })
|
||||
// Normalize property values to valid keyframe syntax:
|
||||
// [x, y] to [{to: [x, y]}] or {to: x} to [{to: x}] or keep keys syntax [{}, {}, {}...]
|
||||
// const keyframes = isArr(propValue) ? propValue.length === 2 && !isObj(propValue[0]) ? [{ to: propValue }] : propValue : [propValue];
|
||||
if (isPropValueArray) {
|
||||
const arrayLength = /** @type {Array} */(propValue).length;
|
||||
const isNotObjectValue = !helpers.isObj(propValue[0]);
|
||||
// Convert [x, y] to [{to: [x, y]}]
|
||||
if (arrayLength === 2 && isNotObjectValue) {
|
||||
keyObjectTarget.to = /** @type {TweenParamValue} */(/** @type {unknown} */(propValue));
|
||||
keyframesTargetArray[0] = keyObjectTarget;
|
||||
keyframes = keyframesTargetArray;
|
||||
// Convert [x, y, z] to [[x, y], z]
|
||||
} else if (arrayLength > 2 && isNotObjectValue) {
|
||||
keyframes = [];
|
||||
/** @type {Array.<Number>} */(propValue).forEach((v, i) => {
|
||||
if (!i) {
|
||||
fastSetValuesArray[0] = v;
|
||||
} else if (i === 1) {
|
||||
fastSetValuesArray[1] = v;
|
||||
keyframes.push(fastSetValuesArray);
|
||||
} else {
|
||||
keyframes.push(v);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
keyframes = /** @type {Array.<TweenKeyValue>} */(propValue);
|
||||
}
|
||||
} else {
|
||||
keyframesTargetArray[0] = propValue;
|
||||
keyframes = keyframesTargetArray;
|
||||
}
|
||||
|
||||
let siblings = null;
|
||||
let prevTween = null;
|
||||
let firstTweenChangeStartTime = NaN;
|
||||
let lastTweenChangeEndTime = 0;
|
||||
let tweenIndex = 0;
|
||||
|
||||
for (let l = keyframes.length; tweenIndex < l; tweenIndex++) {
|
||||
|
||||
const keyframe = keyframes[tweenIndex];
|
||||
|
||||
if (helpers.isObj(keyframe)) {
|
||||
key = keyframe;
|
||||
} else {
|
||||
keyObjectTarget.to = /** @type {TweenParamValue} */(keyframe);
|
||||
key = keyObjectTarget;
|
||||
}
|
||||
|
||||
toFunctionStore.func = null;
|
||||
fromFunctionStore.func = null;
|
||||
|
||||
const computedComposition = values.getFunctionValue(values.setValue(key.composition, tComposition), target, ti, tl, null, null);
|
||||
const tweenComposition = helpers.isNum(computedComposition) ? computedComposition : consts.compositionTypes[computedComposition];
|
||||
if (!siblings && tweenComposition !== consts.compositionTypes.none) siblings = composition.getTweenSiblings(target, propName);
|
||||
// Timelines pass the last sibling tween if it belongs to the same timeline
|
||||
// Standalone animations only pass prevTween when the property has multiple keyframes
|
||||
const tailTween = siblings ? siblings._tail : null;
|
||||
const prevSiblingTween = parent && tailTween && tailTween.parent.parent === parent ? tailTween : prevTween;
|
||||
const computedToValue = values.getFunctionValue(key.to, target, ti, tl, toFunctionStore, prevSiblingTween);
|
||||
|
||||
let tweenToValue;
|
||||
// Allows function based values to return an object syntax value ({to: v})
|
||||
if (helpers.isObj(computedToValue) && !helpers.isUnd(computedToValue.to)) {
|
||||
key = computedToValue;
|
||||
tweenToValue = computedToValue.to;
|
||||
} else {
|
||||
tweenToValue = computedToValue;
|
||||
}
|
||||
const tweenFromValue = values.getFunctionValue(key.from, target, ti, tl, fromFunctionStore, prevSiblingTween);
|
||||
const easeToParse = key.ease || tEasing;
|
||||
|
||||
const easeFunctionResult = values.getFunctionValue(easeToParse, target, ti, tl, null, prevSiblingTween);
|
||||
const keyEasing = helpers.isFnc(easeFunctionResult) || helpers.isStr(easeFunctionResult) ? easeFunctionResult : easeToParse;
|
||||
|
||||
const hasSpring = !helpers.isUnd(keyEasing) && !helpers.isUnd(/** @type {Spring} */(keyEasing).ease);
|
||||
const tweenEasing = hasSpring ? /** @type {Spring} */(keyEasing).ease : keyEasing;
|
||||
// Calculate default individual keyframe duration by dividing the tl of keyframes
|
||||
const tweenDuration = hasSpring ? /** @type {Spring} */(keyEasing).settlingDuration : values.getFunctionValue(values.setValue(key.duration, (l > 1 ? values.getFunctionValue(tDuration, target, ti, tl, null, prevSiblingTween) / l : tDuration)), target, ti, tl, null, prevSiblingTween);
|
||||
// Default delay value should only be applied to the first tween
|
||||
const tweenDelay = values.getFunctionValue(values.setValue(key.delay, (!tweenIndex ? tDelay : 0)), target, ti, tl, null, prevSiblingTween);
|
||||
// Modifiers are treated differently and don't accept function based value to prevent having to pass a function wrapper
|
||||
const tweenModifier = key.modifier || tModifier;
|
||||
const hasFromvalue = !helpers.isUnd(tweenFromValue);
|
||||
const hasToValue = !helpers.isUnd(tweenToValue);
|
||||
const isFromToArray = helpers.isArr(tweenToValue);
|
||||
const isFromToValue = isFromToArray || (hasFromvalue && hasToValue);
|
||||
// Capture the update-start in local time, the previous sibling's end for keyframes after the first, zero for the first keyframe. Used to derive a precision-matched _absoluteUpdateStartTime below.
|
||||
const tweenUpdateStartLocal = prevTween ? lastTweenChangeEndTime : 0;
|
||||
const tweenStartTime = prevTween ? lastTweenChangeEndTime + tweenDelay : tweenDelay;
|
||||
// Rounding is necessary here to minimize floating point errors when working in seconds
|
||||
const absoluteStartTime = helpers.round(absoluteOffsetTime + tweenStartTime, 12);
|
||||
// Match the rounding pattern of prevSibling._absoluteEndTime so the composition overlap check compares cleanly when keyframes touch at a boundary.
|
||||
const absoluteUpdateStartTime = helpers.round(absoluteOffsetTime + tweenUpdateStartLocal, 12);
|
||||
|
||||
// Force a onRender callback if the animation contains at least one from value and autoplay is set to false
|
||||
if (!shouldTriggerRender && (hasFromvalue || isFromToArray)) shouldTriggerRender = 1;
|
||||
|
||||
let prevSibling = prevTween;
|
||||
|
||||
if (tweenComposition !== consts.compositionTypes.none) {
|
||||
let nextSibling = siblings._head;
|
||||
// Walk prior siblings up to the new tween, skipping overridden ones so the chain resolves to the latest live value instead of stopping at the first override.
|
||||
while (nextSibling && nextSibling._absoluteStartTime <= absoluteStartTime) {
|
||||
if (!nextSibling._isOverridden) prevSibling = nextSibling;
|
||||
nextSibling = nextSibling._nextRep;
|
||||
// Overrides all the next siblings if the next sibling starts at the same time of after as the new tween start time
|
||||
if (nextSibling && nextSibling._absoluteStartTime >= absoluteStartTime) {
|
||||
while (nextSibling) {
|
||||
composition.overrideTween(nextSibling);
|
||||
// This will ends both the current while loop and the upper one once all the next sibllings have been overriden
|
||||
nextSibling = nextSibling._nextRep;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Decompose values
|
||||
if (isFromToValue) {
|
||||
values.decomposeRawValue(isFromToArray ? values.getFunctionValue(tweenToValue[0], target, ti, tl, fromFunctionStore, prevSiblingTween) : tweenFromValue, fromTargetObject);
|
||||
values.decomposeRawValue(isFromToArray ? values.getFunctionValue(tweenToValue[1], target, ti, tl, toFunctionStore, prevSiblingTween) : tweenToValue, toTargetObject);
|
||||
// Needed to force an inline style registration
|
||||
const originalValue = values.getOriginalAnimatableValue(target, propName, tweenType, inlineStylesStore);
|
||||
if (fromTargetObject.t === consts.valueTypes.NUMBER) {
|
||||
if (prevSibling) {
|
||||
if (prevSibling._valueType === consts.valueTypes.UNIT) {
|
||||
fromTargetObject.t = consts.valueTypes.UNIT;
|
||||
fromTargetObject.u = prevSibling._unit;
|
||||
}
|
||||
} else {
|
||||
values.decomposeRawValue(
|
||||
originalValue,
|
||||
values.decomposedOriginalValue
|
||||
);
|
||||
if (values.decomposedOriginalValue.t === consts.valueTypes.UNIT) {
|
||||
fromTargetObject.t = consts.valueTypes.UNIT;
|
||||
fromTargetObject.u = values.decomposedOriginalValue.u;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (hasToValue) {
|
||||
values.decomposeRawValue(tweenToValue, toTargetObject);
|
||||
} else {
|
||||
if (prevTween) {
|
||||
values.decomposeTweenValue(prevTween, toTargetObject);
|
||||
} else {
|
||||
// No need to get and parse the original value if the tween is part of a timeline and has a previous sibling part of the same timeline
|
||||
values.decomposeRawValue(parent && prevSibling && prevSibling.parent.parent === parent ? prevSibling._value :
|
||||
values.getOriginalAnimatableValue(target, propName, tweenType, inlineStylesStore), toTargetObject);
|
||||
}
|
||||
}
|
||||
if (hasFromvalue) {
|
||||
values.decomposeRawValue(tweenFromValue, fromTargetObject);
|
||||
} else {
|
||||
if (prevTween) {
|
||||
values.decomposeTweenValue(prevTween, fromTargetObject);
|
||||
} else {
|
||||
// No need to get and parse the original value if the tween is part of a timeline and has a previous sibling part of the same timeline
|
||||
values.decomposeRawValue(parent && prevSibling && prevSibling.parent.parent === parent ? prevSibling._value :
|
||||
values.getOriginalAnimatableValue(target, propName, tweenType, inlineStylesStore), fromTargetObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Apply operators
|
||||
if (fromTargetObject.o) {
|
||||
fromTargetObject.n = values.getRelativeValue(
|
||||
!prevSibling ? values.decomposeRawValue(
|
||||
values.getOriginalAnimatableValue(target, propName, tweenType, inlineStylesStore),
|
||||
values.decomposedOriginalValue
|
||||
).n : prevSibling._toNumber,
|
||||
fromTargetObject.n,
|
||||
fromTargetObject.o
|
||||
);
|
||||
}
|
||||
|
||||
if (toTargetObject.o) {
|
||||
toTargetObject.n = values.getRelativeValue(fromTargetObject.n, toTargetObject.n, toTargetObject.o);
|
||||
}
|
||||
|
||||
// Values omogenisation in cases of type difference between "from" and "to"
|
||||
if (fromTargetObject.t !== toTargetObject.t) {
|
||||
if (fromTargetObject.t === consts.valueTypes.COMPLEX || toTargetObject.t === consts.valueTypes.COMPLEX) {
|
||||
const complexValue = fromTargetObject.t === consts.valueTypes.COMPLEX ? fromTargetObject : toTargetObject;
|
||||
const notComplexValue = fromTargetObject.t === consts.valueTypes.COMPLEX ? toTargetObject : fromTargetObject;
|
||||
notComplexValue.t = consts.valueTypes.COMPLEX;
|
||||
notComplexValue.s = helpers.cloneArray(complexValue.s);
|
||||
notComplexValue.d = complexValue.d.map(() => notComplexValue.n);
|
||||
} else if (fromTargetObject.t === consts.valueTypes.UNIT || toTargetObject.t === consts.valueTypes.UNIT) {
|
||||
const unitValue = fromTargetObject.t === consts.valueTypes.UNIT ? fromTargetObject : toTargetObject;
|
||||
const notUnitValue = fromTargetObject.t === consts.valueTypes.UNIT ? toTargetObject : fromTargetObject;
|
||||
notUnitValue.t = consts.valueTypes.UNIT;
|
||||
notUnitValue.u = unitValue.u;
|
||||
} else if (fromTargetObject.t === consts.valueTypes.COLOR || toTargetObject.t === consts.valueTypes.COLOR) {
|
||||
const colorValue = fromTargetObject.t === consts.valueTypes.COLOR ? fromTargetObject : toTargetObject;
|
||||
const notColorValue = fromTargetObject.t === consts.valueTypes.COLOR ? toTargetObject : fromTargetObject;
|
||||
notColorValue.t = consts.valueTypes.COLOR;
|
||||
notColorValue.d = colorValue.d.map(() => 0);
|
||||
}
|
||||
}
|
||||
|
||||
// Unit conversion
|
||||
if (fromTargetObject.u !== toTargetObject.u) {
|
||||
let valueToConvert = toTargetObject.u ? fromTargetObject : toTargetObject;
|
||||
valueToConvert = units.convertValueUnit(/** @type {DOMTarget} */(target), valueToConvert, toTargetObject.u ? toTargetObject.u : fromTargetObject.u, false);
|
||||
// TODO:
|
||||
// convertValueUnit(target, to.u ? from : to, to.u ? to.u : from.u);
|
||||
}
|
||||
|
||||
// Fill in non existing complex values
|
||||
if (toTargetObject.d && fromTargetObject.d && (toTargetObject.d.length !== fromTargetObject.d.length)) {
|
||||
const longestValue = fromTargetObject.d.length > toTargetObject.d.length ? fromTargetObject : toTargetObject;
|
||||
const shortestValue = longestValue === fromTargetObject ? toTargetObject : fromTargetObject;
|
||||
// TODO: Check if n should be used instead of 0 for default complex values
|
||||
shortestValue.d = longestValue.d.map((/** @type {Number} */_, /** @type {Number} */i) => helpers.isUnd(shortestValue.d[i]) ? 0 : shortestValue.d[i]);
|
||||
shortestValue.s = helpers.cloneArray(longestValue.s);
|
||||
}
|
||||
|
||||
// Tween factory
|
||||
|
||||
// Rounding is necessary here to minimize floating point errors when working in seconds
|
||||
const tweenUpdateDuration = helpers.round(+tweenDuration || consts.minValue, 12);
|
||||
|
||||
// Copy the value of the iniline style if it exist and imediatly nullify it to prevents false positive on other targets
|
||||
let inlineValue = inlineStylesStore[propName];
|
||||
if (!helpers.isNil(inlineValue)) inlineStylesStore[propName] = null;
|
||||
|
||||
// Resolve the adapter setter once so render skips the lookup per frame.
|
||||
const tweenSetter = adapterProp ? adapterProp.set : null;
|
||||
|
||||
// Rounding is necessary here to minimize floating point errors when working in seconds
|
||||
lastTweenChangeEndTime = helpers.round(tweenStartTime + tweenUpdateDuration, 12);
|
||||
|
||||
const fromD = fromTargetObject.d;
|
||||
const toD = toTargetObject.d;
|
||||
const toS = toTargetObject.s;
|
||||
|
||||
/** @type {Tween} */
|
||||
const tween = {
|
||||
parent: this,
|
||||
id: tweenId++,
|
||||
property: propName,
|
||||
target: target,
|
||||
_value: null,
|
||||
_toFunc: toFunctionStore.func,
|
||||
_fromFunc: fromFunctionStore.func,
|
||||
_ease: parser.parseEase(tweenEasing),
|
||||
_fromNumbers: fromD ? helpers.cloneArray(fromD) : consts.emptyArray,
|
||||
_toNumbers: toD ? helpers.cloneArray(toD) : consts.emptyArray,
|
||||
_strings: toS ? helpers.cloneArray(toS) : consts.emptyArray,
|
||||
_fromNumber: fromTargetObject.n,
|
||||
_toNumber: toTargetObject.n,
|
||||
_numbers: fromD ? helpers.cloneArray(fromD) : consts.emptyArray, // For additive tween and animatables
|
||||
_number: fromTargetObject.n, // For additive tween and animatables
|
||||
_unit: toTargetObject.u,
|
||||
_modifier: tweenModifier,
|
||||
_currentTime: 0,
|
||||
_startTime: tweenStartTime,
|
||||
_delay: +tweenDelay,
|
||||
_updateDuration: tweenUpdateDuration,
|
||||
_changeDuration: tweenUpdateDuration,
|
||||
_absoluteStartTime: absoluteStartTime,
|
||||
_absoluteUpdateStartTime: absoluteUpdateStartTime,
|
||||
_absoluteEndTime: helpers.round(absoluteOffsetTime + lastTweenChangeEndTime, 12),
|
||||
_hasFromValue: hasFromvalue || isFromToArray ? 1 : 0,
|
||||
// NOTE: Investigate bit packing to stores ENUM / BOOL
|
||||
_tweenType: tweenType,
|
||||
_setter: tweenSetter,
|
||||
_valueType: toTargetObject.t,
|
||||
_composition: tweenComposition,
|
||||
_isOverlapped: 0,
|
||||
_isOverridden: 0,
|
||||
_renderTransforms: 0,
|
||||
_inlineValue: inlineValue,
|
||||
_prevRep: null, // For replaced tween
|
||||
_nextRep: null, // For replaced tween
|
||||
_prevAdd: null, // For additive tween
|
||||
_nextAdd: null, // For additive tween
|
||||
_prev: null,
|
||||
_next: null,
|
||||
};
|
||||
|
||||
if (tweenComposition !== consts.compositionTypes.none) {
|
||||
composition.composeTween(tween, siblings);
|
||||
}
|
||||
|
||||
// Pre-compute the tween end value for function-based value chaining (ie morphTo / scrambleText in keyframe arrays and timelines)
|
||||
const vt = tween._valueType;
|
||||
if (vt === consts.valueTypes.COMPLEX) {
|
||||
tween._value = values.composeComplexValue(tween, 1, -1);
|
||||
} else if (vt === consts.valueTypes.UNIT) {
|
||||
tween._value = `${tweenModifier(tween._toNumber)}${tween._unit}`;
|
||||
} else if (vt === consts.valueTypes.COLOR) {
|
||||
const d = toTargetObject.d;
|
||||
tween._value = `rgba(${helpers.round(d[0], 0)},${helpers.round(d[1], 0)},${helpers.round(d[2], 0)},${d[3]})`;
|
||||
} else {
|
||||
tween._value = tweenModifier(tween._toNumber);
|
||||
}
|
||||
|
||||
if (isNaN(firstTweenChangeStartTime)) {
|
||||
firstTweenChangeStartTime = tween._startTime;
|
||||
}
|
||||
|
||||
prevTween = tween;
|
||||
animationAnimationLength++;
|
||||
|
||||
helpers.addChild(this, tween);
|
||||
|
||||
}
|
||||
|
||||
// Update animation timings with the added tweens properties
|
||||
|
||||
if (isNaN(iterationDelay) || firstTweenChangeStartTime < iterationDelay) {
|
||||
iterationDelay = firstTweenChangeStartTime;
|
||||
}
|
||||
|
||||
if (isNaN(iterationDuration) || lastTweenChangeEndTime > iterationDuration) {
|
||||
iterationDuration = lastTweenChangeEndTime;
|
||||
}
|
||||
|
||||
// TODO: Find a way to inline tween._renderTransforms = 1 here
|
||||
if (tweenType === consts.tweenTypes.TRANSFORM) {
|
||||
lastTransformGroupIndex = animationAnimationLength - tweenIndex;
|
||||
lastTransformGroupLength = animationAnimationLength;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Set _renderTransforms to last transform property to correctly render the transforms list
|
||||
if (!isNaN(lastTransformGroupIndex)) {
|
||||
let i = 0;
|
||||
helpers.forEachChildren(this, (/** @type {Tween} */tween) => {
|
||||
if (i >= lastTransformGroupIndex && i < lastTransformGroupLength) {
|
||||
tween._renderTransforms = 1;
|
||||
if (tween._composition === consts.compositionTypes.blend) {
|
||||
helpers.forEachChildren(additive.additive.animation, (/** @type {Tween} */additiveTween) => {
|
||||
if (additiveTween.id === tween.id) {
|
||||
additiveTween._renderTransforms = 1;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
i++;
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (!targetsLength) {
|
||||
console.warn(`No target found. Make sure the element you're trying to animate is accessible before creating your animation.`);
|
||||
}
|
||||
|
||||
if (iterationDelay) {
|
||||
helpers.forEachChildren(this, (/** @type {Tween} */tween) => {
|
||||
// If (startTime - delay) equals 0, this means the tween is at the begining of the animation so we need to trim the delay too
|
||||
if (!(tween._startTime - tween._delay)) {
|
||||
tween._delay -= iterationDelay;
|
||||
}
|
||||
tween._startTime -= iterationDelay;
|
||||
});
|
||||
iterationDuration -= iterationDelay;
|
||||
} else {
|
||||
iterationDelay = 0;
|
||||
}
|
||||
|
||||
// Prevents iterationDuration to be NaN if no valid animatable props have been provided
|
||||
// Prevents _iterationCount to be NaN if no valid animatable props have been provided
|
||||
if (!iterationDuration) {
|
||||
iterationDuration = consts.minValue;
|
||||
this.iterationCount = 0;
|
||||
}
|
||||
/** @type {TargetsArray} */
|
||||
this.targets = parsedTargets;
|
||||
/** @type {String|Number} */
|
||||
this.id = !helpers.isUnd(id) ? id : JSAnimationId;
|
||||
/** @type {Number} */
|
||||
this.duration = iterationDuration === consts.minValue ? consts.minValue : helpers.clampInfinity(((iterationDuration + this._loopDelay) * this.iterationCount) - this._loopDelay) || consts.minValue;
|
||||
/** @type {Callback<this>} */
|
||||
this.onRender = onRender || animDefaults.onRender;
|
||||
/** @type {EasingFunction} */
|
||||
this._ease = parsedAnimPlaybackEase;
|
||||
/** @type {Number} */
|
||||
this._delay = iterationDelay;
|
||||
// NOTE: I'm keeping delay values separated from offsets in timelines because delays can override previous tweens and it could be confusing to debug a timeline with overridden tweens and no associated visible delays.
|
||||
// this._delay = parent ? 0 : iterationDelay;
|
||||
// this._offset += parent ? iterationDelay : 0;
|
||||
/** @type {Number} */
|
||||
this.iterationDuration = iterationDuration;
|
||||
|
||||
if (!this._autoplay && shouldTriggerRender) this.onRender(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Number} newDuration
|
||||
* @return {this}
|
||||
*/
|
||||
stretch(newDuration) {
|
||||
const currentDuration = this.duration;
|
||||
if (currentDuration === helpers.normalizeTime(newDuration)) return this;
|
||||
const timeScale = newDuration / currentDuration;
|
||||
// NOTE: Find a better way to handle the stretch of an animation after stretch = 0
|
||||
helpers.forEachChildren(this, (/** @type {Tween} */tween) => {
|
||||
// Rounding is necessary here to minimize floating point errors
|
||||
tween._updateDuration = helpers.normalizeTime(tween._updateDuration * timeScale);
|
||||
tween._changeDuration = helpers.normalizeTime(tween._changeDuration * timeScale);
|
||||
tween._currentTime *= timeScale;
|
||||
tween._delay *= timeScale;
|
||||
tween._startTime *= timeScale;
|
||||
tween._absoluteStartTime *= timeScale;
|
||||
tween._absoluteUpdateStartTime *= timeScale;
|
||||
tween._absoluteEndTime *= timeScale;
|
||||
});
|
||||
return super.stretch(newDuration);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {this}
|
||||
*/
|
||||
refresh() {
|
||||
helpers.forEachChildren(this, (/** @type {Tween} */tween) => {
|
||||
const toFunc = tween._toFunc;
|
||||
const fromFunc = tween._fromFunc;
|
||||
if (toFunc || fromFunc) {
|
||||
if (fromFunc) {
|
||||
values.decomposeRawValue(fromFunc(), fromTargetObject);
|
||||
if (fromTargetObject.u !== tween._unit && tween.target[consts.isDomSymbol]) {
|
||||
units.convertValueUnit(/** @type {DOMTarget} */(tween.target), fromTargetObject, tween._unit, true);
|
||||
}
|
||||
tween._fromNumbers = helpers.cloneArray(fromTargetObject.d);
|
||||
tween._fromNumber = fromTargetObject.n;
|
||||
} else if (toFunc) {
|
||||
// When only toFunc exists, get from value from target
|
||||
values.decomposeRawValue(values.getOriginalAnimatableValue(tween.target, tween.property, tween._tweenType), values.decomposedOriginalValue);
|
||||
tween._fromNumbers = helpers.cloneArray(values.decomposedOriginalValue.d);
|
||||
tween._fromNumber = values.decomposedOriginalValue.n;
|
||||
}
|
||||
if (toFunc) {
|
||||
values.decomposeRawValue(toFunc(), toTargetObject);
|
||||
tween._toNumbers = helpers.cloneArray(toTargetObject.d);
|
||||
tween._strings = helpers.cloneArray(toTargetObject.s);
|
||||
// Make sure to apply relative operators https://github.com/juliangarnier/anime/issues/1025
|
||||
tween._toNumber = toTargetObject.o ? values.getRelativeValue(tween._fromNumber, toTargetObject.n, toTargetObject.o) : toTargetObject.n;
|
||||
}
|
||||
}
|
||||
});
|
||||
// This forces setter animations to render once
|
||||
if (this.duration === consts.minValue) this.restart();
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancel the animation and revert all the values affected by this animation to their original state
|
||||
* @return {this}
|
||||
*/
|
||||
revert() {
|
||||
super.revert();
|
||||
return styles.revertValues(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* @typedef {this & {then: null}} ResolvedJSAnimation
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {Callback<ResolvedJSAnimation>} [callback]
|
||||
* @return Promise<this>
|
||||
*/
|
||||
then(callback) {
|
||||
return super.then(callback);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {TargetsParam} targets
|
||||
* @param {AnimationParams} parameters
|
||||
* @return {JSAnimation}
|
||||
*/
|
||||
const animate = (targets, parameters) => {
|
||||
if (globals.globals.editor) {
|
||||
return globals.globals.editor.addAnimation(targets, parameters);
|
||||
} else {
|
||||
return new JSAnimation(targets, parameters, null, 0, false).init();
|
||||
}
|
||||
};
|
||||
|
||||
exports.JSAnimation = JSAnimation;
|
||||
exports.animate = animate;
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
export class JSAnimation extends Timer {
|
||||
/**
|
||||
* @param {TargetsParam} targets
|
||||
* @param {AnimationParams} parameters
|
||||
* @param {Timeline} [parent]
|
||||
* @param {Number} [parentPosition]
|
||||
* @param {Boolean} [fastSet=false]
|
||||
* @param {Number} [index=0]
|
||||
* @param {TargetsArray} [allTargets]
|
||||
*/
|
||||
constructor(targets: TargetsParam, parameters: AnimationParams, parent?: Timeline, parentPosition?: number, fastSet?: boolean, index?: number, allTargets?: TargetsArray);
|
||||
/** @type {Tween} */
|
||||
_head: Tween;
|
||||
/** @type {Tween} */
|
||||
_tail: Tween;
|
||||
/** @type {TargetsArray} */
|
||||
targets: TargetsArray;
|
||||
/** @type {Callback<this>} */
|
||||
onRender: Callback<this>;
|
||||
/** @type {EasingFunction} */
|
||||
_ease: EasingFunction;
|
||||
/**
|
||||
* @param {Number} newDuration
|
||||
* @return {this}
|
||||
*/
|
||||
stretch(newDuration: number): this;
|
||||
/**
|
||||
* @return {this}
|
||||
*/
|
||||
refresh(): this;
|
||||
/**
|
||||
* Cancel the animation and revert all the values affected by this animation to their original state
|
||||
* @return {this}
|
||||
*/
|
||||
revert(): this;
|
||||
/**
|
||||
* @typedef {this & {then: null}} ResolvedJSAnimation
|
||||
*/
|
||||
/**
|
||||
* @param {Callback<ResolvedJSAnimation>} [callback]
|
||||
* @return Promise<this>
|
||||
*/
|
||||
then(callback?: Callback<this & {
|
||||
then: null;
|
||||
}>): Promise<any>;
|
||||
}
|
||||
export function animate(targets: TargetsParam, parameters: AnimationParams): JSAnimation;
|
||||
import { Timer } from '../timer/timer.js';
|
||||
import type { Tween } from '../types/index.js';
|
||||
import type { TargetsArray } from '../types/index.js';
|
||||
import type { Callback } from '../types/index.js';
|
||||
import type { EasingFunction } from '../types/index.js';
|
||||
import type { TargetsParam } from '../types/index.js';
|
||||
import type { AnimationParams } from '../types/index.js';
|
||||
import type { Timeline } from '../timeline/timeline.js';
|
||||
+743
@@ -0,0 +1,743 @@
|
||||
/**
|
||||
* Anime.js - animation - ESM
|
||||
* @version v4.5.0
|
||||
* @license MIT
|
||||
* @copyright 2026 - Julian Garnier
|
||||
*/
|
||||
|
||||
import { K, compositionTypes, valueTypes, emptyArray, minValue, isDomSymbol, tweenTypes } from '../core/consts.js';
|
||||
import { mergeObjects, isUnd, isKey, isObj, cloneArray, isNil, round, addChild, forEachChildren, clampInfinity, normalizeTime, isArr, isNum, isFnc, isStr } from '../core/helpers.js';
|
||||
import { globals } from '../core/globals.js';
|
||||
import { registerTargets } from '../core/targets.js';
|
||||
import { resolveAdapterEntry } from '../adapters/registry.js';
|
||||
import { setValue, getTweenType, getFunctionValue, decomposeRawValue, getOriginalAnimatableValue, createDecomposedValueTargetObject, decomposedOriginalValue, getRelativeValue, composeComplexValue, decomposeTweenValue } from '../core/values.js';
|
||||
import { sanitizePropertyName, revertValues } from '../core/styles.js';
|
||||
import { convertValueUnit } from '../core/units.js';
|
||||
import { parseEase } from '../easings/eases/parser.js';
|
||||
import { Timer } from '../timer/timer.js';
|
||||
import { getTweenSiblings, overrideTween, composeTween } from './composition.js';
|
||||
import { additive } from './additive.js';
|
||||
|
||||
/**
|
||||
* @import {
|
||||
* Tween,
|
||||
* TweenKeyValue,
|
||||
* TweenParamsOptions,
|
||||
* TweenValues,
|
||||
* DurationKeyframes,
|
||||
* PercentageKeyframes,
|
||||
* AnimationParams,
|
||||
* TweenPropValue,
|
||||
* ArraySyntaxValue,
|
||||
* TargetsParam,
|
||||
* TimerParams,
|
||||
* TweenParamValue,
|
||||
* DOMTarget,
|
||||
* TargetsArray,
|
||||
* Callback,
|
||||
* EasingFunction,
|
||||
* } from '../types/index.js'
|
||||
*
|
||||
* @import {
|
||||
* Timeline,
|
||||
* } from '../timeline/timeline.js'
|
||||
*
|
||||
* @import {
|
||||
* Spring,
|
||||
* } from '../easings/spring/index.js'
|
||||
*/
|
||||
|
||||
// Defines decomposed values target objects only once and mutate their properties later to avoid GC
|
||||
// TODO: Maybe move the objects creation to values.js and use the decompose function to create the base object
|
||||
const fromTargetObject = createDecomposedValueTargetObject();
|
||||
const toTargetObject = createDecomposedValueTargetObject();
|
||||
const inlineStylesStore = {};
|
||||
const toFunctionStore = { func: null };
|
||||
const fromFunctionStore = { func: null };
|
||||
const keyframesTargetArray = [null];
|
||||
const fastSetValuesArray = [null, null];
|
||||
/** @type {TweenKeyValue} */
|
||||
const keyObjectTarget = { to: null };
|
||||
|
||||
let tweenId = 0;
|
||||
let JSAnimationId = 0;
|
||||
let keyframes;
|
||||
/** @type {TweenParamsOptions & TweenValues} */
|
||||
let key;
|
||||
|
||||
/**
|
||||
* @param {DurationKeyframes | PercentageKeyframes} keyframes
|
||||
* @param {AnimationParams} parameters
|
||||
* @return {AnimationParams}
|
||||
*/
|
||||
const generateKeyframes = (keyframes, parameters) => {
|
||||
/** @type {AnimationParams} */
|
||||
const properties = {};
|
||||
if (isArr(keyframes)) {
|
||||
const propertyNames = [].concat(.../** @type {DurationKeyframes} */(keyframes).map(key => Object.keys(key))).filter(isKey);
|
||||
for (let i = 0, l = propertyNames.length; i < l; i++) {
|
||||
const propName = propertyNames[i];
|
||||
const propArray = /** @type {DurationKeyframes} */(keyframes).map(key => {
|
||||
/** @type {TweenKeyValue} */
|
||||
const newKey = {};
|
||||
for (let p in key) {
|
||||
const keyValue = /** @type {TweenPropValue} */(key[p]);
|
||||
if (isKey(p)) {
|
||||
if (p === propName) {
|
||||
newKey.to = keyValue;
|
||||
}
|
||||
} else {
|
||||
newKey[p] = keyValue;
|
||||
}
|
||||
}
|
||||
return newKey;
|
||||
});
|
||||
properties[propName] = /** @type {ArraySyntaxValue} */(propArray);
|
||||
}
|
||||
|
||||
} else {
|
||||
const totalDuration = /** @type {Number} */(setValue(parameters.duration, globals.defaults.duration));
|
||||
const keys = Object.keys(keyframes)
|
||||
.map(key => { return {o: parseFloat(key) / 100, p: keyframes[key]} })
|
||||
.sort((a, b) => a.o - b.o);
|
||||
keys.forEach(key => {
|
||||
const offset = key.o;
|
||||
const prop = key.p;
|
||||
for (let name in prop) {
|
||||
if (isKey(name)) {
|
||||
let propArray = /** @type {Array} */(properties[name]);
|
||||
if (!propArray) propArray = properties[name] = [];
|
||||
const duration = offset * totalDuration;
|
||||
let length = propArray.length;
|
||||
let prevKey = propArray[length - 1];
|
||||
const keyObj = { to: prop[name] };
|
||||
let durProgress = 0;
|
||||
for (let i = 0; i < length; i++) {
|
||||
durProgress += propArray[i].duration;
|
||||
}
|
||||
if (length === 1) {
|
||||
keyObj.from = prevKey.to;
|
||||
}
|
||||
if (prop.ease) {
|
||||
keyObj.ease = prop.ease;
|
||||
}
|
||||
keyObj.duration = duration - (length ? durProgress : 0);
|
||||
propArray.push(keyObj);
|
||||
}
|
||||
}
|
||||
return key;
|
||||
});
|
||||
|
||||
for (let name in properties) {
|
||||
const propArray = /** @type {Array} */(properties[name]);
|
||||
let prevEase;
|
||||
// let durProgress = 0
|
||||
for (let i = 0, l = propArray.length; i < l; i++) {
|
||||
const prop = propArray[i];
|
||||
// Emulate WAPPI easing parameter position
|
||||
const currentEase = prop.ease;
|
||||
prop.ease = prevEase ? prevEase : undefined;
|
||||
prevEase = currentEase;
|
||||
// durProgress += prop.duration;
|
||||
// if (i === l - 1 && durProgress !== totalDuration) {
|
||||
// propArray.push({ from: prop.to, ease: prop.ease, duration: totalDuration - durProgress })
|
||||
// }
|
||||
}
|
||||
if (!propArray[0].duration) {
|
||||
propArray.shift();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return properties;
|
||||
};
|
||||
|
||||
class JSAnimation extends Timer {
|
||||
/**
|
||||
* @param {TargetsParam} targets
|
||||
* @param {AnimationParams} parameters
|
||||
* @param {Timeline} [parent]
|
||||
* @param {Number} [parentPosition]
|
||||
* @param {Boolean} [fastSet=false]
|
||||
* @param {Number} [index=0]
|
||||
* @param {TargetsArray} [allTargets]
|
||||
*/
|
||||
constructor(
|
||||
targets,
|
||||
parameters,
|
||||
parent,
|
||||
parentPosition,
|
||||
fastSet = false,
|
||||
index = 0,
|
||||
allTargets
|
||||
) {
|
||||
|
||||
super(/** @type {TimerParams & AnimationParams} */(parameters), parent, parentPosition);
|
||||
|
||||
/** @type {Tween} */
|
||||
this._head;
|
||||
/** @type {Tween} */
|
||||
this._tail;
|
||||
|
||||
++JSAnimationId;
|
||||
|
||||
const parsedTargets = registerTargets(targets);
|
||||
const targetsLength = parsedTargets.length;
|
||||
|
||||
// If the parameters object contains a "keyframes" property, convert all the keyframes values to regular properties
|
||||
|
||||
const kfParams = /** @type {AnimationParams} */(parameters).keyframes;
|
||||
const params = /** @type {AnimationParams} */(kfParams ? mergeObjects(generateKeyframes(/** @type {DurationKeyframes} */(kfParams), parameters), parameters) : parameters);
|
||||
|
||||
const {
|
||||
id,
|
||||
delay,
|
||||
duration,
|
||||
ease,
|
||||
playbackEase,
|
||||
modifier,
|
||||
composition,
|
||||
onRender,
|
||||
} = params;
|
||||
|
||||
const animDefaults = parent ? parent.defaults : globals.defaults;
|
||||
const animEase = setValue(ease, animDefaults.ease);
|
||||
const animPlaybackEase = setValue(playbackEase, animDefaults.playbackEase);
|
||||
const parsedAnimPlaybackEase = animPlaybackEase ? parseEase(animPlaybackEase) : null;
|
||||
const hasSpring = !isUnd(/** @type {Spring} */(animEase).ease);
|
||||
const tEasing = hasSpring ? /** @type {Spring} */(animEase).ease : setValue(ease, parsedAnimPlaybackEase ? 'linear' : animDefaults.ease);
|
||||
const tDuration = hasSpring ? /** @type {Spring} */(animEase).settlingDuration : setValue(duration, animDefaults.duration);
|
||||
const tDelay = setValue(delay, animDefaults.delay);
|
||||
const tModifier = modifier || animDefaults.modifier;
|
||||
// If no composition is defined and the targets length is high (>= 1000) set the composition to 'none' (0) for faster tween creation
|
||||
const tComposition = isUnd(composition) && targetsLength >= K ? compositionTypes.none : !isUnd(composition) ? composition : animDefaults.composition;
|
||||
// const absoluteOffsetTime = this._offset;
|
||||
const absoluteOffsetTime = this._offset + (parent ? parent._offset : 0);
|
||||
// This allows targeting the current animation in the spring onComplete callback
|
||||
if (hasSpring) /** @type {Spring} */(animEase).parent = this;
|
||||
|
||||
let iterationDuration = NaN;
|
||||
let iterationDelay = NaN;
|
||||
let animationAnimationLength = 0;
|
||||
let shouldTriggerRender = 0;
|
||||
|
||||
for (let targetIndex = 0; targetIndex < targetsLength; targetIndex++) {
|
||||
|
||||
const target = parsedTargets[targetIndex];
|
||||
const ti = index || targetIndex;
|
||||
const tl = allTargets || parsedTargets;
|
||||
|
||||
let lastTransformGroupIndex = NaN;
|
||||
let lastTransformGroupLength = NaN;
|
||||
|
||||
for (let p in params) {
|
||||
|
||||
if (isKey(p)) {
|
||||
|
||||
const tweenType = getTweenType(target, p);
|
||||
const adapterProp = resolveAdapterEntry(target, p);
|
||||
|
||||
const propName = sanitizePropertyName(p, target, tweenType);
|
||||
|
||||
let propValue = params[p];
|
||||
|
||||
const isPropValueArray = isArr(propValue);
|
||||
|
||||
if (fastSet && !isPropValueArray) {
|
||||
fastSetValuesArray[0] = propValue;
|
||||
fastSetValuesArray[1] = propValue;
|
||||
propValue = fastSetValuesArray;
|
||||
}
|
||||
|
||||
// TODO: Allow nested keyframes inside ObjectValue value (prop: { to: [.5, 1, .75, 2, 3] })
|
||||
// Normalize property values to valid keyframe syntax:
|
||||
// [x, y] to [{to: [x, y]}] or {to: x} to [{to: x}] or keep keys syntax [{}, {}, {}...]
|
||||
// const keyframes = isArr(propValue) ? propValue.length === 2 && !isObj(propValue[0]) ? [{ to: propValue }] : propValue : [propValue];
|
||||
if (isPropValueArray) {
|
||||
const arrayLength = /** @type {Array} */(propValue).length;
|
||||
const isNotObjectValue = !isObj(propValue[0]);
|
||||
// Convert [x, y] to [{to: [x, y]}]
|
||||
if (arrayLength === 2 && isNotObjectValue) {
|
||||
keyObjectTarget.to = /** @type {TweenParamValue} */(/** @type {unknown} */(propValue));
|
||||
keyframesTargetArray[0] = keyObjectTarget;
|
||||
keyframes = keyframesTargetArray;
|
||||
// Convert [x, y, z] to [[x, y], z]
|
||||
} else if (arrayLength > 2 && isNotObjectValue) {
|
||||
keyframes = [];
|
||||
/** @type {Array.<Number>} */(propValue).forEach((v, i) => {
|
||||
if (!i) {
|
||||
fastSetValuesArray[0] = v;
|
||||
} else if (i === 1) {
|
||||
fastSetValuesArray[1] = v;
|
||||
keyframes.push(fastSetValuesArray);
|
||||
} else {
|
||||
keyframes.push(v);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
keyframes = /** @type {Array.<TweenKeyValue>} */(propValue);
|
||||
}
|
||||
} else {
|
||||
keyframesTargetArray[0] = propValue;
|
||||
keyframes = keyframesTargetArray;
|
||||
}
|
||||
|
||||
let siblings = null;
|
||||
let prevTween = null;
|
||||
let firstTweenChangeStartTime = NaN;
|
||||
let lastTweenChangeEndTime = 0;
|
||||
let tweenIndex = 0;
|
||||
|
||||
for (let l = keyframes.length; tweenIndex < l; tweenIndex++) {
|
||||
|
||||
const keyframe = keyframes[tweenIndex];
|
||||
|
||||
if (isObj(keyframe)) {
|
||||
key = keyframe;
|
||||
} else {
|
||||
keyObjectTarget.to = /** @type {TweenParamValue} */(keyframe);
|
||||
key = keyObjectTarget;
|
||||
}
|
||||
|
||||
toFunctionStore.func = null;
|
||||
fromFunctionStore.func = null;
|
||||
|
||||
const computedComposition = getFunctionValue(setValue(key.composition, tComposition), target, ti, tl, null, null);
|
||||
const tweenComposition = isNum(computedComposition) ? computedComposition : compositionTypes[computedComposition];
|
||||
if (!siblings && tweenComposition !== compositionTypes.none) siblings = getTweenSiblings(target, propName);
|
||||
// Timelines pass the last sibling tween if it belongs to the same timeline
|
||||
// Standalone animations only pass prevTween when the property has multiple keyframes
|
||||
const tailTween = siblings ? siblings._tail : null;
|
||||
const prevSiblingTween = parent && tailTween && tailTween.parent.parent === parent ? tailTween : prevTween;
|
||||
const computedToValue = getFunctionValue(key.to, target, ti, tl, toFunctionStore, prevSiblingTween);
|
||||
|
||||
let tweenToValue;
|
||||
// Allows function based values to return an object syntax value ({to: v})
|
||||
if (isObj(computedToValue) && !isUnd(computedToValue.to)) {
|
||||
key = computedToValue;
|
||||
tweenToValue = computedToValue.to;
|
||||
} else {
|
||||
tweenToValue = computedToValue;
|
||||
}
|
||||
const tweenFromValue = getFunctionValue(key.from, target, ti, tl, fromFunctionStore, prevSiblingTween);
|
||||
const easeToParse = key.ease || tEasing;
|
||||
|
||||
const easeFunctionResult = getFunctionValue(easeToParse, target, ti, tl, null, prevSiblingTween);
|
||||
const keyEasing = isFnc(easeFunctionResult) || isStr(easeFunctionResult) ? easeFunctionResult : easeToParse;
|
||||
|
||||
const hasSpring = !isUnd(keyEasing) && !isUnd(/** @type {Spring} */(keyEasing).ease);
|
||||
const tweenEasing = hasSpring ? /** @type {Spring} */(keyEasing).ease : keyEasing;
|
||||
// Calculate default individual keyframe duration by dividing the tl of keyframes
|
||||
const tweenDuration = hasSpring ? /** @type {Spring} */(keyEasing).settlingDuration : getFunctionValue(setValue(key.duration, (l > 1 ? getFunctionValue(tDuration, target, ti, tl, null, prevSiblingTween) / l : tDuration)), target, ti, tl, null, prevSiblingTween);
|
||||
// Default delay value should only be applied to the first tween
|
||||
const tweenDelay = getFunctionValue(setValue(key.delay, (!tweenIndex ? tDelay : 0)), target, ti, tl, null, prevSiblingTween);
|
||||
// Modifiers are treated differently and don't accept function based value to prevent having to pass a function wrapper
|
||||
const tweenModifier = key.modifier || tModifier;
|
||||
const hasFromvalue = !isUnd(tweenFromValue);
|
||||
const hasToValue = !isUnd(tweenToValue);
|
||||
const isFromToArray = isArr(tweenToValue);
|
||||
const isFromToValue = isFromToArray || (hasFromvalue && hasToValue);
|
||||
// Capture the update-start in local time, the previous sibling's end for keyframes after the first, zero for the first keyframe. Used to derive a precision-matched _absoluteUpdateStartTime below.
|
||||
const tweenUpdateStartLocal = prevTween ? lastTweenChangeEndTime : 0;
|
||||
const tweenStartTime = prevTween ? lastTweenChangeEndTime + tweenDelay : tweenDelay;
|
||||
// Rounding is necessary here to minimize floating point errors when working in seconds
|
||||
const absoluteStartTime = round(absoluteOffsetTime + tweenStartTime, 12);
|
||||
// Match the rounding pattern of prevSibling._absoluteEndTime so the composition overlap check compares cleanly when keyframes touch at a boundary.
|
||||
const absoluteUpdateStartTime = round(absoluteOffsetTime + tweenUpdateStartLocal, 12);
|
||||
|
||||
// Force a onRender callback if the animation contains at least one from value and autoplay is set to false
|
||||
if (!shouldTriggerRender && (hasFromvalue || isFromToArray)) shouldTriggerRender = 1;
|
||||
|
||||
let prevSibling = prevTween;
|
||||
|
||||
if (tweenComposition !== compositionTypes.none) {
|
||||
let nextSibling = siblings._head;
|
||||
// Walk prior siblings up to the new tween, skipping overridden ones so the chain resolves to the latest live value instead of stopping at the first override.
|
||||
while (nextSibling && nextSibling._absoluteStartTime <= absoluteStartTime) {
|
||||
if (!nextSibling._isOverridden) prevSibling = nextSibling;
|
||||
nextSibling = nextSibling._nextRep;
|
||||
// Overrides all the next siblings if the next sibling starts at the same time of after as the new tween start time
|
||||
if (nextSibling && nextSibling._absoluteStartTime >= absoluteStartTime) {
|
||||
while (nextSibling) {
|
||||
overrideTween(nextSibling);
|
||||
// This will ends both the current while loop and the upper one once all the next sibllings have been overriden
|
||||
nextSibling = nextSibling._nextRep;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Decompose values
|
||||
if (isFromToValue) {
|
||||
decomposeRawValue(isFromToArray ? getFunctionValue(tweenToValue[0], target, ti, tl, fromFunctionStore, prevSiblingTween) : tweenFromValue, fromTargetObject);
|
||||
decomposeRawValue(isFromToArray ? getFunctionValue(tweenToValue[1], target, ti, tl, toFunctionStore, prevSiblingTween) : tweenToValue, toTargetObject);
|
||||
// Needed to force an inline style registration
|
||||
const originalValue = getOriginalAnimatableValue(target, propName, tweenType, inlineStylesStore);
|
||||
if (fromTargetObject.t === valueTypes.NUMBER) {
|
||||
if (prevSibling) {
|
||||
if (prevSibling._valueType === valueTypes.UNIT) {
|
||||
fromTargetObject.t = valueTypes.UNIT;
|
||||
fromTargetObject.u = prevSibling._unit;
|
||||
}
|
||||
} else {
|
||||
decomposeRawValue(
|
||||
originalValue,
|
||||
decomposedOriginalValue
|
||||
);
|
||||
if (decomposedOriginalValue.t === valueTypes.UNIT) {
|
||||
fromTargetObject.t = valueTypes.UNIT;
|
||||
fromTargetObject.u = decomposedOriginalValue.u;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (hasToValue) {
|
||||
decomposeRawValue(tweenToValue, toTargetObject);
|
||||
} else {
|
||||
if (prevTween) {
|
||||
decomposeTweenValue(prevTween, toTargetObject);
|
||||
} else {
|
||||
// No need to get and parse the original value if the tween is part of a timeline and has a previous sibling part of the same timeline
|
||||
decomposeRawValue(parent && prevSibling && prevSibling.parent.parent === parent ? prevSibling._value :
|
||||
getOriginalAnimatableValue(target, propName, tweenType, inlineStylesStore), toTargetObject);
|
||||
}
|
||||
}
|
||||
if (hasFromvalue) {
|
||||
decomposeRawValue(tweenFromValue, fromTargetObject);
|
||||
} else {
|
||||
if (prevTween) {
|
||||
decomposeTweenValue(prevTween, fromTargetObject);
|
||||
} else {
|
||||
// No need to get and parse the original value if the tween is part of a timeline and has a previous sibling part of the same timeline
|
||||
decomposeRawValue(parent && prevSibling && prevSibling.parent.parent === parent ? prevSibling._value :
|
||||
getOriginalAnimatableValue(target, propName, tweenType, inlineStylesStore), fromTargetObject);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Apply operators
|
||||
if (fromTargetObject.o) {
|
||||
fromTargetObject.n = getRelativeValue(
|
||||
!prevSibling ? decomposeRawValue(
|
||||
getOriginalAnimatableValue(target, propName, tweenType, inlineStylesStore),
|
||||
decomposedOriginalValue
|
||||
).n : prevSibling._toNumber,
|
||||
fromTargetObject.n,
|
||||
fromTargetObject.o
|
||||
);
|
||||
}
|
||||
|
||||
if (toTargetObject.o) {
|
||||
toTargetObject.n = getRelativeValue(fromTargetObject.n, toTargetObject.n, toTargetObject.o);
|
||||
}
|
||||
|
||||
// Values omogenisation in cases of type difference between "from" and "to"
|
||||
if (fromTargetObject.t !== toTargetObject.t) {
|
||||
if (fromTargetObject.t === valueTypes.COMPLEX || toTargetObject.t === valueTypes.COMPLEX) {
|
||||
const complexValue = fromTargetObject.t === valueTypes.COMPLEX ? fromTargetObject : toTargetObject;
|
||||
const notComplexValue = fromTargetObject.t === valueTypes.COMPLEX ? toTargetObject : fromTargetObject;
|
||||
notComplexValue.t = valueTypes.COMPLEX;
|
||||
notComplexValue.s = cloneArray(complexValue.s);
|
||||
notComplexValue.d = complexValue.d.map(() => notComplexValue.n);
|
||||
} else if (fromTargetObject.t === valueTypes.UNIT || toTargetObject.t === valueTypes.UNIT) {
|
||||
const unitValue = fromTargetObject.t === valueTypes.UNIT ? fromTargetObject : toTargetObject;
|
||||
const notUnitValue = fromTargetObject.t === valueTypes.UNIT ? toTargetObject : fromTargetObject;
|
||||
notUnitValue.t = valueTypes.UNIT;
|
||||
notUnitValue.u = unitValue.u;
|
||||
} else if (fromTargetObject.t === valueTypes.COLOR || toTargetObject.t === valueTypes.COLOR) {
|
||||
const colorValue = fromTargetObject.t === valueTypes.COLOR ? fromTargetObject : toTargetObject;
|
||||
const notColorValue = fromTargetObject.t === valueTypes.COLOR ? toTargetObject : fromTargetObject;
|
||||
notColorValue.t = valueTypes.COLOR;
|
||||
notColorValue.d = colorValue.d.map(() => 0);
|
||||
}
|
||||
}
|
||||
|
||||
// Unit conversion
|
||||
if (fromTargetObject.u !== toTargetObject.u) {
|
||||
let valueToConvert = toTargetObject.u ? fromTargetObject : toTargetObject;
|
||||
valueToConvert = convertValueUnit(/** @type {DOMTarget} */(target), valueToConvert, toTargetObject.u ? toTargetObject.u : fromTargetObject.u, false);
|
||||
// TODO:
|
||||
// convertValueUnit(target, to.u ? from : to, to.u ? to.u : from.u);
|
||||
}
|
||||
|
||||
// Fill in non existing complex values
|
||||
if (toTargetObject.d && fromTargetObject.d && (toTargetObject.d.length !== fromTargetObject.d.length)) {
|
||||
const longestValue = fromTargetObject.d.length > toTargetObject.d.length ? fromTargetObject : toTargetObject;
|
||||
const shortestValue = longestValue === fromTargetObject ? toTargetObject : fromTargetObject;
|
||||
// TODO: Check if n should be used instead of 0 for default complex values
|
||||
shortestValue.d = longestValue.d.map((/** @type {Number} */_, /** @type {Number} */i) => isUnd(shortestValue.d[i]) ? 0 : shortestValue.d[i]);
|
||||
shortestValue.s = cloneArray(longestValue.s);
|
||||
}
|
||||
|
||||
// Tween factory
|
||||
|
||||
// Rounding is necessary here to minimize floating point errors when working in seconds
|
||||
const tweenUpdateDuration = round(+tweenDuration || minValue, 12);
|
||||
|
||||
// Copy the value of the iniline style if it exist and imediatly nullify it to prevents false positive on other targets
|
||||
let inlineValue = inlineStylesStore[propName];
|
||||
if (!isNil(inlineValue)) inlineStylesStore[propName] = null;
|
||||
|
||||
// Resolve the adapter setter once so render skips the lookup per frame.
|
||||
const tweenSetter = adapterProp ? adapterProp.set : null;
|
||||
|
||||
// Rounding is necessary here to minimize floating point errors when working in seconds
|
||||
lastTweenChangeEndTime = round(tweenStartTime + tweenUpdateDuration, 12);
|
||||
|
||||
const fromD = fromTargetObject.d;
|
||||
const toD = toTargetObject.d;
|
||||
const toS = toTargetObject.s;
|
||||
|
||||
/** @type {Tween} */
|
||||
const tween = {
|
||||
parent: this,
|
||||
id: tweenId++,
|
||||
property: propName,
|
||||
target: target,
|
||||
_value: null,
|
||||
_toFunc: toFunctionStore.func,
|
||||
_fromFunc: fromFunctionStore.func,
|
||||
_ease: parseEase(tweenEasing),
|
||||
_fromNumbers: fromD ? cloneArray(fromD) : emptyArray,
|
||||
_toNumbers: toD ? cloneArray(toD) : emptyArray,
|
||||
_strings: toS ? cloneArray(toS) : emptyArray,
|
||||
_fromNumber: fromTargetObject.n,
|
||||
_toNumber: toTargetObject.n,
|
||||
_numbers: fromD ? cloneArray(fromD) : emptyArray, // For additive tween and animatables
|
||||
_number: fromTargetObject.n, // For additive tween and animatables
|
||||
_unit: toTargetObject.u,
|
||||
_modifier: tweenModifier,
|
||||
_currentTime: 0,
|
||||
_startTime: tweenStartTime,
|
||||
_delay: +tweenDelay,
|
||||
_updateDuration: tweenUpdateDuration,
|
||||
_changeDuration: tweenUpdateDuration,
|
||||
_absoluteStartTime: absoluteStartTime,
|
||||
_absoluteUpdateStartTime: absoluteUpdateStartTime,
|
||||
_absoluteEndTime: round(absoluteOffsetTime + lastTweenChangeEndTime, 12),
|
||||
_hasFromValue: hasFromvalue || isFromToArray ? 1 : 0,
|
||||
// NOTE: Investigate bit packing to stores ENUM / BOOL
|
||||
_tweenType: tweenType,
|
||||
_setter: tweenSetter,
|
||||
_valueType: toTargetObject.t,
|
||||
_composition: tweenComposition,
|
||||
_isOverlapped: 0,
|
||||
_isOverridden: 0,
|
||||
_renderTransforms: 0,
|
||||
_inlineValue: inlineValue,
|
||||
_prevRep: null, // For replaced tween
|
||||
_nextRep: null, // For replaced tween
|
||||
_prevAdd: null, // For additive tween
|
||||
_nextAdd: null, // For additive tween
|
||||
_prev: null,
|
||||
_next: null,
|
||||
};
|
||||
|
||||
if (tweenComposition !== compositionTypes.none) {
|
||||
composeTween(tween, siblings);
|
||||
}
|
||||
|
||||
// Pre-compute the tween end value for function-based value chaining (ie morphTo / scrambleText in keyframe arrays and timelines)
|
||||
const vt = tween._valueType;
|
||||
if (vt === valueTypes.COMPLEX) {
|
||||
tween._value = composeComplexValue(tween, 1, -1);
|
||||
} else if (vt === valueTypes.UNIT) {
|
||||
tween._value = `${tweenModifier(tween._toNumber)}${tween._unit}`;
|
||||
} else if (vt === valueTypes.COLOR) {
|
||||
const d = toTargetObject.d;
|
||||
tween._value = `rgba(${round(d[0], 0)},${round(d[1], 0)},${round(d[2], 0)},${d[3]})`;
|
||||
} else {
|
||||
tween._value = tweenModifier(tween._toNumber);
|
||||
}
|
||||
|
||||
if (isNaN(firstTweenChangeStartTime)) {
|
||||
firstTweenChangeStartTime = tween._startTime;
|
||||
}
|
||||
|
||||
prevTween = tween;
|
||||
animationAnimationLength++;
|
||||
|
||||
addChild(this, tween);
|
||||
|
||||
}
|
||||
|
||||
// Update animation timings with the added tweens properties
|
||||
|
||||
if (isNaN(iterationDelay) || firstTweenChangeStartTime < iterationDelay) {
|
||||
iterationDelay = firstTweenChangeStartTime;
|
||||
}
|
||||
|
||||
if (isNaN(iterationDuration) || lastTweenChangeEndTime > iterationDuration) {
|
||||
iterationDuration = lastTweenChangeEndTime;
|
||||
}
|
||||
|
||||
// TODO: Find a way to inline tween._renderTransforms = 1 here
|
||||
if (tweenType === tweenTypes.TRANSFORM) {
|
||||
lastTransformGroupIndex = animationAnimationLength - tweenIndex;
|
||||
lastTransformGroupLength = animationAnimationLength;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Set _renderTransforms to last transform property to correctly render the transforms list
|
||||
if (!isNaN(lastTransformGroupIndex)) {
|
||||
let i = 0;
|
||||
forEachChildren(this, (/** @type {Tween} */tween) => {
|
||||
if (i >= lastTransformGroupIndex && i < lastTransformGroupLength) {
|
||||
tween._renderTransforms = 1;
|
||||
if (tween._composition === compositionTypes.blend) {
|
||||
forEachChildren(additive.animation, (/** @type {Tween} */additiveTween) => {
|
||||
if (additiveTween.id === tween.id) {
|
||||
additiveTween._renderTransforms = 1;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
i++;
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (!targetsLength) {
|
||||
console.warn(`No target found. Make sure the element you're trying to animate is accessible before creating your animation.`);
|
||||
}
|
||||
|
||||
if (iterationDelay) {
|
||||
forEachChildren(this, (/** @type {Tween} */tween) => {
|
||||
// If (startTime - delay) equals 0, this means the tween is at the begining of the animation so we need to trim the delay too
|
||||
if (!(tween._startTime - tween._delay)) {
|
||||
tween._delay -= iterationDelay;
|
||||
}
|
||||
tween._startTime -= iterationDelay;
|
||||
});
|
||||
iterationDuration -= iterationDelay;
|
||||
} else {
|
||||
iterationDelay = 0;
|
||||
}
|
||||
|
||||
// Prevents iterationDuration to be NaN if no valid animatable props have been provided
|
||||
// Prevents _iterationCount to be NaN if no valid animatable props have been provided
|
||||
if (!iterationDuration) {
|
||||
iterationDuration = minValue;
|
||||
this.iterationCount = 0;
|
||||
}
|
||||
/** @type {TargetsArray} */
|
||||
this.targets = parsedTargets;
|
||||
/** @type {String|Number} */
|
||||
this.id = !isUnd(id) ? id : JSAnimationId;
|
||||
/** @type {Number} */
|
||||
this.duration = iterationDuration === minValue ? minValue : clampInfinity(((iterationDuration + this._loopDelay) * this.iterationCount) - this._loopDelay) || minValue;
|
||||
/** @type {Callback<this>} */
|
||||
this.onRender = onRender || animDefaults.onRender;
|
||||
/** @type {EasingFunction} */
|
||||
this._ease = parsedAnimPlaybackEase;
|
||||
/** @type {Number} */
|
||||
this._delay = iterationDelay;
|
||||
// NOTE: I'm keeping delay values separated from offsets in timelines because delays can override previous tweens and it could be confusing to debug a timeline with overridden tweens and no associated visible delays.
|
||||
// this._delay = parent ? 0 : iterationDelay;
|
||||
// this._offset += parent ? iterationDelay : 0;
|
||||
/** @type {Number} */
|
||||
this.iterationDuration = iterationDuration;
|
||||
|
||||
if (!this._autoplay && shouldTriggerRender) this.onRender(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Number} newDuration
|
||||
* @return {this}
|
||||
*/
|
||||
stretch(newDuration) {
|
||||
const currentDuration = this.duration;
|
||||
if (currentDuration === normalizeTime(newDuration)) return this;
|
||||
const timeScale = newDuration / currentDuration;
|
||||
// NOTE: Find a better way to handle the stretch of an animation after stretch = 0
|
||||
forEachChildren(this, (/** @type {Tween} */tween) => {
|
||||
// Rounding is necessary here to minimize floating point errors
|
||||
tween._updateDuration = normalizeTime(tween._updateDuration * timeScale);
|
||||
tween._changeDuration = normalizeTime(tween._changeDuration * timeScale);
|
||||
tween._currentTime *= timeScale;
|
||||
tween._delay *= timeScale;
|
||||
tween._startTime *= timeScale;
|
||||
tween._absoluteStartTime *= timeScale;
|
||||
tween._absoluteUpdateStartTime *= timeScale;
|
||||
tween._absoluteEndTime *= timeScale;
|
||||
});
|
||||
return super.stretch(newDuration);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {this}
|
||||
*/
|
||||
refresh() {
|
||||
forEachChildren(this, (/** @type {Tween} */tween) => {
|
||||
const toFunc = tween._toFunc;
|
||||
const fromFunc = tween._fromFunc;
|
||||
if (toFunc || fromFunc) {
|
||||
if (fromFunc) {
|
||||
decomposeRawValue(fromFunc(), fromTargetObject);
|
||||
if (fromTargetObject.u !== tween._unit && tween.target[isDomSymbol]) {
|
||||
convertValueUnit(/** @type {DOMTarget} */(tween.target), fromTargetObject, tween._unit, true);
|
||||
}
|
||||
tween._fromNumbers = cloneArray(fromTargetObject.d);
|
||||
tween._fromNumber = fromTargetObject.n;
|
||||
} else if (toFunc) {
|
||||
// When only toFunc exists, get from value from target
|
||||
decomposeRawValue(getOriginalAnimatableValue(tween.target, tween.property, tween._tweenType), decomposedOriginalValue);
|
||||
tween._fromNumbers = cloneArray(decomposedOriginalValue.d);
|
||||
tween._fromNumber = decomposedOriginalValue.n;
|
||||
}
|
||||
if (toFunc) {
|
||||
decomposeRawValue(toFunc(), toTargetObject);
|
||||
tween._toNumbers = cloneArray(toTargetObject.d);
|
||||
tween._strings = cloneArray(toTargetObject.s);
|
||||
// Make sure to apply relative operators https://github.com/juliangarnier/anime/issues/1025
|
||||
tween._toNumber = toTargetObject.o ? getRelativeValue(tween._fromNumber, toTargetObject.n, toTargetObject.o) : toTargetObject.n;
|
||||
}
|
||||
}
|
||||
});
|
||||
// This forces setter animations to render once
|
||||
if (this.duration === minValue) this.restart();
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancel the animation and revert all the values affected by this animation to their original state
|
||||
* @return {this}
|
||||
*/
|
||||
revert() {
|
||||
super.revert();
|
||||
return revertValues(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* @typedef {this & {then: null}} ResolvedJSAnimation
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {Callback<ResolvedJSAnimation>} [callback]
|
||||
* @return Promise<this>
|
||||
*/
|
||||
then(callback) {
|
||||
return super.then(callback);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {TargetsParam} targets
|
||||
* @param {AnimationParams} parameters
|
||||
* @return {JSAnimation}
|
||||
*/
|
||||
const animate = (targets, parameters) => {
|
||||
if (globals.editor) {
|
||||
return globals.editor.addAnimation(targets, parameters);
|
||||
} else {
|
||||
return new JSAnimation(targets, parameters, null, 0, false).init();
|
||||
}
|
||||
};
|
||||
|
||||
export { JSAnimation, animate };
|
||||
+386
@@ -0,0 +1,386 @@
|
||||
/**
|
||||
* Anime.js - animation - CJS
|
||||
* @version v4.5.0
|
||||
* @license MIT
|
||||
* @copyright 2026 - Julian Garnier
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
var consts = require('../core/consts.cjs');
|
||||
var helpers = require('../core/helpers.cjs');
|
||||
var styles = require('../core/styles.cjs');
|
||||
var engine = require('../engine/engine.cjs');
|
||||
var additive = require('./additive.cjs');
|
||||
|
||||
/**
|
||||
* @import {
|
||||
* TweenReplaceLookups,
|
||||
* TweenAdditiveLookups,
|
||||
* TweenPropertySiblings,
|
||||
* Tween,
|
||||
* Target,
|
||||
* TargetsArray,
|
||||
* Renderable,
|
||||
* } from '../types/index.js'
|
||||
*
|
||||
* @import {
|
||||
* JSAnimation,
|
||||
* } from '../animation/animation.js'
|
||||
*/
|
||||
|
||||
const lookups = {
|
||||
/** @type {TweenReplaceLookups} */
|
||||
_rep: new WeakMap(),
|
||||
/** @type {TweenAdditiveLookups} */
|
||||
_add: new Map(),
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {Target} target
|
||||
* @param {String} property
|
||||
* @param {String} lookup
|
||||
* @return {TweenPropertySiblings}
|
||||
*/
|
||||
const getTweenSiblings = (target, property, lookup = '_rep') => {
|
||||
const lookupMap = lookups[lookup];
|
||||
let targetLookup = lookupMap.get(target);
|
||||
if (!targetLookup) {
|
||||
targetLookup = {};
|
||||
lookupMap.set(target, targetLookup);
|
||||
}
|
||||
return targetLookup[property] ? targetLookup[property] : targetLookup[property] = {
|
||||
_head: null,
|
||||
_tail: null,
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {Tween} p
|
||||
* @param {Tween} c
|
||||
* @return {Number|Boolean}
|
||||
*/
|
||||
const addTweenSortMethod = (p, c) => {
|
||||
return p._isOverridden || p._absoluteStartTime > c._absoluteStartTime;
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {Tween} tween
|
||||
*/
|
||||
const overrideTween = tween => {
|
||||
tween._isOverlapped = 1;
|
||||
tween._isOverridden = 1;
|
||||
tween._changeDuration = consts.minValue;
|
||||
tween._currentTime = consts.minValue;
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {Tween} tween
|
||||
* @param {TweenPropertySiblings} siblings
|
||||
* @return {Tween}
|
||||
*/
|
||||
const composeTween = (tween, siblings) => {
|
||||
|
||||
const tweenCompositionType = tween._composition;
|
||||
|
||||
// Handle replaced tweens
|
||||
|
||||
if (tweenCompositionType === consts.compositionTypes.replace) {
|
||||
|
||||
const tweenAbsStartTime = tween._absoluteStartTime;
|
||||
|
||||
helpers.addChild(siblings, tween, addTweenSortMethod, '_prevRep', '_nextRep');
|
||||
|
||||
const prevSibling = tween._prevRep;
|
||||
|
||||
// Update the previous siblings for composition replace tweens
|
||||
|
||||
if (prevSibling) {
|
||||
|
||||
const prevParent = prevSibling.parent;
|
||||
const prevAbsEndTime = prevSibling._absoluteEndTime;
|
||||
|
||||
// Handle looped animations tween
|
||||
|
||||
if (
|
||||
// Check if the previous tween is from a different animation
|
||||
tween.parent.id !== prevParent.id &&
|
||||
// Check if the animation has loops
|
||||
prevParent.iterationCount> 1 &&
|
||||
// Check if _absoluteChangeEndTime of last loop overlaps the current tween
|
||||
prevAbsEndTime + (prevParent.duration - prevParent.iterationDuration) > tweenAbsStartTime
|
||||
) {
|
||||
|
||||
// TODO: Find a way to only override the iterations overlapping with the tween
|
||||
overrideTween(prevSibling);
|
||||
|
||||
let prevPrevSibling = prevSibling._prevRep;
|
||||
|
||||
// If the tween was part of a set of keyframes, override its siblings
|
||||
while (prevPrevSibling && prevPrevSibling.parent.id === prevParent.id) {
|
||||
overrideTween(prevPrevSibling);
|
||||
prevPrevSibling = prevPrevSibling._prevRep;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Read the precision-matched update-start instead of subtracting tween._delay live so sequential keyframes touching at a boundary don't trigger a phantom overlap from float drift.
|
||||
const absoluteUpdateStartTime = tween._absoluteUpdateStartTime;
|
||||
|
||||
if (prevAbsEndTime > absoluteUpdateStartTime) {
|
||||
|
||||
const prevChangeStartTime = prevSibling._startTime;
|
||||
const prevTLOffset = prevAbsEndTime - (prevChangeStartTime + prevSibling._updateDuration);
|
||||
// Rounding is necessary here to minimize floating point errors when working in seconds
|
||||
const updatedPrevChangeDuration = helpers.round(absoluteUpdateStartTime - prevTLOffset - prevChangeStartTime, 12);
|
||||
|
||||
prevSibling._changeDuration = updatedPrevChangeDuration;
|
||||
prevSibling._currentTime = updatedPrevChangeDuration;
|
||||
prevSibling._isOverlapped = 1;
|
||||
|
||||
// Override the previous tween if its new _changeDuration is lower than minValue
|
||||
// TODO: See if it's even neceseeary to test against minValue, checking for 0 might be enough
|
||||
if (updatedPrevChangeDuration < consts.minValue) {
|
||||
overrideTween(prevSibling);
|
||||
}
|
||||
}
|
||||
|
||||
// Skip the cancel cascade when both tweens share the same parent timeline, a timeline cannot replace itself.
|
||||
const tweenParentTL = tween.parent.parent;
|
||||
if (!tweenParentTL || tweenParentTL !== prevParent.parent) {
|
||||
|
||||
let pausePrevParentAnimation = true;
|
||||
|
||||
helpers.forEachChildren(prevParent, (/** @type Tween */t) => {
|
||||
if (!t._isOverlapped) pausePrevParentAnimation = false;
|
||||
});
|
||||
|
||||
if (pausePrevParentAnimation) {
|
||||
const prevParentTL = prevParent.parent;
|
||||
if (prevParentTL) {
|
||||
let pausePrevParentTL = true;
|
||||
helpers.forEachChildren(prevParentTL, (/** @type JSAnimation */a) => {
|
||||
if (a !== prevParent) {
|
||||
helpers.forEachChildren(a, (/** @type Tween */t) => {
|
||||
if (!t._isOverlapped) pausePrevParentTL = false;
|
||||
});
|
||||
}
|
||||
});
|
||||
if (pausePrevParentTL) {
|
||||
prevParentTL.cancel();
|
||||
}
|
||||
} else {
|
||||
prevParent.cancel();
|
||||
// Previously, calling .cancel() on a timeline child would affect the render order of other children
|
||||
// Worked around this by marking it as .completed and using .pause() for safe removal in the engine loop
|
||||
// This is no longer needed since timeline tween composition is now handled separately
|
||||
// Keeping this here for reference
|
||||
// prevParent.completed = true;
|
||||
// prevParent.pause();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// let nextSibling = tween._nextRep;
|
||||
|
||||
// // All the next siblings are automatically overridden
|
||||
|
||||
// if (nextSibling && nextSibling._absoluteStartTime >= tweenAbsStartTime) {
|
||||
// while (nextSibling) {
|
||||
// overrideTween(nextSibling);
|
||||
// nextSibling = nextSibling._nextRep;
|
||||
// }
|
||||
// }
|
||||
|
||||
// if (nextSibling && nextSibling._absoluteStartTime < tweenAbsStartTime) {
|
||||
// while (nextSibling) {
|
||||
// overrideTween(nextSibling);
|
||||
// console.log(tween.id, nextSibling.id);
|
||||
// nextSibling = nextSibling._nextRep;
|
||||
// }
|
||||
// }
|
||||
|
||||
// Handle additive tweens composition
|
||||
|
||||
} else if (tweenCompositionType === consts.compositionTypes.blend) {
|
||||
|
||||
const additiveTweenSiblings = getTweenSiblings(tween.target, tween.property, '_add');
|
||||
const additiveAnimation = additive.addAdditiveAnimation(lookups._add);
|
||||
|
||||
let lookupTween = additiveTweenSiblings._head;
|
||||
|
||||
if (!lookupTween) {
|
||||
lookupTween = { ...tween };
|
||||
lookupTween._composition = consts.compositionTypes.replace;
|
||||
lookupTween._updateDuration = consts.minValue;
|
||||
lookupTween._startTime = 0;
|
||||
lookupTween._numbers = helpers.cloneArray(tween._fromNumbers);
|
||||
lookupTween._number = 0;
|
||||
lookupTween._next = null;
|
||||
lookupTween._prev = null;
|
||||
helpers.addChild(additiveTweenSiblings, lookupTween);
|
||||
helpers.addChild(additiveAnimation, lookupTween);
|
||||
}
|
||||
|
||||
// Convert the values of TO to FROM and set TO to 0
|
||||
|
||||
const toNumber = tween._toNumber;
|
||||
tween._fromNumber = lookupTween._fromNumber - toNumber;
|
||||
tween._toNumber = 0;
|
||||
tween._numbers = helpers.cloneArray(tween._fromNumbers);
|
||||
tween._number = 0;
|
||||
lookupTween._fromNumber = toNumber;
|
||||
|
||||
if (tween._toNumbers.length) {
|
||||
const toNumbers = helpers.cloneArray(tween._toNumbers);
|
||||
toNumbers.forEach((value, i) => {
|
||||
tween._fromNumbers[i] = lookupTween._fromNumbers[i] - value;
|
||||
tween._toNumbers[i] = 0;
|
||||
});
|
||||
lookupTween._fromNumbers = toNumbers;
|
||||
}
|
||||
|
||||
helpers.addChild(additiveTweenSiblings, tween, null, '_prevAdd', '_nextAdd');
|
||||
|
||||
}
|
||||
|
||||
return tween;
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {Tween} tween
|
||||
* @return {Tween}
|
||||
*/
|
||||
const removeTweenSliblings = tween => {
|
||||
const tweenComposition = tween._composition;
|
||||
if (tweenComposition !== consts.compositionTypes.none) {
|
||||
const tweenTarget = tween.target;
|
||||
const tweenProperty = tween.property;
|
||||
const replaceTweensLookup = lookups._rep;
|
||||
const replaceTargetProps = replaceTweensLookup.get(tweenTarget);
|
||||
const tweenReplaceSiblings = replaceTargetProps[tweenProperty];
|
||||
helpers.removeChild(tweenReplaceSiblings, tween, '_prevRep', '_nextRep');
|
||||
if (tweenComposition === consts.compositionTypes.blend) {
|
||||
const addTweensLookup = lookups._add;
|
||||
const addTargetProps = addTweensLookup.get(tweenTarget);
|
||||
if (!addTargetProps) return;
|
||||
const additiveTweenSiblings = addTargetProps[tweenProperty];
|
||||
const additiveAnimation = additive.additive.animation;
|
||||
helpers.removeChild(additiveTweenSiblings, tween, '_prevAdd', '_nextAdd');
|
||||
// If only one tween is left in the additive lookup, it's the tween lookup
|
||||
const lookupTween = additiveTweenSiblings._head;
|
||||
if (lookupTween && lookupTween === additiveTweenSiblings._tail) {
|
||||
helpers.removeChild(additiveTweenSiblings, lookupTween, '_prevAdd', '_nextAdd');
|
||||
helpers.removeChild(additiveAnimation, lookupTween);
|
||||
let shouldClean = true;
|
||||
for (let prop in addTargetProps) {
|
||||
if (addTargetProps[prop]._head) {
|
||||
shouldClean = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (shouldClean) {
|
||||
addTweensLookup.delete(tweenTarget);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return tween;
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {TargetsArray} targetsArray
|
||||
* @param {JSAnimation} animation
|
||||
* @param {String} [propertyName]
|
||||
* @return {Boolean}
|
||||
*/
|
||||
const removeTargetsFromJSAnimation = (targetsArray, animation, propertyName) => {
|
||||
let tweensMatchesTargets = false;
|
||||
helpers.forEachChildren(animation, (/**@type {Tween} */tween) => {
|
||||
const tweenTarget = tween.target;
|
||||
if (targetsArray.includes(tweenTarget)) {
|
||||
const tweenName = tween.property;
|
||||
const tweenType = tween._tweenType;
|
||||
const normalizePropName = styles.sanitizePropertyName(propertyName, tweenTarget, tweenType);
|
||||
if (!normalizePropName || normalizePropName && normalizePropName === tweenName) {
|
||||
// Make sure to flag the previous CSS transform tween to renderTransform
|
||||
if (tween.parent._tail === tween &&
|
||||
tween._tweenType === consts.tweenTypes.TRANSFORM &&
|
||||
tween._prev &&
|
||||
tween._prev._tweenType === consts.tweenTypes.TRANSFORM
|
||||
) {
|
||||
tween._prev._renderTransforms = 1;
|
||||
}
|
||||
// Removes the tween from the selected animation
|
||||
helpers.removeChild(animation, tween);
|
||||
// Detach the tween from its siblings to make sure blended tweens are correctlly removed
|
||||
removeTweenSliblings(tween);
|
||||
tweensMatchesTargets = true;
|
||||
}
|
||||
}
|
||||
}, true);
|
||||
return tweensMatchesTargets;
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {TargetsArray} targetsArray
|
||||
* @param {Renderable} [renderable]
|
||||
* @param {String} [propertyName]
|
||||
*/
|
||||
const removeTargetsFromRenderable = (targetsArray, renderable, propertyName) => {
|
||||
const parent = /** @type {Renderable|typeof engine} **/(renderable ? renderable : engine.engine);
|
||||
let removeMatches;
|
||||
if (parent._hasChildren) {
|
||||
let iterationDuration = 0;
|
||||
helpers.forEachChildren(parent, (/** @type {Renderable} */child) => {
|
||||
if (!child._hasChildren) {
|
||||
removeMatches = removeTargetsFromJSAnimation(targetsArray, /** @type {JSAnimation} */(child), propertyName);
|
||||
// Remove the child from its parent if no tweens and no children left after the removal
|
||||
if (removeMatches && !child._head) {
|
||||
child.cancel();
|
||||
helpers.removeChild(parent, child);
|
||||
} else {
|
||||
// Calculate the new iterationDuration value to handle onComplete with last child in render()
|
||||
const childTLOffset = child._offset + child._delay;
|
||||
const childDur = childTLOffset + child.duration;
|
||||
if (childDur > iterationDuration) {
|
||||
iterationDuration = childDur;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Make sure to also remove engine's children targets
|
||||
// NOTE: Avoid recursion?
|
||||
if (child._head) {
|
||||
removeTargetsFromRenderable(targetsArray, child, propertyName);
|
||||
} else {
|
||||
child._hasChildren = false;
|
||||
}
|
||||
}, true);
|
||||
// Update iterationDuration value to handle onComplete with last child in render()
|
||||
if (!helpers.isUnd(/** @type {Renderable} */(parent).iterationDuration)) {
|
||||
/** @type {Renderable} */(parent).iterationDuration = iterationDuration;
|
||||
}
|
||||
} else {
|
||||
removeMatches = removeTargetsFromJSAnimation(
|
||||
targetsArray,
|
||||
/** @type {JSAnimation} */(parent),
|
||||
propertyName
|
||||
);
|
||||
}
|
||||
if (removeMatches && !parent._head) {
|
||||
parent._hasChildren = false;
|
||||
// Cancel the parent if there are no tweens and no children left after the removal
|
||||
// We have to check if the .cancel() method exist to handle cases where the parent is the engine itself
|
||||
if (/** @type {Renderable} */(parent).cancel) /** @type {Renderable} */(parent).cancel();
|
||||
}
|
||||
};
|
||||
|
||||
exports.composeTween = composeTween;
|
||||
exports.getTweenSiblings = getTweenSiblings;
|
||||
exports.overrideTween = overrideTween;
|
||||
exports.removeTargetsFromRenderable = removeTargetsFromRenderable;
|
||||
exports.removeTweenSliblings = removeTweenSliblings;
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
export function getTweenSiblings(target: Target, property: string, lookup?: string): TweenPropertySiblings;
|
||||
export function overrideTween(tween: Tween): void;
|
||||
export function composeTween(tween: Tween, siblings: TweenPropertySiblings): Tween;
|
||||
export function removeTweenSliblings(tween: Tween): Tween;
|
||||
export function removeTargetsFromRenderable(targetsArray: TargetsArray, renderable?: Renderable, propertyName?: string): void;
|
||||
import type { Target } from '../types/index.js';
|
||||
import type { TweenPropertySiblings } from '../types/index.js';
|
||||
import type { Tween } from '../types/index.js';
|
||||
import type { TargetsArray } from '../types/index.js';
|
||||
import type { Renderable } from '../types/index.js';
|
||||
+380
@@ -0,0 +1,380 @@
|
||||
/**
|
||||
* Anime.js - animation - ESM
|
||||
* @version v4.5.0
|
||||
* @license MIT
|
||||
* @copyright 2026 - Julian Garnier
|
||||
*/
|
||||
|
||||
import { compositionTypes, minValue, tweenTypes } from '../core/consts.js';
|
||||
import { forEachChildren, removeChild, isUnd, addChild, round, cloneArray } from '../core/helpers.js';
|
||||
import { sanitizePropertyName } from '../core/styles.js';
|
||||
import { engine } from '../engine/engine.js';
|
||||
import { additive, addAdditiveAnimation } from './additive.js';
|
||||
|
||||
/**
|
||||
* @import {
|
||||
* TweenReplaceLookups,
|
||||
* TweenAdditiveLookups,
|
||||
* TweenPropertySiblings,
|
||||
* Tween,
|
||||
* Target,
|
||||
* TargetsArray,
|
||||
* Renderable,
|
||||
* } from '../types/index.js'
|
||||
*
|
||||
* @import {
|
||||
* JSAnimation,
|
||||
* } from '../animation/animation.js'
|
||||
*/
|
||||
|
||||
const lookups = {
|
||||
/** @type {TweenReplaceLookups} */
|
||||
_rep: new WeakMap(),
|
||||
/** @type {TweenAdditiveLookups} */
|
||||
_add: new Map(),
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {Target} target
|
||||
* @param {String} property
|
||||
* @param {String} lookup
|
||||
* @return {TweenPropertySiblings}
|
||||
*/
|
||||
const getTweenSiblings = (target, property, lookup = '_rep') => {
|
||||
const lookupMap = lookups[lookup];
|
||||
let targetLookup = lookupMap.get(target);
|
||||
if (!targetLookup) {
|
||||
targetLookup = {};
|
||||
lookupMap.set(target, targetLookup);
|
||||
}
|
||||
return targetLookup[property] ? targetLookup[property] : targetLookup[property] = {
|
||||
_head: null,
|
||||
_tail: null,
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {Tween} p
|
||||
* @param {Tween} c
|
||||
* @return {Number|Boolean}
|
||||
*/
|
||||
const addTweenSortMethod = (p, c) => {
|
||||
return p._isOverridden || p._absoluteStartTime > c._absoluteStartTime;
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {Tween} tween
|
||||
*/
|
||||
const overrideTween = tween => {
|
||||
tween._isOverlapped = 1;
|
||||
tween._isOverridden = 1;
|
||||
tween._changeDuration = minValue;
|
||||
tween._currentTime = minValue;
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {Tween} tween
|
||||
* @param {TweenPropertySiblings} siblings
|
||||
* @return {Tween}
|
||||
*/
|
||||
const composeTween = (tween, siblings) => {
|
||||
|
||||
const tweenCompositionType = tween._composition;
|
||||
|
||||
// Handle replaced tweens
|
||||
|
||||
if (tweenCompositionType === compositionTypes.replace) {
|
||||
|
||||
const tweenAbsStartTime = tween._absoluteStartTime;
|
||||
|
||||
addChild(siblings, tween, addTweenSortMethod, '_prevRep', '_nextRep');
|
||||
|
||||
const prevSibling = tween._prevRep;
|
||||
|
||||
// Update the previous siblings for composition replace tweens
|
||||
|
||||
if (prevSibling) {
|
||||
|
||||
const prevParent = prevSibling.parent;
|
||||
const prevAbsEndTime = prevSibling._absoluteEndTime;
|
||||
|
||||
// Handle looped animations tween
|
||||
|
||||
if (
|
||||
// Check if the previous tween is from a different animation
|
||||
tween.parent.id !== prevParent.id &&
|
||||
// Check if the animation has loops
|
||||
prevParent.iterationCount> 1 &&
|
||||
// Check if _absoluteChangeEndTime of last loop overlaps the current tween
|
||||
prevAbsEndTime + (prevParent.duration - prevParent.iterationDuration) > tweenAbsStartTime
|
||||
) {
|
||||
|
||||
// TODO: Find a way to only override the iterations overlapping with the tween
|
||||
overrideTween(prevSibling);
|
||||
|
||||
let prevPrevSibling = prevSibling._prevRep;
|
||||
|
||||
// If the tween was part of a set of keyframes, override its siblings
|
||||
while (prevPrevSibling && prevPrevSibling.parent.id === prevParent.id) {
|
||||
overrideTween(prevPrevSibling);
|
||||
prevPrevSibling = prevPrevSibling._prevRep;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Read the precision-matched update-start instead of subtracting tween._delay live so sequential keyframes touching at a boundary don't trigger a phantom overlap from float drift.
|
||||
const absoluteUpdateStartTime = tween._absoluteUpdateStartTime;
|
||||
|
||||
if (prevAbsEndTime > absoluteUpdateStartTime) {
|
||||
|
||||
const prevChangeStartTime = prevSibling._startTime;
|
||||
const prevTLOffset = prevAbsEndTime - (prevChangeStartTime + prevSibling._updateDuration);
|
||||
// Rounding is necessary here to minimize floating point errors when working in seconds
|
||||
const updatedPrevChangeDuration = round(absoluteUpdateStartTime - prevTLOffset - prevChangeStartTime, 12);
|
||||
|
||||
prevSibling._changeDuration = updatedPrevChangeDuration;
|
||||
prevSibling._currentTime = updatedPrevChangeDuration;
|
||||
prevSibling._isOverlapped = 1;
|
||||
|
||||
// Override the previous tween if its new _changeDuration is lower than minValue
|
||||
// TODO: See if it's even neceseeary to test against minValue, checking for 0 might be enough
|
||||
if (updatedPrevChangeDuration < minValue) {
|
||||
overrideTween(prevSibling);
|
||||
}
|
||||
}
|
||||
|
||||
// Skip the cancel cascade when both tweens share the same parent timeline, a timeline cannot replace itself.
|
||||
const tweenParentTL = tween.parent.parent;
|
||||
if (!tweenParentTL || tweenParentTL !== prevParent.parent) {
|
||||
|
||||
let pausePrevParentAnimation = true;
|
||||
|
||||
forEachChildren(prevParent, (/** @type Tween */t) => {
|
||||
if (!t._isOverlapped) pausePrevParentAnimation = false;
|
||||
});
|
||||
|
||||
if (pausePrevParentAnimation) {
|
||||
const prevParentTL = prevParent.parent;
|
||||
if (prevParentTL) {
|
||||
let pausePrevParentTL = true;
|
||||
forEachChildren(prevParentTL, (/** @type JSAnimation */a) => {
|
||||
if (a !== prevParent) {
|
||||
forEachChildren(a, (/** @type Tween */t) => {
|
||||
if (!t._isOverlapped) pausePrevParentTL = false;
|
||||
});
|
||||
}
|
||||
});
|
||||
if (pausePrevParentTL) {
|
||||
prevParentTL.cancel();
|
||||
}
|
||||
} else {
|
||||
prevParent.cancel();
|
||||
// Previously, calling .cancel() on a timeline child would affect the render order of other children
|
||||
// Worked around this by marking it as .completed and using .pause() for safe removal in the engine loop
|
||||
// This is no longer needed since timeline tween composition is now handled separately
|
||||
// Keeping this here for reference
|
||||
// prevParent.completed = true;
|
||||
// prevParent.pause();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// let nextSibling = tween._nextRep;
|
||||
|
||||
// // All the next siblings are automatically overridden
|
||||
|
||||
// if (nextSibling && nextSibling._absoluteStartTime >= tweenAbsStartTime) {
|
||||
// while (nextSibling) {
|
||||
// overrideTween(nextSibling);
|
||||
// nextSibling = nextSibling._nextRep;
|
||||
// }
|
||||
// }
|
||||
|
||||
// if (nextSibling && nextSibling._absoluteStartTime < tweenAbsStartTime) {
|
||||
// while (nextSibling) {
|
||||
// overrideTween(nextSibling);
|
||||
// console.log(tween.id, nextSibling.id);
|
||||
// nextSibling = nextSibling._nextRep;
|
||||
// }
|
||||
// }
|
||||
|
||||
// Handle additive tweens composition
|
||||
|
||||
} else if (tweenCompositionType === compositionTypes.blend) {
|
||||
|
||||
const additiveTweenSiblings = getTweenSiblings(tween.target, tween.property, '_add');
|
||||
const additiveAnimation = addAdditiveAnimation(lookups._add);
|
||||
|
||||
let lookupTween = additiveTweenSiblings._head;
|
||||
|
||||
if (!lookupTween) {
|
||||
lookupTween = { ...tween };
|
||||
lookupTween._composition = compositionTypes.replace;
|
||||
lookupTween._updateDuration = minValue;
|
||||
lookupTween._startTime = 0;
|
||||
lookupTween._numbers = cloneArray(tween._fromNumbers);
|
||||
lookupTween._number = 0;
|
||||
lookupTween._next = null;
|
||||
lookupTween._prev = null;
|
||||
addChild(additiveTweenSiblings, lookupTween);
|
||||
addChild(additiveAnimation, lookupTween);
|
||||
}
|
||||
|
||||
// Convert the values of TO to FROM and set TO to 0
|
||||
|
||||
const toNumber = tween._toNumber;
|
||||
tween._fromNumber = lookupTween._fromNumber - toNumber;
|
||||
tween._toNumber = 0;
|
||||
tween._numbers = cloneArray(tween._fromNumbers);
|
||||
tween._number = 0;
|
||||
lookupTween._fromNumber = toNumber;
|
||||
|
||||
if (tween._toNumbers.length) {
|
||||
const toNumbers = cloneArray(tween._toNumbers);
|
||||
toNumbers.forEach((value, i) => {
|
||||
tween._fromNumbers[i] = lookupTween._fromNumbers[i] - value;
|
||||
tween._toNumbers[i] = 0;
|
||||
});
|
||||
lookupTween._fromNumbers = toNumbers;
|
||||
}
|
||||
|
||||
addChild(additiveTweenSiblings, tween, null, '_prevAdd', '_nextAdd');
|
||||
|
||||
}
|
||||
|
||||
return tween;
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {Tween} tween
|
||||
* @return {Tween}
|
||||
*/
|
||||
const removeTweenSliblings = tween => {
|
||||
const tweenComposition = tween._composition;
|
||||
if (tweenComposition !== compositionTypes.none) {
|
||||
const tweenTarget = tween.target;
|
||||
const tweenProperty = tween.property;
|
||||
const replaceTweensLookup = lookups._rep;
|
||||
const replaceTargetProps = replaceTweensLookup.get(tweenTarget);
|
||||
const tweenReplaceSiblings = replaceTargetProps[tweenProperty];
|
||||
removeChild(tweenReplaceSiblings, tween, '_prevRep', '_nextRep');
|
||||
if (tweenComposition === compositionTypes.blend) {
|
||||
const addTweensLookup = lookups._add;
|
||||
const addTargetProps = addTweensLookup.get(tweenTarget);
|
||||
if (!addTargetProps) return;
|
||||
const additiveTweenSiblings = addTargetProps[tweenProperty];
|
||||
const additiveAnimation = additive.animation;
|
||||
removeChild(additiveTweenSiblings, tween, '_prevAdd', '_nextAdd');
|
||||
// If only one tween is left in the additive lookup, it's the tween lookup
|
||||
const lookupTween = additiveTweenSiblings._head;
|
||||
if (lookupTween && lookupTween === additiveTweenSiblings._tail) {
|
||||
removeChild(additiveTweenSiblings, lookupTween, '_prevAdd', '_nextAdd');
|
||||
removeChild(additiveAnimation, lookupTween);
|
||||
let shouldClean = true;
|
||||
for (let prop in addTargetProps) {
|
||||
if (addTargetProps[prop]._head) {
|
||||
shouldClean = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (shouldClean) {
|
||||
addTweensLookup.delete(tweenTarget);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return tween;
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {TargetsArray} targetsArray
|
||||
* @param {JSAnimation} animation
|
||||
* @param {String} [propertyName]
|
||||
* @return {Boolean}
|
||||
*/
|
||||
const removeTargetsFromJSAnimation = (targetsArray, animation, propertyName) => {
|
||||
let tweensMatchesTargets = false;
|
||||
forEachChildren(animation, (/**@type {Tween} */tween) => {
|
||||
const tweenTarget = tween.target;
|
||||
if (targetsArray.includes(tweenTarget)) {
|
||||
const tweenName = tween.property;
|
||||
const tweenType = tween._tweenType;
|
||||
const normalizePropName = sanitizePropertyName(propertyName, tweenTarget, tweenType);
|
||||
if (!normalizePropName || normalizePropName && normalizePropName === tweenName) {
|
||||
// Make sure to flag the previous CSS transform tween to renderTransform
|
||||
if (tween.parent._tail === tween &&
|
||||
tween._tweenType === tweenTypes.TRANSFORM &&
|
||||
tween._prev &&
|
||||
tween._prev._tweenType === tweenTypes.TRANSFORM
|
||||
) {
|
||||
tween._prev._renderTransforms = 1;
|
||||
}
|
||||
// Removes the tween from the selected animation
|
||||
removeChild(animation, tween);
|
||||
// Detach the tween from its siblings to make sure blended tweens are correctlly removed
|
||||
removeTweenSliblings(tween);
|
||||
tweensMatchesTargets = true;
|
||||
}
|
||||
}
|
||||
}, true);
|
||||
return tweensMatchesTargets;
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {TargetsArray} targetsArray
|
||||
* @param {Renderable} [renderable]
|
||||
* @param {String} [propertyName]
|
||||
*/
|
||||
const removeTargetsFromRenderable = (targetsArray, renderable, propertyName) => {
|
||||
const parent = /** @type {Renderable|typeof engine} **/(renderable ? renderable : engine);
|
||||
let removeMatches;
|
||||
if (parent._hasChildren) {
|
||||
let iterationDuration = 0;
|
||||
forEachChildren(parent, (/** @type {Renderable} */child) => {
|
||||
if (!child._hasChildren) {
|
||||
removeMatches = removeTargetsFromJSAnimation(targetsArray, /** @type {JSAnimation} */(child), propertyName);
|
||||
// Remove the child from its parent if no tweens and no children left after the removal
|
||||
if (removeMatches && !child._head) {
|
||||
child.cancel();
|
||||
removeChild(parent, child);
|
||||
} else {
|
||||
// Calculate the new iterationDuration value to handle onComplete with last child in render()
|
||||
const childTLOffset = child._offset + child._delay;
|
||||
const childDur = childTLOffset + child.duration;
|
||||
if (childDur > iterationDuration) {
|
||||
iterationDuration = childDur;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Make sure to also remove engine's children targets
|
||||
// NOTE: Avoid recursion?
|
||||
if (child._head) {
|
||||
removeTargetsFromRenderable(targetsArray, child, propertyName);
|
||||
} else {
|
||||
child._hasChildren = false;
|
||||
}
|
||||
}, true);
|
||||
// Update iterationDuration value to handle onComplete with last child in render()
|
||||
if (!isUnd(/** @type {Renderable} */(parent).iterationDuration)) {
|
||||
/** @type {Renderable} */(parent).iterationDuration = iterationDuration;
|
||||
}
|
||||
} else {
|
||||
removeMatches = removeTargetsFromJSAnimation(
|
||||
targetsArray,
|
||||
/** @type {JSAnimation} */(parent),
|
||||
propertyName
|
||||
);
|
||||
}
|
||||
if (removeMatches && !parent._head) {
|
||||
parent._hasChildren = false;
|
||||
// Cancel the parent if there are no tweens and no children left after the removal
|
||||
// We have to check if the .cancel() method exist to handle cases where the parent is the engine itself
|
||||
if (/** @type {Renderable} */(parent).cancel) /** @type {Renderable} */(parent).cancel();
|
||||
}
|
||||
};
|
||||
|
||||
export { composeTween, getTweenSiblings, overrideTween, removeTargetsFromRenderable, removeTweenSliblings };
|
||||
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* Anime.js - animation - CJS
|
||||
* @version v4.5.0
|
||||
* @license MIT
|
||||
* @copyright 2026 - Julian Garnier
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
var animation = require('./animation.cjs');
|
||||
|
||||
|
||||
|
||||
exports.JSAnimation = animation.JSAnimation;
|
||||
exports.animate = animation.animate;
|
||||
@@ -0,0 +1 @@
|
||||
export * from "./animation.js";
|
||||
@@ -0,0 +1,8 @@
|
||||
/**
|
||||
* Anime.js - animation - ESM
|
||||
* @version v4.5.0
|
||||
* @license MIT
|
||||
* @copyright 2026 - Julian Garnier
|
||||
*/
|
||||
|
||||
export { JSAnimation, animate } from './animation.js';
|
||||
Reference in New Issue
Block a user