# react-canvas-base

> The react canvas base was created to simplify creating canvas components for react in a performant way. Most canvas libraries use react sub components and include them in reacts render cycle. I wanted a component that makes use of the full performance a c

Latest version **1.0.3** (published 2024-06-07) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-canvas-base
pnpm add react-canvas-base
yarn add react-canvas-base
bun add react-canvas-base
```

## Health

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

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.3 |
| Published | 2024-06-07 |
| First published | 2019-12-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 2 |
| Unpacked size | 92.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Oliver Küchen |
| Maintainers | okuechen |
| Keywords | react, component, canvas, performance |

## Links

- npm: https://www.npmjs.com/package/react-canvas-base
- Repository: https://github.com/okuechen/react-canvas-base
- Issues: https://github.com/okuechen/react-canvas-base/issues
- npm.io page: https://npm.io/package/react-canvas-base

## Dependencies (2)

- [react](https://npm.io/package/react.md) ^16.8.0 || ^17.0.0 || ^18.0.0
- [react-dom](https://npm.io/package/react-dom.md) ^18.3.1

## 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.3 (latest) — 2024-06-07
- 1.0.2 — 2024-05-29
- 1.0.1 — 2024-05-27
- 1.0.0 — 2024-05-26
- 0.2.0 — 2020-01-28
- 0.1.4 — 2019-12-15
- 0.1.3 — 2019-12-15
- 0.1.2 — 2019-12-14
- 0.1.1 — 2019-12-14
- 0.1.0 — 2019-12-13

## README

# react-canvas-base

The react canvas base was created to simplify creating canvas components for react in a performant way. Most canvas libraries use react sub components and include them in reacts render cycle. I wanted a component that makes use of the full performance a canvas can give to you.

You can add it to your project via NPM.

`npm install react-canvas-base`

## Examples

Here some examples how to handle the canvas base.

### Draw a simple rectangle and a line through it

```react
import React from 'react';
import { Canvas } from 'react-canvas-base';

function App() {
    return (
        <Canvas width={500} height={500} onDraw={(crc) => {
            // use the CanvasRenderingContext to draw a filled rect and line
            crc.setFillStyle("#444");
            crc.setStrokeStyle("#999", 2);
            crc.drawRect(10, 10, 100, 50);
            crc.drawLine(10, 10, 110, 60);
        }} />
    );
}
```

### Draw a centered label inside a rectangle

```react
import React from 'react';
import { Canvas, CanvasFont } from 'react-canvas-base';

function App() {
    return (
        <Canvas width={500} height={500} onDraw={(crc) => {
            crc.setFillStyle("#444");
            crc.setStrokeStyle("#999", 2);
            crc.drawRect(10, 10, 100, 50);

            // for best performance, store fonts somewhere globally so no recreation happens
            const font = new CanvasFont(16, "Arial");

            crc.setFont(font);
            crc.setFillStyle("#FFF");
            crc.setTextBaseline("middle");
            crc.drawText("Button", 40, 35, 90, true);
        }} />
    );
}
```

### Draw a simple animation with frame-time and continuous drawing

```react
import React from 'react';
import { Canvas } from 'react-canvas-base';

function App() {
    let size = 10;
    let direction = 0.2;

    return (
        <Canvas width={500} height={500} drawMode={1} onDraw={(crc, frameTime) => {
            crc.clear();
            crc.setFillStyle("#444");
            crc.setStrokeStyle("#999", 2);
            crc.drawRect(200 - size / 2, 200 - size / 2, size, size);

            size += direction * frameTime;
            if (size < 10) direction = 0.2;
            if (size > 200) direction = -0.2;
        }} />
    );
}
```

### Draw counter parallel to html span with continuous drawing using a react state

```react
import React from 'react';
import { createRoot } from 'react-dom/client';
import { Canvas, CanvasDrawMode, CanvasRenderingContext } from './index';

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

    React.useEffect(() => {
        const timeout = setTimeout(() => {
            setCounter(counter => counter + 1);
        }, 1000);

        return () => clearInterval(timeout);
    }, [counter]);

    function onDraw(crc: CanvasRenderingContext, frameTime: number) {
        // simple example
        crc.clear();

        crc.setStrokeStyle("#F00", 2);
        crc.drawLine(10, 25, 130, 25);
        crc.setFont({ size: 16, family: "Arial", weight: "bold", style: "" });

        crc.drawText(`Canvas Counter: ${counter}`, 10, 20, 200);
    };

    return (
        <div style={{ display: 'flex', flexDirection: 'column'}}>
            <span>HTML Counter: {counter}</span>
            <Canvas width={200} height={200} onDraw={onDraw} drawMode={CanvasDrawMode.Continuous}/>
        </div>
    );
};

```

## Changelog

v1.0.3

- Better state integration. Updated App example to test it.

v1.0.2

- Add many missing canvas options for text and image control.

v1.0.1

- Export type fix in rollup process.

v1.0.0

- Fixed aspect ratio.
- Update to newest react version.
- Vite and rollup instead of react-script.
- Better animation frame handling.
- Detached drawing (especially in continuous mode) from react states for way better performance.

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