Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/stesel/react-frame-rate

Create smooth animation in React components with ~60FPS.
https://github.com/stesel/react-frame-rate

frame-rate hacktoberfest react-components

Last synced: about 1 month ago
JSON representation

Create smooth animation in React components with ~60FPS.

Awesome Lists containing this project

README

        

# react-frame-rate [![Coverage](./badges/coverage-jest%20coverage.svg)](https://github.com/stesel/react-frame-rate/actions/workflows/coverage.yml) [![Build](https://github.com/stesel/react-frame-rate/actions/workflows/build.yml/badge.svg)](https://github.com/stesel/react-frame-rate/actions/workflows/build.yml) [![npm](https://img.shields.io/npm/v/react-frame-rate.svg)](https://www.npmjs.com/package/react-frame-rate)
Create smooth animation in React components with ~60FPS.

![Demo](https://raw.githubusercontent.com/stesel/react-frame-rate/master/demo.gif)

## Usage

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

### ◦ React hook `useFrameRateManager`:
```typescript
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

{counter}
;
}
```
#### 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`:
```typescript
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(options)((props: Props) => (
<>{props.counter}>
)),
[options]
);

return ;
}
```

#### 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](https://codesandbox.io/s/21zko1o25j)

### [◦ Demo](https://github.com/stesel/react-frame-rate-demo)

## Contributing

Contributing are Welcome 🎉
Please check out the [Contributing guide](CONTRIBUTING.md).

### LICENSE

[MIT License](LICENSE.md)

### Keywords

`requestAnimatioFrame` `react` `smooth animation`