cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

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.

            
              * { margin: 0; padding: 0; }
body {  overflow: hidden; }
canvas { background: #fffff3; }
            
          
!
            
              $(function(){
  
  // initialize drawing status
  var shape = {
    status: false,
    color: 'rgba(34,121,192,.85)',
    shadowColor: 'rgba(34,121,192,1)',
    startX: 0,
    startY: 0,
    coords: []
  };
  
  // get the canvas, set to full width
  var canvas = $('canvas')[0];
  canvas.width = $(document).width();
  canvas.height = $(document).height();
    
  // get the context
  var ctx = canvas.getContext('2d');
  
  // set fill, stroke, and shadow styles
  ctx.fillStyle = shape.color;
  ctx.strokeStyle = shape.color;
  ctx.lineWidth = 1;
  ctx.shadowOffsetX = 0;
  ctx.shadowOffsetY = 0; 
  ctx.shadowBlur = 1;
  ctx.shadowColor = shape.shadowColor;
  
  // set the click event to start drawing
  $(canvas).click(function(e){
       
    // 1. get the x,y just clicked
    var currentX = e.clientX;
    var currentY = e.clientY;
    
    // 2. if shape not drawn yet, set init values and draw start
    if ( !shape.status ) {
      shape.status = true;
      shape.startX = currentX;
      shape.startY = currentY;
      ctx.beginPath();
      ctx.moveTo(currentX, currentY);
      ctx.fillRect(currentX, currentY, 2, 2);
    }
    
    // 3. else, we can draw a line of our shape...
    else {
      
      // 3.a get the previous x,y clicked
      var lastX = shape.coords[shape.coords.length - 1][0];
      var lastY = shape.coords[shape.coords.length - 1][1];
      
      // 3.b draw the line
      ctx.lineTo(currentX, currentY);
      ctx.stroke();
      
      // 3.c if this is within 10px of the initial coordinate and there are less than three points in the shape so far, close it and fill the shape
      if ( Math.abs( shape.startX - currentX ) < 11 && Math.abs( shape.startY - currentY ) < 11 && shape.coords.length > 2 ) {
        
        ctx.lineTo( shape.startX, shape.startY);
        ctx.closePath();
        ctx.stroke();
        ctx.fill();
        
        // reset shape and wash your hands of it
        shape.status = false; 
        shape.coords = [];
        return;
      }
    }
    
    // 4. add this coordinate to our array
    shape.coords.push([currentX,currentY]);        
  });
  
})
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console