# Yet Another Way to Draw Lines

tutorial

Sure, why not, right?

One of the things I was trying to accomplish with the shaky drawing from my last post was to give the idea of hand-drawn lines. By adding in a bit of random variance, lines and shapes can seem like they are drawn by a human rather than perfectly rendered by a computer.

But at some point I was actually sketching something by hand and realized another big difference between computer drawing and human sketching. Very often when a person is sketching, rather than boldly drawing what they hope is a straight line from point A to B, they will instead make a number of very light lines that roughly go between the two points. You’ve seen it, you’ve done it, you know what I mean. So let’s replicate that.

The idea is that when we want to draw a line from `x0, y0` to `x1, y1` for example, we’ll actually draw multiple lines – not exactly to those to points, but from somewhere around the first point, to somewhere around the second one.

In this case, I’m going to dive right into a function. This was my first attempt:

```function sketchLine(context, x0, y0, x1, y1, count, rand) {
context.beginPath();
for (let i = 0; i < count; i++) {
let x = x0 + Math.random() * rand - rand / 2;
let y = y0 + Math.random() * rand - rand / 2;
context.moveTo(x, y);

x = x1 + Math.random() * rand - rand / 2;
y = y1 + Math.random() * rand - rand / 2;
context.lineTo(x, y);
}
context.stroke();
}
```

We can then call this like so:

```context.lineWidth = 0.2;
sketchLine(context, 100, 100, 700, 700, 5, 20);
```

Note that I set the line width down to 0.2 to make very light lines. We’ll let the multiple light lines build up to create the idea of a sketch, the same way you would do it by hand. So this will draw 5 lines with a variance of up to 10 pixels in any direction from the starting and ending points. The result:

Not that great, to be honest. The lines vary a bit, but they are still too uniform. I can bump up the randomness to, say, 50, but that looks even less like what a real person would sketch.

The thing with sketching, particularly in longer lines, is that you don’t always sketch the entire length of the line on each stroke. You might start around the first point and stroke maybe half way to the other point. And then you might stroke the middle third of the distance between them, and then something closer to the ending point. You’d do a bunch of strokes with varying start and end distances. Well, we can do something like that too.

```function sketchLine(context, x0, y0, x1, y1, count, rand) {
const dx = x1 - x0;
const dy = y1 - y0;

context.beginPath();
for (let i = 0; i < count; i++) {
let t0 = Math.random() * 0.5;
let t1 = Math.random() * 0.5 + 0.5;

let x = x0 + dx * t0 + Math.random() * rand - rand / 2;
let y = y0 + dy * t0 + Math.random() * rand - rand / 2;
context.moveTo(x, y);

x = x0 + dx * t1 + Math.random() * rand - rand / 2;
y = y0 + dy * t1 + Math.random() * rand - rand / 2;
context.lineTo(x, y);
}
context.stroke();
}
```

This should look somewhat familiar if you’ve followed along with the other two posts in this series. First I get length of the line on the x and y axes – `dx` and `dy`. In the loop, I create a `t` value between 0 and 1. In this case, I create two of these – `t0` and `t1` – one for the start of the line and one for the end. `t0` will range from 0.0 to 0.5 and `t1` will go from 0.5 to 1.0. This should give us a random assortment of lines – some from very near the start point to very near the end point, some more in the first part of the line, some in the middle and some more near the end. Calling this again with:

```context.lineWidth = 0.2;
sketchLine(context, 100, 100, 700, 700, 5, 20);
```

We get this image:

That’s something I could almost start to believe was done by hand. Let’s do a rectangle function!

```function sketchRect(context, x, y, w, h, count, rand) {
sketchLine(context, x, y, x + w, y, count, rand);
sketchLine(context, x + w, y, x + w, y + h, count, rand);
sketchLine(context, x + w, y + h, x, y + h, count, rand);
sketchLine(context, x, y + h, x, y, count, rand);
}
```

Obviously, I just copied and pasted the `shakyRect` from the previous post and changed the method names and parameters. So let’s call this with:

```context.lineWidth = 0.2;
sketchRect(context, 100, 100, 600, 600, 5, 20);
```

Pretty cool, but this brings up an issue we couldn’t immediately see when drawing a single line. All too often, the lines don’t reach all the way to those starting or ending points. Too many are falling right in the middle. We can shift our random parameters a bit to make up for that.

