p5.js MicroSim Architecture
This interactive diagram illustrates the standard structure of a p5.js MicroSim, showing how educational simulations are organized with distinct regions for visualization and user controls.
About This MicroSim
The diagram displays the key architectural components of a p5.js MicroSim:
- Browser Window - The HTML page container that hosts the simulation
- Container Element - The
mainordivelement that the canvas attaches to for responsive sizing - Draw Region - The top area with an aliceblue background where visualizations and animations are rendered
- Control Region - The bottom area with a white background containing sliders, buttons, and labels
Hover over any component to see detailed information about its purpose and implementation.
Key Concepts
- Fixed Heights, Variable Width - The draw region and control region have fixed heights (drawHeight and controlHeight), while the canvas width adapts to the container width for responsive design
- updateCanvasSize() - This function must be called as the first line in setup() to read the container width and establish proper canvas dimensions
- Canvas Attachment - The canvas is attached to
document.querySelector('main')to integrate with the MkDocs Material page layout - Separation of Concerns - Visual content stays in the draw region while interactive controls are organized in the control region
p5.js Editor Template
You can experiment with this code in the p5.js web editor.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | |
Lesson Plan
Learning Objectives
By the end of this lesson, students will be able to:
- Identify the key structural components of a p5.js MicroSim
- Explain the purpose of separating draw and control regions
- Implement responsive canvas sizing using updateCanvasSize()
- Create a basic MicroSim following the standard architecture pattern
Activities
- Exploration (5 min) - Interact with the diagram, hovering over each component to understand its role
- Code Review (10 min) - Copy the template code to the p5.js editor and identify each architectural element
- Modification (15 min) - Modify the template to add a second slider and display its value
- Creation (20 min) - Build a simple animation MicroSim using the architectural pattern
Discussion Questions
- Why is it important to call updateCanvasSize() as the first line in setup()?
- What are the benefits of using fixed heights for regions while allowing variable width?
- How does attaching the canvas to the main element help with MkDocs Material integration?
References
- p5.js Reference - Official p5.js documentation
- createCanvas() - Canvas creation function
- createSlider() - Slider UI element