0.1.5 • Published 3 years ago

@react-canvas-animation/core v0.1.5

Weekly downloads
-
License
ISC
Repository
github
Last release
3 years ago

React Canvas Animation

Package structure

  • @react-canvas-animation/core
    • contains the Canvas component
  • @react-canvas-animation/draw
    • contains helper functions to simplify creation of sketches
    • depends on @react-canvas-animation/core
  • @react-canvas-animation/math
    • contains a bunch of math functions
    • totally independent of the other packages
  • @react-canvas-animation/vector
    • implements vectors and vector related functions
    • totally independent of the other packages

Installation

// with npm
npm i --save @react-canvas-animation/core @react-canvas-animation/draw

// with yarn
yarn add @react-canvas-animation/core @react-canvas-animation/draw

Usage

Here is a quick example to get you started:

import Canvas from '@react-canvas-animation/core';;
import '@react-canvas-animation/draw';

const mySketch = ({width, height, fill, circle}) => {
  fill('red');
  circle(width / 2, height / 2, 50);
};

function App() {
  return <Canvas sketch={mySketch}/>;
}

Canvas component

Props

NameTypeDefaultDescription
sketchfunctionThis function is called for every frame of the animation. It should contain the drawing logic.Signature: function(draw: object) => voiddraw: A reference to the context of the Canvas component (not the context of the canvas element)
widthnumberwindow.innerWidthWidth of the canvas in pixel
heightnumberwindow.innerHeightHeight of the canvas in pixel

The sketch function

This function takes one argument which is a reference to the context of the corresponding Canvas component (not to be confused with the context of the canvas element). The context contains properties of the canvas such as its width and height as well as the drawing methods if @react-canvas-animation/draw was imported. Since the sketch function shares the same context with the Canvas component, all these methods and properties are also accessible via this.

Available canvas properties:
NameTypeDescription
widthnumberWidth of the canvas
heightnumberHeight of the canvas
ctxobjectContext of the canvas element
frameCountnumberThe number of times the sketch function has been called

Responsiveness

If no width and height are given then the component handles window resizes on its own and updates the width and height. So if you want to create a responsive sketch don't provide the width and height prop and construct your sketch function in such a way that positions and sizes are not hardcoded but depend on the width and height properties of the Canvas component.

0.1.5

3 years ago

0.1.4

4 years ago

0.1.3

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago

0.0.26

4 years ago

0.0.22

4 years ago

0.0.23

4 years ago

0.0.24

4 years ago

0.0.25

4 years ago

0.0.20

4 years ago

0.0.21

4 years ago

0.0.19

4 years ago

0.0.18

4 years ago

0.0.17

4 years ago

0.0.16

4 years ago

0.0.15

4 years ago

0.0.14

4 years ago

0.0.13

4 years ago

0.0.12

4 years ago

0.0.10

4 years ago

0.0.11

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.7

4 years ago

0.0.6

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago