Tired of working with single-file p5.js sketches?
This package has a solution for you!
Providing oop abstraction over p5.js functionality and cool features.
$ npm i oop5-js
Configure webpack or just use my configuration file. It will bundle your code, create html file and setup server with live reload.
$ npx webpack serve
If you use my config file, your application will be available at
localhost:9000
By default Webpack will create its own html file. But if you want to customize it, you can add path to html template in webpack.config.js
plugins: [
new HtmlWebpackPlugin({
// template: "src/index.html",
}),
],
import { OOP5 } from "oop5-js";
import { Component } from "./components/MyComponent";
class App extends OOP5 {
setup() {
//setup canvas size and bg
this.app.createCanvas(400, 400);
this.app.background(0);
this.components.set([
new Component
]);
}
draw() {
this.app.background(0);
//call draw for each component in list
this.components.list.forEach(component => {
component.draw();
});
//or more compact way
this.components.draw();
}
}
// Don't forget to call an instance of App somewhere
new App();
import { P5Component } from "oop5-js";
export class Component extends P5Component {
private x;
private y;
constructor() {
super();
this.x = this.app.random(0, this.app.width);
this.y = this.app.random(0, this.app.height);
}
draw(): void {
this.app.circle(this.x, this.y, 100);
}
}
Any p5.js method or enum available via
this.app
in classes that extends OOP5 or P5Component
App components should be stored inside App
field this.components
. It's not a regullar array but ComponentList
class.
Make sure, to modify list of components only via this class methods as it handles proper destroying of gone components. Such as removing events listeners etc.
Fields | Return type | Description |
---|---|---|
list | Array | native array of components |
Methods | Arguments | Description |
---|---|---|
set | (component: P5Component | P5Component[]) => number | set single or multiple components as current list |
((list: P5Component[]) => P5Component[])) => number | callback will receive current component list and returning value will be set as current list | |
add | (component: P5Component | P5Component[]) => number | add single or multiple components to the list |
Event handlers are available in App
and in Component
classes as metods.
This way you can simply implement handler inside class and oop5
will manage it.
Currently package has native support of next events:
- keyPressed
- keyReleased
- keyIsDown
- keyTyped
- mouseMoved
- mouseDragged
- mousePressed
- mouseReleased
- mouseClicked
- doubleClicked
- mouseWheel
class App extends OOP5 {
//...setup code
keyPressed(event?: KeyboardEvent, next: Function): void {
console.log('catch in app', event);
if(event.key === 'Escape'){
console.log('Escape catch, event will not propagate to any of components');
return;
}
next();
}
}
class Component extends P5Component {
//...setup code
keyPressed(event?: KeyboardEvent, next: Function): void {
console.log('catch in component', event);
next();
}
}
Make sure, to call next()
function any time if you want to propagate event deeper. Or do not if you don't want it to propagate.
In example above, any
keyPress
event (exceptEscape
key) will trigger handlers in Component also, asnext()
function is called.
class App extends OOP5 {
setup(): void {
this.app.deviceTurned = this.deviceTurned;
}
deviceTurned(){
console.log('turn');
}
}
on github https://github.com/muzmich/OOP5/issues
on mail muzmich@icloud.com