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 id="container">
  <p>
    <label for="upload-image">1. upload an image</label>
    <input type="file" id="upload-image" name="upload-image" />
  </p>

  <p id="sketch-container" class="hide">
    <label>2. Draw some edges or erase for a "sketched" look.</label>

    <canvas id="sketch-canvas"></canvas>
  </p>
</div>

<div id="menu"> 

 <div class="tray">
    <p><a href="index.html">DE-SKETCHING CASSATT<br /> a <code>var t;</code> demo</a></p>
    <p><small>upload an image<br />make it an art<br /><a href="index.html">learn about mary cassatt</a></small></p>
    <div id="sketch-tools">
      <button class="clear">Clear</button>
      <button class="save">Save</button> <br />
      <button class="pencil">Pencil</button> 
      <button class="eraser">Eraser</button>
    </div>
  </div>
</div>
              
            
!

CSS

              
                body { font-family: Helvetica; padding: 1em; }

#container p { margin: 0 0 2em; }
label { font-weight: bold; text-transform: uppercase; font-size: 1.5em; display: block; margin: .5em 0; width: 500px;}

#menu { position: absolute; top: 10px; right: 10px; text-align: right;   }
#menu a { text-decoration: none; color: black; font-weight: bold; font-size: 1.2em; }
#menu p { margin-bottom: 10px; }
#menu small { font-family: monospace; }

#sketch-container { position: relative; }

button { background-color: #ccc; border-color: #ccc; margin: 5px;}
button.in-use { background-color: lime; border-color: lime; }

.hide { display: none; }
              
            
!

JS

              
                $(function(){

  var $uploadInput = $('#upload-image');
  var $sketchContainer = $('#sketch-container');
  var $sketchTools = $('#sketch-tools').addClass('hide');
  var canvas = $('#sketch-canvas')[0];
  var ctx = canvas.getContext('2d');

  var $buttonClear = $('button.clear');
  var $buttonSave = $('button.save');
  
  var $buttonPencil = $('button.pencil').addClass('in-use');
  var $buttonEraser = $('button.eraser');

  var sketchRadius = 2;
  var sketchColor = '#897171';
  var tool = 'pencil';

  /** EVENTS **/

  // when uploading an image, embed it onto canvas and show it
  $uploadInput.change(function(){

    // embed image onto canvas
    var uploadedImage = $uploadInput[0].files[0];
    var reader = new FileReader();
    reader.onload = function(e){
      var img = new Image();
      img.onload =function() {
        canvas.width = img.width;
        canvas.height = img.height;
        ctx.drawImage(img,0,0);
      }
      img.src = e.target.result;
    };
    reader.readAsDataURL(uploadedImage);

    // show sketch container
    $sketchContainer.removeClass('hide');
    $sketchTools.removeClass('hide');
  });

  // clear canvas when clear button clicked
  $buttonClear.click(function(){
    ctx.fillStyle = "#ffffff";
    ctx.fillRect(0,0,canvas.width,canvas.height);

    $sketchContainer.addClass('hide');
    $sketchTools.addClass('hide');
  });

  // open png in new window when save button clicked
  $buttonSave.click(function(){
    window.open(canvas.toDataURL('image/png'));
  });
  
  // use pencil when pencil button clicked
  $buttonPencil.click(function(){
    tool = 'pencil';
    $buttonPencil.addClass('in-use');
    $buttonEraser.removeClass('in-use');
  });
  
  // use eraser when eraser button clicked
  $buttonEraser.click(function(){
    tool = 'eraser';
    $buttonEraser.addClass('in-use');
    $buttonPencil.removeClass('in-use');
  });
  
  

  /** DE-SKETCHING **/

  $(canvas).mousedown(function(e){
    $(canvas).mousemove(function(e){

      // get current pixel position
      var offset = $(this).offset(); 
      var x = e.pageX - offset.left;
      var y = e.pageY - offset.top;

      // draw 2px wide circle with grey around the point of mouse
      ctx.beginPath();
      
      // if pencil, draw edges. if eraser, do the magical erase thing
      if ( tool === 'pencil' ) {
        ctx.arc(x, y, sketchRadius, 0, 2 * Math.PI, false);
        ctx.fillStyle = sketchColor;
        ctx.fill();
      } 
      else if ( tool === 'eraser' ) {
        // magical erase thing will go here!
        console.log('erase!');
      }
      
    });
  });

  // when mouse goes up, stop de-sketching
  $(document).mouseup(function(e){
    $(canvas).unbind('mousemove');
  });

});
              
            
!
999px

Console