p5.js
animation into a web page. It includes a series of questions and answers to test readers' understanding of basic p5.js
concepts. The accompanying animation demonstrates both the integration process and the concepts in action. The Actor metaphor is used twice, first to introduce the concepts and second for the implementation.The objective is to provide basic p5.js understanding, sufficient to integrate simple animations into a web page.
- The section Experiment demonstrates the integration of
p5.js
animation into this web page. - The section QA tests the readers understanding of
p5.js
through a series of questions and answers. It provides:- a mental model to think about
p5.js
; - a trivial example ;
- the starting point that leads to the animation in the Experiment section.
- a mental model to think about
- Finally, the complete code of the article is available here: GitHub.
The objective of the experiment is to display a grid of interconnected
nodes. An animation will demonstrate how probes
propagate from one node to
another by sensing the neighbors of each node and choosing to jump to the next
node in a given direction.
A grid of nodes is displayed below. Three probes are started at the top left corner, each with a given direction.
What is p5.js built for?
p5.js
is a tool for learning to code and make art.p5.js
allows to draw images on a web page using JavaScript.
Provide a way to think about p5.js
.
- One way to think about
p5.js
is in terms of Actors. - Providen an HTML Canvas
canvas
,p5 ≡ p5.js(canvas)
is the Actor we study. p5
sends itself messages:setup()
.draw()
60 times per seconds.
- By default, when
p5
receives thesetup()
message, it does nothing. - By default, when
p5
receives thedraw()
message, it does nothing.
Provide a way to think about the p5.js
programmer
.
- One way to think about the
programmer
is in terms of Actors. - The
programmer
tellsp5
what to do when it receives the messagessetup()
anddraw()
- Given the programmer instructions,
p5
does something instead of nothing.
The programmer
wants p5
to:
- Create a canvas ;
- Paint it in red.
What could be the dialogue between the programmer
actor and the p5
actor?
Here a possible dialogue:
programmer
top5
:- When you receive the
setup()
message, then: create your canvas. It should be 600 pixels wide and 300 pixels high. - When you receive the
draw()
message, then: paint your canvas red.
- When you receive the
That is how programmer
and p5
interact. As a programmer
, the objective is to add more and more things to setup
and draw
to make p5
draw what is desired on the canvas.
The programmer
wants p5
to:
- Create a canvas ;
- Paint it in red.
What could be the code written by the programmer
and received by the p5
actor?
- Given the vocabulary understood by
p5
; - the code might be:
function setup() { createCanvas(600, 300); } function draw() { background("red"); } - The result can be tested using: p5.js editor.
Provide a way to think about p5.js
.
- One way to think about
p5.js
is in terms of Actors. - A
programmer
tellsp5
what to do when it receivessetup()
anddraw()
messages. - The vocabulary that
programmer
can use to influencep5
behavior is given byp5
documentation.
How might this code be interpreted?
The programmer
tells p5
:
- When you receive
setup()
: create a canvas, 600px wide and 400px high. - When you receive
draw()
:- Draw the background blue.
- The next shape will be filled with yellow.
- The stroke of the next shape will be orange.
- The stroke weight of the next shape will be 20.
- Draw a circle shape with a center at (300px, 200px) and a diameter of 100px.
We call grid the grid shown in the Experiment section. What does it mean to map the grid to actors
?
- We define a state as the data that gives one frame of the grid.
- We define the computation as a function that transforms one frame to the next.
- The state of the grid is partitioned into a set of states, each encapsulated into an Actor.
- The computation is partitioned into all the functions of all actors.
Describe the grid.
A possible description is:
- There is a square grid of
n
positions. - At each position, there is a node.
- Each node is connected to its neighbors.
- Given a start node,
probes
have propagated in every direction possible. - Each direction has a color.
Describe the grid state in terms of actors.
A description might be:
- There is a grid, an instance of
Grid
.- It has a center position w.r.t the top left corner of the canvas.
- It has a width.
- It has a color.
- It fits several nodes per line.
- There are nodes, instances of
Node
. A node has:- a position ;
- a radius ;
- a color ;
- neighbors.
- There are probes, instances of
Probe
. A probe has:- a direction ;
- a color ;
- nodes that have been visited.
Describe the grid computation in terms of actors.
The computation might be mapped into the following sequence of events:
p5
receives thesetup()
event.- The
canvas
is created. - The
grid
is created.- Nodes are created.
- Nodes are interconnected.
- Probes are created.
- The
p5
receives thedraw()
event.- The canvas background is painted
- The grid is drawn.
- Probes are drawn.
- Each probe try to reach a neighbor in its direction if any.
- If all probes are immobile, then: stop the computation.
Assume that the HTML canvas may be built like so:
Provide an implementation of setup
.
An implementation might be:
Provide an implementation of draw
.
An implementation might be:
Provide an implementation of Node
.
An implementation might be:
Provide an implementation of Probe
.
An implementation might be:
- It would be preferable to avoid using the global scope — i.e. to encapsulate
setup
anddraw
. - A few details have been left out — e.g. the use of
frameRate
to slow down the animation. - The integration has been left out — e.g. the implementation of
Canvas
. The interested reader may consult the code. - A few concepts have been intentionally left out — e.g.
class Direction { … }
. This omission illustrates two important concepts:Thinking Above The Code
(YouTube) and howWhishful Thinking
helps programming.Wishful Thinking
is a very powerful programming practice:Before implementing a component you write some of the code that actually uses it. This way you discover what functions with what parameters you really need, which leads to a very good interface. You will also have some good test code for your component.
The idea is based on the fact that an interface's purpose is to simplify the code that uses the component, not to simplify the code that implements it.
- One way to think about
p5.js
using the actor model has been introduced. - A way to implement an animation using
p5.js
and the actor model has been introduced. - An animation using
p5.js
has been integrated into this web page. - A way to build non-trivial animation has been introduced.
- A few Programming principles have been illustrated, in particular: Thinking Above The Code and Whishful Programming.
- This web page code is available here: GitHub