createCanvas fails when target element is missing — slim build and hidden-tab loop decisions for a low-traffic sketch
0 reputation · 25 Mar 2024, 14:05 UTC
0 reputation · 25 Mar 2024, 14:05 UTC
I'm consolidating a low-traffic p5.js page and hit the documented setup failure: calling createCanvas(w, h) with a parent element that doesn't exist in the DOM yet throws an error from the core createCanvas function and halts sketch setup entirely. In my case the sketch is loaded in the document head while the container div is rendered later, so the parent validation fails.
While fixing this I want to settle two cost-related decisions for the same deployment. First, I'm considering switching from the full library to the official slim build (p5.min.js), which excludes p5.sound and p5.dom, but I'm unsure how to confirm none of my sketches transitively depend on those addons before the swap breaks audio or DOM helpers in production. Second, I noticed p5.js keeps running the draw() loop when the browser tab is hidden — frameCount keeps incrementing — and the library leaves pause policy to the developer via windowFocus/windowBlur handlers rather than a documented default.
My questions:
32275 reputation · 25 Mar 2024, 18:40 UTC
The missing-parent error is a result of createCanvas() validating the DOM immediately upon execution; there is no built-in mechanism to defer attachment. To resolve this, you must ensure the target element exists before the sketch initializes. Regarding the slim build, a static symbol audit for sound and DOM APIs is the safest verification method. Finally, p5.js does not auto-pause the draw() loop on tab blur; using noLoop() within visibility handlers is the accepted pattern.
Confirmed Behavior: p5.js requires the parent element to be present in the DOM at the exact moment createCanvas() is called. If the script is in the <head> and the container is in the <body>, the lookup fails and the sketch halts.
Steps for Resolution:
DOMContentLoaded listener or move the <script> tag to the end of the HTML body.new p5(sketch, container)), ensure the container element is defined before the constructor is called. Passing a string ID still requires that ID to exist in the DOM at that moment.There is no documented p5.js API to create a canvas and attach it to a parent at a later time.
Confirmed Behavior: The official slim build (p5.min.js) excludes the p5.sound and p5.dom libraries. Core drawing and math functions remain intact.
Audit Process:
loadSound, play(), amplitude, fft, p5.SoundFile.createElement, createButton, createSlider, select, selectAll, createDiv.If no matching symbols are found and the sketch functions in a staging environment using the slim build, the swap is safe.
Confirmed Behavior: p5.js does not provide a default pause policy for hidden tabs. The draw() loop and frameCount continue to increment unless explicitly stopped by the developer.
Accepted Pattern:
The standard approach is to use the visibilitychange event or p5's built-in window handlers to toggle the loop state:
document.addEventListener('visibilitychange', () => {
if (document.hidden) {
noLoop();
} else {
loop();
}
});
// Alternatively, using p5 handlers:
function windowBlur() { noLoop(); }
function windowFocus() { loop(); }
Diagnostic Detail: Please confirm your p5.js version. While these behaviors are consistent in version 1.4+, significantly older versions may exhibit different error messaging or instance mode constraints.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.