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.

            
              div.clockBackground
canvas#myCircleClock(width = "300",height = "300")
canvas#myClockOutPut(width = "300",height = "300")

    
            
          
!
            
              #myCircleClock{
  position:absolute;
  top:0;
  left:0;
  border:1px dashed #ccc;
}
#myClockOutPut{
  position:absolute;
  left:0;
  top:0;
  border:1px solid #ccc;
}
.clockBackground{
  position:absolute;
  top:0;
  left:0;
  width:300px;
  height:300px;
  border:1px solid #99FF99;
  background-color:#99FF99;
}
            
          
!
            
              var canvas = document.getElementById('myCircleClock');
var clockCanvas = document.getElementById('myClockOutPut');
//获取日期
var now = new Date();
/*园弧数据*/
    /*时针*/
var x = 150,y = 150;
var hourHand = {
    lineWidth: 20,
    x: x,
    y: y,
    circle_r: 100,
    color: "#aaa",
    begin: -Math.PI/2,
    end: -Math.PI/2,
  },/*分针*/
  minuteHand = {
    lineWidth: 20,
    x: x,
    y: y,
    circle_r: 120,
    color: "#bbb",
    begin: -Math.PI/2,
    end: -Math.PI/2,
  },/*秒针*/
  secondHand = {
    lineWidth: 20,
    x: x,
    y: y,
    circle_r: 140,
    color: "#ccc",
    begin: -Math.PI/2,
    end: -Math.PI/2,
  };

if (canvas.getContext){
  /*获取绘画上下文*/
  var ctx = canvas.getContext('2d');
  var cctx = clockCanvas.getContext('2d');
  /*设置渐变色彩*/
  var seclingrad = ctx.createLinearGradient(0,0,150,150),
      minlingrad = ctx.createLinearGradient(0,0,150,150),
      hourlingrad = ctx.createLinearGradient(0,0,150,150);
  seclingrad.addColorStop(0, '#006666');
  seclingrad.addColorStop(0.5,'#0099CC');
  seclingrad.addColorStop(0.9, '#009900');
  
  minlingrad.addColorStop(0, '#009966');
  minlingrad.addColorStop(0.5,'#00CCCC');
  minlingrad.addColorStop(0.9, '#00CC00');
  
  hourlingrad.addColorStop(0, '#00CC66');
  hourlingrad.addColorStop(0.5,'#00FFCC');
  hourlingrad.addColorStop(0.9, '#00FF00');
  
  secondHand.color = seclingrad||secondHand.color;
  minuteHand.color = minlingrad||minuteHand.color;
  hourHand.color = hourlingrad||hourHand.color;
  
  //计时文字样式
  cctx.font = "14px Times";
  cctx.textAlign = "center";
  cctx.strokeText(now.getHours()+":"+now.getMinutes()+":"+now.getSeconds(),x,y);
  //初始化时钟初始时间
  secondHand.end += now.getSeconds() * Math.PI/30;
  minuteHand.end += now.getMinutes() * Math.PI/30;
  hourHand.end += (now.getHours()%12) * Math.PI/6;
  drawCircle(secondHand,ctx);
  drawCircle(minuteHand,ctx);
  drawCircle(hourHand,ctx);
  //开始计时.
  countTime(cctx,now);
  HandRotate(secondHand,minuteHand,hourHand,ctx);
}else{
  alert("unsupported!");
}
/*文字计时函数*/
function countTime(ctx,date){
  var h = date.getHours(),m = date.getMinutes(),s = date.getSeconds();
  var timmer = setInterval(function(){
    s++;
    if(s>=60){
      m++;
      s=0;
    }
    if(m>=60){
      h++;
      m=0;
    }
    if(h>=24){
      h=0;
    }
    ctx.clearRect(0,0,x*2,y*2);
    ctx.strokeText(h+":"+m+":"+s,x,y);
  },1000);
}
/*圆环绘制函数*/
function drawCircle(config,ctx){
	ctx.strokeStyle = config.color;
	ctx.lineWidth = config.lineWidth;
	ctx.beginPath();
	ctx.arc(config.x,config.y,config.circle_r,config.begin,config.end);
	ctx.stroke();
}

/*时钟动画函数*/
function HandRotate(secconfig,minconfig,houreconfig,ctx){
  var s = Math.PI/1800,m=s/60,h=m/12;
  var rotate = requestAnimationFrame(step);
  function step(){
    secconfig.end+=s;
    if(secconfig.end >= 3*Math.PI/2){
      secconfig.end = -Math.PI/2;
    }
    minconfig.end+=m;
    if(minconfig.end >= 3*Math.PI/2){
      secconfig.end = -Math.PI/2;
    }
    houreconfig.end+=h;
    if(houreconfig.end >= 3*Math.PI/2){
      houreconfig.end = -Math.PI/2;
    }
    ctx.clearRect(0,0,x*2,y*2);
    drawCircle(secconfig,ctx);
    drawCircle(minconfig,ctx);
    drawCircle(houreconfig,ctx);
    requestAnimationFrame(step);
  }
}

            
          
!
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