-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Open
Labels
Description
Increasing access
unsure
Most appropriate sub-area of p5.js?
- Accessibility
- Color
- Core/Environment/Rendering
- Data
- DOM
- Events
- Image
- IO
- Math
- Typography
- Utilities
- WebGL
- Build process
- Unit testing
- Internationalization
- Friendly errors
- Other (specify if possible)
Feature request details
The current global and instance modes are not ideal for integrating p5 into larger web applications:
- Global pollutes the namespace
- Instance requires you to pass the
sketchobject to every class that wants to use the p5 library, making the code more complex.
It would be great for 2.0 to add a third instantiation mode that's more in line with how THREE, Babylon or other libraries work.
In this mode, p5 functions would be neatly contained in a single global object, getting us closer to the modular approach that's already underway. Lingdong explains it very well in his q5xjs library.
So instead of doing this:
class Rectangle {
constructor(sketch) {
this.sketch = sketch;
}
draw() {
this.sketch.fill(255);
this.sketch.rect(50, 50, 50, 50);
}
}
let sketch = function(p) {
let rectangle;
p.setup = function() {
p.createCanvas(100,100);
rectangle = new Rectangle(p);
};
p.draw = function(){
p.background(0);
rectangle.draw();
}
};
let myp5 = new p5(sketch);we would do this:
class Rectangle {
draw() {
q5.fill(255);
q5.rect(50, 50, 50, 50);
}
}
let q5 = new p5();
let rectangle;
q5.setup = function(){
q5.createCanvas(100,100);
rectangle = new Rectangle();
}
q5.draw = function(){
q5.background(0);
rectangle.draw();
}which seems like a very small gain but can lead to much more readable code as the application grows.
Reactions are currently unavailable