@douyinfe/semi-animation

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

半自定义动画基础库

中文使用文档
文档目录5 节
  1. 安装
  2. 使用量
  3. 动画
  4. API 密码
  5. 许可证

提供基本的JS动画引擎:

  • 根据内插模拟动画变化,性能更自然
  • 支持界定各种放松功能
  • 提供完整的生命周期钩和操作方法,允许开发者自由控制动画

安装

npm install @douyinfe/semi-animation

使用量

动画

semi-animation 提供名为 Animation . 它拥有完整的生命周期钩和控制方法,以支持音频和视频等操作动画.

  • 用于联署材料
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;
});
  • 在反应中使用
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>;
    }
}
  • 在 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>

显示结果 :

API 密码

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

道具

Propa 名称类型需求默认说明
从对象Y初始状态
改为对象Y终止状态

配置

Propa 名称类型需求默认说明
会期数目无1 000个动画持续时间. 如果通过此参数, 动画的放松功能将使用松动或线性函数, unit: ms
放宽函数 QQ 串无减轻动画功能. 如果持续时间没有通过,则默认使用弹簧松动功能. 如果持续时间参数被传入,则默认会使用线性宽松函数. 例如,收到的 "cubic-bezier(.17,.67,.83,.67)" 将会使动画帧更新按照此轻松功能进行
紧张数目无170国紧张,用于弹簧松动功能
摩擦数目无页:1Friction,用于弹簧松动功能

实例方法

名称参数返回时说明
开始开始动画
暂停暂停动画 挖掘后,您必须使用恢复方法继续播放,而不是使用开始 .
恢复继续动画 只有动画暂停时才有效果.
倒转倒转动画
停下来停止动画。 只在动画重播、暂停和结束后产生效果
结束立即终止动画,并将动画的最终状态值传递给回调方法
重设重置动画
销毁摧毁动画
获得初始状态对象获取初始状态
获取当前状态对象获取当前状态
获取最终状态对象获取最终状态
打开事件Name:字符串, 事件Handler: 功能( 程序: 对象): 无效绑定事件调用方法。 每个调用收到的参数是当前动画状态对象 .

支助活动

  • start: 动画开始时触发
  • pause:动画暂停时触发(当动画状态从播放变为暂停时触发事件,在其他情况下不会被触发)
  • resume: 当动画继续播放时触发( 当动画状态从暂停状态变为播放状态时触发事件, 而在其他情况下不会被触发)
  • frame: 动画框架更新时触发
  • rest: 当动画结束时触发( 当动画正常结束时事件会被触发)
  • stop: 动画停止时触发( 当实例停止方法调用时触发此事件)

开发者可以使用 animation.on(eventName: string, cb: Function(currentStyle: object)) 以约束上述事件.

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 => { /* ... */ });

许可证

麻省理工学院

版本与发布记录

@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 是什么?

半自定义动画基础库

如何安装 @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 和提交记录判断。