Security News
The Risks of Misguided Research in Supply Chain Security
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
A Node.js implementation of the p5.js library.
(using node-canvas
and jsdom
, along with a bunch of other modules)
Why?
yes
Does everything from p5 work?
most things work, except for stuff like 3D (webgl is not a thing in node), interacting with the canvas (mouse, keyboard, window events) - because, duh, there's no window, and certain things that i didn't feel like implementing because there are a bunch of better alternatives for Node (like httpDo, httpGet, and httpPost can be easily replaced by just using
request
oraxios
)
Is the syntax the same?
for most things, yes - you can find the docs below for the features that are different from the official p5 browser library
$ npm i --s node-p5
All p5 methods must be preceded by the variable you pass to the sketch function.
const p5 = require('node-p5');
function sketch(p) {
p.setup = () => {
p.createCanvas(200, 200);
}
p.draw = () => {
p.background(50);
p.text('hello world!', 50, 100);
}
}
let p5Instance = p5.createSketch(sketch);
Of course, we can't see this because there is no window, so we should save the canvas as an image.
const p5 = require('node-p5');
function sketch(p) {
p.setup = () => {
let canvas = p.createCanvas(200, 200);
setTimeout(() => {
p.saveCanvas(canvas, 'myCanvas', 'png').then(filename => {
console.log(`saved the canvas as ${filename}`);
});
}, 100);
}
p.draw = () => {
p.background(50);
p.text('hello world!', 50, 100);
}
}
let p5Instance = p5.createSketch(sketch);
I couldn't get the p5 preload to work. I wrote my own version.
You can pass the built-in preload functions
const p5 = require('node-p5');
let resourcesToPreload = {
catImage: p5.loadImage('cat.png')
}
function sketch(p, preloaded) {
let catImg = preloaded.catImage;
p.setup = () => {
let canvas = p.createCanvas(100, 100);
p.image(catImg, 20, 20, 60, 60);
p.saveCanvas(canvas, 'out', 'png').then(() => {
console.log('saved canvas as out.png');
}).catch(console.error)
p.noLoop();
}
}
let p5Instance = p5.createSketch(sketch, resourcesToPreload);
or you can pass in your own functions (both sync and async work!)
let resourcesToPreload = {
myVar: () => {
return 5+2;
},
myVar2: () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('thing');
}, 500);
});
},
myVar3: new Promise((resolve, reject) => {
setTimeout(() => {
resolve('thing');
}, 500);
})
}
"Why can I not use
loadFont
in preload?" - you might askand the answer to that is: because I couldn't get it to work lmao
Loading custom fonts can be done using the loadFont
method
p5.loadFont({ path: String, family: String})
//or
p5.loadFont(filename: String)
example:
const p5 = require('node-p5');
let sugarpunch;
sugarpunch = p5.loadFont({ path: 'SugarpunchDEMO.otf', family: 'Sugarpunch' });
//OR
sugarpunch = p5.loadFont('SugarpunchDEMO.otf');
function sketch(p) {
p.setup = () => {
let canvas = p.createCanvas(100, 100);
p.textFont(sugarpunch); // or p.textFont('Sugarpunch')
p.text('some text using the font', 20, 20);
p.saveCanvas(canvas, 'out', 'png').then(f => {
console.log(`saved canvas as ${f}`);
}).catch(console.error)
p.noLoop();
}
}
let p5Instance = p5.createSketch(sketch);
note: when passed as preload functions to p5.createSketch you shouldn't use the callback; otherwise, if you're using them inside your sketch you can either use the callback or .then
as they return promises.
note2: when using them in preload they are methods of the p5
object, when using them inside your sketch they are methods of the p
(in my case) object.
loadImage(filename: String[, callback: Function])
returns: Promise
loadJSON(filename: String[, callback: Function]);
returns: Promise
loadStrings(filename: String[, callback: Function]);
returns: Promise
saveCanvas(canvas: Canvas, filename: String [, extension: String])
returns: Promise
Saves the frames as png
or jpg
in the output folder with names like frame-001.png
, or as a gif with the name of the folder.
saveFrames(canvas: Canvas, outFolder: String, extension: String, duration: Number, frameRate: Number)
png
or jpg
saveFrames(canvas: Canvas, outFolder: String, options: Object, duration: Number, frameRate: Number)
by replacing the extension with an object we are saving the animation as a gif
available options:
returns: Promise
Saves an Object as a JSON file
saveJSON(object: Object, filename: String[, optimize: Boolean])
returns: Promise
Saves an array of strings as a file
saveStrings(list: Array, filename: String[, extension: String, separator: String])
\n
(new line)]returns: Promise
p5.registerMethod(name: String, method: Function)
p5.registerMethod('addNumbers', (x, y) => {
return x + y;
});
...
function sketch(p) {
p.setup = () => {
console.log(p.addNumbers(20, 5)); //25
p.noLoop();
}
}
Don't use arrow notation if you want to use p5 features in your methods. If you use arrow notation the module cannot bind p5 to your method and thus you won't be able to use it.
p5.registerMethod('methodName', (arg1, arg2) => {
console.log(this.p5); //undefined
});
p5.registerMethod('methodName', function(arg1, arg2) {
console.log(this.p5); //the p5 object (with all the methods)
});
The following strings can be passed to registerMethod
instead of the method name to affect when that method is automatically called.
draw()
. It can affect drawing.draw()
.p5
constructor) is executed.Example:
p5.registerMethod('init', () => {
console.log('called when initialization is finished');
});
p5.registerMethod('pre', () => {
console.log('every frame, at the start of draw');
});
p5.registerMethod('post', () => {
console.log('every frame, at the end of draw');
});
Want to create your own plugins? I've got you covered fam.
//myp5Plugin.js
module.exports = {
mapNumbers: function(arg1, arg2, arg3, arg4, arg5) {
return this.p5.map(arg1, arg2, arg3, arg4, arg5);
}
}
let p5 = require('node-p5');
p5.registerPlugin(require('./myp5Plugin'));
function sketch(p) {
p.setup = () => {
console.log(p.mapNumbers(5, 1, 10, 1, 100)); //45
p.noLoop();
}
}
let p5Instance = p5.createSketch(sketch);
FAQs
A Node.js port of the p5.js library
We found that node-p5 demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
Research
Security News
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.