p5.js global mode versus instance mode for multiple sketches on one page
0 reputation · 23 May 2023, 10:19 UTC
0 reputation · 23 May 2023, 10:19 UTC
Goal: run multiple independent p5.js sketches on a single HTML page without variable name collisions or shared canvas interference.
In p5.js 1.x, global mode attaches sketch logic to the window object, which is convenient for one sketch but risks collisions when several sketches share a page. Instance mode wraps each sketch in a function that receives a p5 object, scoping variables and canvas elements to that instance.
Instance mode does not create a separate JavaScript realm, so all sketches still share the same global p5 library and browser environment. Shared globals and third-party extensions may still conflict regardless of mode.
Which mode better supports multiple independent sketches on one page, and what shared-state risks remain after choosing instance mode?
35775 reputation · 23 May 2023, 17:21 UTC
Use instance mode for multiple independent p5.js sketches on one page. It scopes each sketch's variables, canvas, and draw loop to a dedicated p5 instance, preventing the window-level collisions that global mode causes. However, instance mode does not create a separate JavaScript realm—sketches still share the same p5 library, DOM, browser event loop, and GPU resources.
new p5(sketch, container)).let x = 0;) stay local to that function closure; they never attach to window.background(), draw(), and mouse/keyboard handlers operate on that canvas only.setup() and draw() as properties of window, so a second sketch overwrites the first's functions and shares a single implicit canvas.Third-party p5 add-ons that extend p5.prototype (e.g., p5.prototype.myEffect = ...) affect every instance on the page because all instances inherit from the same prototype.
Calls like p5.disableFriendlyErrors() or p5._friendlyError = false are global; they cannot be scoped per instance.
Each WEBGL canvas consumes a GPU context. Browsers impose a hard limit (often 8–16 contexts). Running many WEBGL sketches simultaneously can exhaust this pool, causing later canvases to fall back to 2D or fail silently.
If you attach raw window.addEventListener('mousemove', ...) or keyPressed() without scoping to the instance's canvas, events fire for every sketch. Use the instance's mouseX/mouseY (relative to its canvas) and bind listeners via instance.canvas.addEventListener(...).
<div id="sketch1">/sketch2 container.let sketch1Value = 42;).window.sketch1Value and window.sketch2Value are undefined.background(255,0,0) in sketch1's draw() and background(0,0,255) in sketch2's; verify only the targeted canvas changes color.Are any of your sketches using WEBGL and running simultaneously? If yes, count the concurrent WEBGL canvases and test against the browser's context limit (typically 8–16). Exceeding it forces fallback or failure, which no mode choice can avoid.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.