https://github.com/drsensor/p5-global2instance
Convert p5js code/snippet from global to instance mode
https://github.com/drsensor/p5-global2instance
Last synced: 11 months ago
JSON representation
Convert p5js code/snippet from global to instance mode
- Host: GitHub
- URL: https://github.com/drsensor/p5-global2instance
- Owner: DrSensor
- License: mit
- Created: 2017-12-04T02:45:40.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2022-01-25T15:19:05.000Z (over 4 years ago)
- Last Synced: 2024-05-07T13:03:10.582Z (about 2 years ago)
- Language: JavaScript
- Size: 24.4 KB
- Stars: 19
- Watchers: 2
- Forks: 7
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# p5-global2instance
[](https://circleci.com/gh/DrSensor/p5-global2instance)
Convert p5js code/snippet from global to instance mode.
## Install
```bash
npm install p5-global2instance
```
## Usage
There are 2 away to use this script.
### CLI
```bash
p5-global2instance sourceCode.js
```
This will produce __file__ `sourceCode.p5.js`.
For more details use `--help`
```bash
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
```javascript
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
```javascript
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](http://esprima.readthedocs.io/en/latest/syntactic-analysis.html?highlight=configuration) and [escodegen](https://github.com/estools/escodegen/wiki/API) options.
```javascript
p5Convert(sourceCode, {
esprima: {},
escodegen: {},
instance: 'sketch' // default $_p
})
```