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.

Quick-add: + add another resource

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.

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.

            
              <section>
<header>
  <h1>Sketchbook with the Canvas Tag</h1>
</header>

<article>
  
  <canvas width="575" height="300" id="canvas"></canvas>
  
  <div id="marker-container">
  
    <h2>Change Marker Width</h2>
  
    
    <input id="marker" type="range" max="10" min="1" value="1">
    
    <div id="shape"></div>
    
    
  </div>
  
  <button id="clear">Clear Sketchbook</button>
  
</article>
</section>
            
          
!
            
              @import "compass/css3";

* {
  box-sizing:border-box;
  font-family:sans-serif;
}

h1 {
  color:#cc3300;
  margin:0 0 25px;
  font-size:2rem;
}

section {
  max-width:650px;
  margin:25px auto;
  background:#CCC;
  padding:25px;
  border-radius:10px;
  text-align:center;
  canvas {
    background:#FFF;
    border-radius:10px;
    box-shadow:0 0 15px #999;
    cursor:crosshair;
  }

  #marker-container {
    max-width:300px;
    background:#333;
    margin:25px auto;
    padding:15px;
    border-radius:10px;
    h2 {
      color:#FFF;
      margin:0 0 15px;
      font-weight:normal;
      font-size:1.2rem;
    }
  }
}



#clear {
  font-size:1rem;
  background:#cc3300;
  color:#FFF;
  border:0;
  padding:10px;
  border-radius:5px;
  cursor:pointer;
  &:hover {
    background:#FFF;
    color:#cc3300;
  }
}

#shape {
  width: 0;
  height: 0;
  border-left: 125px solid transparent;
  border-right: 0px solid transparent;
  border-bottom: 15px solid #999;
  margin:5px auto;
}
            
          
!
            
              var marker = "rgba(0,0,0,1)";
var markerWidth = 1;

var lastEvent;
var mouseDown = false;

var context = $('#canvas')[0].getContext('2d');
var $canvas = $('#canvas');


$canvas.mousedown(function(e){
    lastEvent = e;
    mouseDown = true;
    console.log(lastEvent);
}).mousemove(function(e){
  if(mouseDown){
    context.beginPath();
    
    context.moveTo(lastEvent.offsetX,lastEvent.offsetY);
    context.lineTo(e.offsetX,e.offsetY);
    context.lineWidth=markerWidth;
    context.strokeStyle = marker;
    context.lineCap='round';
    context.stroke();
    
    lastEvent = e;
  }
  
}).mouseup(function(){
  mouseDown = false; 
});


/****CLEAR****/

var clear = function(){
  context.clearRect(0,0,575,300);
};

$('#clear').on("click",clear);

/****CHANGE MARGER WIDTH****/

var changeWidth =  function(){  
  markerWidth = $("#marker").val();
  console.log(markerWidth);
};


$("#marker").change(changeWidth);
            
          
!
999px
Loading ..................

Console