function drawHour(hour,minute){
ctx.save();
ctx.beginPath();
var rad=2*Math.PI/12*hour;
var mrad=2*Math.PI/12/60 *minute;
ctx.rotate(rad+mrad);
ctx.lineWidth=6;
ctx.lineCap='round';
ctx.moveTo(0,10);
ctx.lineTo(0,-r/2);
ctx.stroke();
ctx.restore();
}
ctx.save();
ctx.beginPath();
var rad=2*Math.PI/12*hour;
var mrad=2*Math.PI/12/60 *minute;
ctx.rotate(rad+mrad);
ctx.lineWidth=6;
ctx.lineCap='round';
ctx.moveTo(0,10);
ctx.lineTo(0,-r/2);
ctx.stroke();
ctx.restore();
}
2016-12-24
function drawMinute(minute){
ctx.save();
ctx.beginPath();
var rad=2*Math.PI/60*minute;
ctx.rotate(rad);
ctx.lineWidth=3;
ctx.lineCap='round';
ctx.moveTo(0,10);
ctx.lineTo(0,-r+30);
ctx.stroke();
ctx.restore();
}
ctx.save();
ctx.beginPath();
var rad=2*Math.PI/60*minute;
ctx.rotate(rad);
ctx.lineWidth=3;
ctx.lineCap='round';
ctx.moveTo(0,10);
ctx.lineTo(0,-r+30);
ctx.stroke();
ctx.restore();
}
2016-12-24
function drawSecond(second){
ctx.save();
ctx.beginPath();
ctx.fillStyle='#c14543';
var rad = 2 * Math.PI / 60 * second;
ctx.rotate(rad);
ctx.moveTo(-2,20);
ctx.lineTo(2,20);
ctx.lineTo(1, -r + 18);
ctx.lineTo(-1, -r + 18);
ctx.fill();
ctx.restore();
}
ctx.save();
ctx.beginPath();
ctx.fillStyle='#c14543';
var rad = 2 * Math.PI / 60 * second;
ctx.rotate(rad);
ctx.moveTo(-2,20);
ctx.lineTo(2,20);
ctx.lineTo(1, -r + 18);
ctx.lineTo(-1, -r + 18);
ctx.fill();
ctx.restore();
}
2016-12-24
function drawDot(){
ctx.beginPath();
ctx.fillStyle='#fff';
ctx.arc(0,0,3,0,2*Math.PI,false);
ctx.fill();
}
ctx.beginPath();
ctx.fillStyle='#fff';
ctx.arc(0,0,3,0,2*Math.PI,false);
ctx.fill();
}
2016-12-24
function draw(){
ctx.clearRect(0,0,width,height);
var now=new Date();
var hour =now.getHours();
var minute=now.getMinutes();
var second=now.getSeconds();
drawBackground();
drawHour(hour,minute);
drawMinute(minute);
drawSecond(second);
drawDot();
ctx.restore();
}
setInterval(draw,1000);
ctx.clearRect(0,0,width,height);
var now=new Date();
var hour =now.getHours();
var minute=now.getMinutes();
var second=now.getSeconds();
drawBackground();
drawHour(hour,minute);
drawMinute(minute);
drawSecond(second);
drawDot();
ctx.restore();
}
setInterval(draw,1000);
2016-12-24
最赞回答 / Qzhor
理论上讲我也觉得是r-10,线宽10那两边不都得减10么,哈哈,但是。。实际上是r-5才能实现效果,具体我也不懂T T~~前面一堆长篇大论的不知道在云云什么东东。。
2016-12-22
已采纳回答 / udnernahs
var canvas = document.getElementById("clock") canvas.width =200 canvas.height = 200 var ctx = canvas.getContext("2d") var width=ctx.canvas.width; var height=ctx.canvas.height; var r=width/2; function drawDot(){ ...
2016-12-20
已采纳回答 / 慕勒5926719
你的<canvas>的id是clack,var dom=document.getElementById('clock');这个获得的又是clock,不一致
2016-12-20
最新回答 / Future晨
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Canvas Clock</title></head><body><div style="margin:100px 100px;"> <canvas id="clock" height="200" width="200"></ca...
2016-12-20