Let’s make sure we understand the key ideas of using loops in JavaScript by using them in a miniature project.
As with all challenges, do this work in pairs and in class. This means you will both submit the same work and should work together on one computer to produce the project. You can take turns typing stuff in.
To start up you should:
loops-challengecart253 repositoryWith your partner(s):
Get in line
We’re going to start with some example code for you to modify, so start paste the following code into your script.js:
/**
* Lines
* Pippin Barr
*
* A series of lines across the canvas
*/
"use strict";
/**
* Creates the canvas
*/
function setup() {
createCanvas(500, 500);
}
/**
* Draws lines across the canvas with increasing thickness and
* gradually lightening colour
*/
function draw() {
background("pink");
stroke(0);
line(0, 0, 0, height);
stroke(25);
line(50, 0, 50, height);
stroke(50);
line(100, 0, 100, height);
stroke(75);
line(150, 0, 150, height);
stroke(100);
line(200, 0, 200, height);
stroke(125);
line(250, 0, 250, height);
stroke(150);
line(300, 0, 300, height);
stroke(175);
line(350, 0, 350, height);
stroke(200);
line(400, 0, 400, height);
stroke(225);
line(450, 0, 450, height);
stroke(250);
line(500, 0, 500, height);
}
Convert the challenge code by adding variables and using a while-loop to replace the incredibly repetitive drawing instructions.
🌶️ Consider varying some other quality of the lines like their stroke weight, length or even their rotation through the loop as well.
Now that you have a lovely loop example, add another loop to draw horizontal lines from the left to the right of the canvas.
🌶️ What if you keep one end of the lines fixed while the other end changes?
Make the background more interesting by drawing a gradient across it made out of lines. Use a for-loop for this one. You’ll probably want to draw the lines very close together (maybe just one pixel apart?) and you could consider using map() to set their colour relative to their position on the canvas.
🌶️ What happens if you use colorMode(HSB) to change the hue across the canvas?
🌶️ What happens if you use sin() to affect the colour going across the canvas?
When you’re finished, show the instructor or teaching assistant your work and they will confirm you’ve passed the challenge and can submit it on the Moodle.
This challenge is pass/fail and is worth 1% of your final grade.
Once you’re cleared to submit, go to the Moodle and both submit your work. You should all separately submit:
Note: It’s fine if the project is only in one of your repositories, but it’s not a bad idea to make sure you both have it for your own reference.