How to Create the Drawing Interaction on DEV's Offline Page

Ali Spittel - Jul 3 '19 - - Dev Community

Since more and more people have been noticing DEV's offline page, I thought I would do a quick tutorial on how to replicate the code for it!

Canvas is for creating graphics with JavaScript -- we can build fun, interactive tools using it. When I normally build interactive artwork like this, I use P5.js, which makes the Canvas API easier to work with. We wanted to make the offline page as self-contained and lightweight as possible, though, so the offline page doesn't use any external code.

The first thing that we need to do is to create a <canvas> tag in our HTML. You will also need to add in CSS to make the canvas take up space -- so give it a height and a width. I made a Codepen template with some starter CSS for us to work with:

Now, on to the JavaScript!

The first thing that we need to do, is to select the canvas element that exists in the HTML already so we can interact with it. We will also have to create a variable for the context of the canvas. We will use a 2D context because our drawing will only be two dimensional:

const canvas = document.querySelector('canvas')
const context = canvas.getContext('2d')
Enter fullscreen mode Exit fullscreen mode

We will also want to set the size of the canvas in the JavaScript so that our images aren't distorted:

canvas.setAttribute('width', window.innerWidth)
canvas.setAttribute('height', window.innerHeight)
Enter fullscreen mode Exit fullscreen mode

Now we need to add some event listeners. For the drawing app, we want to add these:

  • 'mousedown' - when a user presses their mouse we want to start drawing

  • 'touchstart' - when a user is on their phone, we again want to start drawing

  • 'mousemove' - when a user moves their mouse, we want to draw a line from the mouse's previous place to the current place

  • 'touchmove' - the same as above, but when the user is on their phone

  • 'mouseup' - when a user stops pressing down, we want to stop drawing

  • 'mouseleave'- when a user's mouse leaves the area, we also want to stop drawing

  • 'touchend' - when a user is on their phone and stops pressing down, we again want to stop drawing

So, we need three event handling functions that will respond to the above events. Let's start with the startPaint function that will run each time the person starts drawing.

We can add an event listener the same way that we can with any other element in JavaScript:


function startPaint (e) {

}

canvas.addEventListener('mousedown', startPaint)
canvas.addEventListener('touchstart', startPaint)
Enter fullscreen mode Exit fullscreen mode

We want the startPaint function to do a couple things:

  • First, we need a variable that keeps track of whether or not we are currently drawing so that the mousemove handler only works when we are currently painting. We need to set that to true whenever we start drawing.

  • Then, we need to get the coordinates of where the person is clicking. We need to keep track of those coordinates so that we can move from the current point to the next one when the person then moves their mouse.


let x, y, isPainting;

function getCoordinates(event) {
  // check to see if mobile or desktop
  if (["mousedown", "mousemove"].includes(event.type)) {
    // click events
    return [event.pageX - canvas.offsetLeft, event.pageY - canvas.offsetTop];
  } else {
    // touch coordinates
    return [
      event.touches[0].pageX - canvas.offsetLeft,
      event.touches[0].pageY - canvas.offsetTop
    ];
  }
}

function startPaint(e) {
  // change the old coordinates to the new ones*
  isPainting = true;
  let coordinates = getCoordinates(e);
  x = coordinates[0];
  y = coordinates[1];
}
Enter fullscreen mode Exit fullscreen mode

Then, we need to handle when the person moves their mouse to draw. Here we have to:

  • Check to see if we are painting (i.e. the mouse is down)

  • We need to get the new mouse coordinates

  • We need to draw a line from the old coordinates to the new ones

  • We need to set the old coordinates to the new ones so that our next "draw" starts at the current point

function drawLine(firstX, firstY, secondX, secondY) {
  // set the attributes of the line
  context.strokeStyle = "black";
  context.lineJoin = "round";
  context.lineWidth = 5;

  context.beginPath();
  context.moveTo(secondX, secondY);
  context.lineTo(firstX, firstY);
  context.closePath();

  // actually draw the path*
  context.stroke();
}

function paint(e) {
  if (isPainting) {
    let [newX, newY] = getCoordinates(e);
    drawLine(x, y, newX, newY);

    // Set x and y to our new coordinates
    x = newX;
    y = newY;
  }
}

canvas.addEventListener("mousemove", paint);
canvas.addEventListener("touchmove", paint);
Enter fullscreen mode Exit fullscreen mode

Now, we just have to stop drawing when we release our mouse!


function exit() {
  isPainting = false;
}

canvas.addEventListener("mouseup", exit);
canvas.addEventListener("mouseleave", exit);
canvas.addEventListener("touchend", exit);
Enter fullscreen mode Exit fullscreen mode

Now, here's a finished version with changing colors and the ability to resize the page!

I love building art with code, especially that people can interact with. If you want to learn more, I have a few more posts on this topic if you're interested!

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player