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

Save Automatically?

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 id="wrapper">
  <!-- (A) CANVAS -->
  <canvas id="canvas"></canvas>

  <!-- (B) DRAW CONTROLS -->
  <div id="controls">
    <input type="button" value="Clear" id="cClear">
    <input type="color" id="cColor">
    <input type="number" min="1" max="9" value="1" id="cSize">
    <a id="cSave" download="draw.jpg">Save</a>
  </div>
</div>


<!-- (X) VISIT CODE-BOXX -->
<div id="code-boxx">
  Visit
  <a href="https://code-boxx.com/javascript-drawing-app/" target="_blank">
    Code Boxx
  </a> for more details.
</div>
              
            
!

CSS

              
                /* (A) WHOLE SITE */
* {
  font-family: Arial, Helvetica, sans-serif;
  box-sizing: border-box;
}

/* (B) WRAPPER */
#wrapper {
  width: 600px;
  margin: 0 auto;
}

/* (C) CANVAS */
#canvas {
  width: 600px;
  height: 400px;
  border: 1px solid black;
}

/* (D) CONTROLS */
#controls {
  display: flex;
  border: 1px solid #aaa;
}
#controls * {
  height: 50px;
  padding: 10px;
  border: 0;
}
#cClear, #cColor, #cSave { cursor: pointer; }
#cColor { flex-grow: 1; }
#cClear {
  color: #fff;
  background: #c51c1c;
}
#cSave {
  display: flex;
  color: #fff;
  background: #4e74cb;
  align-items: center;
  justify-content: center;
}


/* (X) NOT IMPORTANT */
body {
  background: url(https://images.unsplash.com/photo-1606926693996-d1dfbed4e8c9?crop=entropy&cs=tinysrgb&fm=jpg&ixid=MnwzMjM4NDZ8MHwxfHJhbmRvbXx8fHx8fHx8fDE2NjAxMTcxMDE&ixlib=rb-1.2.1&q=80);
  background-repeat: no-repeat;
  background-position: center;
  background-size: cover;
}
#code-boxx {
  text-align: center;
  font-weight: 600;
  margin-top: 30px;
}
#code-boxx a {
  display: inline-block;
  border: 0;
  padding: 5px;
  text-decoration: none;
  background: #b90a0a;
  color: #fff;
}
              
            
!

JS

              
                // CREDITS
// https://dev.to/0shuvo0/lets-create-a-drawing-app-with-js-4ej3
// https://riptutorial.com/html5-canvas/example/11659/detecting-mouse-position-on-the-canvas

var draw = {
  // (A) PROPERTIES
  canvas : null, // html canvas
  ctx : null, // html canvas context
  px: null, py : null, // previous mouse coordinates
  cx: null, cy : null, // current mouse coordinates
  go : false, // flag to control paint

  // (B) INIT
  init : () => {
    // (B1) GET HTML ELEMENTS
    draw.canvas = document.getElementById("canvas");
    draw.ctx = draw.canvas.getContext("2d");

    // (B2) SET CANVAS WIDTH & HEIGHT
    draw.canvas.width = draw.canvas.offsetWidth;
    draw.canvas.height = draw.canvas.offsetHeight;

    // (B3) DEFAULT DRAW SETTINGS
    draw.ctx.fillStyle = "#ffffff";
    draw.ctx.strokeStyle = "#000000";
    draw.ctx.lineWidth = 1;
    draw.ctx.fillRect(0, 0, draw.canvas.width, draw.canvas.height);

    // (B4) ATTACH DRAW CONTROLS
    // (B4-1) CLEAR CANVAS
    document.getElementById("cClear").onclick = () => {
      draw.ctx.fillRect(0, 0, draw.canvas.width, draw.canvas.height);
    };

    // (B4-2) DRAW COLOR
    document.getElementById("cColor").onchange = (e) => {
      draw.ctx.strokeStyle = e.target.value;
    };

    // (B4-3) DRAW SIZE
    document.getElementById("cSize").onchange = (e) => {
      draw.ctx.lineWidth = e.target.value;
    };

    // (B4-4) SAVE CANVAS
    document.getElementById("cSave").onclick = (e) => {
      e.target.href = draw.canvas.toDataURL("image/jpg");
    };

    // (B5) ENGAGE/DISENGAGE PAINT
    let disengage = () => {
      draw.px = null; draw.py = null; draw.go = false;
    };
    draw.canvas.addEventListener("mousedown", (e) => draw.go = true);
    draw.canvas.addEventListener("mouseup", disengage);
    draw.canvas.addEventListener("mouseout", disengage);
    draw.canvas.addEventListener("mousemove", draw.paint);
  },

  // (C) PAINT
  paint : (e) => { if (draw.go) {
    // (C1) GET CANVAS MOUSE COORDINATES
    let cRect = draw.canvas.getBoundingClientRect();
    draw.cx = Math.round(e.clientX - cRect.left);
    draw.cy = Math.round(e.clientY - cRect.top);

    // (C2) SET INITIAL COORDINATES
    if (draw.px == null || draw.py == null) {
      draw.px = draw.cx;
      draw.py = draw.cy;
    }

    // (C3) DRAW
    draw.ctx.beginPath();
    draw.ctx.moveTo(draw.px, draw.py);
    draw.ctx.lineTo(draw.cx, draw.cy);
    draw.ctx.stroke();

    // (C4) UPDATE COORDINATES
    draw.px = draw.cx;
    draw.py = draw.cy;
  }}
};
window.onload = draw.init;
              
            
!
999px

Console