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="container">
  <div id="toolbar" class="controls-wrapper clearfix">
    <div class="controls">
      Line Width: <span id="lineWidthLabel" class="line-width-value">16</span>
      <div id="lineWidthUp" class="line-width-control">+</div>
      <div id="lineWidthDown" class="line-width-control">-</div>
      <button type="button" id="colorsBtn" class="colors-btn">Pick a Color</button>
    </div>

    <div id="colorsWrapper" class="colors-wrapper clearfix">
      <span id="closeBtn" class="close-btn">X</span>
    </div>
  </div>
  <!-- /#toolbar-->

  <canvas id="myCanvas" width="600" height="300">
    <p>This is fallback content for users of assistive technologies or of browsers that don't have full support for the Canvas API.</p>
  </canvas>

  <button id="clearBtn" class="clear-btn" type="button">Clear Canvas</button>
</div>

<p class="p">Demo by Maria Antonietta Perna. <a href="http://www.sitepoint.com" target="_blank">See article</a>.</p>
              
            
!

CSS

              
                html {
  box-sizing: border-box;
  font-family: sans-serif;
  font-size: 100%;
  line-height: 1.5;
  color: #333;
}

*,
*:before,
*:after {
  box-sizing: inherit;
}


/* Clearfix */

.clearfix:before,
.clearfix:after {
  content: ' ';
  display: table;
}

.clearfix:after {
  clear: both;
}

h1,
h2,
h3 {
  font-weight: normal;
}

body {
  font-size: 100%;
  line-height: 1.5;
  margin: 0;
  padding: 20px 0 0;
}

canvas,
img,
figure {
  display: block;
}

.container {
  max-width: 600px;
  margin: 0 auto;
}

.controls-wrapper {
  background-color: #222;
  color: white;
  padding: 0.5em;
  font-size: 1.5em;
  position: relative;
}

.line-width-value {
  display: inline-block;
  margin-right: 0.5em;
  margin-left: 0.5em;
  width: 50px;
  text-align: center;
}

.line-width-control {
  background-color: #444;
  border-radius: 5px;
  cursor: pointer;
  display: inline-block;
  height: 30px;
  margin-left: 0.3em;
  margin-right: 0.3em;
  font-size: 1em;
  font-weight: 700;
  line-height: 30px;
  text-align: center;
  width: 30px;
  transition: background-color 0.3s linear;
  user-selct: none;
}

.line-width-control:hover,
.line-width-control:focus {
  background-color: #222;
}

.colors-btn,
.clear-btn {
  margin-left: 1.5em;
  padding: 0.5em;
  background-color: #444;
  color: white;
  font-size: 0.7em;
  border: 1px solid white;
  border-radius: 5px;
  text-transform: uppercase;
  text-align: center;
  cursor: pointer;
  transition: background-color 0.3s linear;
}

.clear-btn {
  border-color: #333;
  padding: 0.6em;
  font-size: 1.2em;
}

.colors-btn:hover,
.colors-btn:focus,
.clear-btn:hover,
.clear-btn:focus {
  background-color: #222;
}

.colors-wrapper {
  background-color: #222;
  position: absolute;
  top: 0;
  right: 0;
  max-width: 250px;
  padding: 0.5em 1em;
  box-shadow: -2px 2px 1px rgba(0, 0, 0, 0.8);
  transform: translateY(-100px);
  visibility: hidden;
  opacity: 0;
  transition: transform 0.3s linear, visibility 0.3s linear, opacity 0.3s linear;
}

.colors-wrapper.is-visible {
  visibility: visible;
  opacity: 1;
  transform: translateY(0px);
}

.close-btn {
  display: inline-block;
  float: right;
  height: 40px;
  width: 40px;
  text-align: center;
  font-size: 20px;
  line-height: 40px;
  border-radius: 50%;
  background-color: #444;
  box-shadow: 2px 2px 2px rgba(0, 0, 0, 0.8);
  cursor: pointer;
  transition: box-shadow 0.3s linear, background-color 0.3s linear;
}

.close-btn:hover,
.close-btn:focus {
  box-shadow: 1px 1px 1px rgba(0, 0, 0, 0.3);
  background-color: #111;
}

.color {
  width: 50px;
  height: 50px;
  display: inline-block;
  margin: 0.4em 0.4em 0.3em 0;
  border-radius: 5px;
  cursor: pointer;
  outline: 2px solid transparent;
  transition: outline-color 0.3s linear, border-radius 0.3s linear;
}

.color:hover,
.color:focus,
.color.selected {
  outline-color: white;
  border-radius: 0;
}

canvas {
  margin: 2em auto;
  max-width: 100%;
  outline: 2px solid #444;
}

.p {
  text-align: center;
  font-size: 14px;
  padding-top: 50px;
}
              
            
!

