cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

Looking for quick-add? Try the external resource search, it's quicker and gives you access to the most recent version of thousands of libraries. ☝️

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Looking for quick-add? Try the external resource search, it's quicker and gives you access to the most recent version of thousands of libraries. ☝️

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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.

            
              <canvas id="matisse"></canvas>

<div id="menu"> 
  
  <div class="tray">
    <button class="clear">Clear</button>
    <button class="save">Save</button>
    
    <ul id="colors">
      <li><a title="blue" class="blue"> </a></li>
      <li><a title="green" class="green"> </a></li>
      <li><a title="red" class="red"> </a></li>
      <li><a title="pink" class="pink"> </a></li>
      <li><a title="orange" class="orange"> </a></li>
    </ul>
  </div>
</div>
            
          
!
            
              * { margin: 0; padding: 0; }
body {  overflow: hidden; }

#menu { position: absolute; top: 10px; right: 10px; text-align: right; }
#colors { list-style-type: none; }
#colors li { display: inline-block; margin: 5px 1px; }
#colors li a { display: inline-block; width: 15px; height: 15px; }
#colors li a:hover { cursor: pointer; }

.blue { background-color: rgba(19,88,163,1); }
.green { background-color: rgba(48,156,80,1); }
.red { background-color: rgba(215,50,44,1); }
.pink { background-color: rgba(207,65,139,1); }
.orange { background-color: rgba(247,148,44,1); }

            
          
!
            
              $(function(){
  
  // initialize drawing status
  var shape = {
    status: false,
    color: 'rgba(19,88,163,1)',
    shadowColor: 'rgba(0,0,0,.25)',
    startX: 0,
    startY: 0,
    coords: []
  };
  
  // get the menu and buttons
  var $menu = $('#menu');
  var $buttonClear = $('#menu .clear');
  var $buttonSave = $('#menu .save');
  var $colorButtons = $('#colors li a');
  
  // 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');
  
  // fill the initial canvas
  ctx.fillStyle = "#fffff3";
  ctx.fillRect(0,0,canvas.width,canvas.height);
  
  // 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, hide menu, and draw start
    if ( !shape.status ) {
      
      $menu.hide();

      // 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;
      
      // draw the shape
      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 and show menu again
      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();
        
        $menu.show();
        
        
        // 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]);        
  });
  
  // clear canvas when clear button clicked
  $buttonClear.click(function(){
    ctx.fillStyle = "#fffff3";
    ctx.fillRect(0,0,canvas.width,canvas.height);
  });
  
  // open png in new window when save button clicked
  $buttonSave.click(function(){
    window.open(canvas.toDataURL('image/png'));
  });
  
  // change color of cut-out based on color button chosen
  $colorButtons.click(function(){
    shape.color = $(this).css('background-color');
  });
  
})
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console