@fal-works/p5-inst
TypeScript icon, indicating that this package has built-in type declarations

0.2.0 • Public • Published

p5-inst

Tiny utility for instance mode of p5.js.

ES6/TypeScript friendly.

How to load

With <script> tag

Something like this in the <head> tag:

<script src="https://cdn.jsdelivr.net/npm/p5@1.2.0/lib/p5.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@fal-works/p5-inst@0.2.0/lib/p5-inst.js"></script>

<script src="path/to/your/sketch.js"></script>

Now in your source code you can use a global variable p5Inst, which is defined by p5-inst.js.

const { createSketch } = p5Inst;

As ES Module

npm install -D @fal-works/p5-inst
import { createSketch } from "@fal-works/p5-inst";

How to use

  1. Import the createSketch() function (see above).

  2. Prepare any p5 related functions such as setup() and draw().
    Unlike the usual use of p5.js, each function should accept a p5 instance as the first argument.

  3. Call createSketch(), passing the functions you prepared.
    This returns a Sketch object.

  4. Call new p5(), passing the Sketch object you created above.

// Import createSketch() in some way

const setup = (p) => {
  p.createCanvas(400, 400);
  // prepare something
};

const draw = (p) => {
  p.background(240);
  // draw something
};

const sketch = createSketch({
  setup,
  draw,
  // you may include other methods as well, e.g. mousePressed()
});

new p5(sketch);

View sample code on p5.js Web Editor

Readme

Keywords

Package Sidebar

Install

npm i @fal-works/p5-inst

Weekly Downloads

1

Version

0.2.0

License

CC0-1.0

Unpacked Size

17.4 kB

Total Files

15

Last publish

Collaborators

  • fal-works