内容简介:Takes care of running an animating function, provided as the first argument, while keeping track of its progress.When usedThe provided function receives the animation progress and a
useRequestAnimationFrame
Takes care of running an animating function, provided as the first argument, while keeping track of its progress.
When used useRequestAnimationFrame
immediately starts a looping call the provided function by using window.requestAnimationFrame
under the hood.
The provided function receives the animation progress and a next
function to be performed to keep the animation alive.
The loop ends when the animation progress reaches the value of 100. (or any other value provided as finishAt
, check the options)
Why? :bulb:
- Easily manage a requestAnimationFrame function within a React component
Basic Usage
import { useRef } from 'react'; import { Alert } from 'beautiful-react-ui'; import { useRequestAnimationFrame } from 'beautiful-react-hooks'; const AnimationExample = () => { const ref = useRef(); useRequestAnimationFrame((progress, next) => { ref.current.style.transform = `translateX(${progress}px)`; next(); }); return ( <DisplayDemo> <div ref={ref}> <Alert color="primary" >Animating content</Alert> </div> </DisplayDemo> ); }; <AnimationExample />
Options
An object of options can be used as second argument to control the animation.
Please note: options.finishAt = -1 will cause an infinite animation
import { useRef } from 'react'; import { Alert } from 'beautiful-react-ui'; import { useRequestAnimationFrame } from 'beautiful-react-hooks'; const AnimationExample = () => { const ref = useRef(); const options = {increment: 0.5, startAt: 0, finishAt: -1 }; useRequestAnimationFrame((progress, next) => { ref.current.style.transform = `rotate(${progress}deg)`; next(); }, options); return ( <DisplayDemo> <div ref={ref}> <Alert color="primary" >Animating content</Alert> </div> </DisplayDemo> ); }; <AnimationExample />
onFinish callback
The hook returns an function to possibly set a callback when the animation finishes:
import { useRef, useState } from 'react'; import { Alert, Paragraph } from 'beautiful-react-ui'; import { useRequestAnimationFrame } from 'beautiful-react-hooks'; const AnimationExample = () => { const ref = useRef(); const [isFinished, setIsFinished] = useState(false); const onFinish = useRequestAnimationFrame((progress, next) => { ref.current.style.transform = `translateX(${progress}px)`; next(); }); onFinish(() => setIsFinished(true)); return ( <DisplayDemo> <div ref={ref}> <Alert color="primary" >Animating content</Alert> </div> {isFinished && <Paragraph>Animation completed!</Paragraph>} </DisplayDemo> ); }; <AnimationExample />
Control the speed of your animation by changing the increment value
Mastering the hook
- When in need to perform requestAnimationFrame without re-rendering the component on each frame
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
JSP信息系统开发实例精选
赛奎春 / 机械工业出版社 / 2006-1 / 44.00元
本书精选了大学生就业求职网、物流短信平台、化奥汽车销售集团网站、佳美网络购物中心、科研成果申报管理系统、安瑞奥国际商务不院招生网、明日宽带影院、雄霸天下游戏网等8个综合的网络信息系统工程作为案例,深入剖析了实际的网络信息系统的开发思路、方法和技巧。帮助读者透彻掌握JSP开发网络信息系统的方法和步骤,从而设计出具有实用价值和商用价值的信息系统。 本书产例具有很强的实用性和工程实践性,在讲解......一起来看看 《JSP信息系统开发实例精选》 这本书的介绍吧!