1.4.1 • Published 1 year ago

react-p5 v1.4.1

Weekly downloads
753
License
MIT
Repository
github
Last release
1 year ago

GitHub ggpack

sponsors backers

For Tips and Advanced Usage you can read this Blog Post

Installation

  • npm

    npm i --save react-p5
  • yarn

    yarn add react-p5

Usage

For Tips and Advanced Usage you can read this Blog Post

JavaScript

import React from "react";
import Sketch from "react-p5";

let x = 50;
let y = 50;
export default (props) => {
  const setup = (p5, canvasParentRef) => {
    // use parent to render the canvas in this ref
    // (without that p5 will render the canvas outside of your component)
    p5.createCanvas(500, 500).parent(canvasParentRef);
  };

  const draw = (p5) => {
    p5.background(0);
    p5.ellipse(x, y, 70, 70);
    // NOTE: Do not use setState in the draw function or in functions that are executed
    // in the draw function...
    // please use normal variables or class properties for these purposes
    x++;
  };

  return <Sketch setup={setup} draw={draw} />;
};

Typescript

import React from "react";
import Sketch from "react-p5";
import p5Types from "p5"; //Import this for typechecking and intellisense

interface ComponentProps {
  // Your component props
}

let x = 50;
const y = 50;
  
const YourComponent: React.FC<ComponentProps> = (props: ComponentProps) => {

  // See annotations in JS for more information
  const setup = (p5: p5Types, canvasParentRef: Element) => {
    p5.createCanvas(500, 500).parent(canvasParentRef);
  };

  const draw = (p5: p5Types) => {
    p5.background(0);
    p5.ellipse(x, y, 70, 70);
    x++;
  };

  return <Sketch setup={setup} draw={draw} />;
};

Tips

  • If you need to get the browser event object inside your p5 methods like mouseClicked or others you can do it by accessing the second arg.
mouseClicked(_p5, event) {
  console.log(event)
}

Events that are accessed using props are always attached to window.

That means that events are triggered throughout the whole page (see the p5 docs for reference).

If you would like to attach events only to canvas see the example below. As an example limiting click events to the canvas:

const setup = (p5, canvasParentRef) => {
  cnv = p5.createCanvas(width, height).parent(canvasParentRef)
  cnv.mousePressed((event) => {
    console.log("Clicked on the canvas. Event:", event)
  })
}

Using it in an SSR environment (Next.js or Gatsby)

Importing this package for example in a Next.js or Gatsby app may give you this error:

ReferenceError: window is not defined

This is because importing p5 requires window to be available, and it isn't when server side rendering.

For Next.js we can fix this using Next.js dynamic imports with No SSR.

import dynamic from 'next/dynamic'

// Will only import `react-p5` on client-side
const Sketch = dynamic(() => import('react-p5').then((mod) => mod.default), {
  ssr: false,
})

For Gatsby we can use loadable-components. See Gatsby docs: Load client-side dependent components with loadable-components.

import Loadable from "@loadable/component"

const Sketch = Loadable(
  () => import("react-p5")
);

export default Sketch;

With p5.sound

I frequently see this question even if the implimentation is super simple. The only needed thing is to import the p5.sound lib. I created a special CodeSandbox demo if someone needs to see the implementation.

With p5.sound + next.js (or other framework which has support for SSR)

This question also is frequently asked and the only difference from the normal aprouch is that in SSR mode the react-p5 lib should not be loaded because p5 doesn't support SSR and there is no sense for it to be support. So, if you are using react-p5 plus next.js and you need p5.sound as well, then try to use dynamic imports as in the code below which will definitely help you.

import dynamic from 'next/dynamic'

// Will only import `react-p5` on client-side
const Sketch = dynamic(() => import("react-p5").then((mod) => {

  // importing sound lib only after react-p5 is loaded
  require('p5/lib/addons/p5.sound');

  // returning react-p5 default export
  return mod.default
}), {
  ssr: false
});

When using dynamic import, the type information for the methods extended by p5.sound.d.ts is not loaded. Therefore, we can use Type-Only Imports to address this.

import dynamic from 'next/dynamic'

// importing only type information from p5.sound addons.
import type from "p5/lib/addons/p5.sound"

// Will only import `react-p5` on client-side
const Sketch = dynamic(() => import("react-p5").then((mod) => {

  // importing sound lib only after react-p5 is loaded.
  require('p5/lib/addons/p5.sound');

  // returning react-p5 default export
  return mod.default
}), {
  ssr: false
});

