UNPKG

devextreme

Version:

JavaScript/TypeScript Component Suite for Responsive Web Development

217 lines (213 loc) • 6.85 kB
/** * DevExtreme (esm/__internal/viz/core/renderers/animation.js) * Version: 26.1.6 * Build date: Fri Oct 16 2026 * * Copyright (c) 2012 - 2026 Developer Express Inc. ALL RIGHTS RESERVED * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/ */ import { cancelAnimationFrame, requestAnimationFrame } from "../../../common/core/animation/frame"; export const noop = function() {}; export const easingFunctions = { easeOutCubic: (pos, start, end) => 1 === pos ? end : (1 - (1 - pos) ** 3) * (end - start) + +start, linear: (pos, start, end) => 1 === pos ? end : pos * (end - start) + +start }; export const animationSvgStep = { segments(elem, params, progress, easing, currentParams) { const { from: from } = params; const { to: to } = params; let curSeg; let seg; let i; let j; const segments = []; for (i = 0; i < from.length; i++) { curSeg = from[i]; seg = [curSeg[0]]; if (curSeg.length > 1) { for (j = 1; j < curSeg.length; j++) { seg.push(easing(progress, curSeg[j], to[i][j])) } } segments.push(seg) } currentParams.segments = params.end && 1 === progress ? params.end : segments; elem.attr({ segments: segments }) }, arc(elem, params, progress, easing) { const { from: from } = params; const { to: to } = params; const current = {}; for (const i in from) { current[i] = easing(progress, from[i], to[i]) } elem.attr(current) }, transform(elem, params, progress, easing, currentParams) { const { from: from } = params; const { to: to } = params; const current = {}; for (const i in from) { current[i] = currentParams[i] = easing(progress, from[i], to[i]) } elem.attr(current) }, base(elem, params, progress, easing, currentParams, attributeName) { const obj = {}; obj[attributeName] = currentParams[attributeName] = easing(progress, params.from, params.to); elem.attr(obj) }, _: noop, complete(element, currentSettings) { element.attr(currentSettings) } }; function step(now) { const that = this; const animateStep = that._animateStep; let attrName; that._progress = that._calcProgress(now); for (attrName in that.params) { const anim = animateStep[attrName] || animateStep.base; anim(that.element, that.params[attrName], that._progress, that._easing, that._currentParams, attrName) } that.options.step && that.options.step(that._easing(that._progress, 0, 1), that._progress); if (1 === that._progress) { return that.stop() } return true } function delayTick(now) { if (now - this._startTime >= this.delay) { this.tick = step } return true } function start(now) { this._startTime = now; this.tick = this.delay ? delayTick : step; return true } export class Animation { constructor(element, params, options) { this._progress = 0; this.element = element; this.params = params; this.options = options; this.duration = options.partitionDuration ? options.duration * options.partitionDuration : options.duration; this.delay = options.delay && options.duration * options.delay || 0; this._animateStep = options.animateStep || animationSvgStep; this._easing = easingFunctions[options.easing] || easingFunctions.easeOutCubic; this._currentParams = {}; this.tick = start } _calcProgress(now) { return Math.min(1, (now - this.delay - this._startTime) / this.duration) } stop(disableComplete) { const { options: options } = this; const animateStep = this._animateStep; this.stop = this.tick = noop; animateStep.complete && animateStep.complete(this.element, this._currentParams); options.complete && !disableComplete && options.complete() } } export let AnimationController = class { constructor(element) { this._animationCount = 0; this._timerId = null; this._animations = {}; this.element = element } _loop() { const animations = this._animations; let activeAnimation = 0; const now = (new Date).getTime(); let an; const endAnimation = this._endAnimation; for (an in animations) { if (!animations[an].tick(now)) { delete animations[an] } activeAnimation++ } if (0 === activeAnimation) { this.stop(); this._endAnimationTimer = endAnimation && setTimeout(() => { if (0 === this._animationCount) { endAnimation(); this._endAnimation = null } }); return } this._timerId = requestAnimationFrame.call(null, () => { this._loop() }, this.element) } addAnimation(animation) { this._animations[this._animationCount++] = animation; clearTimeout(this._endAnimationTimer); if (!this._timerId) { clearTimeout(this._startDelay); this._startDelay = setTimeout(() => { this._timerId = 1; this._loop() }, 0) } } animateElement(elem, params, options) { if (elem && params && options) { elem.animation && elem.animation.stop(); this.addAnimation(elem.animation = new Animation(elem, params, options)) } } onEndAnimation(endAnimation) { this._animationCount ? this._endAnimation = endAnimation : endAnimation() } dispose() { this.stop(); this.element = null } stop() { this._animations = {}; this._animationCount = 0; cancelAnimationFrame(this._timerId); clearTimeout(this._startDelay); clearTimeout(this._endAnimationTimer); this._timerId = null } lock() { let an; const animations = this._animations; let unstoppable; let hasUnstoppableInAnimations; for (an in animations) { unstoppable = animations[an].options.unstoppable; hasUnstoppableInAnimations = hasUnstoppableInAnimations || unstoppable; if (!unstoppable) { animations[an].stop(true); delete animations[an] } }!hasUnstoppableInAnimations && this.stop() } };