提供基本的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国 | 紧张,用于弹簧松动功能 |
| 摩擦 | 数目 | 无 | 页:1 | Friction,用于弹簧松动功能 |
实例方法
| 名称 | 参数 | 返回时 | 说明 |
|---|---|---|---|
| 开始 | 开始动画 | ||
| 暂停 | 暂停动画 挖掘后,您必须使用恢复方法继续播放,而不是使用开始 . | ||
| 恢复 | 继续动画 只有动画暂停时才有效果. | ||
| 倒转 | 倒转动画 | ||
| 停下来 | 停止动画。 只在动画重播、暂停和结束后产生效果 | ||
| 结束 | 立即终止动画,并将动画的最终状态值传递给回调方法 | ||
| 重设 | 重置动画 | ||
| 销毁 | 摧毁动画 | ||
| 获得初始状态 | 对象 | 获取初始状态 | |
| 获取当前状态 | 对象 | 获取当前状态 | |
| 获取最终状态 | 对象 | 获取最终状态 | |
| 打开 | 事件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 => { /* ... */ });
许可证
麻省理工学院