Props

PropRequiredTypeDescription
className:x:StringClassName for canvas parent ref
style:x:ObjectStyles for canvas parent ref
setup:heavy_check_mark:FunctionThe setup() function is called once when the program starts.
draw:x:FunctionCalled directly after setup(), the draw() function continuously executes the lines of code contained inside its block until the program is stopped or noLoop() is called.
windowResized:x:FunctionThe windowResized() function is called once every time the browser window is resized.
preload:x:FunctionCalled directly before setup(), the preload() function is used to handle asynchronous loading of external files in a blocking way.
mouseClicked:x:FunctionThe mouseClicked() function is called once after a mouse button has been pressed and then released.
mouseMoved:x:FunctionThe mouseMoved() function is called every time the mouse moves and a mouse button is not pressed.
doubleClicked:x:FunctionThe doubleClicked() function is executed every time a event listener has detected a dblclick event which is a part of the DOM L3 specification.
mousePressed:x:FunctionThe mousePressed() function is called once after every time a mouse button is pressed.
mouseWheel:x:FunctionThe function mouseWheel() is executed every time a vertical mouse wheel event is detected either triggered by an actual mouse wheel or by a touchpad.
mouseDragged:x:FunctionThe mouseDragged() function is called once every time the mouse moves and a mouse button is pressed. If no mouseDragged() function is defined, the touchMoved() function will be called instead if it is defined.
mouseReleased:x:FunctionThe mouseReleased() function is called every time a mouse button is released.
keyPressed:x:FunctionThe keyPressed() function is called once every time a key is pressed. The keyCode for the key that was pressed is stored in the keyCode variable.
keyReleased:x:FunctionThe keyReleased() function is called once every time a key is released. See key and keyCode for more information.
keyTyped:x:FunctionThe keyTyped() function is called once every time a key is pressed, but action keys such as Backspace, Delete, Ctrl, Shift, and Alt are ignored.
touchStarted:x:FunctionThe touchStarted() function is called once after every time a touch is registered.
touchMoved:x:FunctionThe touchMoved() function is called every time a touch move is registered.
touchEnded:x:FunctionThe touchEnded() function is called every time a touch ends. If no touchEnded() function is defined, the mouseReleased() function will be called instead if it is defined.
deviceMoved:x:FunctionThe deviceMoved() function is called when the device is moved by more than the threshold value along X, Y or Z axis. The default threshold is set to 0.5. The threshold value can be changed using setMoveThreshold()
deviceTurned:x:FunctionThe deviceTurned() function is called when the device rotates by more than 90 degrees continuously.
deviceShaken:x:FunctionThe deviceShaken() function is called when the device total acceleration changes of accelerationX and accelerationY values is more than the threshold value. The default threshold is set to 30.

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

Or you can sponsor via Open Collective

Author

@Gherciu/react-p5 Released under the MIT License. Authored and maintained by GHERCIU GHEORGHE with help from contributors (list).

If you like this repository star:star: and watch:eyes: on GitHub

1.4.1

1 year ago

1.3.35

2 years ago

1.3.34

2 years ago

1.3.31

2 years ago

1.3.32

2 years ago

1.3.33

2 years ago

1.3.29

3 years ago

1.3.30

3 years ago

1.3.28

3 years ago

1.3.24

3 years ago

1.3.25

3 years ago

1.3.23

3 years ago

1.3.26

3 years ago

1.3.27

3 years ago

1.3.22

3 years ago

1.3.20

3 years ago

1.3.21

3 years ago

1.3.19

4 years ago

1.3.18

4 years ago

1.3.17

4 years ago

1.3.15

4 years ago

1.3.16

4 years ago

1.3.14

4 years ago

1.3.13

4 years ago

1.3.12

4 years ago

1.3.11

4 years ago

1.3.10

4 years ago

1.3.9

4 years ago

1.3.8

4 years ago

1.3.7

4 years ago

1.3.6

4 years ago

1.3.5

4 years ago

1.3.4

4 years ago

1.3.3

4 years ago

1.3.2

5 years ago

1.2.0

5 years ago

1.2.7

5 years ago

1.2.6

5 years ago

1.2.5

5 years ago

1.2.4

5 years ago

1.2.3

5 years ago

1.2.2

5 years ago

1.3.0

5 years ago

1.2.1

5 years ago

1.1.8

5 years ago

1.1.7

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

5 years ago

1.1.0

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 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