References (official)
Basic policy of this Skill
- Philosophy: "Sketch with code" — the entire browser is a sketchbook.
- Structure: The basic loop is initialization in setup() and drawing every frame in draw() (up to 60 FPS).
- Accessibility: The core of P5.js is inclusivity. "Do not add new features unless they increase access."
- Performance: 2D by default; for heavy processing, consider WebGL mode + Framebuffer + shaders.
Philosophy (decision rules)
- Start from a sketch — verify behavior with a small experimental sketch before integrating.
- The three functions setup/draw/preload are the skeleton — load resources in preload, initialize in setup, draw in draw.
- The coordinate system has its origin at the top left — (0,0) is top left, +x is right, +y is down.
- Interaction uses built-in variables — mouseX/mouseY/keyPressed, etc., are automatically updated every frame.
- Be mindful of the cost of pixel operations — get()/set() are easy but slow. If speed is needed, use the pixels[] array or shaders.
- WebGL mode is a different world — Framebuffers (GPU-complete) and buildGeometry() can avoid recalculation.
Write a structural specification before drawing
Do not draw immediately;first verbalize the structureand then begin. List the screen composition, placement and proportions of elements, and movement trajectories as bullet points,predict common failuresand enumerate them (e.g., elements going outside the canvas / coordinate system being flipped vertically / overlapping and collapsing).
Output format (always in this order)
- Recommended policy (1–3 lines)
- Rationale (expression / performance / accessibility)
- Design proposal (drawing strategy / interaction / optimization / add-on libraries)
- Checklist (confirm before implementation)
- Pitfalls (to avoid)
- Next actions (order for trying small steps)
Checklist
- Are the spellings of preload/setup/draw correct (case-sensitive, preLoad is NG)
- Is the size of createCanvas() appropriate for the target environment
- Is an appropriate upper limit set with frameRate() (leave at 60 FPS if unnecessary)
- Are you mindful of pixelDensity() (it doubles on Retina displays)
- Are you calling loadPixels() before get()/set()/pixels[]
- Are heavy processes moved outside draw() (controlled in setup() or with conditionals)
- In WebGL mode, are you optimizing with Framebuffer or buildGeometry()?
- Before drawing, did you write the structural specification (composition/placement ratios/trajectories) and the list of potential failures?
Common pitfalls
- Typo in preload() (
preLoad()If you write it this way, it will fail silently) - Variable scope issues (JavaScript is strictly case-sensitive)
- The asynchronous timing of print() (it may be displayed after the contents of an array have changed)
- JavaScript's leniency (errors are harder to find than in Processing/Java)
- Performance of pixel operations (JS loops are slow; shaders allow parallel processing)
- Transferring p5.Graphics to the GPU every frame in WebGL mode (avoid by converting to an image with .get())
- pixelDensity inconsistency between browsers (consider explicitly unifying with pixelDensity(1))
- Starting to draw suddenly without preparing a structural specification (you won't notice overall failures)