Pen Settings

HTML

CSS

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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

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.

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

              
                <h2>Faces with SVG and Canvas</h2>
<p>Although SVG is the basis of most web dataviz, and the large majority of D3 examples, the 'canvas' drawing context is becoming more popular and offers performance advantages over SVG. Rather than use HTML tags to draw, elements are drawn to a canvas using a JavaScript API:</p>
<ul><li>Check out some documenation at <a href="https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Drawing_shapes">Mozilla's site.</a></li></ul>

<div class="container">
  <div>
    <h3>Drawing with SVG</h3>
 <svg class="faces svg-faces">
  <g class="face" transform="translate(100, 100)">
    <ellipse class="head" cx="0" cy="0" rx="80" ry="100" />
        <circle class="eye" cx="-40" cy="-50" r="15" />
        <circle class="eye" cx="40" cy="-50" r="15" />
        <ellipse class="mouth" cx="0" cy="50" rx="30" ry="10" />
        <polygon class="nose" points="0 -60
                                  -15 30
                                  0 35 15 30" />
    
  </g>
</svg>    
    
    <p>This face is drawn using SVG tags such as 'circle', 'ellipse' and 'polygon'. Check out the HTML tab to see how it's done.</p>
    
  </div>


  <div>
    <h3>Using the canvas context</h3>
<canvas width="200" height="200" id="canvas-face" class="faces canvas-faces"></canvas>  
    
    <p>Check out the JS tab to see how this face is drawn onto a pixel canvas using the '2d' drawing context. </p>
    
  </div>

 
</div>

              
            
!

CSS

              
                body {
  font-family: "Helvetica", sans-serif;
}

.container {
  display: flex;
}

.container div {
  flex: 1;
  text-align: center;
  margin: 20px;
}

h2, h3 {
  text-align: center;
}

p {
  text-align: initial;
}

.faces {
  display: inline-block;
}

.svg-faces {
  width: 200px;
  height: 200px;  
}

.canvas-faces {
}

.head {
  fill: goldenrod
}

.eye {
  fill: blue;
}

.nose {
  fill: green;
}

.mouth {
  fill: red;
}
              
            
!

JS

              
                // We select the 'canvas' tag by Id and store
var myCanvas = document.getElementById("canvas-face");

// We get a 2D drawing contect from the canvas element
var ctx = myCanvas.getContext('2d');
// Set the fill color for the shape
ctx.fillStyle = 'goldenrod'
// Begin this drawing round
ctx.beginPath()
// Draw our face ellipse (x, y, cx, cy)
ctx.ellipse(100, 100, 80, 100, 0, 0, 2 * Math.PI);
ctx.fill()
//ctx.endPath()
// setting blue eyes
ctx.fillStyle = "blue"
ctx.beginPath()
ctx.arc(60, 50, 15, 0, 2 * Math.PI, false);
ctx.arc(140, 50, 15, 0, 2 * Math.PI, false);
ctx.fill()

ctx.fillStyle = "green"
ctx.beginPath()
ctx.moveTo(100, 40)
ctx.lineTo(85,100)
ctx.lineTo(100, 120)
ctx.lineTo(115, 100)
//ctx.closePath()
ctx.fill()
//ctx.fillStyle = "green"


ctx.fillStyle = "red"
ctx.fillRect(70, 140, 60, 20)

              
            
!
999px

Console