sketching
sketching copied to clipboard
drawing without a (draw)
In many of the most basic Processing tutorials that I've worked through, drawing is done without setup/draw. This is an example: https://processing.org/examples/shapeprimitives.html
I've been using (no-loop) to recreate this in sketching but it would be better if I could leave draw and setup out. If I was teaching Sketching to beginners, I'd have them drawing some rectangles, lines, and ellipses before introducing the draw loop.
One of my own examples using (no-loop): https://github.com/ericcervin/getting-started-with-sketching/blob/main/pg023.rkt
I have been thinking about this issue, but haven't come to a conclusion yet.
On one hand allowing the user to leave out setup
and draw
makes it easy to produce a single image.
On the other, as I see it, allowing no call to setup complicates at least two things:
- the handing of size
- using the sketching as a library from a
#lang racket
program
The user calls size
to setup the size of the canvas (and the window displaying it).
If a user writes this:
(point 0 0) ; in principle no canvas yes
(size 200 200)
(point 10 10)
he is in principle calling (point 0 0)
before a canvas is available.
The P5.js project decided not to allow any uses at all of Processing functions outside setup
and draw
. Their online editor makes it clear, they expect the user to provide both a setup
and a draw
function:
https://github.com/processing/p5.js/wiki/p5.js-overview#why-cant-i-assign-variables-using-p5-functions-and-variables-before-setup
They explain why here:
https://github.com/processing/p5.js/wiki/p5.js-overview#why-cant-i-assign-variables-using-p5-functions-and-variables-before-setup
But what about the original Java version of Processing? I found some notes in their README here:
https://github.com/processing/processing4/tree/master/core
settings() is required
Prior to Processing 3, dark magic was used to make the size() command work. This was done to hide an enormous amount of complexity from users. Over time, the hacks involved became untenable or just unsustainable. The process was like this: ...omitted... The previous step gave fits to any other variants of Processing (like Python or Ruby or Scala) We had a tricky, stuttery situation where some things would happen automatically, other things would be delayed slightly. In the Android version of Processing, these methods weren't possible, so we enhanced the preprocessor to parse the size() command used in the sketch and create methods called sketchWidth() and sketchHeight() and so on, that returned the values found in setup().
It seems they regretted allowing programs to run without calling setup
first.
This explains the choice made in the newer P5 project.
I believe I heard Schiffman say something similar in one of the videos.
Now I still think it is a good idea to make it simple to make a static sketch easily without needing to use a setup
function with no-loop
. Since Racket is more flexible than Java, it might even be possible to figure a better solution out. One option is to introduce options in the #lang
line.
Maybe #lang sketching static
could setup a no-loop
program with some default size?
Another possibility is to detect that both setup
and draw
is missing and make that trigger a no-loop
and at the same time, make a call to size
an error (with an error message that says how to use setup
if a non-default size is needed.
But then again - if the Processing project is moving towards a non-optional setup
(or settings
) approach, then it might be worth waiting to see what they end up with.
I might be getting out of the loop (pun intended) with where Processing is going. The examples that I'm rewriting are from 2014/2015. I'm not sure that I have any knowledge of Processing other than being able to manipulate my existing code.
I've been thinking about this setup/draw issue a bit. I'm a huge functional programming fanboy, so I like the idea of starting people out inside functions.
While I'm talking about functions, I want to mention the defsketch function in the quil (clojure) flavor of Processing. That is a way of spelling out how the sketch will run. (I might be talking about 2014/2015 again)
(q/defsketch trigonometry :size [300 300] :setup setup :draw draw)
http://quil.info/api/environment#defsketch
@all-contributors please add @ericcervin for documentation