We introduce a variable, xL (shorthand for "left").
We could have called it anything we wanted, but xL was a better name than "smurf". (Why?)
Every variable has a declaration, where we tell the computer that we will be reserving the name xL for something that we care about.
Every variable also has an assignment, where we give it the initial value we want it to have.
We can then access the value stored in the variable, such as in each line command.
Now we can change all 7 lines just by changing one value! Let's change xL to 75.
So far we've been using variables to represent coordinates. But really we can use them to control just about anything.
I spaced my lines by multiples of 50. Why?
Because I happen to know my 50 times-table. But what if my lines looked better when they were spaced out by multiples of 43.7?
Reckoning these values is a special kind of hell.
This introduces a general principle of creative coding, which is that every constant is an opportunity for change.
Here, for example:
These variables are changing over time because their values are assigned inside the draw() loop, which is executed 60 times per second.
See how I'm using the self-referential assignment to change the value of i.
Since I'm just adding 1 to i each time, this is also called incrementation.
So —
A good rule in creative coding is that if you're doing something more than twice, there's probably a better way to do it.
A good rule in creative coding is that if you're doing something more than twice, there's probably a better way to do it.
A good rule in creative coding is that if you're doing something more than twice, there's probably a better way to do it.
Let's introduce iteration!
Here we introduce the for-loop structure. I'm sorry it looks terrible. Let's talk about its pieces.
The variable i here has a special name; it's called the iterator variable. It's like the tally you hold in your head (or your fingers) when you're counting.
This part is the initialization. In this example, we're going to start counting at 1. But we could start counting at a different number:
for (let i=1;
Here is the termination condition. We count so long as this is true. In this case, I'm gonna count so long as i is less than or equal to 7:
i<=7;
The last thing in the parentheses is the "updation", or method of updating the counting variable. This is how we're going to count. We could be counting by twos or tens. Instead we're going to count by ones, always adding one to our previous value:
i=i+1)
Finally, we're going to execute whatever is inside of the code block defined by the curly braces:
{ line(xL, i*sp, xR, i*sp); }
That's the stuff that gets repeated. The code inside this block doesn't have to use the i variable, but it often does. Indenting this code helps us see the structure.
Let's take a short digression to discuss scope.
It matters where in the code a variable is declared. Here, the variable xL is scoped within the draw() block.
Clicking causes an error, because the code inside the mousePressed() function is unable to know about xL; xL is "hidden" from anything inside mousePressed(). The only code that knows about xL is inside the draw() block.
xxxxxxxxxx
function setup() {
createCanvas(400,400);
}
function draw() {
background('lightblue');
strokeWeight(10);
strokeCap(SQUARE);
stroke('black');
line(75, 50, 325, 50);
line(75,100, 325,100);
line(75,150, 325,150);
line(75,200, 325,200);
line(75,250, 325,250);
line(75,300, 325,300);
line(75,350, 325,350);
}