0.9.0 • Published 6 years ago

p5-global2instance v0.9.0

Weekly downloads
3
License
MIT
Repository
github
Last release
6 years ago

p5-global2instance

Convert p5js code/snippet from global to instance mode

Install

npm install p5-global2instance

Usage

There are 2 away to use this script.

CLI

p5-global2instance sourceCode.js

This will produce file sourceCode.p5.js. For more details use --help

node test.js --help

  Usage: test [options] [file]


  Options:

    -o, --output [file]  Save output file to [file]
    -p, --print          Print result to stdout
    -h, --help           output usage information

Import as module

Take this example code

const p5Convert = require('p5-global2instance')

const sourceCode = `
var current;
var previous;

function setup () {
  createCanvas(720, 400);
};

function draw () {
  background(0);
};
`

let output = p5Convert(sourceCode)
console.log(output)

It will output

import p5 from 'p5';
export default function (sketch) {
  var current;
  var previous;

  sketch.setup = function () {
    sketch.createCanvas(720, 400);
  };

  sketch.draw = function () {
    sketch.background(0);
  };
}

Options

You can also pass esprima and escodegen options.

p5Convert(sourceCode, {
  esprima: {},
  escodegen: {}
})