hooks 与 animejs

本文写于 2020 年 1 月 13 日

animejs 是现如今非常不错的一个 js 动画库。我们将其与 React Hooks 融合,使它更方便的在 React 中使用。

最终效果:

const Animate: React.FC = () => {
const { animateTargetRef, animationRef } = useAnime({
translateX: 300,
loop: true,
duration: 2000,
autoplay: false,
}); useEffect(() => {
setTimeout(() => {
animationRef.current?.play?.();
}, 3000);
}, [animationRef]); return (
<div
ref={animateTargetRef}
style={{ width: '100px', height: '100px', backgroundColor: 'black' }}
/>
);
};

首先看看 animejs 在一般的 JS 和 HTML 中如何使用:

<div class="xxx"></div>
import anime from 'animejs';

const animation = anime({
translateX: 300,
loop: true,
duration: 2000,
autoplay: false,
targets: '.xxx',
}); animation.play();

但是在 React 中,我们不想要到处写这些玩意儿。我们喜欢 hooks!

所以我们可以封装一个 useAnime hook。

第一步,我们可以引入 AnimeParams,让我们的 hook 拥有代码提示:

import anime, { AnimeParams } from 'animejs';

export const useAnime = (props: AnimeParams) => {
anime(props);
};

然后我们通过 useRef 将 anime 的 targets 绑定,并且暴露出去,供其他地方使用。

//...
const animateTargetRef = useRef<any>(null); useLayoutEffect(() => {
if (!animationTargetRef.current) {
console.warn('please bind animation target ref');
return;
}
animate({
...props,
targets: [animationTargetRef.current],
});
}, [props]); return { animateTargetRef };
//...

通过观察发现,animate 返回的是 AnimeInstance 类型,我们从 animejs 中导入:

import anime, { AnimeParams, AnimeInstance } from 'animejs';

// ...

const animationRef = useRef<AnimeInstance>();

// ...
animationRef.current = animate({
...props,
targets: [animationTargetRef.current],
});
// ... return { animateTargetRef, animationRef };

这样就轻松完成了 useAnime 的封装。

完整代码:

import anime, { AnimeParams, AnimeInstance } from 'animejs';
import { useRef, useLayoutEffect } from 'react'; export const useAnime = (props: AnimeParams = {}) => {
const animateTargetRef = useRef<any>();
const animationRef = useRef<AnimeInstance>(); useLayoutEffect(() => {
if (!animateTargetRef.current) {
console.warn('please bind the anime ref while useAnime');
return;
}
animationRef.current = anime({
...props,
targets: [animateTargetRef.current],
});
}, [props]); return { animateTargetRef, animationRef };
};

(完)

最新文章

  1. Ajax基础(小猫)
  2. SQL Server 变更数据捕获(CDC)监控表数据
  3. Linq专题之提高编码效率—— 第三篇 你需要知道的枚举类
  4. Android Studio Tips and Tricks
  5. winform C#获得Mac地址,IP地址,子网掩码,默认网关
  6. [转载] Go语言并发之美
  7. C# 子窗体点击按钮产生的新子窗体放在父窗体里
  8. Android -- Support包特性
  9. s3c-u-boot-1.1.6源码分析之一start.s
  10. QT QSqlQuery QSqlQueryModel
  11. vs2008 edit spin 十六进制实现
  12. Bootstrap Alert 使用
  13. asp.net下cookie 的基础使用
  14. 【剑指offer】二叉搜索树转双向链表
  15. .NET基础——ASSCII码表
  16. PHP成长之路之PHP连接MySql数据库(一)
  17. The `XXXX` target overrides the `HEADER_SEARCH_PATHS` build setting defined in `Pods/Target Support Files/Pods-game-desktop/Pods-game-desktop.release.xcconfig&#39;. This can lead to prob
  18. [学习OpenCV攻略][001][Ubuntu安装及配置]
  19. Tomcat系列(9)——Tomcat 6方面调优(内存,线程,IO,压缩,缓存,集群)
  20. nodejs EventEmitter 发送消息

热门文章

  1. 乱序数组中第k大的数(顺序统计量)
  2. 攻防世界 Ditf misc
  3. 12_非线性理论基础_Lyapunov直接方法
  4. 静态变量和成员变量的区别、final修饰特点、创建对象的内存图、静态内存图
  5. 防止自己的页面不被其他网站的页面的iframe引用
  6. Centos6.9 安装zabbix3.4 过程
  7. 使用babel编译器将es6代码转为es5的方法
  8. 调试了一个早上, 定位了一个chrome的新问题, 新版chrome 不能有效的追踪客户来源Referer了
  9. 在oracle控制台当你输入错误的时候,还不能删除,回退的解决方法
  10. Prometheus介绍及docker安装方式