toolness / p5.js-widget

A reusable widget for embedding editable p5 sketches in web pages.
https://toolness.github.io/p5.js-widget/
GNU Lesser General Public License v2.1
162 stars 44 forks source link

Allow sketches to be created without setup/draw. #31

Closed toolness closed 8 years ago

toolness commented 8 years ago

From https://github.com/processing/p5.js/issues/1359#issuecomment-209658934:

When I was helping with Taeyoon's first Signing Coders workshop I noticed that a lot of people got hung up on the concept of functions at the very beginning. They also had a hard time making sure that their code was actually inside a function (i.e. before the final closing bracket).

This made me wonder if it would be possible for p5 to always export the createCanvas function, so that a sketch could actually consist of the following:

createCanvas(100, 100);
fill(255, 0, 200);

Always exporting createCanvas isn't an option for p5, but we can support this use case from the widget.

Since we're doing static analysis already, it shouldn't be too hard for us to walk the AST and find out if they've declared a setup or draw function. If they haven't, we can just wrap the user's code in setup() ourselves.