Lecture 17 - Textures

Published

November 12, 2026

Today, we are going to talk about how to make your pieces less flat through the use of texture. We are going to take a look at a smattering of works from current algorithmic artists to get a sense of how they make their works a little more compelling through texture. Then we will unpack some of the techniques so you can use them as well.

There are a lot of different techniques, but we will be looking at just a few of them today.

These are not all mutually exclusive. We frequently will use a mixture of these techniques.

Fill effects

Gradients

We will start with gradients because I gave this code to you right at the very beginning of the semester and I don’t think I’ve seen any uses of it yet (though there is a collection of things I’ve not had a close look at yet)

There are two primary approaches to making gradients in p5.js

One approach is to make it by hand by drawing a lot of lines.

This is the basic idea:

function gradientRectLine(x, y, w, h, c1, c2) {
    push()
    strokeWeight(2);
    translate(x, y);

    for (let i = 0; i < w; i++) {
        const c = lerpColor(c1, c2, i / w);
        stroke(c);
        line(i, 0, i, h);
    }
    pop();
}

The other approach is to use the built in tool. This bypasses p5.js and uses the tool built into the canvas that p5.js is using to display content on the webpage.

function gradientRectBuiltin(x, y, w, h, c1, c2) {
    const gradient = drawingContext.createLinearGradient(bounds.x, 0, bounds.x + bounds.width, 0);
    gradient.addColorStop(0, c1);
    gradient.addColorStop(1, c2);
    push();
    noStroke();
    drawingContext.fillStyle = gradient;
    rect(x,y,w,h);

    pop();
}

There are a lot of advantages to using the built-in function. It is

  • much faster
  • makes it easier to do multicolor gradients
  • can work with any arbitrary shape with less overhead

The only reason we would would prefer the line model is if we wanted to change the underlying function to not be a linear transition or if we wanted some other function of the lines (like making them something other than straight)

Clip masks

If you read my gradients example, you will have already seen the clip mask code. Using a clip mask is a way to get around the difficulties of doing the line approach to gradients for arbitrary shapes.

The basic idea is that we draw some geometry and specify it as a mask. The mask affects everything that we draw afterwards. The mask either hides everything that is not under the mask OR, if we invert it, will hide everything under the mask. You can think of it like cutting a shape out of a piece of paper. The normal mode is like laying the piece of paper with the hole on top of another image. In the inverted mode, we are using the piece we cut out.

We can use clip masks for a lot of things, but we are going to focus on using it to texture shapes. The advantage of the clip mask is that we can apply texture to arbitrary shapes without having to tailor our code to match the shape we are trying to fill. We can even use pictures.

it is, of course, worth noting that this also isn’t the only way to paste textures onto shapes. If we work in WEBGL mode then we can use the texture function. We can also write a shader to provide the texture for us.

p5.js has two ways to specify a crop.

The first uses beginCrop and endCrop

Everything that we put in between is the mask. This affects everything we draw afterwards.

function texturedCircle(x, y, size, tex) {
    push();

    beginClip();
    circle(x, y, size);
    endClip();
    image(tex, 0, 0);
    pop();

}

The other style is to call clip and pass it a function that draws the shape.

function texturedRect(x, y, w, h, tex) {
    push();

    clip(() => {
        rect(x, y, w, h);
    });

    image(tex, 0, 0);
    pop();

}

The advantage of the second form is that we could make it a separate function and reuse it multiple times.

Dots

A primary technique that we use to break up flat fields of color is to speckle it using little dots.

The dots allow us to control the density of our coverage.

function shadedRect(x,y,w,h){
   push();
    strokeWeight(2);
    const speckles = w * h;

    for (let i = 0; i < speckles; i++) {
        const xR = min(1, random() + random());
        const yR = random(y, y+h);
        point(lerp(x+w, x, xR), yR)
        
    }
    pop();
}

We can also add some transparency to allow us to get a little smoothing as well.

function shadedRect(x,y,w,h){
   push();
    strokeWeight(3);
    stroke(0, 8);
    const speckles = w * h*10;

    for (let i = 0; i < speckles; i++) {
        const xR = min(1, random() + random());
        const yR = random(y, y+h);
        point(lerp(x+w, x, xR), yR)
        
    }
    pop();
}

Another approach is to create color noise. Here we start with a base color and slightly modulate it to get variations that are within the chosen color.

There are some magic numbers in here to make the noisiness scale work reasonably

function noisyRect(x, y, w, h, noisiness, noiseSize=3) {

    const currentColor = color(drawingContext.fillStyle);
    push();
    colorMode(HSB);
    const currentHue = hue(currentColor);
    const currentSaturation = saturation(currentColor);
    const currentBrightness = brightness(currentColor);

    push();
    strokeWeight(noiseSize);
    const speckles = w * h * (10 / noiseSize); // make sure we have enough speckles to cover the field
    const offset = map(noisiness, 0, 1, 0, 20); // 20 is a magic number that makes the scale work reasonably

    for (let i = 0; i < speckles; i++) {
        const newHue = randomGaussian(currentHue + 360, offset) % 360;
        const newSaturation = randomGaussian(currentSaturation, offset);
        const newBrightness = randomGaussian(currentBrightness, offset/2);

        stroke(newHue, newSaturation, newBrightness);
        const u = random(x, x + w);
        const v = random(y, y + h);
        point(u, v);
    }
    pop();

    noFill();
    rect(x, y, w, h);
    pop();
}

How could we modify this to make a circle?

Lines

We can use lines to create texture as well. In general, the principle is the same as with the dots, we just draw lines instead. however, with lines we not have directionality, which can give us a variety of looks.

Let’s start with one of the fills that James described: a line that oscillates back and forth between the top and the bottom of a rectangle as it advances over the shape.

let x = originX;
let y = originY + h / 2;

while (x < originX + w) {
    vertex(x, y);
    x += constrain(randomGaussian(spread, deviation), .1, spread * 2);

    if (y === originY) {
        y = y + h;
    } else {
        y = originY;
    }
}

The randomGaussian in there allows us to make the oscillations a little more interesting.

The next thing we could do is to fill the rect with random lines instead of dots. This will give us a texture like felt or a woven cloth.

There are several ways to approach this. One approach would be to just pick two points inside of the rect and draw a line between them. My version gives me a little more control. I select a single point inside of the rect and then create a random angle and radius to generate the second point. This allows me to have tighter control on the length of the line.

for (let i = 0; i < lines; i++) {
    const angle = random(TWO_PI);
    const radius = randomGaussian(threadLength, fringe);

    p0 = createVector(random(x + threadLength, x + w - threadLength), 
        random(y + threadLength, y + h - threadLength));
    p1 = createVector(p0.x + radius * cos(angle), 
        p0.y + radius * sin(angle));

    line(p0.x, p0.y, p1.x, p1.y);
}

The threadLength here is controlling the general length of the threads and fringe is a rough measure of how much we allow the threads to overhang the edge.

We could also “comb” the hairs a bit by limiting the angle.