2.1.14 • Published 7 months ago

react-frame-rate v2.1.14

Weekly downloads
1
License
MIT
Repository
github
Last release
7 months ago

react-frame-rate Coverage Build npm

Create smooth animation in React components with ~60FPS.

Demo

Usage

npm install react-frame-rate --save or yarn add react-frame-rate

◦ React hook useFrameRateManager:

import * as React from "react";

import { useFrameRateManager } from "react-frame-rate";

export function Counter() {
  const [counter, setCounter] = React.useState(0);

  const {
    updateCallback,
    updateFrameRate,
    updateAnimation
  } = useFrameRateManager();

  React.useEffect(() => {
    updateCallback(() => setCounter((value) => value + 1));
  }, [updateCallback, setCounter]);

  React.useEffect(() => {
    updateFrameRate(30);
  }, [updateFrameRate]);

  React.useEffect(() => {
    updateAnimation(true);
    return () => {
      updateAnimation(false);
    };
  }, [updateAnimation]);

  return <div>{counter}</div>;
}

Props:

  • updateCallback - set callback which is called on each frame.
  • updateFrameRate - set desired frame rate value, optimal values 60/30/20/15/10/6/5/3/1.
  • updateAnimation- set start/stop animation with boolean flag.

◦ React HOC withReactFrameRate:

import * as React from "react";

import withReactFrameRate, { BaseUpdateProps } from "react-frame-rate";

type Props = Readonly<{
  counter: number;
}> &
  BaseUpdateProps;

export function Counter() {
  const [isAnimating, setIsAnimation] = React.useState(true);

  const updateState = React.useCallback<(state: Props) => Props>(
    (state: Props) => {
      const newCounter = state.counter + 1;
      if (newCounter >= 100) {
        setIsAnimation(false);
      }
      return { ...state, counter: newCounter };
    },
    [setIsAnimation]
  );

  const options = React.useMemo(
    () => ({
      updateState,
      frameRate: 30
    }),
    [updateState]
  );

  const WithAnimation = React.useMemo(
    () =>
      withReactFrameRate<Props>(options)((props: Props) => (
        <>{props.counter}</>
      )),
    [options]
  );

  return <WithAnimation counter={0} isAnimating={isAnimating} />;
}

Options:

  • updateState - refresh state on each frame.
  • frameRate - current frame rate for updateState. For efficient animation use frameRate - 60/30/20/15/10/6/5/3/1.

◦ Codesandbox

◦ Demo

Contributing

Contributing are Welcome 🎉 Please check out the Contributing guide.

LICENSE

MIT License

Keywords

requestAnimatioFrame react smooth animation

2.1.14

7 months ago

2.1.13

7 months ago

2.1.12

1 year ago

2.1.1

2 years ago

2.1.0

2 years ago

2.1.11

2 years ago

2.0.2

4 years ago

2.0.1

4 years ago

2.0.0

5 years ago

1.1.9

5 years ago

1.1.7

5 years ago

1.1.6

5 years ago

1.1.5

5 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago