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. 学习Apache(四)

    介绍 Apache HTTP 服务器被设计为一个功能强大,并且灵活的 web 服务器, 可以在很多平台与环境中工作.不同平台和不同的环境往往需要不同 的特性,或可能以不同的方式实现相同的特性最有效率. ...

  2. 数据库学习之"清理表内所有数据"

    今天在写定时任务的时候表内的数据都出现了问题,所以用了 1 truncate table 表名 来清空表内的数据

  3. 动态规划 洛谷P1048 [NOIP2005 普及组] 采药

    洛谷P1048 [NOIP2005 普及组] 采药 洛谷的一个谱架-的题目,考的是01背包问题,接下来分享一下我的题解代码. AC通过图: 我的代码: 1 //动态规划 洛谷P1048 [NOIP20 ...

  4. 六、cadence叠层和布线前规则设置详细步骤

    一.叠层设置 1.颜色设置 2.层叠设置setup-cross section,如下图: 3.布线规则设置 a>线宽设置 b>添加差分对logic-Assign Differenital ...

  5. IT架构和架构类型

    What is IT Architecture & Types of Architectures | ITARCH.INFO What is IT Architecture & Typ ...

  6. Proxy相比于defineProperty的优势

    本文原链接:https://www.jianshu.com/p/860418f0785c https://blog.csdn.net/sinat_17775997/article/details/83 ...

  7. VueJs项目笔记

    ======================知识点总结=========================== 一.keep-alive(实现页面的缓存) 二. 移动端固定定位的解决方案 三. Vue表 ...

  8. CSS揭秘之《多重边框》

    1.box-shadow还接受第四个参数(称作"扩张半径"), 通过指定正值或负值, 可以让投影面积加大或者减小2.如果我们想要一道实线边框其实也是可以通过box-shadow来模 ...

  9. web开发者踏入人工智能的利器_Tensorflow.js

    前言 最近公司向员工搜集公司杂志的文章,刚好最近学习了机器学习相关课程.为了赚取购买课程的费用,所以写了如下文章投稿赚取稿费. 如下文章可能涉及一些我所购买课程的内容,所以不便将所有资源进行展示. 当 ...

  10. mysql 合并查询结果

     UNION 使用 UNION 关键字是,数据库系统会将所有的查询结果合并到一起,然后去除掉相同的记录:   UNION ALL 使用 UNION ALL,不会去除掉系统的记录: