Tutorial Seven
2026-11-20T23:59
Goals
- Learn how to build some of your own stroke effects
Getting started
- Start a new repository by accepting the assignment.
- Clone the repository to your local computer (in your shell, type
git cloneand the name of the repository)
I have noticed that Classroom 50 adds a lot of noise to the name of your repositories, which makes it difficult to distinguish between files in a file browser.
You can rename the directory without breaking anything, and I suggest that you do so.
git even provides you a way to do this at the moment when you clone the repository:
git clone <repo address> <new name>Task
For this tutorial we are going to explore some different ways to draw a line. You are going to produce a line “sampler” - a row of line examples. The goal is to larger maintain a consistent interface so we could just swap in the new lines, but we will allow some special parameters for some of the lines styles.

Links to the references for the included functions can be found at the bottom.
Part 0: Familiarize yourself with the code
I set you up with the basic scaffolding so you can focus purely on line drawing. The setup and draw functions are already written for you. You can see that I have it producing a collection of identical lines so we can see the style differences. I’ve drawn the first one for you using the built in line function. I also have some controls for setting the stroke weight and color. Because we will be using randomness, but also want the script to respond to the controls, I have set a fixed random seed and given you a “New Seed” button that will pick a new one using the current date and time.
Part 1: A line alternative
The first line you will draw is not one that would be terribly useful itself, but it will provide you with the building blocks needed the understand the others.
For the first line (altLine) the goal is to create a reasonable facsimile of the behavior of the built-in line function. You will still use the line function, but we will simulate the stroke weight by drawing several lines with a weight of 1.
This is by no means how thick lines are actually drawn
If you look at the function definition I’ve provided (altLine(x0, y0, x1, y1)), there is no argument for the stroke weight or the stroke color. As we did in class for the fill color, we are going to handle this internally using the current drawing context.
drawingContext.lineWidth- the stroke weightdrawingContext.strokeStyle- the stroke color
Create two variables in altLine to hold those values.
Since you are going to make a stack of lines that all follow the same path, we need a way to offset them that is reliable for any orientation of the line. We are going to do that by finding the perp vector of the line. In 2D, the counterclockwise perpendicular of a vector \(\vec{v}\) is \((-v_y, v_x)\). Of course, we don’t have a vector, we have two end points. To get the vector, we need to subtract one point from the other.
Use createVector to make a vector v from the two points. You don’t need to make the points themselves vectors – just do the subtraction for each component separately.
Use createVector again to make the perp vector and call it perp. Call perp.normalize to make perp a unit vector.
Now we can draw the line. You are going to adjust fill and stroke properties, so start with push and end the drawing wth pop.
Set the stroke weight to 1 and set both the stroke and the fill to the color you read from drawingContext.strokeStyle.
To draw the line itself, we are going to make weight parallel lines, where weight is the value you read from drawingContext.lineWidth. We also want the new line to be centered on the specified line. So the offset from the original line to the farthest line will be weight/2 along the perp vector.

We want to draw that line, and then one under that, and one under that, carrying past the original line to -weight/2 (i.e., the same value, but in the opposite direction of perp. ).
Write a loop that iterates from -weight/2 up to and including weight/2, drawing the corresponding line for each one with the line function.
Run that and you should see a rotated rectangle. It won’t match the original line in two ways.
First, it doesn’t have the end caps. We can add the end caps by drawing circles at the two endpoints. Make the circle size be the weight of the line.
Second, we can see some of the background shining through the line where the 1 pixel thickness lines don’t quite mesh together. We aren’t going to fix that one. In its current state, it makes for a different texture, and honesty, the value to writing this function is to think about how to deconstruct thick lines. If you want a function that makes thick lines that look just like those produced by line, you should use line.
Part 2: scratchyLine
The second line you will make is one that looks like it could have been made with some kind of brush. At the start and end of the stroke, the brush isn’t fully contacting the surface – there are just a couple of strands leaving color.
To make this line, you are going to do almost exactly what you did for the last line, you are just going to start and stop the lines in different places. As such, start by copying the body of the altLine and pasting it into scratchyLine.
For this function, in addition to the offset perpendicular to the line, we want offsets along the line. For this, we will recall our parametric equation for a line: \(P_0 + t \vec{v}\), where \(\vec{v} = P_1 - P_0\). When \(t = 0\), we are at \(P_0\) and when \(t = 1\), we are at \(P_1\). To get our fringe, we will start at values of \(t\) that are around \(0\) and likewise for the end we will end our separate lines at a value of \(t\) around \(1\).
We are going to pick offsets that are essentially within the end cap of the line, in other word, \(\pm \frac{weight}{2}\). That is easy to compute in pixels, but we need to do it in terms of \(t\) which varies with the length of the line. Fortunately, we know that \(t\) varies from 0 to 1 over the length of the line, so the conversion from pixels to \(t\) is just \(\frac{1}{length}\). Since we have the vector v, we can just call mag() to get the length of the line. Put these together to get the change in \(t\) represented by half of the line weight. Store this in a variable called deltaT.
Inside of the for loop, crate variables t0 and t1, which will be the values of t on which to start and stop. t0 should be a random value between -deltaT and deltaT. t1 should be a random value between 1 - deltaT and 1 + deltaT. Rewrite the line to use the parametric form of the line with these new values of t for the end points.
Part 3: speckleLine
Your next line will have the same starting place, so copy over the code yuo just wrote in scratchyLine into speckleLine.
Just as we did for the fill, we are going to use small dots to provide texture. The general idea is that instead of drawing a line, we are going to draw a random collection of semi-transparent circles along where the line would be.
Since you are going to be drawing circles instead of lines, inside of the push, turn off the stroke and set the fill to the color. We also want to set the alpha value of the color to a lower value. We can do that with setAlpha, but the color needs to be a proper p5.js color, which is not what you get from drawingContext.strokeStyle. If yuo pass the value into the color() function, you will get a p5.js color object. Set the alpha value to 25.
In the loop, replace the call to line with another loop. Inside the loop, randomly generate a t value between the start and end values you already randomly chose. use the parametric line equation to draw a size 1 circle at the location dictated by t. Run the loop 100 times. You should now have something like a crayon stroke.
You should notice that the function has a density parameter that you haven’t used yet. The idea is for that to control the density of the dots. When the value is 0, there should be no dots and when the density is 1, you should practically have a solid line. We will make this work by deciding how many points would make the line look solid. We know the line’s length, and the circles will cover two pixels of the line. We are also drawing the circles with an alpha value of 25, so it would take four circles to get the color at full strength. So, length * 2 is probably a reasonable ballpark estimate for full density. Use map and this formula to get the number of dots you want to draw. Replace the 100 in the inner loop with this value.
Part 4: wiggleLine
The idea behind wiggleLine is that naturally made lines (like hand drawn lines) tend not to be completely straight. To make the line you are going to draw it in segments, and at each point you will add a small random offset to the position. There are a variety of ways to interpret that, but we are going to make it a small bump along the perp vector to maintain the rest of the structure of the line.
Copy in the code for getting the vector along the line and the perp. You are going to dra the line using beingShape() and endShape(), so we can rely on the normal techniques for setting the color and weight.
I’ve added two parameters to this function
segLen- the length between breaks in the linedev- the maximum deviation away from the line
Use the length of the line and segLen to get the number of segments you are going to make.
Use beginShape and endShape and a loop to draw the line. Use t as the iterator, ranging from 0 to 1, incrementing by 1 divided by the number of segments. This should just look like the original line.
Inside of the loop, generate a random offset between -dev and dev. For the x and y components of the vertex, add in the appropriate perp component multiplied by the offset. The line should now be a little wiggly.
The line is also a little ugly because we have sharp peaks. Swap your calls to vertex out for curveVertex.
This looks better, but the line got shorter. Recall that when we use curveVertex, the first and last points of the shape are taken to be control points. We can cheat a little bit by repeating these points. Add a normal vertex for the start point before the loop and another for the end point after the loop.
Part 5: wispyLine
Your last line combines together the ideas of the last two lines – you are going to make wiggly lines, but then you will draw dots along the lines instead of connecting them as one long line. In addition, you will make multiple passes, and on each pass, you will make more changes.
Copy the code from wiggleLine. The wiggle will come from a different mechanism, so remove the random offset (which will mean you can get rid of the perp calculation as well).
Instead of drawing the line with beginShape and vertex, store the points you calculate in an array called points. Use createVector to make a Vector object and push it into the array.
Check your work by iterating over the array and drawing the shape:
// drawing loop
beginShape();
for (let i = 0; i < points.length; i++){
vertex(points[i].x, points[i].y);
}
endShape();You should see a straight line.
wispyDraw
We are going to pull out the code that does the actual drawing into a new function called wispyDraw. Replace the drawing loop with a call to wispyDraw(points, dev).
Get the current stroke color.
Inside of a push / pop, turn off the stroke and set the current stroke color as the fill color like you did before.
Write a loop that iterates over the indices of the points.
For each point, you are going to draw a collection of circles randomly placed between the current point and the next point. The complex part of this is that we want our points to fall on the curve that would occur between these points if we were still using curveVertex. Fortunately, we have the curvePoint function that will tell us this. Unfortunately, this function needs information about four different points (because we need to know what the control points are doing). So the picture looks like this:

The current point is is yellow. The next point is in blue. We also need the point before the current point and the one following the next point.
Inside your loop, create four variables, p0, p1, p2, and p3. p1 will be the current point (points[i]). p0 will be the one before and p2 and p3 will be the two following ones. There are obvious (at least it should be obvious) problems with this because you are going to walk off the array. You could limit the range of your loop (start at 1 and stop when you hit length - 3), but we will miss out on some intervals (like between the first point and the second). Of course, we recall that two of those intervals weren’t going to be drawn anyway unless we duplicate the end points.
So, our solution is to combine the two things. First, adjust the loop so it stops at length - 2 instead of length - 1 as normal (if you look at the picture, we will draw the interval between the two points while we are still on the first one). Second, add a condition that checks if i-1 will go negative if it does, just use points[0] for p0 instead of points[i-1]. Third, add another condition that does the same thing at the other end of the list for p3.
Once you have your four points, add an inner for loop that runs WISP_DOTS_PER_SEGMENT times (its a constant at the top). Inside there you want to make random circles between p1 and p2. If you look at how curvePoint works, you can see that given the four points (or more properly, the x or y component of the four points) we can sample the curve with a t variable. This is the same t we have been using. At 0 it will return the component at p1 and at 1 it will return the component at p2. So, generate a random t between 0 and 1 and plug it in. You will need to calculate the x and y points separately. Draw a circle with size 1 at the random point.
You should now have a faint line.
Add a loop around the outer loop that runs WISP_LINES times.
The line should now be darker.
Now you can add in the offsets.
Inside of the outer most for loop and after the the loop iterating over the points, add another for loop. This loop is going to move our points around. It would be best if we didn’t move the first and last points, so don’t include the first and last indices in your loop.
For each point (other than the first and last one) change the x and y components by a random amount between -dev and dev.
Why are we changing the base line instead of just drawing the points with an offset?
If every point is randomly offset we will just have chaos. By doing it this way, we get an effect akin to noise because each new layer is just a small change from the last line, but the changes will add up over time as well.
You should now have a line that is speckled and changes width across its length. The final step is to set the alpha value of the color and turn it down to something like 20. This will give it more of a wispy look, where the larger bump outs look like sand being blown off the line.
Part 6: wispyCircle
You will see there is one more function in the file: wispyCircle. Because we pulled out the drawing functionality into wispyDraw, we can draw any collection of points using the function. Generate a set of points to make a circle that matches the input parameters to the function and then call wispyDraw to draw it.
Requirements
altLineworksscratchyLineworksspeckleLineworkswiggleLineworkswispyLineworkswispyCircleworks
Finishing up
- Add and commit your changes and push those commit(s) to GitHub.
- Submit your repository to Gradescope
References
Links to the reference pages for the functions you will be using:
background
beginShape
circle
color
createButton
createCanvas
createColorPicker
createSlider
createVector
endShape
fill
line
noFill
noStroke
p5.Vector
pop
push
randomSeed
random
stroke
strokeWeight
translate