@douyinfe/semi-animation

中文文档
2.105.0发布于 2026/11/12MITTypeScriptESM + CJS近 30 天下载 8.7万

animation base library for semi-ui

原始使用文档
文档目录5 节
  1. Install
  2. Usage
  3. Animation
  4. API
  5. Licence

Provides basic JS animation engine:

  • Simulate animation changes based on interpolation,the performance is more natural
  • Support the definition of various easing functions
  • Provides a complete life cycle hook and operation method, allowing developers to freely control the animation

Install

npm install @douyinfe/semi-animation

Usage

Animation

semi-animation provides a class called Animation . It has a complete life cycle hook and control method to support operating animation like audio and video.

  • Use in JS
import { Animation } from '@douyinfe/semi-animation';

const div = document.createElement('span');
div.style.display = 'inline-block';
document.body.appendChild(div);

const animation = new Animation({
    from: { value: 0 },
    to: { value: 1 },
});

animation.on('frame', props => {
    const num = props.value.toFixed(2);
    div.style.transform = `scale(${num})`;
    div.innerText = num;
});
  • Use in React
import { Animation } from '@douyinfe/semi-animation';
import { Component } from 'react';

class App extends Component {
    constructor(props) {
        super(props);
        this.state = { value: 0 };

        this.animation = new Animation({
            from: { value: 0 },
            to: { value: 1 },
        });

        this.animation.on('frame', props => {
            this.setState({ value: props.value.toFixed(2) });
        });
    }

    componentDidMount() {
        this.animation.start();
    }

    componentWillUnmount() {
        this.animation.destroy();
    }

    render() {
        const { value } = this.state;

        return <div style={{ display: 'inline-block', transform: `scale(${value})` }}>{value}</div>;
    }
}
  • Use in Vue
<template>
    <div :style="{ transform: `scale(${value})`, display: 'inline-block' }">{{value}}</div>
</template>

<script>
    import { Animation } from '@douyinfe/semi-animation';

    export default {
        data() {
            return { value: 0 };
        },
        created() {
            this.animation = new Animation({
                from: { value: 0 },
                to: { value: 1 },
            });

            this.animation.on('frame', props => {
                this.value = props.value;
            });
        },
        mounted() {
            this.animation.start();
        },
        beforeDestroy() {
            this.animation.destroy();
        },
    };
</script>

Show results:

API

new Animation({ ...props }, { ...config });

props

Prop NameTypeRequiredDefaultDescription
fromObjectYInitial state
toObjectYTermination state

config

Prop NameTypeRequiredDefaultDescription
durationNumberN1000Animation duration. If this parameter is passed in, the easing function of the animation will use easing or linear function,unit: ms
easingFunction|StringNEasing function for animation. If duration is not passed, the spring easing function is used by default. If the duration parameter is passed in, the linear easing function will be used by default.For example, incoming "cubic-bezier(.17,.67,.83,.67)" will cause the animation frame update performed according to this easing function
tensionNumberN170Tension, used for spring easing function
frictionNumberN14Friction, used for spring easing function

Instance methods

NameParamsReturnDescription
startStart the animation
pausePause the animation.After pausing, you must use the resume method to continue playing, not using start.
resumeContinue the animation.Only has an effect when the animation is paused.
reverseReverse the animation
stopStop the animation.Only have an effect during animation playback, pause, and after end
endImmediately terminate the animation, and pass the final state value of the animation to the callback method
resetReset the animation
destroyDestroy the animation
getInitialStatesObjectGet the initial state
getCurrentStatesObjectGet the current state
getFinalStatesObjectGet the final state
oneventName:string, eventHandler:Function(props: object): voidBinding event callback method。The parameters received by each callback are the current animation state objects.

Supported events

  • start: Triggered when the animation starts
  • pause: Triggered when the animation pauses(The event is triggered when the animation state is changed from playing to pause, and it will not be triggered in other cases)
  • resume: Triggered when the animation continues to play(The event is triggered when the animation state is changed from paused to playing, and it will not be triggered in other cases)
  • frame: Triggered when the animation frame is updated
  • rest: Triggered when the animation ends(The event will be triggered when the animation ends normally)
  • stop: Triggered when the animation stops(This event is triggered when the instance stop method is called)

Developers can use animation.on(eventName: string, cb: Function(currentStyle: object)) to bind the above events.

import { Animation } from '@douyinfe/semi-animation';
// ...
const animation = new Animation(
    {
        from: { value1: 0, value2: 1 /* ... */ },
        to: { value1: 1, value2: 2 /* ... */ },
    },
    {
        duration: 1000, // After passing in duration, the default is linear interpolation
    }
);

animation.on('frame', currentState => {
    // currentState: is the state value object at the current moment
    // { value1: xxx, value2: xxx, ... }
});

// The callbacks such as start and pause are the same as the frame above, and the parameters are also the same.
animation.on('start', currentState => { /* ... */ });
animation.on('pause', currentState => { /* ... */ });
animation.on('resume', currentState => { /* ... */ });
animation.on('rest', currentState => { /* ... */ });
animation.on('stop', currentState => { /* ... */ });

Licence

MIT

版本与发布记录

@douyinfe/semi-animation 共发布 590 个版本,首次发布于 2021/10/25;过去 12 个月发布了 67 个版本。

版本发布日期距今
2.105.0latest2026/11/121 天前
2.104.02026/10/091 个月前
2.103.02026/09/012 个月前
2.102.02026/07/313 个月前
2.38.4-alpha.2-3117-31202026/07/293 个月前
2.101.12026/07/203 个月前

其他发布通道:next → 2.1.0-next.1,2.18.1-alpha.0 → 2.18.1-alpha.0,test → 2.38.3-alpha.4,patch-table → 2.38.3-alpha.2-patch-table,patch-tooltip → 2.70.1-alpha.1-patch-tooltip,2.58.0-hotfix-aria → 2.58.0-hotfix-aria。

关于 @douyinfe/semi-animation 的常见问题

@douyinfe/semi-animation 是什么?

animation base library for semi-ui

如何安装 @douyinfe/semi-animation?

在项目目录运行 npm install @douyinfe/semi-animation,或使用 pnpm add / yarn add / bun add。

@douyinfe/semi-animation 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@douyinfe/semi-animation 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@douyinfe/semi-animation 可以商用吗?

@douyinfe/semi-animation 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@douyinfe/semi-animation 的下载量有多少?

根据 npm 官方下载统计,@douyinfe/semi-animation 在 2026/10/13 – 2026/11/11 这 30 天内被下载 87,380 次,日均约 2,913 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@douyinfe/semi-animation 还在维护吗?

最新版本 2.105.0 发布于 2026/11/12(1 天前);至今共发布 590 个版本,其中过去 12 个月发布了 67 个;首次发布于 2021/10/25。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。