Hi, i try to extends class in p5.js sketch, but not works…
How do you make this?
Any simple example please?
Hi, i try to extends class in p5.js sketch, but not works…
How do you make this?
Any simple example please?
can you give us an example of what you’re trying to do to work off of?
JavaScript got a prototype inheritance system, in which we make available all properties to be extended by annexing them all to a constructor function’s property object {}
called prototype:
For example If we wanna extend the class p5 (classes are constructor functions btW) w/ a new method called square(), we can simply annex it to p5.prototype{} object, like this:
p5.prototype.square = function (x, y, size) {
return this.rect(x, y, size, size);
};
And a more complex example, but now extending the class p5.Image instead, w/ a new method called resizeNN():
Thanks for this info, i looking for use “class” and “extends” on p5.js
It´s possible?
i ´m a noob in javaScript.
Yes, it is possible. But in order to use an extended p5 subclass, you’re gonna need to write your code using the dreadful “instance mode” approach:
<script defer src=https://CDN.JsDelivr.net/npm/p5></script>
<script defer src=sketch.js></script>
/**
* Class p5 Extended (v1.0.1)
* GoToLoop (2018-Jun-13)
* https://Discourse.Processing.org/t/how-i-extends-class-in-p5-js/894/5
*/
"use strict";
class p5Plus extends p5 {
square(x, y, size) {
return this.rect(x, y, size, size);
}
}
function mySketch(p) {
const RATIO = .75, BOLD = 3.5;
p.setup = function () {
p.createCanvas(p.displayWidth * RATIO, p.displayHeight * RATIO);
p.rectMode(p.CENTER).colorMode(p.RGB).noLoop();
p.fill('yellow').stroke('red').strokeWeight(BOLD);
};
p.draw = function () {
p.background(0).square(p.width >> 1, p.height >> 1, p.width >> 1);
};
}
new p5Plus(mySketch);
Thanks for the info!!
Thxs for the demo @GoToLoop. I have some questions on my own.
This concept of global vs. instance more is common in js? With instance mode, you create your own scope, right? That is what allows to work with several instances. If you get to write js from scratch, it is enough to define a class to be able to have this instance mode
feature?
I have to ask… class is not a closure, or is it?
Lastly, how does prototype gets inherit?
I will have to write my own little demo to see these concepts in action.
Kf
For my part I use a straight ECMA6 syntax for this…
class Foo{
constructor(){
//...
}
someMethod(){//...}
}
class Bar extends Foo{
//...
}
And so on. To me this approach is easier, since JS isn’t really my native language. I was wondering if those of you who know a lot more Javascript than I do could tell me if there are any significant disadvantages to doing it this way. I only see it rarely in other people’s code.
I tried this in the processing IDE in p5.js mode and it does not work.
and some other things that work like the reserved word “const”.
I do not understand why this happens to me.
AFAIK, PDE’s p5js Mode doesn’t allow the instance mode approach I guess.
The easiest way to run my demo sketches is by having any Firefox-based browser installed in your OS.
I recommend Firefox Developer Edition, Waterfox & Palemoon:
https://www.mozilla.org/en-US/firefox/developer/all/
P.S.: For Chromium-based browsers, read on this site below:
On the contrary; by using the keyword class
, the JS language automatically annexes all the methods in the constructor()'s prototype{} object the most correct way.
There are many ES5 folks out there who create the methods over & over inside the construtor function itself, rather than its prototype{}; which isn’t RAM savvy btW!
This is a Processing concept thingy! Our “Global Mode” approach is strange to JS world!
Basically, it auto instantiates, then dumps & binds the whole p5 class to the global scope, which allows us to access them all w/o using the dot .
operator.
Yup! And that’s the standard way most apps do in the JS world!
Correct. However, we still can have multiple p5 “global mode” instances by placing them all inside separate <iframe>
tags, like I did below:
Do you mean my “Class p5 Extended” hack example?
B/c we have to pass our function sketch as its argument: new p5Plus(mySketch);
That sketch needs to be using the “instance mode” approach.
Along w/ closures and how the keyword this
works under JS, the innards of the prototype{} inheritance chain are the hardest parts of the JS language!
As I had already posted in this thread at my 1st reply, we can read a fully tech explanation about it here:
But I’m gonna attempt a shorter explanation below…
__proto__
:new
to create an object, it automatically assigns its __proto__
property the reference of its constructor’s prototype{} object:.
dot or brackets []
, if that property doesn’t exist in that object, the JS engine follows its __proto__
instead.__proto__
correctly points out to its original constructor’s prototype{} object, it should find the “missing” property there instead.A post was split to a new topic: Make class get all functions of p5.js
Hey GoToLoop, I realize this is an old post, but would you mind explaining why you say the instance mode is dreadful?
What is your preferred method for writing P5.js code so that it is not in the global namespace (and potentially conflicting with other js libraries)?
Thanks very much
.
in order to use their API.static final float BOLD = 1.5;
static final color STROKE = #FF00FF, FILL = -1;
static final color ALL_COLORS = #000000;
void setup() {
size(800, 600);
noLoop();
colorMode(RGB);
blendMode(REPLACE);
strokeCap(ROUND);
strokeJoin(MITER);
strokeWeight(BOLD);
stroke(STROKE);
fill(FILL);
}
void draw() {
background((color) random(ALL_COLORS));
print(frameCount, TAB);
}
void mousePressed() {
redraw();
}
void keyPressed() {
mousePressed();
}
.
operator is never used anywhere in the sketch above..
operator:static final int W = 800, H = 600;
static final float BOLD = 1.5;
static final color STROKE = #FF00FF, FILL = -1;
static final color ALL_COLORS = PImage.ALPHA_MASK;
void settings() {
this.size(W, H, PConstants.JAVA2D);
this.noLoop();
}
void setup() {
this.colorMode(PApplet.RGB);
this.blendMode(this.REPLACE);
this.strokeCap(PConstants.ROUND);
this.strokeJoin(PApplet.MITER);
this.strokeWeight(BOLD);
this.stroke(STROKE);
this.fill(FILL);
}
void draw() {
this.background((color) this.random(ALL_COLORS));
PApplet.print(this.frameCount, PConstants.TAB);
}
void mousePressed() {
this.redraw = true;
}
void keyPressed() {
this.mousePressed();
}
No 1 writes Processing sketches like the above, obviously!
However, the use of the keyword this
reveals that a Java Mode sketch is actually a class.
More precisely, a subclass of Processing’s main class PApplet.
And if a member happens to be static
, Java allows us to optionally access them via its class or interface name instead.
For example, static
field REPLACE from interface PConstants can be accessed as this.REPLACE
, PApplet.REPLACE
or PConstants.REPLACE
.
In p5js, our sketches aren’t a subclass of its main class p5 though.
Instead, they’re a callback argument passed to p5’s constructor().
Well, at least that’s the case when we use the “instance mode” approach.
In “global mode”, the class p5 is automatically instantiated.
Then that instance is dumped into JS’ global context, as properties of the window{} object:
var
or function
are automatically added to the window{} object too..
operator behavior very well via its “global mode” style.As a Java/Python Mode user too, I very much prefer p5js’ global mode.
<iframe>
if we’d rather prefer to keep them on “global mode”. Here’s an online example which got both global & instance mode versions of a sketch running together at the same time:
Each 1 is inside its own <iframe>
. But we can easily go w/ 1 global mode sketch along many other instance mode sketches sharing the same window{} global context:
Bl.ocks.org/GoSubRoutine/raw/60b154e52336f7fee7c5f1fe817ceb22/all.html
<iframe>
in order to have its own separate window{}.