Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="canvasWrap">
  <canvas width="1600" height="900" id="etch-a-sketch"></canvas>
  <div class="buttons">
    <button class="shake">Shake!</button>
  </div>
</div>
              
            
!

CSS

              
                body {
  min-height: 100vh;
  display: grid;
  align-items: center;
  justify-items: center;
  background: white;
  background: url(https://s3.amazonaws.com/media.locally.net/original/HABA_ALT_2017-08-02-13-22-45.jpg);
  background-size: cover;
}

canvas {
  border: 30px solid #e80000;
  border-radius: 10px;
  /* Set the width and height to half the actual size so it doesn't look pixelated */
  width: 800px;
  height: 450px;
  background: white;
}

canvas.shake {
  animation: shake 0.5s linear 1;
}

@keyframes shake {
  10%,
  90% {
    transform: translate3d(-1px, 0, 0);
  }

  20%,
  80% {
    transform: translate3d(2px, 0, 0);
  }

  30%,
  50%,
  70% {
    transform: translate3d(-4px, 0, 0);
  }

  40%,
  60% {
    transform: translate3d(4px, 0, 0);
  }
}

button {
  color: white;
  background: #d60087;
  padding: 0.5rem;
  border: 0;
  border: 2px solid transparent;
  border-radius: 10px;
  text-decoration: none;
  font-weight: 600;
  font-size: 2rem;
}

              
            
!

JS

              
                // grab canvas and it's context, plus the shake button
const canvas = document.querySelector("#etch-a-sketch");
const ctx = canvas.getContext("2d");
const shakebutton = document.querySelector(".shake");
const MOVE_AMOUNT = 10;

// setup the canvas for drawing
const { width, height } = canvas;
// randomize the starting point of the line
let x = Math.floor(Math.random() * width);
let y = Math.floor(Math.random() * height);

// set a hue amount to rainbow-ize the stroke!
let hue = 0;
ctx.strokeStyle = `hsl(${hue}, 100%, 50%)`;
// set the style to be round instead of squar, and make the line size bigger
ctx.lineJoin = "round";
ctx.lineCap = "round";
ctx.lineWidth = MOVE_AMOUNT;

// this will actually start the line drawing, so the starting point shows on the page
ctx.beginPath();
ctx.moveTo(x, y);
ctx.lineTo(x, y);
ctx.stroke();

// write a draw function
function draw({ key }) {
  // increment hue by 1 for each time
  hue += 3;
  ctx.strokeStyle = `hsl(${hue}, 100%, 50%)`;
  ctx.beginPath();
  // go to where x & y used to be
  ctx.moveTo(x, y);
  // move x & y depending on key direction pressed
  switch (key) {
    case "ArrowUp":
      y -= MOVE_AMOUNT;
      break;
    case "ArrowDown":
      y += MOVE_AMOUNT;
      break;
    case "ArrowRight":
      x += MOVE_AMOUNT;
      break;
    case "ArrowLeft":
      x -= MOVE_AMOUNT;
      break;
    default:
      break;
  }
  ctx.lineTo(x, y);
  ctx.stroke();
}

// arrow key handlers
function handleKey(e) {
  if (e.key.includes("Arrow")) {
    // prevent the default, which is moving the screen
    e.preventDefault();
    draw({ key: e.key });
  }
}

// handle the shake button press, to clear the canvas
function clearCanvas() {
  canvas.classList.add("shake");
  ctx.clearRect(0, 0, width, height);
  // listen to animation end then remove class, so we can run function multiple times
  canvas.addEventListener(
    "animationend",
    function () {
      canvas.classList.remove("shake");
    },
    { once: true }
  );
  // remove the event listener with once, otherwise it will keep adding a new one each time we click the button
}

// listen for arrow key inputs
window.addEventListener("keydown", handleKey);

// listen for button click to clear canvas
shakebutton.addEventListener("click", clearCanvas);

              
            
!
999px

Console