npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-use-controllable-animation

v1.0.1

Published

A React hook for creating animations with play/pause/stop/reset controls

Downloads

104

Readme

react-use-controllable-animation

A React hook for creating animations with start/pause/stop/reset controls

Installation

npm install react-use-controllable-animation

How to Use

Import or require the default export useControllableAnimation. The module also exports easingFunctions if needed by your application.

Parameters

The hook accepts an object with the following parameters: | Parameter | type | default | description | |-----------|---------|------------------------------|--------------------------------------------------------------------------------------------------------------------------| | autoplay | boolean | true | Specify if the animation should begin on initialization | alternate | boolean | false | Specify if the animation should run backwards every other loop (i.e. loop 1: 0 -> 1, loop 2: 1 -> 0) | alternateDelay | boolean | false | Specify if the delay should be applied on every loop or every other loop (only affects animation if alternate is set to true) | delay | number | 0 | Amount of time in ms to delay the animation | duration | number | 1000 | Duration of the animation in ms | easing | string or function | "linear" | Specify the name of the easing function to use (must be one of the keys exported in easingFunctions) or define your own custom easing function ((val: number[0-1]) => number[0-1]) | loops | number | Infinity | Number of times animation should run before stopping (Note: alternating animations count each 0-1 or 1-0 as a loop) | onFinish | function | undefined | Callback to be run after the animation completes running loops times | onLoop | function | undefined | Callback to be run after each loop completes. Passes current loop as argument. | onProgress | function | | Callback to be run on each frame. Passes progress (with easing) and an object including uneasedProgress, progressWithDelay, and loop as arguments.

Returns

The hook returns an object with the following function properties:

start - begins/unpauses the animation if it's not already running

pause - pauses the animation on the current frame

reset - resets the animation to the initial state (progress = 0, loop = 0) without starting or pausing the animation

stop - pauses the animation and resets it

Example

import { useCallback, useEffect, useState } from "react";
import useControllableAnimation, { easingFunctions } from "react-use-controllable-animation";

function Example() {
  const [ bounceEl, setBounceEl ] = useState();
  const [ sineEl, setSineEl ] = useState();
  
  const onProgress = useCallback((progress, { uneasedProgress, progressWithDelay, loop }) => {
    if (!bounceEl || !sineEl) return;
    
    bounceEl.style.left = `${(progress * 100).toFixed(2)}%`;
    sineEl.style.left = `${(easingFunctions.easeInOutSine(uneasedProgress)) * 100).toFixed(2)}%`;
    
    console.log(`Overall animation progress including delay time: ${progressWithDelay}`);
    console.log(`Current loop: ${loop}`);
  }, [ bounceEl, sineEl ]);
  
  const { start, pause, reset, stop } = useControllableAnimation({
    autoplay: !!bounceEl && !!sineEl,
    alternate: true,
    alternateDelay: true,
    delay: 1000,
    duration: 2000,
    easing: "easeInBounce",
    onProgress
  });
  
  return (
    <div>
      <div ref={setBounceEl}/>
      <div ref={setSineEl}/>
      <button onClick={start}>START</button>
      <button onClick={pause}>PAUSE</button>
      <button onClick={reset}>RESET</button>
      <button onClick={stop}>STOP</button>
    </div>
  );
}
    

Demo

The package includes a demo found in the example folder. You can either build it yourself (npm install react-use-controllable-animation && npm run start), or view a live version at this link:

https://react-use-controllable-animation.netlify.app

Enjoy