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. 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

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

              
                <body onload = "init()">
    <canvas width="300" height="300" id="clock"></canvas>
    <p id = "time"></p>
  </body>
              
            
!

CSS

              
                
              
            
!

JS

              
                var ctx, h, m, s;    //グローバル変数

function gobj(id){return document.getElementById(id);}

function init(){    //bodyを読んだらでここを実行する
  ctx = gobj("clock").getContext("2d");
  setInterval(tick, 1000);    //1000ミリ秒=1秒に一度tickを実行する
}

function tick(){    //時刻を取得する
  var now = new Date();
  h = now.getHours() % 12;
  m = now.getMinutes();
  s = now.getSeconds();
  gobj("time").textContent = now.toTimeString();    //timeに時刻をテキストで表示する
  paint();
}

function paint(){    //目盛りと針を描画する
  ctx.clearRect(0, 0, 300, 300);
  ctx.save();
  ctx.translate(150, 150);
  ctx.strokeStyle = "black";
  var pitch = Math.PI * 2 / 60;

  for (var i = 0 ; i < 60 ; i++){    //目盛りを描画する
    ctx.beginPath();
    ctx.lineWidth = (i % 5) == 0 ? 3 : 1;    //5目盛りごとに太くする
    ctx.moveTo(0, -120);
    ctx.lineTo(0, -140);
    ctx.stroke();
    ctx.rotate(pitch);
  }
  ctx.restore();

  var radH = (Math.PI * 2) / 12 * h + (Math.PI * 2) / 12 * (m / 60);    //針の角度
  var radM = (Math.PI * 2) / 60 * m;
  var radS = (Math.PI * 2) / 60 * s;
  drawHand(radH, 80, 6, "blue");    //drawHandに引数を渡す
  drawHand(radM, 120, 4, "blue");
  drawHand(radS, 140, 2, "red");
}

function drawHand(rotation, length, width, color){    //針を描画する
  ctx.save()
  ctx.lineWidth = width;
  ctx.strokeStyle = color;
  ctx.translate(150, 150);
  ctx.rotate(rotation);
  ctx.beginPath();
  ctx.moveTo(0, 0);
  ctx.lineTo(0, -length);
  ctx.stroke();
  ctx.restore();
}
              
            
!
999px

Console