JS

              
                (function($) {

  var $myCanvas = $('#myCanvas');
  var $offset = $myCanvas.offset();
  //Btn to clear all canvas surface
  var $clearBtn = $('#clearBtn');

  //Shows the current line width
  var $lineWidthLabel = $('#lineWidthLabel');
  //Increase line width control
  var $lineWidthUp = $('#lineWidthUp');
  //Decrease line width control
  var $lineWidthDown = $('#lineWidthDown');
  //Numeric value of line width
  var $lineWidthVal = parseInt($lineWidthLabel.text());
  //Minimum value for the line width
  var lineWidthMin = 1;
  //Max value for the line width
  var lineWidthMax = 50;
  //Current line color
  var lineColor = '#333';

  //btn to show the colors
  var $colorsBtn = $('#colorsBtn');
  var $closeBtn = $('#closeBtn'); //hides the colors 

  //Range of colors 
  var colors = ['red', 'orange', 'gray', 'yellow', 'aqua', 'steelblue', 'pink', 'blue', 'brown', 'black'];

  var isMouseDown = false;
  var pos = {
    x: 0,
    y: 0
  };
  var lastPos = {
    x: 0,
    y: 0
  };

  //jCanvas drawLine() method
  function paintLine(x1, y1, x2, y2, paintWidth, paintColor) {
    $myCanvas.drawLine({
      strokeStyle: paintColor,
      strokeWidth: paintWidth,
      rounded: true,
      strokeJoin: 'round',
      strokeCap: 'round',
      x1: x1,
      y1: y1,
      x2: x2,
      y2: y2
    });
  }

  function displayColors() {
    //Container for the colors
    var $colorsWrapper = $('#colorsWrapper');
    //Color square HTML fragment
    var $color = '<div class="color">';

    $.each(colors, function(i, val) {
      $colorsWrapper.append($color);
      //select each single color square
      var $colorSquare = $('.color:eq(' + i + ')');
      //apply bg color from array to each square
      $colorSquare.css('background-color', val);
      //attach a click event listener to each square
      $colorSquare.on('click', function() {
        setLineColor($colorSquare); //Click on a square to change line color
      });
    });
  }

  function setLineColor(square) {
    //New color from the background color 
    //of the selected color square
    var newLineColor = $(square).css('background-color');
    //Container for the colors
    var $colorsWrapper = $('#colorsWrapper');
    lineColor = newLineColor;
    //Remove hightlihgt style from all color squares 
    $colorsWrapper.children('div').removeClass('selected');
    //Highlight the square with the selected color
    square.addClass('selected');
  }

  function displayColorsBox() {
    //Container for the colors
    var $colorsWrapper = $('#colorsWrapper');
    //Slide down the box with the colors display
    $colorsWrapper.addClass('is-visible');
  }

  function hideColorsBox() {
    //Container for the colors
    var $colorsWrapper = $('#colorsWrapper');
    //Remove the box with the colors display
    $colorsWrapper.removeClass('is-visible');
  }

  //Colors squares are built and lined up inside container
  displayColors();

  //Clicking Pick a Color btn 
  //slides the box with color squares
  $colorsBtn.on('click', displayColorsBox);

  //Clicking X button in colors box hides colors box
  $closeBtn.on('click', hideColorsBox);

  //Clicking + sign increases the line width size
  //up to the upper limit
  $lineWidthUp.on('click', function() {
    if ($lineWidthVal >= lineWidthMax) {
      $lineWidthVal = lineWidthMax;
    } else {
      $lineWidthVal++;
    }
    $lineWidthLabel.text($lineWidthVal);
  });

  //Clicking - sign decreases the line width size
  //up to the lower limit
  $lineWidthDown.on('click', function() {
    if ($lineWidthVal <= lineWidthMin) {
      $lineWidthVal = lineWidthMin;
    } else {
      $lineWidthVal--;
    }
    $lineWidthLabel.text($lineWidthVal);
  });

  /*  
   ** PAINTING FUNCTIONALITY **
   */

  //On mousedown the painting functionality kicks in
  $myCanvas.on('mousedown', function(e) {
    isMouseDown = true;
  });

  //On mouseup the painting functionality stops
  $myCanvas.on('mouseup', function() {
    isMouseDown = false;
    return;
  });

  //On mousemove store the mouse coordinates and 
  //use jCanvas drawLine() method
  $myCanvas.on('mousemove', function(e) {

    lastPos.x = pos.x;
    lastPos.y = pos.y;
    pos.x = e.pageX - $offset.left;
    pos.y = e.pageY - $offset.top;

    if (isMouseDown) {

      paintLine(lastPos.x, lastPos.y, pos.x, pos.y, $lineWidthVal, lineColor);

    }
  });

  //Clears all canvas surface
  $clearBtn.on('click', function() {
    $myCanvas.clearCanvas();
  });

})(jQuery);
              
            
!
999px

Console