```function sketchLine(context, x0, y0, x1, y1, count, rand) {
const dx = x1 - x0;
const dy = y1 - y0;

context.beginPath();
for (let i = 0; i < count; i++) {
let t0 = Math.random() * 0.4 - 0.1;
let t1 = Math.random() * 0.4 + 0.7;

let x = x0 + dx * t0 + Math.random() * rand - rand / 2;
let y = y0 + dy * t0 + Math.random() * rand - rand / 2;
context.moveTo(x, y);

x = x0 + dx * t1 + Math.random() * rand - rand / 2;
y = y0 + dy * t1 + Math.random() * rand - rand / 2;
context.lineTo(x, y);
}
context.stroke();
}
```

Now, `t0` will range from -0.1 to +0.3, and `t1` will go from 0.7 to 1.1. This makes it more likely that some lines will make it closer to one of the points, and may even overshoot it a bit, which is just what you’d do by hand. The results of this change:

Not bad, in my opinion. You’ll still have some rectangles with open corners, but not as often as before. Here’s one with a `count` of 20 and a `rand` of 40:

Finally, just for the heck of it, I combined the shaky and sketchy techniques with this function:

```function sketchLine(context, x0, y0, x1, y1, count, rand) {
const dx = x1 - x0;
const dy = y1 - y0;

context.beginPath();
for (let i = 0; i < count; i++) {
let t0 = Math.random() * 0.4 - 0.1;
let t1 = Math.random() * 0.4 + 0.7;

let xA = x0 + dx * t0 + Math.random() * rand - rand / 2;
let yA = y0 + dy * t0 + Math.random() * rand - rand / 2;
let xB = x0 + dx * t1 + Math.random() * rand - rand / 2;
let yB = y0 + dy * t1 + Math.random() * rand - rand / 2;
shakyLine(context, xA, yA, xB, yB, 20, 2);
}
context.stroke();
}
```

Here, I calculated the start and end points of each sketched line and rather than using `moveTo` and `lineTo` I called the `shakyLine` function from my last post. I hard coded it with a `res` of 20 and a `rand` of 2, which makes it just a little bit less than a perfectly straight line and possibly a little bit more like something hand drawn. Called with:

```context.lineWidth = 0.2;
sketchRect(context, 100, 100, 600, 600, 10, 20);
```

it gives us:

Even that might be too much shake, but you can easily make it more subtle.

Here’s one final example. If this doesn’t look hand sketched, I don’t know what does.

Well, that’s that. I don’t have a ready-made library to share with you on this one, but hopefully you’ll find this useful or at least inspiring.

The rest of the How to Draw a Line series:

# More Ways to Draw Lines

tutorial

In the last post, I discussed an alternate way of rendering lines, that could give a glitchy look, painterly look, or many other looks based on what parameters you used. In this post, I’ll discuss another way to draw lines.

The theme to these posts is that using `moveTo` / `drawTo` is fine but gives you clinical, antiseptic, perfect straight lines (or curves). And sometimes it’s nice to shake things up a bit. In fact, shaking things up is exactly what we’re going to do here today.

I’m going to start with something very similar to one of the earlier examples in the last post, but instead of drawing individual pixels along the length of the line, I’m going to draw short line segments.

```const dx = x1 - x0;
const dy = y1 - y0;
const dist = Math.sqrt(dx * dx + dy * dy);
const res = 10;

context.beginPath();
context.moveTo(x0, y0);

for (let i = res; i < dist; i += res) {
let t = i / dist;
let x = x0 + dx * t;
let y = y0 + dy * t;
context.lineTo(x, y);
}
context.lineTo(x1, y1);
context.stroke()
```

Like before, I’m starting with two points defined by `x0, y0` and `x1, y1`. And I’ve defined a `res` variable that dictates the length of those intermediate segments. Note that I `moveTo` the first point and set `i` equal to `res` to initialize the loop, and then `lineTo` the last point. This ensures that the start and end of the line remain constant (which will be important later). Otherwise, things should be familiar here if you read the previous post. Here’s what this gives us:

It’s exactly what you’d get if you just drew a line from the first to last point. So big deal. But now that we have these intermediate points, we can shake things up as promised.

