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 };
};

(完)

hooks 与 animejs的更多相关文章

  1. 使用 Git Hooks 实现自动项目部署

    最近在某服务器上面搭建 git 开发和部署环境,git 开发环境很简单,按照 ProGit 一书的相关知识就可以轻松搞定,实现了类似 Github 的使用 SSH + 私有 Clone 的方式. 关于 ...

  2. CI框架之HOOKS使用流程及原理

        Ci框架中Hooks可以理解:在框架的执行流程过程中,允许开发者在固定的某些时间点上(如:调用控制器前,调用控制器后等时间点上),调用其他函数来扩充CI框架执行流程的一种方法.技术上来就是通过 ...

  3. 深入浏览器兼容 细数jQuery Hooks 属性篇

    关于钩子:http://www.cnblogs.com/aaronjs/p/3387906.html 本章的目的很简单,通过钩子函数更细节的了解浏览器差异与处理方案, 版本是2.0.3所以不兼容ie6 ...

  4. jQuery-1.9.1源码分析系列(七) 钩子(hooks)机制及浏览器兼容

    处理浏览器兼容问题实际上不是jQuery的精髓,毕竟让技术员想方设法取弥补浏览器的过错从而使得代码乱七八糟不是个好事.一些特殊情况的处理,完全实在浪费浏览器的性能:突兀的兼容解决使得的代码看起来既不美 ...

  5. 如何创建一个GitLab Web Hooks?

    Git Hooks Git 能在特定的重要动作发生时触发自定义的脚本. 这些脚本都被存储在 Git 目录下的 hooks 子目录中(.git/hooks).当 git init 初始化一个仓库时,Gi ...

  6. (翻译)Emacs Hooks

    Table of Contents 1. 51.2.2 Hooks 51.2.2 Hooks Hooks(钩子或挂钩,为了保持文章的纯正性,这种专有名词不做翻译,后续以hooks为主),是定制化Ema ...

  7. ubantu svn 安装、卸载、配置hooks

    1.安装之前先看是否已经安装了 svn -version 若已经安装会有以下提示,若没有安装,进行下一步 若想卸载了执行命令 ( sudo apt-get remove --purge subvers ...

  8. linux svn hooks代码自动更新至项目

    由于开发移动端web,ui需要及时看到样式变化,所以通过svn hooks(钩子)来提交文件,然后再把文件同步到测试服务器项目目录,步骤如下: 1.进入 /home/svn/cmall/hooks ( ...

  9. php利用svn hooks将程序自动发布到测试环境

    利用svn hooks将php程序自动发布到测试环境 复制仓库hooks目录下的post-commit.tmpl为post-commit cp post-commit.tmpl post-commit ...

随机推荐

  1. @Controller 注解?

    该注解表明该类扮演控制器的角色,Spring 不需要你继承任何其他控制器基类或 引用 Servlet API.

  2. 集合学习之"将集合对象List<Product>转换为Map"

    将集合对象List<Product>转换为Map key = Product对象的sku value =Product对象 1 List<Product> products = ...

  3. SCTF 2018_Simple PHP Web

    SCTF 2018_Simple PHP Web 进入环境注意观察url http://www.bmzclub.cn:23627/?f=login.php 有点像是文件读取我们尝试读一下/etc/pa ...

  4. CSS 3-浮动、定位

    文档流 文档流是一种默认定位方式,在文档流中元素框的位置由元素在html中的位置决定,文档流中元素的position属性为默认的static或继承来的static并按照普通流定位.块级元素独占一行,自 ...

  5. three.js 入门详解(一)

    1. 概述 1.1 什么是WebGL? WebGL是在浏览器中实现三维效果的一套规范 想要使用WebGL原生的API来写3D效果的话,很吃力.three.js是WebGL的一个开源框架,它省去了很多麻 ...

  6. es6零碎记忆

    1:substring 和 substr var str = '0123456789' console.log(str.substring(1)); //123456789 console.log(s ...

  7. 快如闪电,触控优先。新一代的纯前端控件集 WijmoJS发布新版本了

    全球最大的控件提供商葡萄城宣布,新一代纯前端控件 WijmoJS 发布2018 v1 版本,进一步增强产品功能,并支持在 Npm 上的安装和发布,极大的提升了产品的易用性. WijmoJS 是用 Ty ...

  8. java中StringBuffer的用法

    2.StringBuffer StringBuffer:String类同等的类,它允许字符串改变(原因见上一段所说).Overall, this avoids creating many tempor ...

  9. 论文阅读总结-Patient clustering improves efficiency of federated machine learning to predict mortality and hospital stay time using distributed electronic medical records

    一.论文提出的方法: 使用进入ICU前48h的用药特征作为预测因子预测重症监护患者的死亡率和ICU住院时间. 用到了联邦学习,自编码器,k-means聚类算法,社区检测. 数据集:从50家患者人数超过 ...

  10. SpringMVC异常(404,接收参数类型转换错误)

    内容 一.异常信息 HTTP Status 400 - type Status report message description The request sent by the client wa ...