9.11: Instance Mode (aka "namespacing") - p5.js Tutorial

The Coding Train · 29K views · 2016 · 14:30

Open on YouTube

Creative coding & generative artWeb developmenttutorial

Why Stack Overflow recommends it

Stack Overflow users link it when answering questions about p5.js instance mode, especially putting multiple canvases or sketches on one page.

Recommended by 4 people across 4 different months, 2016 to 2024.

What it is

A Coding Train tutorial by Daniel Shiffman on p5.js instance mode, which wraps a sketch in a function so it avoids global variable conflicts. It also covers how to run more than one canvas on the same web page.

What people said

Strong rec julianmueller · 2016-12 · Stack Overflow · on “p5js instance mode and object orientation”

Oh yes, I see. sorry this is just some made up code, my focus was actually on the instance mode and how to refer to it as p5 objects. I found this video to be extremely helpful: [this video]

Recommends AngelSalazar · 2017-05 · Stack Overflow · on “Two canvases on one page p5.js”

are you using p5 instance mode ?, I believe you are overwriting your canvases, that's why you only see one of them, please take a look to this video [this video]

Recommends CrackbuddyMethusalem · 2021-06 · Stack Overflow · on “In p5js instance mode for several canvases p5 object is not defined”

I am running a p5.js sketch on a flask server. I want to draw several canvases, so i instanced my code like Daniel Schiffman showed in his "9.11: Instance Mode (aka "namespacing") - p5.js Tutorial"-Video [this video] , but when i run the code it gives me "Uncaught ReferenceError: p5 is not defined" on the line i instantiate my canvas var drawCanvas = new p5(firstcanvas); When i run the code in the p5 web editor it works. The script tag in my html looks like this <script src=" [LINK] " integrity="sha512-a5hlZKgpC1LVAuKgVeXdP0D9Yfacj0hLtNdzx9zFMkIWRrQyO37KtIPiqArGmVuaBYu3ON6Vt0N3+G/JaLXQYQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script> so thats the newest version. any idea? thank

Recommends JerryHamm · 2022-08 · Stack Overflow · on “Switching between P2d and WEBGL”

After a bit of trial and error, I figured out a way to get around this. I used instance mode (a.k.a namespacing) and the show, hide and position method in p5.js A video explaining namespacing well in p5.js: [this video] p5 reference for show: [LINK] p5 reference for hide: [LINK] p5 reference for position: [LINK] sketch showing how show, hide and position is used: [LINK] This is the sample code i tried it on and it worked. //webgl sketch var canvas; var webgl_sketch = function(w) { w.setup = function() { w.canvas = w.createCanvas(600, 600, WEBGL); w.canvas.position(0,0); w.canvas.hide(); } w.draw = function() { w.background(200); w.rotateX(w.frameCount * 0.01); w.rotateY(w.frameCount * 0.01); w.bo

Cites user15772486 · 2021-04 · Stack Overflow · on “How can I display two p5js sketches on the same html page?”

I'm hoping to set up a website for my animation studio, and I'm hoping to have a few p5 sketches set up in the background to add some flair. For right now, I have two basic sketches set up, just to be sure that everything is working properly. One displays as pink [LINK] , and one displays as blue [LINK] , and they're designed to be drawn above and below the line "Who we are". My problem is that my html page seems to be capable of loading one of my scripts, but not both. I can comment out the pink script, and the blue will show up, properly parented to my div, and vice versa. Does anyone know how to make these two sketches show up on the same page? Thanks p.s. I've tried using instance mode [

Cites ggorlen · 2024-08 · Stack Overflow · on “How does p5.js instance mode work where the argument to instantiate the object is a function?”

This is nothing unusual if you're accustomed to first-class functions [LINK] in JS. Functions are the same as any other object--you can store them in variables and pass them as parameters. It's also common to use functions in JS as namespaces, to close over [LINK] a set of variables. Both principles are at play here. The Sketch function is a closure and p5.js calls it with the instance it's created passed as a parameter. You then add the draw / setup callbacks to the instance within the closure as you see fit. There's nothing circular here because the p5 parameter name replaces the p5 declared in the outer scope within the Sketch function. You can make that parameter any name you want. p is