I’ll just randomly shift each `x, y` point a bit before drawing to it. (Just showing the for loop here:

```for (let i = res; i < dist; i += res) {
let t = i / dist;
let x = x0 + dx * t;
let y = y0 + dy * t;
x += Math.random() * 4 - 2;
y += Math.random() * 4 - 2;
context.lineTo(x, y);
}
```

This puts each `x, y` location somewhere from -2 to +2 on each axis. And gives us this:

I then put this all into a function to make it easily reusable:

```function shakyLine(context, x0, y0, x1, y1, res, rand) {
const dx = x1 - x0;
const dy = y1 - y0;
const dist = Math.sqrt(dx * dx + dy * dy);

context.beginPath();
context.moveTo(x0, y0);

for (let i = res; i < dist; i += res) {
let t = i / dist;
let x = x0 + dx * t;
let y = y0 + dy * t;
x += Math.random() * rand - rand / 2;
y += Math.random() * rand - rand / 2;
context.lineTo(x, y);
}
context.lineTo(x1, y1);
context.stroke();
}
```

Now I can just call it like so:

```shakyLine(context, 100, 100, 700, 700, 10, 10);
```

And it creates a line like this:

Note that the random factor is 10 here, so it’s a lot more shaky than the earlier example. Note that the `res` and `rand` parameters both contribute in different ways to the shakiness of the line. So if I move `res` down to 5, but keep `rand` at 10, we get something more shaky, but in a tight way.

But here, I’ve bumped both `res` and `rand` up significantly:

```shakyLine(context, 100, 100, 700, 700, 50, 40);
```

This gives us a line that varies a lot more, but because each segment is longer, it’s more of a chunky random shake.

So you can play these two parameters off each other to get various effects.

The next thing to do is create a `shakyRect` function. It looks like this:

```function shakyRect(context, x, y, w, h, res, rand) {
shakyLine(context, x, y, x + w, y, res, rand);
shakyLine(context, x + w, y, x + w, y + h, res, rand);
shakyLine(context, x + w, y + h, x, y + h, res, rand);
shakyLine(context, x, y + h, x, y, res, rand);
}
```

As mentioned before, the fact that each line starts and ends on a non-shaky point means that the rectangle will be continuous. You can call it like this:

```shakyRect(context, 100, 100, 600, 600, 10, 10);
```

And get a rectangle like this:

To show how `res` and `rand` relate, I made this for loop that draws a bunch of squares on the canvas. `res` increases from left to right and `rand` increases from top to bottom. It gives you a good idea of the different effects you can create.

```for (let y = 20; y < 730; y += 70) {
for (let x = 20; x < 730; x += 70) {
shakyRect(context, x, y, 50, 50, x/730 * 10, y/730 * 10);
}
}
```

Now, this rectangle function isn’t the best because it doesn’t support fills, but you can probably work up a better solution. You might also want to create functions for drawing other shaky shapes.

Or, maybe you just want a drop in library you can use. I created a whole JavaScript `shaky` library about 7 years ago. I can’t guarantee how well it has stood the test of time. There are undoubtedly some best practices in there that could be updated. But the core code should be good enough to work out things for yourself. It supports not only lines and rectangles, but circles, arcs, quadratic curves, bezier curves, and arcs. And as of this writing, it has 101 stars, which is kind of neat!

https://github.com/bit101/shaky

All the posts in the How to Draw a Line series:

# How to Draw a Line

tutorial

… in code of course.

First, I’m going to assume that you are working in some kind of system that has a drawing API. But wait, even the simplest of drawing APIs have a function to draw a line already. All right then, we’ll start with that. Assuming you are using something like HTML, JS and Canvas, you’re going to do something like this:

```context.moveTo(x0, y0);
context.lineTo(x1, y1);
context.stroke();
```

You start with two points, defined by two x, y coordinates. You move to the first one, you line to the second one, you stroke that path. And you wind up with:

Easy. And Boring. Sure you can change the width and the color and transparency, but it’s still just a boring line. In order to do a bit more with it, let’s abandon the built-in line functionality and make our own.

The idea is to start at the first point and draw a series of small squares between it and the second point. We’ll use the Pythagorean theorem to get the distance between the two points. That will let us know how many steps we have to take. Then just linearly interpolate between the x and y positions and draw a square there.

```const dx = x1 - x0;
const dy = y1 - y0;
const dist = Math.sqrt(dx * dx + dy * dy);

for (let i = 0; i < dist; i++) {
let t = i / dist;
let x = x0 + dx * t;
let y = y0 + dy * t;
context.fillRect(x - 0.5, y - 0.5, 1, 1);
}
```

And the result of that:

Not half bad really. Of course, if you want to have a really fast and more accurate line drawing routine, you’d probably want to go with Bresenham’s algorithm or maybe Wu’s algorithm. Honestly, I’m really just referencing them so people don’t comment to correct me and tell me I should be using them. But we’re just getting started here. This still isn’t any more interesting than the built-in line function. It’s just too straight, too even, to regular. Let’s mix it up a bit.

We can start by changing the increment from `i++` to, say, `i += 5`, and get:

Now we have some space between each “pixel” that makes up the line. We can then increase the size of these “pixels”:

```context.fillRect(x - 1.5, y - 1.5, 3, 3);
```

Which has the general effect of pixel art:

In the next example, I changed the increment to 8 and used the following code to draw a circle instead of a square:

```context.beginPath();
context.arc(x, y, 4, 0, Math.PI * 2);
context.fill();
```

And got:

You can keep playing with this, making lines out of any shapes with any amount of spacing. But let’s throw in some randomness.

Instead of drawing a pixel at every single point along the line, let’s draw them at random positions along the line. We can do that just by making `t` a random value from 0 to 1. Altering the first version, we get:

```for (let i = 0; i < dist; i++){
let t = Math.random();
let x = x0 + dx * t;
let y = y0 + dy * t;
context.fillRect(x - 0.5, y - 0.5, 1, 1);
}
```

And an image that will look something like this:

Now we’re getting something a bit more interesting.

Let’s play with size and transparency next. I’ll make a `size` variable that will let us easily change the size of each pixel. And then set the transparency down real low. This will let the pixels overlap and build up more gradually in different areas. I’ll also use `dist*2` as the pixel count, which gives us a lot more pixels to play with.

```const size = 4;
context.fillStyle = "rgba(0, 0, 0, 0.05)";

for (let i = 0; i < dist*2; i++){
let t = Math.random();
let x = x0 + dx * t;
let y = y0 + dy * t;
context.fillRect(x - size / 2, y - size / 2, size, size);
}
```

And that gives us:

Finally, lets go all out, add even more pixels, vary the size, vary the transparency, even vary the position of each individual pixel.

```for (let i = 0; i < dist*4; i++){
let t = Math.random();
let x = x0 + dx * t;
let y = y0 + dy * t;
x += Math.random() * 4 - 2;
y += Math.random() * 4 - 2;
let size = Math.random() * 8;
context.fillStyle = "rgba(0,0,0, " + Math.random()*0.2 + ")";
context.fillRect(x - size / 2, y - size / 2, size, size);
}
```

Now we have something that looks kind of like ink on wet paper.

But we don’t have to limit it to straight lines. Here’s the built-in code for drawing a quadratic curve – defined by three points:

```const x0 = 100;
const y0 = 700;
const x1 = 400;
const y1 = -100;
const x2 = 700;
const y2 = 700;

context.beginPath();
context.moveTo(x0, y0);
context.stroke()
```

This gives you a bland curve:

But we can apply the same concept here. We’ll need a function that gives us a point somewhere along the curve. Fortunately, such a function is well known. We can again choose a random `t` value and start drawing random pixels. Here’s the code – note I chose an arbitrary value of 3000 for number of pixels. Use more or less to get a denser or lighter line.

```for (let i = 0; i < 3000; i++){
let t = Math.random();
let p = quadraticPoint(x0, y0, x1, y1, x2, y2, t);
x = p.x + Math.random() * 4 - 2;
y = p.y + Math.random() * 4 - 2;
let size = Math.random() * 8;
context.fillStyle = "rgba(0,0,0, " + Math.random()*0.2 + ")";
context.fillRect(x - size / 2, y - size / 2, size, size);
}

function quadraticPoint(x0, y0, x1, y1, x2, y2, t) {
const oneMinusT = 1.0 - t;
const m0 = oneMinusT * oneMinusT;
const m1 = 2.0 * oneMinusT * t;
const m2 = t * t;
return {
x: m0 * x0 + m1 * x1 + m2 * x2,
y: m0 * y0 + m1 * y1 + m2 * y2,
}
}
```

And the curve:

This looks even better drawn with circles:

This looks interesting all by itself, but I suggest you create some parameterized functions for drawing lines, curves and shapes like this and see if it doesn’t change the overall look of your creative pieces.

The whole series of How to Draw a Line: