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="result">
    <header>
         <h1>Tick tock.</h1>
    </header>
    <canvas id="analog" width="310" height="310">Sorry, your browser does not support the most awesome HTML5 element, canvas. Have you considered <a href="https://google.com/chrome">updating</a>?</canvas>
    <div id="digital"></div>
</div>

              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css?family=Open+Sans:400, 700, 600');

html, body {
    font-family:"Open Sans", san-serif;
    color:#232323;
    background:#369;
}
#result {
    width:100%;
    max-width:960px;
    padding:30px;
    background:#eee;
    margin:10px auto;
}
div, h1, header{
    width:100%;
    margin:10px auto;
}
h1, h2 {
    font-weight:700;
    display:block;
    text-align:center;
    color:#369;
    border-bottom:5px solid #aaa;
}
h1{
    font-size:2.2em;
}
#analog{
  display:block;
  margin:10px auto;
  width:310px;
}
#digital{
  margin:20px auto;
  width:100%;
  text-align:center;
}
.digital{
  font-size:2em;
  padding:5px 10px;
  border: 5px solid #666;
  border-radius:5px;
  background:#000;
  color:#0f0;
}

              
            
!

JS

              
                analogTime()

function drawClock(){
    var d = new Date();
    var hours = d.getHours() % 12;
    var min = d.getMinutes();
    var seconds = d.getSeconds();
    var minutes = min + hours*60;
    var angle = minutes/2;
    var minangle = min*6;
    var secangle = seconds*6;
        
    //set canvas variables
    var canvas = document.getElementById('analog'),
        ctx = canvas.getContext('2d');
    
        canvas.width = canvas.width;
    
    //draw circle for clock outline    
    ctx.beginPath();
    ctx.arc(155, 155, 150, 0, 2 * Math.PI);
    ctx.lineWidth = 5;
    ctx.strokeStyle = '#000';
    ctx.stroke();
    ctx.translate(0, 0);
    ctx.rotate(0);
    ctx.save();
    ctx.save();
    ctx.save();
    
    //draw and rotate hours arm
    ctx.translate(155, 155);
    ctx.rotate(angle*Math.PI/180);
    ctx.beginPath();
    ctx.moveTo(0, 0)
    ctx.lineTo(0, -100);
    ctx.lineTo(0, 20);
    ctx.lineWidth = 5;
    ctx.stroke();
    
    //draw and rotate seconds arm
    ctx.restore();
    ctx.translate(155,155);
    ctx.rotate(secangle*Math.PI/180);
    ctx.beginPath();
    ctx.moveTo(0, 0);
    ctx.lineTo(0, -130);
    ctx.lineTo(0, 20);
    ctx.lineWidth = 1;
    ctx.strokeStyle = "#F00";
    ctx.stroke();
    
    //draw and rotate minutes arm
    ctx.restore();
    ctx.translate(155,155);
    ctx.rotate(minangle*Math.PI/180);
    ctx.beginPath();
    ctx.moveTo(0, 0)
    ctx.lineTo(0, -130);
    ctx.lineTo(0, 20);
    ctx.lineWidth = 3;
    ctx.stroke();
    
    //add numbers to clock   
    ctx.restore();
    ctx.translate(155,155)
    ctx.font = "30px Helvetica";
    ctx.fillText("12",-20,-120);
    ctx.fillText("11", -75, -100);
    ctx.fillText("10", -120, -50);
    ctx.fillText("9", -140, 12);
    ctx.fillText("8",-120, 70);
    ctx.fillText("7", -75, 115);
    ctx.fillText("6", -8, 140);
    ctx.fillText("5", 65, 115);
    ctx.fillText("4",105, 70);
    ctx.fillText("3", 125, 12);
    ctx.fillText("2", 105, -50);
    ctx.fillText("1", 50, -100);
    
    //display digital clock above analog clock
    //check if hours = 0 to return 12
    if(hours == 0){
        hours = 12;
    }
    if(min < 10){//if min less than 10 add a zero in front of min.
        min = '0' + min;
    }
    if(seconds < 10){//if min less than 10 add a zero in front of min.
        seconds = '0' + seconds;
    }
    document.getElementById('digital').innerHTML = '<span class="digital">' + hours + ':' + min + ':' + seconds + '</span>';
}

function analogTime(){
    document.getElementById('result').style.display = "block";
    
    drawClock();
    
    setInterval(function(){
    
    var canvas = document.getElementById('analog');
        canvas.width = canvas.width;
        
        drawClock();
        
    }, 100);
}
              
            
!
999px

Console