# @stkterry/react-reanim

> Simplify HTML5 canvas animations in React!

Latest version **1.0.1** (published 2020-09-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install @stkterry/react-reanim
pnpm add @stkterry/react-reanim
yarn add @stkterry/react-reanim
bun add @stkterry/react-reanim
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2020-09-19 |
| First published | 2020-09-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Node | >=10 |
| Dependencies | 0 |
| Unpacked size | 21.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | stkterry |
| Maintainers | stkterry |
| Keywords | react, HTML5, canvas, animation |

## Links

- npm: https://www.npmjs.com/package/@stkterry/react-reanim
- Repository: https://github.com/stkterry/react-reanim
- Homepage: https://github.com/stkterry/react-reanim#readme
- Issues: https://github.com/stkterry/react-reanim/issues
- npm.io page: https://npm.io/package/@stkterry/react-reanim

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.0.1 (latest) — 2020-09-19
- 1.0.0 — 2020-09-19

## README

# @stkterry/react-reanim

> Simplify HTML5 canvas animations in React!

[![NPM](https://img.shields.io/npm/v/@stkterry/react-reanim.svg)](https://www.npmjs.com/package/@stkterry/react-reanim) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)

This project has two primary goals:

1) Prevent React from constantly re-rendering when drawing to the canvas.  Essentially,
turn the canvas into a mostly static component.
2) Separate the *step* / *draw* functionality from the associated boiler-plate common
to virtually every canvas animation.

## Install

```bash
npm install --save @stkterry/react-reanim
```

## Usage
The following results in a simple, spinning cube as a basic example.
```jsx
import React, { Component } from 'react'

import Reanim from '@stkterry/react-reanim'

export default function Simple() {

  const [angle, setAngle] = useState(0);

  const [style, setStyle] = useState();
  useEffect(() => {
    setStyle({ width: "250px", height: "250px",});
  }, [])

  const step = () => setAngle(angle + 1);

  const draw = ctx => {
    const width = ctx.canvas.width;
    const height = ctx.canvas.height;
    ctx.save();
    ctx.beginPath();
    ctx.clearRect(0, 0, width, height);
    ctx.translate(width / 2, height / 2);
    ctx.rotate(angle * Math.PI / 180);
    ctx.fillStyle = '#4397AC';
    ctx.fillRect(-width / 4, -height / 4, width / 2, height / 2);
    ctx.restore();
  };

  return (
    <Reanim
      step={step}
      draw={draw}
      width="250px"
      height="250px"
      style={style}
    />
  )
}
```
## Reanim Options
<table>
  <tr>
    <th> Name </th>
    <th> Type </th>
    <th> Description </th>
    <th> Default </th>
  </tr>
  <tr>
    <td> step </td>
    <td> function </td>
    <td colspan="2"> Animation step, updated each frame | REQUIRED </td>

  </tr>
  <tr>
    <td> draw </td>
    <td> function </td>
    <td colspan="2"> Drawn to canvas each frame, must be written to include a canvas context input | REQUIRED </td>
  </tr>
  <tr>
    <td> onClick </td>
    <td> function </td>
    <td colspan="2"> Normal 'onClick' functionality, automatically gets memoized* </td>
  </tr>
  <tr>
    <td> onClickVars </td>
    <td> function </td>
    <td> onClick variables that you want to trigger a re-render when onClick is called* </td>
    <td> [ ] </td>
  </tr>

  <tr>
    <td> run </td>
    <td> boolean </td>
    <td> Starts/stops the animation loop </td>
    <td> true </td>
  </tr>

</table>
<sup>* onClick is passed to a useCallback to prevent constant re-render of the canvas
component.  If a re-render is required, users can pass the variables associated with onClick here that they wish to trigger the re-render. onClick is essentially: <code>useCallback(yourOnClickFunction, [...onClickVars])</code></sup>

<br></br>
*Reanim* will accept any other attributes that are common to React/canvas and pass
them accordingly (className, onMouseOver, style, width, height, etc...).    

### Important!
If you pass functions or objects to *Reanim* (excluding step/draw/onClick) without wrapping them in a *useCallback* or otherwise effectively memoizing them, you will trigger re-renders of the component on every frame!  While not necessarily a problem,
this can/will affect performance!  *I'm working on a solution that will
auto-memoize commonly passed variables.*


### Other
Common canvas weirdness still applies to *Reanim*.  It's up to you to know how to 
wield HTML5 canvas effectively.  This component is just a wrapper to separate the
step/draw functions from the typical boiler-plate necessary to get a canvas up
and running.

## Canvas
If you so desire, you can make use of the inner canvas component directly.
```jsx
import React, { useState } from 'react';

import { Canvas } from '@stkterry/react-reanim';

export default function Example(props) {

  // ... some code here
  // ...
  // ...
  const [ctx, setCtx] = useState();
  // do something with ctx

  return (
    <Canvas setCtx={setCtx} />
  )
};
```
Note that the only variable *Canvas* requires is a *setCtx* function which should
assign a 2d context off the node when mounted, otherwise *Canvas* will accept anything your standard canvas will.  *Canvas* is
memoized by default but remember that the contents you pass it may constantly trigger
re-renders anyway. 

*Canvas* is just a memoized regular canvas element with a passback for setting context.

## Bonus Hooks!
### useWindowDims
This hook returns the inner width and height of the current window.  It's
lightweight and will automatically add/remove the event listener.  Updates on resize.
```jsx

import React from 'react';

import { useWindowDims } from '@stkterry/react-reanim';

export default function Example(props) {

  const windowDims = useWindowDims();

  return (
    <div>
      <h2>innerWidth: {windowDims.width}</h2>
      <h2>innerHeight: {windowDims.height}</h2>
    </div>
  )
}
```
You may find this useful if you need to dynamically style or control your canvas
based on window size.  

## To Do
* Add target frame-rate functionality
* Add collection of convenient hooks for working with the canvas
* Implement a context simplification library for drawing
* Add real tests...
## License

MIT © [stkterry](https://github.com/stkterry)

---
_Source: https://npm.io/package/@stkterry/react-reanim · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
