p5-in-node

0.0.2 • Public • Published

p5 in node

Use p5.js inside Node!

Installation

npm install p5-in-node p5

Usage

Currently only supports instance mode so as to not pollute the global scope.

import { runSketch, saveAsPNG } from "p5-in-node";

// Sketch function in instance mode
const sketch = (p) => {
  p.setup = () => {
    p.createCanvas(400, 400);
    p.noLoop();
  };

  p.draw = () => {
    p.background(51);
    p.fill(255);
    p.rect(100, 100, 200, 200);

    saveAsPNG(p, "sketch.png");
  };
};

runSketch(sketch, 400, 400);

Here, the sketch function receives the p5 instance as an argument, which can be used to define event functions, like setup() and draw(). Its API is the same as p5.js.

The library exports two functions - runSketch() and saveAsPNG():

  • runSketch(sketchFn, w, h):
    • sketchFn: The p5 sketch function
    • w: width of the canvas
    • h: height of the canvas
  • saveAsPNG(pInst, filename)
    • pInst: the p5 instance
    • filename: the filename to save the image as

Readme

Keywords

none

Package Sidebar

Install

npm i p5-in-node

Weekly Downloads

2

Version

0.0.2

License

MIT

Unpacked Size

3.74 kB

Total Files

5

Last publish

Collaborators

  • dipamsen