Initial commit xmarticaims
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* Anime.js - timeline - CJS
|
||||
* @version v4.5.0
|
||||
* @license MIT
|
||||
* @copyright 2026 - Julian Garnier
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
var timeline = require('./timeline.cjs');
|
||||
|
||||
|
||||
|
||||
exports.Timeline = timeline.Timeline;
|
||||
exports.createTimeline = timeline.createTimeline;
|
||||
@@ -0,0 +1 @@
|
||||
export * from "./timeline.js";
|
||||
@@ -0,0 +1,8 @@
|
||||
/**
|
||||
* Anime.js - timeline - ESM
|
||||
* @version v4.5.0
|
||||
* @license MIT
|
||||
* @copyright 2026 - Julian Garnier
|
||||
*/
|
||||
|
||||
export { Timeline, createTimeline } from './timeline.js';
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
/**
|
||||
* Anime.js - timeline - 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 values = require('../core/values.cjs');
|
||||
|
||||
/**
|
||||
* @import {
|
||||
* Tickable,
|
||||
* TimelinePosition,
|
||||
* } from '../types/index.js'
|
||||
*/
|
||||
|
||||
/**
|
||||
* @import {
|
||||
* Timeline,
|
||||
* } from './timeline.js'
|
||||
*/
|
||||
|
||||
/**
|
||||
* Timeline's children offsets positions parser
|
||||
* @param {Timeline} timeline
|
||||
* @param {String} timePosition
|
||||
* @return {Number}
|
||||
*/
|
||||
const getPrevChildOffset = (timeline, timePosition) => {
|
||||
if (helpers.stringStartsWith(timePosition, '<')) {
|
||||
const goToPrevAnimationOffset = timePosition[1] === '<';
|
||||
const prevAnimation = /** @type {Tickable} */(timeline._tail);
|
||||
const prevOffset = prevAnimation ? prevAnimation._offset + prevAnimation._delay : 0;
|
||||
return goToPrevAnimationOffset ? prevOffset : prevOffset + prevAnimation.duration;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {Timeline} timeline
|
||||
* @param {TimelinePosition} [timePosition]
|
||||
* @return {Number}
|
||||
*/
|
||||
const parseTimelinePosition = (timeline, timePosition) => {
|
||||
let tlDuration = timeline.iterationDuration;
|
||||
if (tlDuration === consts.minValue) tlDuration = 0;
|
||||
if (helpers.isUnd(timePosition)) return tlDuration;
|
||||
if (helpers.isNum(+timePosition)) return +timePosition;
|
||||
const timePosStr = /** @type {String} */(timePosition);
|
||||
const tlLabels = timeline ? timeline.labels : null;
|
||||
const hasLabels = !helpers.isNil(tlLabels);
|
||||
const prevOffset = getPrevChildOffset(timeline, timePosStr);
|
||||
const hasSibling = !helpers.isUnd(prevOffset);
|
||||
const matchedRelativeOperator = consts.relativeValuesExecRgx.exec(timePosStr);
|
||||
if (matchedRelativeOperator) {
|
||||
const fullOperator = matchedRelativeOperator[0];
|
||||
const split = timePosStr.split(fullOperator);
|
||||
const labelOffset = hasLabels && split[0] ? tlLabels[split[0]] : tlDuration;
|
||||
const parsedOffset = hasSibling ? prevOffset : hasLabels ? labelOffset : tlDuration;
|
||||
const parsedNumericalOffset = +split[1];
|
||||
return values.getRelativeValue(parsedOffset, parsedNumericalOffset, fullOperator[0]);
|
||||
} else {
|
||||
return hasSibling ? prevOffset :
|
||||
hasLabels ? !helpers.isUnd(tlLabels[timePosStr]) ? tlLabels[timePosStr] :
|
||||
tlDuration : tlDuration;
|
||||
}
|
||||
};
|
||||
|
||||
exports.parseTimelinePosition = parseTimelinePosition;
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
export function parseTimelinePosition(timeline: Timeline, timePosition?: TimelinePosition): number;
|
||||
import type { Timeline } from './timeline.js';
|
||||
import type { TimelinePosition } from '../types/index.js';
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
/**
|
||||
* Anime.js - timeline - ESM
|
||||
* @version v4.5.0
|
||||
* @license MIT
|
||||
* @copyright 2026 - Julian Garnier
|
||||
*/
|
||||
|
||||
import { relativeValuesExecRgx, minValue } from '../core/consts.js';
|
||||
import { isUnd, isNum, stringStartsWith, isNil } from '../core/helpers.js';
|
||||
import { getRelativeValue } from '../core/values.js';
|
||||
|
||||
/**
|
||||
* @import {
|
||||
* Tickable,
|
||||
* TimelinePosition,
|
||||
* } from '../types/index.js'
|
||||
*/
|
||||
|
||||
/**
|
||||
* @import {
|
||||
* Timeline,
|
||||
* } from './timeline.js'
|
||||
*/
|
||||
|
||||
/**
|
||||
* Timeline's children offsets positions parser
|
||||
* @param {Timeline} timeline
|
||||
* @param {String} timePosition
|
||||
* @return {Number}
|
||||
*/
|
||||
const getPrevChildOffset = (timeline, timePosition) => {
|
||||
if (stringStartsWith(timePosition, '<')) {
|
||||
const goToPrevAnimationOffset = timePosition[1] === '<';
|
||||
const prevAnimation = /** @type {Tickable} */(timeline._tail);
|
||||
const prevOffset = prevAnimation ? prevAnimation._offset + prevAnimation._delay : 0;
|
||||
return goToPrevAnimationOffset ? prevOffset : prevOffset + prevAnimation.duration;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {Timeline} timeline
|
||||
* @param {TimelinePosition} [timePosition]
|
||||
* @return {Number}
|
||||
*/
|
||||
const parseTimelinePosition = (timeline, timePosition) => {
|
||||
let tlDuration = timeline.iterationDuration;
|
||||
if (tlDuration === minValue) tlDuration = 0;
|
||||
if (isUnd(timePosition)) return tlDuration;
|
||||
if (isNum(+timePosition)) return +timePosition;
|
||||
const timePosStr = /** @type {String} */(timePosition);
|
||||
const tlLabels = timeline ? timeline.labels : null;
|
||||
const hasLabels = !isNil(tlLabels);
|
||||
const prevOffset = getPrevChildOffset(timeline, timePosStr);
|
||||
const hasSibling = !isUnd(prevOffset);
|
||||
const matchedRelativeOperator = relativeValuesExecRgx.exec(timePosStr);
|
||||
if (matchedRelativeOperator) {
|
||||
const fullOperator = matchedRelativeOperator[0];
|
||||
const split = timePosStr.split(fullOperator);
|
||||
const labelOffset = hasLabels && split[0] ? tlLabels[split[0]] : tlDuration;
|
||||
const parsedOffset = hasSibling ? prevOffset : hasLabels ? labelOffset : tlDuration;
|
||||
const parsedNumericalOffset = +split[1];
|
||||
return getRelativeValue(parsedOffset, parsedNumericalOffset, fullOperator[0]);
|
||||
} else {
|
||||
return hasSibling ? prevOffset :
|
||||
hasLabels ? !isUnd(tlLabels[timePosStr]) ? tlLabels[timePosStr] :
|
||||
tlDuration : tlDuration;
|
||||
}
|
||||
};
|
||||
|
||||
export { parseTimelinePosition };
|
||||
+355
@@ -0,0 +1,355 @@
|
||||
/**
|
||||
* Anime.js - timeline - CJS
|
||||
* @version v4.5.0
|
||||
* @license MIT
|
||||
* @copyright 2026 - Julian Garnier
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
var globals = require('../core/globals.cjs');
|
||||
var consts = require('../core/consts.cjs');
|
||||
var helpers = require('../core/helpers.cjs');
|
||||
var values = require('../core/values.cjs');
|
||||
var targets = require('../core/targets.cjs');
|
||||
var render = require('../core/render.cjs');
|
||||
var styles = require('../core/styles.cjs');
|
||||
var timer = require('../timer/timer.cjs');
|
||||
var composition = require('../animation/composition.cjs');
|
||||
var animation = require('../animation/animation.cjs');
|
||||
var parser = require('../easings/eases/parser.cjs');
|
||||
var position = require('./position.cjs');
|
||||
|
||||
/**
|
||||
* @import {
|
||||
* TargetsParam,
|
||||
* Callback,
|
||||
* Tickable,
|
||||
* TimerParams,
|
||||
* AnimationParams,
|
||||
* Target,
|
||||
* Renderable,
|
||||
* TimelineParams,
|
||||
* DefaultsParams,
|
||||
* TimelinePosition,
|
||||
* StaggerFunction,
|
||||
* TargetsArray,
|
||||
* TweakRegister,
|
||||
* } from '../types/index.js'
|
||||
*/
|
||||
|
||||
/**
|
||||
* @import {
|
||||
* WAAPIAnimation,
|
||||
* } from '../waapi/waapi.js'
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {Timeline} tl
|
||||
* @return {Number}
|
||||
*/
|
||||
function getTimelineTotalDuration(tl) {
|
||||
return helpers.clampInfinity(((tl.iterationDuration + tl._loopDelay) * tl.iterationCount) - tl._loopDelay) || consts.minValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* @overload
|
||||
* @param {TimerParams} childParams
|
||||
* @param {Timeline} tl
|
||||
* @param {Number} timePosition
|
||||
* @return {Timeline}
|
||||
*
|
||||
* @overload
|
||||
* @param {AnimationParams} childParams
|
||||
* @param {Timeline} tl
|
||||
* @param {Number} timePosition
|
||||
* @param {TargetsParam} targets
|
||||
* @param {Number} [index]
|
||||
* @param {TargetsArray} [allTargets]
|
||||
* @return {Timeline}
|
||||
*
|
||||
* @param {TimerParams|AnimationParams} childParams
|
||||
* @param {Timeline} tl
|
||||
* @param {Number} timePosition
|
||||
* @param {TargetsParam} [targets]
|
||||
* @param {Number} [index]
|
||||
* @param {TargetsArray} [allTargets]
|
||||
*/
|
||||
function addTlChild(childParams, tl, timePosition, targets, index, allTargets) {
|
||||
const isSetter = helpers.isNum(childParams.duration) && /** @type {Number} */(childParams.duration) <= consts.minValue;
|
||||
// Offset the tl position with -minValue for 0 duration animations or .set() calls in order to align their end value with the defined position
|
||||
const adjustedPosition = isSetter ? timePosition - consts.minValue : timePosition;
|
||||
if (tl.composition) render.tick(tl, adjustedPosition, 1, 1, consts.tickModes.AUTO);
|
||||
const tlChild = targets ?
|
||||
new animation.JSAnimation(targets,/** @type {AnimationParams} */(childParams), tl, adjustedPosition, false, index, allTargets) :
|
||||
new timer.Timer(/** @type {TimerParams} */(childParams), tl, adjustedPosition);
|
||||
if (tl.composition) tlChild.init(true);
|
||||
// TODO: Might be better to insert at a position relative to startTime?
|
||||
helpers.addChild(tl, tlChild);
|
||||
helpers.forEachChildren(tl, (/** @type {Renderable} */child) => {
|
||||
const childTLOffset = child._offset + child._delay;
|
||||
const childDur = childTLOffset + child.duration;
|
||||
if (childDur > tl.iterationDuration) tl.iterationDuration = childDur;
|
||||
});
|
||||
tl.duration = getTimelineTotalDuration(tl);
|
||||
return tl;
|
||||
}
|
||||
|
||||
let TLId = 0;
|
||||
|
||||
class Timeline extends timer.Timer {
|
||||
|
||||
/**
|
||||
* @param {TimelineParams} [parameters]
|
||||
*/
|
||||
constructor(parameters = {}) {
|
||||
super(/** @type {TimerParams&TimelineParams} */(parameters), null, 0);
|
||||
++TLId;
|
||||
/** @type {String|Number} */
|
||||
this.id = !helpers.isUnd(parameters.id) ? parameters.id : TLId;
|
||||
/** @type {Number} */
|
||||
this.duration = 0; // TL duration starts at 0 and grows when adding children
|
||||
/** @type {Record<String, Number>} */
|
||||
this.labels = {};
|
||||
const defaultsParams = parameters.defaults;
|
||||
const globalDefaults = globals.globals.defaults;
|
||||
/** @type {DefaultsParams} */
|
||||
this.defaults = defaultsParams ? helpers.mergeObjects(defaultsParams, globalDefaults) : globalDefaults;
|
||||
/** @type {Boolean} */
|
||||
this.composition = values.setValue(parameters.composition, true);
|
||||
/** @type {Callback<this>} */
|
||||
this.onRender = parameters.onRender || globalDefaults.onRender;
|
||||
const tlPlaybackEase = values.setValue(parameters.playbackEase, globalDefaults.playbackEase);
|
||||
this._ease = tlPlaybackEase ? parser.parseEase(tlPlaybackEase) : null;
|
||||
/** @type {Number} */
|
||||
this.iterationDuration = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @overload
|
||||
* @param {TargetsParam} a1
|
||||
* @param {AnimationParams} a2
|
||||
* @param {TimelinePosition|StaggerFunction<Number|String>|TweakRegister} [a3]
|
||||
* @return {this}
|
||||
*
|
||||
* @overload
|
||||
* @param {TimerParams} a1
|
||||
* @param {TimelinePosition} [a2]
|
||||
* @return {this}
|
||||
*
|
||||
* @param {TargetsParam|TimerParams} a1
|
||||
* @param {TimelinePosition|AnimationParams} a2
|
||||
* @param {TimelinePosition|StaggerFunction<Number|String>|TweakRegister} [a3]
|
||||
*/
|
||||
add(a1, a2, a3) {
|
||||
const isAnim = helpers.isObj(a2);
|
||||
const isTimer = helpers.isObj(a1);
|
||||
if (isAnim || isTimer) {
|
||||
this._hasChildren = true;
|
||||
if (isAnim) {
|
||||
const childParams = /** @type {AnimationParams} */(a2);
|
||||
const editorHook = globals.globals.editor && globals.globals.editor.addTimelineChild;
|
||||
const isStaggerType = a3 && /** @type {TweakRegister} */(a3).type === 'Stagger' && globals.globals.editor;
|
||||
// Check for function or Stagger type children positions
|
||||
const staggeredPosition = helpers.isFnc(a3) ? a3 : null;
|
||||
if (staggeredPosition || isStaggerType) {
|
||||
const parsedTargetsArray = targets.parseTargets(/** @type {TargetsParam} */(a1));
|
||||
// Store initial duration before adding new children that will change the duration
|
||||
const tlDuration = this.duration;
|
||||
// Store initial _iterationDuration before adding new children that will change the duration
|
||||
const tlIterationDuration = this.iterationDuration;
|
||||
// Store the original id in order to add specific indexes to the new animations ids
|
||||
const id = childParams.id;
|
||||
let i = 0;
|
||||
/** @type {Number} */
|
||||
const parsedLength = (parsedTargetsArray.length);
|
||||
// Call editor hook once for the entire stagger group instead of per target
|
||||
const resolvedParams = editorHook ? editorHook(/** @type {TargetsParam} */(a1), childParams, this.id, a3, parsedLength) : null;
|
||||
// Resolve stagger AFTER editor hook so tweaked position value (a3.defaultValue) is used
|
||||
const staggerFn = staggeredPosition || globals.globals.editor.resolveStagger(/** @type {TweakRegister} */(a3).defaultValue);
|
||||
parsedTargetsArray.forEach((/** @type {Target} */target) => {
|
||||
// Create a new parameter object for each staggered children
|
||||
const staggeredChildParams = { ...(resolvedParams || childParams) };
|
||||
// Reset the duration of the timeline iteration before each stagger to prevent wrong start value calculation
|
||||
this.duration = tlDuration;
|
||||
this.iterationDuration = tlIterationDuration;
|
||||
if (!helpers.isUnd(id)) staggeredChildParams.id = id + '-' + i;
|
||||
const staggeredTimePosition = position.parseTimelinePosition(this, staggerFn(target, i, parsedTargetsArray, null, this));
|
||||
addTlChild(
|
||||
staggeredChildParams,
|
||||
this,
|
||||
staggeredTimePosition,
|
||||
target,
|
||||
i,
|
||||
parsedTargetsArray,
|
||||
);
|
||||
i++;
|
||||
});
|
||||
} else {
|
||||
// Call editor hook before resolving position so tweaked values are applied
|
||||
const resolvedChildParams = editorHook ? editorHook(/** @type {TargetsParam} */(a1), childParams, this.id, a3) : childParams;
|
||||
const resolvedPosition = a3 && /** @type {*} */(a3).type ? /** @type {*} */(a3).defaultValue : a3;
|
||||
addTlChild(
|
||||
resolvedChildParams,
|
||||
this,
|
||||
position.parseTimelinePosition(this, resolvedPosition),
|
||||
/** @type {TargetsParam} */(a1),
|
||||
);
|
||||
}
|
||||
} else {
|
||||
// It's a Timer
|
||||
addTlChild(
|
||||
/** @type TimerParams */(a1),
|
||||
this,
|
||||
position.parseTimelinePosition(this,a2),
|
||||
);
|
||||
}
|
||||
if (this.composition) this.init(true);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @overload
|
||||
* @param {Tickable} [synced]
|
||||
* @param {TimelinePosition} [position]
|
||||
* @return {this}
|
||||
*
|
||||
* @overload
|
||||
* @param {globalThis.Animation} [synced]
|
||||
* @param {TimelinePosition} [position]
|
||||
* @return {this}
|
||||
*
|
||||
* @overload
|
||||
* @param {WAAPIAnimation} [synced]
|
||||
* @param {TimelinePosition} [position]
|
||||
* @return {this}
|
||||
*
|
||||
* @param {Tickable|WAAPIAnimation|globalThis.Animation} [synced]
|
||||
* @param {TimelinePosition} [position]
|
||||
*/
|
||||
sync(synced, position) {
|
||||
if (helpers.isUnd(synced) || synced && helpers.isUnd(synced.pause)) return this;
|
||||
synced.pause();
|
||||
const duration = +(/** @type {globalThis.Animation} */(synced).effect ? /** @type {globalThis.Animation} */(synced).effect.getTiming().duration : /** @type {Tickable} */(synced).duration);
|
||||
// Forces WAAPI Animation to persist; otherwise, they will stop syncing on finish.
|
||||
if (!helpers.isUnd(synced) && !helpers.isUnd(/** @type {WAAPIAnimation} */(synced).persist)) {
|
||||
/** @type {WAAPIAnimation} */(synced).persist = true;
|
||||
}
|
||||
const editor = globals.globals.editor;
|
||||
const childHook = editor && editor.addTimelineChild;
|
||||
if (editor && editor.addTimelineSync) {
|
||||
position = editor.addTimelineSync(synced, position, this.id);
|
||||
editor.addTimelineChild = null; // Suppress the per-child hook for the internal .add, sync already registered.
|
||||
}
|
||||
const result = this.add(synced, { currentTime: [0, duration], duration, delay: 0, ease: 'linear', playbackEase: 'linear' }, position);
|
||||
if (editor) editor.addTimelineChild = childHook;
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {TargetsParam} targets
|
||||
* @param {AnimationParams} parameters
|
||||
* @param {TimelinePosition|StaggerFunction<Number|String>|TweakRegister} [position]
|
||||
* @return {this}
|
||||
*/
|
||||
set(targets, parameters, position) {
|
||||
if (helpers.isUnd(parameters)) return this;
|
||||
parameters.duration = consts.minValue;
|
||||
parameters.composition = consts.compositionTypes.replace;
|
||||
return this.add(targets, parameters, position);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Callback<Timer>} callback
|
||||
* @param {TimelinePosition} [position]
|
||||
* @return {this}
|
||||
*/
|
||||
call(callback, position) {
|
||||
if (helpers.isUnd(callback) || callback && !helpers.isFnc(callback)) return this;
|
||||
if (globals.globals.editor && globals.globals.editor.addTimelineCall) position = globals.globals.editor.addTimelineCall(callback, position, this.id);
|
||||
return this.add({ duration: 0, delay: 0, onComplete: () => callback(this) }, position);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {String} labelName
|
||||
* @param {TimelinePosition} [position]
|
||||
* @return {this}
|
||||
*
|
||||
*/
|
||||
label(labelName, position$1) {
|
||||
if (helpers.isUnd(labelName) || labelName && !helpers.isStr(labelName)) return this;
|
||||
if (globals.globals.editor && globals.globals.editor.addTimelineLabel) position$1 = globals.globals.editor.addTimelineLabel(labelName, position$1, this.id);
|
||||
this.labels[labelName] = position.parseTimelinePosition(this, position$1);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {TargetsParam} targets
|
||||
* @param {String} [propertyName]
|
||||
* @return {this}
|
||||
*/
|
||||
remove(targets$1, propertyName) {
|
||||
composition.removeTargetsFromRenderable(targets.parseTargets(targets$1), this, propertyName);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Number} newDuration
|
||||
* @return {this}
|
||||
*/
|
||||
stretch(newDuration) {
|
||||
const currentDuration = this.duration;
|
||||
if (currentDuration === helpers.normalizeTime(newDuration)) return this;
|
||||
const timeScale = newDuration / currentDuration;
|
||||
const labels = this.labels;
|
||||
helpers.forEachChildren(this, (/** @type {JSAnimation} */child) => child.stretch(child.duration * timeScale));
|
||||
for (let labelName in labels) labels[labelName] *= timeScale;
|
||||
return super.stretch(newDuration);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {this}
|
||||
*/
|
||||
refresh() {
|
||||
helpers.forEachChildren(this, (/** @type {JSAnimation|Timer} */child) => {
|
||||
if (/** @type {JSAnimation} */(child).refresh) /** @type {JSAnimation} */(child).refresh();
|
||||
});
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {this}
|
||||
*/
|
||||
revert() {
|
||||
super.revert();
|
||||
helpers.forEachChildren(this, (/** @type {JSAnimation|Timer} */child) => child.revert, true);
|
||||
return styles.revertValues(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* @typedef {this & {then: null}} ResolvedTimeline
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {Callback<ResolvedTimeline>} [callback]
|
||||
* @return Promise<this>
|
||||
*/
|
||||
then(callback) {
|
||||
return super.then(callback);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {TimelineParams} [parameters]
|
||||
* @return {Timeline}
|
||||
*/
|
||||
const createTimeline = parameters => {
|
||||
if (globals.globals.editor) {
|
||||
return /** @type {Timeline} */(/** @type {unknown} */(globals.globals.editor.addTimeline(parameters)));
|
||||
}
|
||||
return new Timeline(parameters).init();
|
||||
};
|
||||
|
||||
exports.Timeline = Timeline;
|
||||
exports.createTimeline = createTimeline;
|
||||
+171
@@ -0,0 +1,171 @@
|
||||
export class Timeline extends Timer {
|
||||
/**
|
||||
* @param {TimelineParams} [parameters]
|
||||
*/
|
||||
constructor(parameters?: TimelineParams);
|
||||
/** @type {Record<String, Number>} */
|
||||
labels: Record<string, number>;
|
||||
/** @type {DefaultsParams} */
|
||||
defaults: DefaultsParams;
|
||||
/** @type {Boolean} */
|
||||
composition: boolean;
|
||||
/** @type {Callback<this>} */
|
||||
onRender: Callback<this>;
|
||||
_ease: import("../types/index.js").EasingFunction;
|
||||
/**
|
||||
* @overload
|
||||
* @param {TargetsParam} a1
|
||||
* @param {AnimationParams} a2
|
||||
* @param {TimelinePosition|StaggerFunction<Number|String>|TweakRegister} [a3]
|
||||
* @return {this}
|
||||
*
|
||||
* @overload
|
||||
* @param {TimerParams} a1
|
||||
* @param {TimelinePosition} [a2]
|
||||
* @return {this}
|
||||
*
|
||||
* @param {TargetsParam|TimerParams} a1
|
||||
* @param {TimelinePosition|AnimationParams} a2
|
||||
* @param {TimelinePosition|StaggerFunction<Number|String>|TweakRegister} [a3]
|
||||
*/
|
||||
add(a1: TargetsParam, a2: AnimationParams, a3?: TimelinePosition | StaggerFunction<number | string> | TweakRegister): this;
|
||||
/**
|
||||
* @overload
|
||||
* @param {TargetsParam} a1
|
||||
* @param {AnimationParams} a2
|
||||
* @param {TimelinePosition|StaggerFunction<Number|String>|TweakRegister} [a3]
|
||||
* @return {this}
|
||||
*
|
||||
* @overload
|
||||
* @param {TimerParams} a1
|
||||
* @param {TimelinePosition} [a2]
|
||||
* @return {this}
|
||||
*
|
||||
* @param {TargetsParam|TimerParams} a1
|
||||
* @param {TimelinePosition|AnimationParams} a2
|
||||
* @param {TimelinePosition|StaggerFunction<Number|String>|TweakRegister} [a3]
|
||||
*/
|
||||
add(a1: TimerParams, a2?: TimelinePosition): this;
|
||||
/**
|
||||
* @overload
|
||||
* @param {Tickable} [synced]
|
||||
* @param {TimelinePosition} [position]
|
||||
* @return {this}
|
||||
*
|
||||
* @overload
|
||||
* @param {globalThis.Animation} [synced]
|
||||
* @param {TimelinePosition} [position]
|
||||
* @return {this}
|
||||
*
|
||||
* @overload
|
||||
* @param {WAAPIAnimation} [synced]
|
||||
* @param {TimelinePosition} [position]
|
||||
* @return {this}
|
||||
*
|
||||
* @param {Tickable|WAAPIAnimation|globalThis.Animation} [synced]
|
||||
* @param {TimelinePosition} [position]
|
||||
*/
|
||||
sync(synced?: Tickable, position?: TimelinePosition): this;
|
||||
/**
|
||||
* @overload
|
||||
* @param {Tickable} [synced]
|
||||
* @param {TimelinePosition} [position]
|
||||
* @return {this}
|
||||
*
|
||||
* @overload
|
||||
* @param {globalThis.Animation} [synced]
|
||||
* @param {TimelinePosition} [position]
|
||||
* @return {this}
|
||||
*
|
||||
* @overload
|
||||
* @param {WAAPIAnimation} [synced]
|
||||
* @param {TimelinePosition} [position]
|
||||
* @return {this}
|
||||
*
|
||||
* @param {Tickable|WAAPIAnimation|globalThis.Animation} [synced]
|
||||
* @param {TimelinePosition} [position]
|
||||
*/
|
||||
sync(synced?: globalThis.Animation, position?: TimelinePosition): this;
|
||||
/**
|
||||
* @overload
|
||||
* @param {Tickable} [synced]
|
||||
* @param {TimelinePosition} [position]
|
||||
* @return {this}
|
||||
*
|
||||
* @overload
|
||||
* @param {globalThis.Animation} [synced]
|
||||
* @param {TimelinePosition} [position]
|
||||
* @return {this}
|
||||
*
|
||||
* @overload
|
||||
* @param {WAAPIAnimation} [synced]
|
||||
* @param {TimelinePosition} [position]
|
||||
* @return {this}
|
||||
*
|
||||
* @param {Tickable|WAAPIAnimation|globalThis.Animation} [synced]
|
||||
* @param {TimelinePosition} [position]
|
||||
*/
|
||||
sync(synced?: WAAPIAnimation, position?: TimelinePosition): this;
|
||||
/**
|
||||
* @param {TargetsParam} targets
|
||||
* @param {AnimationParams} parameters
|
||||
* @param {TimelinePosition|StaggerFunction<Number|String>|TweakRegister} [position]
|
||||
* @return {this}
|
||||
*/
|
||||
set(targets: TargetsParam, parameters: AnimationParams, position?: TimelinePosition | StaggerFunction<number | string> | TweakRegister): this;
|
||||
/**
|
||||
* @param {Callback<Timer>} callback
|
||||
* @param {TimelinePosition} [position]
|
||||
* @return {this}
|
||||
*/
|
||||
call(callback: Callback<Timer>, position?: TimelinePosition): this;
|
||||
/**
|
||||
* @param {String} labelName
|
||||
* @param {TimelinePosition} [position]
|
||||
* @return {this}
|
||||
*
|
||||
*/
|
||||
label(labelName: string, position?: TimelinePosition): this;
|
||||
/**
|
||||
* @param {TargetsParam} targets
|
||||
* @param {String} [propertyName]
|
||||
* @return {this}
|
||||
*/
|
||||
remove(targets: TargetsParam, propertyName?: string): this;
|
||||
/**
|
||||
* @param {Number} newDuration
|
||||
* @return {this}
|
||||
*/
|
||||
stretch(newDuration: number): this;
|
||||
/**
|
||||
* @return {this}
|
||||
*/
|
||||
refresh(): this;
|
||||
/**
|
||||
* @return {this}
|
||||
*/
|
||||
revert(): this;
|
||||
/**
|
||||
* @typedef {this & {then: null}} ResolvedTimeline
|
||||
*/
|
||||
/**
|
||||
* @param {Callback<ResolvedTimeline>} [callback]
|
||||
* @return Promise<this>
|
||||
*/
|
||||
then(callback?: Callback<this & {
|
||||
then: null;
|
||||
}>): Promise<any>;
|
||||
}
|
||||
export function createTimeline(parameters?: TimelineParams): Timeline;
|
||||
import { Timer } from '../timer/timer.js';
|
||||
import type { DefaultsParams } from '../types/index.js';
|
||||
import type { Callback } from '../types/index.js';
|
||||
import type { TargetsParam } from '../types/index.js';
|
||||
import type { AnimationParams } from '../types/index.js';
|
||||
import type { TimelinePosition } from '../types/index.js';
|
||||
import type { StaggerFunction } from '../types/index.js';
|
||||
import type { TweakRegister } from '../types/index.js';
|
||||
import type { TimerParams } from '../types/index.js';
|
||||
import type { Tickable } from '../types/index.js';
|
||||
import type { WAAPIAnimation } from '../waapi/waapi.js';
|
||||
import type { TimelineParams } from '../types/index.js';
|
||||
+352
@@ -0,0 +1,352 @@
|
||||
/**
|
||||
* Anime.js - timeline - ESM
|
||||
* @version v4.5.0
|
||||
* @license MIT
|
||||
* @copyright 2026 - Julian Garnier
|
||||
*/
|
||||
|
||||
import { globals } from '../core/globals.js';
|
||||
import { minValue, compositionTypes, tickModes } from '../core/consts.js';
|
||||
import { isUnd, mergeObjects, isObj, isFnc, isStr, normalizeTime, forEachChildren, isNum, addChild, clampInfinity } from '../core/helpers.js';
|
||||
import { setValue } from '../core/values.js';
|
||||
import { parseTargets } from '../core/targets.js';
|
||||
import { tick } from '../core/render.js';
|
||||
import { revertValues } from '../core/styles.js';
|
||||
import { Timer } from '../timer/timer.js';
|
||||
import { removeTargetsFromRenderable } from '../animation/composition.js';
|
||||
import { JSAnimation } from '../animation/animation.js';
|
||||
import { parseEase } from '../easings/eases/parser.js';
|
||||
import { parseTimelinePosition } from './position.js';
|
||||
|
||||
/**
|
||||
* @import {
|
||||
* TargetsParam,
|
||||
* Callback,
|
||||
* Tickable,
|
||||
* TimerParams,
|
||||
* AnimationParams,
|
||||
* Target,
|
||||
* Renderable,
|
||||
* TimelineParams,
|
||||
* DefaultsParams,
|
||||
* TimelinePosition,
|
||||
* StaggerFunction,
|
||||
* TargetsArray,
|
||||
* TweakRegister,
|
||||
* } from '../types/index.js'
|
||||
*/
|
||||
|
||||
/**
|
||||
* @import {
|
||||
* WAAPIAnimation,
|
||||
* } from '../waapi/waapi.js'
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {Timeline} tl
|
||||
* @return {Number}
|
||||
*/
|
||||
function getTimelineTotalDuration(tl) {
|
||||
return clampInfinity(((tl.iterationDuration + tl._loopDelay) * tl.iterationCount) - tl._loopDelay) || minValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* @overload
|
||||
* @param {TimerParams} childParams
|
||||
* @param {Timeline} tl
|
||||
* @param {Number} timePosition
|
||||
* @return {Timeline}
|
||||
*
|
||||
* @overload
|
||||
* @param {AnimationParams} childParams
|
||||
* @param {Timeline} tl
|
||||
* @param {Number} timePosition
|
||||
* @param {TargetsParam} targets
|
||||
* @param {Number} [index]
|
||||
* @param {TargetsArray} [allTargets]
|
||||
* @return {Timeline}
|
||||
*
|
||||
* @param {TimerParams|AnimationParams} childParams
|
||||
* @param {Timeline} tl
|
||||
* @param {Number} timePosition
|
||||
* @param {TargetsParam} [targets]
|
||||
* @param {Number} [index]
|
||||
* @param {TargetsArray} [allTargets]
|
||||
*/
|
||||
function addTlChild(childParams, tl, timePosition, targets, index, allTargets) {
|
||||
const isSetter = isNum(childParams.duration) && /** @type {Number} */(childParams.duration) <= minValue;
|
||||
// Offset the tl position with -minValue for 0 duration animations or .set() calls in order to align their end value with the defined position
|
||||
const adjustedPosition = isSetter ? timePosition - minValue : timePosition;
|
||||
if (tl.composition) tick(tl, adjustedPosition, 1, 1, tickModes.AUTO);
|
||||
const tlChild = targets ?
|
||||
new JSAnimation(targets,/** @type {AnimationParams} */(childParams), tl, adjustedPosition, false, index, allTargets) :
|
||||
new Timer(/** @type {TimerParams} */(childParams), tl, adjustedPosition);
|
||||
if (tl.composition) tlChild.init(true);
|
||||
// TODO: Might be better to insert at a position relative to startTime?
|
||||
addChild(tl, tlChild);
|
||||
forEachChildren(tl, (/** @type {Renderable} */child) => {
|
||||
const childTLOffset = child._offset + child._delay;
|
||||
const childDur = childTLOffset + child.duration;
|
||||
if (childDur > tl.iterationDuration) tl.iterationDuration = childDur;
|
||||
});
|
||||
tl.duration = getTimelineTotalDuration(tl);
|
||||
return tl;
|
||||
}
|
||||
|
||||
let TLId = 0;
|
||||
|
||||
class Timeline extends Timer {
|
||||
|
||||
/**
|
||||
* @param {TimelineParams} [parameters]
|
||||
*/
|
||||
constructor(parameters = {}) {
|
||||
super(/** @type {TimerParams&TimelineParams} */(parameters), null, 0);
|
||||
++TLId;
|
||||
/** @type {String|Number} */
|
||||
this.id = !isUnd(parameters.id) ? parameters.id : TLId;
|
||||
/** @type {Number} */
|
||||
this.duration = 0; // TL duration starts at 0 and grows when adding children
|
||||
/** @type {Record<String, Number>} */
|
||||
this.labels = {};
|
||||
const defaultsParams = parameters.defaults;
|
||||
const globalDefaults = globals.defaults;
|
||||
/** @type {DefaultsParams} */
|
||||
this.defaults = defaultsParams ? mergeObjects(defaultsParams, globalDefaults) : globalDefaults;
|
||||
/** @type {Boolean} */
|
||||
this.composition = setValue(parameters.composition, true);
|
||||
/** @type {Callback<this>} */
|
||||
this.onRender = parameters.onRender || globalDefaults.onRender;
|
||||
const tlPlaybackEase = setValue(parameters.playbackEase, globalDefaults.playbackEase);
|
||||
this._ease = tlPlaybackEase ? parseEase(tlPlaybackEase) : null;
|
||||
/** @type {Number} */
|
||||
this.iterationDuration = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @overload
|
||||
* @param {TargetsParam} a1
|
||||
* @param {AnimationParams} a2
|
||||
* @param {TimelinePosition|StaggerFunction<Number|String>|TweakRegister} [a3]
|
||||
* @return {this}
|
||||
*
|
||||
* @overload
|
||||
* @param {TimerParams} a1
|
||||
* @param {TimelinePosition} [a2]
|
||||
* @return {this}
|
||||
*
|
||||
* @param {TargetsParam|TimerParams} a1
|
||||
* @param {TimelinePosition|AnimationParams} a2
|
||||
* @param {TimelinePosition|StaggerFunction<Number|String>|TweakRegister} [a3]
|
||||
*/
|
||||
add(a1, a2, a3) {
|
||||
const isAnim = isObj(a2);
|
||||
const isTimer = isObj(a1);
|
||||
if (isAnim || isTimer) {
|
||||
this._hasChildren = true;
|
||||
if (isAnim) {
|
||||
const childParams = /** @type {AnimationParams} */(a2);
|
||||
const editorHook = globals.editor && globals.editor.addTimelineChild;
|
||||
const isStaggerType = a3 && /** @type {TweakRegister} */(a3).type === 'Stagger' && globals.editor;
|
||||
// Check for function or Stagger type children positions
|
||||
const staggeredPosition = isFnc(a3) ? a3 : null;
|
||||
if (staggeredPosition || isStaggerType) {
|
||||
const parsedTargetsArray = parseTargets(/** @type {TargetsParam} */(a1));
|
||||
// Store initial duration before adding new children that will change the duration
|
||||
const tlDuration = this.duration;
|
||||
// Store initial _iterationDuration before adding new children that will change the duration
|
||||
const tlIterationDuration = this.iterationDuration;
|
||||
// Store the original id in order to add specific indexes to the new animations ids
|
||||
const id = childParams.id;
|
||||
let i = 0;
|
||||
/** @type {Number} */
|
||||
const parsedLength = (parsedTargetsArray.length);
|
||||
// Call editor hook once for the entire stagger group instead of per target
|
||||
const resolvedParams = editorHook ? editorHook(/** @type {TargetsParam} */(a1), childParams, this.id, a3, parsedLength) : null;
|
||||
// Resolve stagger AFTER editor hook so tweaked position value (a3.defaultValue) is used
|
||||
const staggerFn = staggeredPosition || globals.editor.resolveStagger(/** @type {TweakRegister} */(a3).defaultValue);
|
||||
parsedTargetsArray.forEach((/** @type {Target} */target) => {
|
||||
// Create a new parameter object for each staggered children
|
||||
const staggeredChildParams = { ...(resolvedParams || childParams) };
|
||||
// Reset the duration of the timeline iteration before each stagger to prevent wrong start value calculation
|
||||
this.duration = tlDuration;
|
||||
this.iterationDuration = tlIterationDuration;
|
||||
if (!isUnd(id)) staggeredChildParams.id = id + '-' + i;
|
||||
const staggeredTimePosition = parseTimelinePosition(this, staggerFn(target, i, parsedTargetsArray, null, this));
|
||||
addTlChild(
|
||||
staggeredChildParams,
|
||||
this,
|
||||
staggeredTimePosition,
|
||||
target,
|
||||
i,
|
||||
parsedTargetsArray,
|
||||
);
|
||||
i++;
|
||||
});
|
||||
} else {
|
||||
// Call editor hook before resolving position so tweaked values are applied
|
||||
const resolvedChildParams = editorHook ? editorHook(/** @type {TargetsParam} */(a1), childParams, this.id, a3) : childParams;
|
||||
const resolvedPosition = a3 && /** @type {*} */(a3).type ? /** @type {*} */(a3).defaultValue : a3;
|
||||
addTlChild(
|
||||
resolvedChildParams,
|
||||
this,
|
||||
parseTimelinePosition(this, resolvedPosition),
|
||||
/** @type {TargetsParam} */(a1),
|
||||
);
|
||||
}
|
||||
} else {
|
||||
// It's a Timer
|
||||
addTlChild(
|
||||
/** @type TimerParams */(a1),
|
||||
this,
|
||||
parseTimelinePosition(this,a2),
|
||||
);
|
||||
}
|
||||
if (this.composition) this.init(true);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @overload
|
||||
* @param {Tickable} [synced]
|
||||
* @param {TimelinePosition} [position]
|
||||
* @return {this}
|
||||
*
|
||||
* @overload
|
||||
* @param {globalThis.Animation} [synced]
|
||||
* @param {TimelinePosition} [position]
|
||||
* @return {this}
|
||||
*
|
||||
* @overload
|
||||
* @param {WAAPIAnimation} [synced]
|
||||
* @param {TimelinePosition} [position]
|
||||
* @return {this}
|
||||
*
|
||||
* @param {Tickable|WAAPIAnimation|globalThis.Animation} [synced]
|
||||
* @param {TimelinePosition} [position]
|
||||
*/
|
||||
sync(synced, position) {
|
||||
if (isUnd(synced) || synced && isUnd(synced.pause)) return this;
|
||||
synced.pause();
|
||||
const duration = +(/** @type {globalThis.Animation} */(synced).effect ? /** @type {globalThis.Animation} */(synced).effect.getTiming().duration : /** @type {Tickable} */(synced).duration);
|
||||
// Forces WAAPI Animation to persist; otherwise, they will stop syncing on finish.
|
||||
if (!isUnd(synced) && !isUnd(/** @type {WAAPIAnimation} */(synced).persist)) {
|
||||
/** @type {WAAPIAnimation} */(synced).persist = true;
|
||||
}
|
||||
const editor = globals.editor;
|
||||
const childHook = editor && editor.addTimelineChild;
|
||||
if (editor && editor.addTimelineSync) {
|
||||
position = editor.addTimelineSync(synced, position, this.id);
|
||||
editor.addTimelineChild = null; // Suppress the per-child hook for the internal .add, sync already registered.
|
||||
}
|
||||
const result = this.add(synced, { currentTime: [0, duration], duration, delay: 0, ease: 'linear', playbackEase: 'linear' }, position);
|
||||
if (editor) editor.addTimelineChild = childHook;
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {TargetsParam} targets
|
||||
* @param {AnimationParams} parameters
|
||||
* @param {TimelinePosition|StaggerFunction<Number|String>|TweakRegister} [position]
|
||||
* @return {this}
|
||||
*/
|
||||
set(targets, parameters, position) {
|
||||
if (isUnd(parameters)) return this;
|
||||
parameters.duration = minValue;
|
||||
parameters.composition = compositionTypes.replace;
|
||||
return this.add(targets, parameters, position);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Callback<Timer>} callback
|
||||
* @param {TimelinePosition} [position]
|
||||
* @return {this}
|
||||
*/
|
||||
call(callback, position) {
|
||||
if (isUnd(callback) || callback && !isFnc(callback)) return this;
|
||||
if (globals.editor && globals.editor.addTimelineCall) position = globals.editor.addTimelineCall(callback, position, this.id);
|
||||
return this.add({ duration: 0, delay: 0, onComplete: () => callback(this) }, position);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {String} labelName
|
||||
* @param {TimelinePosition} [position]
|
||||
* @return {this}
|
||||
*
|
||||
*/
|
||||
label(labelName, position) {
|
||||
if (isUnd(labelName) || labelName && !isStr(labelName)) return this;
|
||||
if (globals.editor && globals.editor.addTimelineLabel) position = globals.editor.addTimelineLabel(labelName, position, this.id);
|
||||
this.labels[labelName] = parseTimelinePosition(this, position);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {TargetsParam} targets
|
||||
* @param {String} [propertyName]
|
||||
* @return {this}
|
||||
*/
|
||||
remove(targets, propertyName) {
|
||||
removeTargetsFromRenderable(parseTargets(targets), this, propertyName);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Number} newDuration
|
||||
* @return {this}
|
||||
*/
|
||||
stretch(newDuration) {
|
||||
const currentDuration = this.duration;
|
||||
if (currentDuration === normalizeTime(newDuration)) return this;
|
||||
const timeScale = newDuration / currentDuration;
|
||||
const labels = this.labels;
|
||||
forEachChildren(this, (/** @type {JSAnimation} */child) => child.stretch(child.duration * timeScale));
|
||||
for (let labelName in labels) labels[labelName] *= timeScale;
|
||||
return super.stretch(newDuration);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {this}
|
||||
*/
|
||||
refresh() {
|
||||
forEachChildren(this, (/** @type {JSAnimation|Timer} */child) => {
|
||||
if (/** @type {JSAnimation} */(child).refresh) /** @type {JSAnimation} */(child).refresh();
|
||||
});
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {this}
|
||||
*/
|
||||
revert() {
|
||||
super.revert();
|
||||
forEachChildren(this, (/** @type {JSAnimation|Timer} */child) => child.revert, true);
|
||||
return revertValues(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* @typedef {this & {then: null}} ResolvedTimeline
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {Callback<ResolvedTimeline>} [callback]
|
||||
* @return Promise<this>
|
||||
*/
|
||||
then(callback) {
|
||||
return super.then(callback);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {TimelineParams} [parameters]
|
||||
* @return {Timeline}
|
||||
*/
|
||||
const createTimeline = parameters => {
|
||||
if (globals.editor) {
|
||||
return /** @type {Timeline} */(/** @type {unknown} */(globals.editor.addTimeline(parameters)));
|
||||
}
|
||||
return new Timeline(parameters).init();
|
||||
};
|
||||
|
||||
export { Timeline, createTimeline };
|
||||
Reference in New Issue
Block a user