5 minutes 10 seconds average read (1032 words)

So in my learning of JavaScript, with Wes Bos, I've now created an Etch-a-Sketch. Use the arrow keys and have a go. Below I'll explain how it works, this is another way of learning. By explaining something means that you  truly understand how it works.


<div class="canvaswrap">
  <canvas width="1600" height="1000" id="etch-a-sketch"></canvas>

This is simple really, a <canvas> to draw upon and a <button> to reset the canvas.


Setting up the Variables

const canvas = document.querySelector(`#etch-a-sketch`);
const shake = document.querySelector(`.shake`);
const ctx = canvas.getContext(`2d`);
const MOVE_AMOUNT = 5;
const { width, height } = canvas;
let x = Math.floor(Math.random() * width);
let y = Math.floor(Math.random() * height);
let hue = 0;


This creates a variable called canvas from the id="etch-a-sketch" which is set on the <canvas> element


This creates a variable called from the class="shake" which is set on the <button> element


This sets the context of the canvas to 2d


This sets a variable for how far the dot will move and how big the dot will be

width, height

These are destructured variables taken directly from the canvas, you can find out move about destructuring assignments at

x, y

Here I am setting x and y to be a random point on the canvas based upon its width & height.


This is setting a starting value for the colour of the dot, this will be then used in a hsl colour setting later on.

Setting up the canvas for drawing on

ctx.lineJoin = `round`;
ctx.lineCap = `round`;
ctx.lineWidth = MOVE_AMOUNT;
ctx.strokeStyle = `hsl(${hue}, 100%, 50%)`;
ctx.moveTo(x, y);
ctx.lineTo(x, y);

Here we are setting up the default settings to start drawing on the <canvas> .

lineJoin & lineCap

These 2 settings tell the line how to join to other lines and how the end of the line will look.

There are 3 options for lineJoin:

There are 3 options for lineCap:


This sets how wide the line will be that's drawn on the <canvas> .


This is used to set how the stroke will look.

There are 3 options for strokeStyle:

In my example, I'm setting the colour to be an hsl value with the hue variable, saturation 100% and lightness 50% - hsl(${hue}, 100%, 50%) .


This simply starts a new path on the <canvas> .


This sets the start point of a new path/stroke on the <canvas> .


This sets where to draw the path/stroke to on the <canvas> .


This will draw the stroke between moveTo() and lineTo() on the <canvas> .

Listening to the Arrow Key events

const handleKey = event => {
  if (event.key.includes('Arrow')) {
    draw({ key: event.key });

This is an arrow functions called handleKey which has a parameter called event . This allows use the say, for every event that happens do these things…

In this case it means:

Drawing stuff on the <canvas>

So at this point after creating all these functions and variable I actually need to draw something on then <canvas> .

const draw = ({key}) =&gt; {
  hue += MOVE_AMOUNT;
  ctx.strokeStyle = `hsl(${hue}, 100%, 50%)`;
  if (x > width) {x = width;}
  if (x < 0) {x = 0;}
  if (y > height) {y = height;}
  if (y < 0) {y = 0;}
  ctx.moveTo(x, y);
  switch (key) {
    case 'ArrowUp':
      y -= MOVE_AMOUNT;
      case 'ArrowDown':
      y += MOVE_AMOUNT;
      case 'ArrowLeft':
      x -= MOVE_AMOUNT;
      case 'ArrowRight':
      x += MOVE_AMOUNT;
    ctx.lineTo(x, y);

So this is relatively simple I'm setting the variables created earlier.

Clear the Canvas when Finished

const clearCanvas = () => {
  ctx.clearRect(0, 0, width, height);
  canvas.addEventListener('animationend', function(){
  {once: true}

This function says when the <button class="shake">Shake</button>, is clicked then clear the <canvas>.