/
githubmirror
/
anime
Обзор
Документация
Войти
/
githubmirror
/
anime
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
v4.0.0
src/animatable.js
119 строк
3 KB
Julian Garnier
v4.0.0
03 апр 2025, 16:31
03 апр 2025, 16:31
66cfea7
Код
Авторство
О чём код?
/// <reference path='./types.js' /> import { compositionTypes, noop, } from './consts.js'; import { globals, } from './globals.js'; import { isKey, isObj, isStr, isUnd, mergeObjects, forEachChildren, isArr, } from './helpers.js'; import { JSAnimation, } from './animation.js'; import { parseEasings, } from './eases.js'; export class Animatable { /** * @param {TargetsParam} targets * @param {AnimatableParams} parameters */ constructor(targets, parameters) { if (globals.scope) globals.scope.revertibles.push(this); /** @type {AnimationParams} */ const globalParams = {}; const properties = {}; this.targets = []; this.animations = {}; if (isUnd(targets) || isUnd(parameters)) return; for (let propName in parameters) { const paramValue = parameters[propName]; if (isKey(propName)) { properties[propName] = paramValue; } else { globalParams[propName] = paramValue; } } for (let propName in properties) { const propValue = properties[propName]; const isObjValue = isObj(propValue); /** @type {TweenParamsOptions} */ let propParams = {}; let to = '+=0'; if (isObjValue) { const unit = propValue.unit; if (isStr(unit)) to += unit; } else { propParams.duration = propValue; } propParams[propName] = isObjValue ? mergeObjects({ to }, propValue) : to; const animParams = mergeObjects(globalParams, propParams); animParams.composition = compositionTypes.replace; animParams.autoplay = false; const animation = this.animations[propName] = new JSAnimation(targets, animParams, null, 0, false).init(); if (!this.targets.length) this.targets.push(...animation.targets); /** @type {AnimatableProperty} */ this[propName] = (to, duration, ease) => { const tween = /** @type {Tween} */(animation._head); if (isUnd(to) && tween) { const numbers = tween._numbers; if (numbers && numbers.length) { return numbers; } else { return tween._modifier(tween._number); } } else { forEachChildren(animation, (/** @type {Tween} */tween) => { if (isArr(to)) { for (let i = 0, l = /** @type {Array} */(to).length; i < l; i++) { if (!isUnd(tween._numbers[i])) { tween._fromNumbers[i] = /** @type {Number} */(tween._modifier(tween._numbers[i])); tween._toNumbers[i] = to[i]; } } } else { tween._fromNumber = /** @type {Number} */(tween._modifier(tween._number)); tween._toNumber = /** @type {Number} */(to); } if (!isUnd(ease)) tween._ease = parseEasings(ease); tween._currentTime = 0; }); if (!isUnd(duration)) animation.stretch(duration); animation.reset(1).resume(); return this; } }; } } revert() { for (let propName in this.animations) { this[propName] = noop; this.animations[propName].revert(); } this.animations = {}; this.targets.length = 0; return this; } } /** * @param {TargetsParam} targets * @param {AnimatableParams} parameters * @return {AnimatableObject} */ export const createAnimatable = (targets, parameters) => /** @type {AnimatableObject} */(new Animatable(targets, parameters));