var s = Math.PI / 1800; var m = s / 60; var h = m / 12; second = second + s; minute = minute + m; hour = hour + h; if(second >= Math.PI * (3/2)){ second = Math.PI * (-1/2); } if(minute >= Math.PI * (3/2)){ minute = Math.PI * (-1/2); } if(second >= Math.PI * (3/2)){ second = Math.PI * (-1/2); }</span>
更新draw.js的完整代码如下:
function draw() { var canvas = document.getElementById('clock'); var currentTime = new Date(); var hour = (currentTime.getHours()%12) * Math.PI/6; var minute = currentTime.getMinutes() * Math.PI/30; var second = currentTime.getSeconds() * Math.PI/30; hour = hour - Math.PI * (1/2); minute = minute - Math.PI * (1/2); second = second - Math.PI * (1/2); if (canvas.getContext){ var ctx = canvas.getContext('2d'); var s = Math.PI / 1800; var m = s / 60; var h = m / 12; var rotate = requestAnimationFrame(step); function step(){ second = second + s; minute = minute + m; hour = hour + h; if(second >= Math.PI * (3/2)){ second = Math.PI * (-1/2); } if(minute >= Math.PI * (3/2)){ minute = Math.PI * (-1/2); } if(second >= Math.PI * (3/2)){ second = Math.PI * (-1/2); } ctx.clearRect(0, 0, 400 , 400); ctx.beginPath(); ctx.arc(200,200,50,Math.PI*(-1/2),hour,false); ctx.moveTo(200,100); ctx.arc(200,200,100,Math.PI*(-1/2),minute,false); ctx.moveTo(200,50); ctx.arc(200,200,150,Math.PI*(-1/2),second,false); ctx.stroke(); requestAnimationFrame(step); } } }
到这步为止,我们已经得到了一个会动的圆形时钟,接下来,我们为其添加上指针。
第四步*:添加时针、分针、秒针。
JS为我们提供了Math.cos() 、Math.sin() 计算指针到达的位置,它们所接收参数的单位也都是弧度。
function draw() { var canvas = document.getElementById('clock'); var currentTime = new Date(); var hour = (currentTime.getHours()%12) * Math.PI/6; var minute = currentTime.getMinutes() * Math.PI/30; var second = currentTime.getSeconds() * Math.PI/30; hour = hour - Math.PI * (1/2); minute = minute - Math.PI * (1/2); second = second - Math.PI * (1/2); if (canvas.getContext){ var ctx = canvas.getContext('2d'); var s = Math.PI / 1800; var m = s / 60; var h = m / 12; var rotate = requestAnimationFrame(step); function step(){ second = second + s; minute = minute + m; hour = hour + h; if(second >= Math.PI * (3/2)){ second = Math.PI * (-1/2); } if(minute >= Math.PI * (3/2)){ minute = Math.PI * (-1/2); } if(second >= Math.PI * (3/2)){ second = Math.PI * (-1/2); } //秒针的终点 xs = 150 * Math.cos(second) + 200; ys = 150 * Math.sin(second) + 200; //分针的终点 xm = 100 * Math.cos(minute) + 200; ym = 100 * Math.sin(minute) + 200; //时针的终点 xh = 50 * Math.cos(hour) + 200; yh = 50 * Math.sin(hour) + 200; ctx.clearRect(0, 0, 400 , 400); ctx.beginPath(); //绘制指针 ctx.moveTo(200,200); ctx.lineTo(xs,ys); ctx.moveTo(200,200); ctx.lineTo(xm,ym); ctx.moveTo(200,200); ctx.lineTo(xh,yh); //绘制圆形轮廓 ctx.moveTo(200,150); ctx.arc(200,200,50,Math.PI*(-1/2),hour,false); ctx.moveTo(200,100); ctx.arc(200,200,100,Math.PI*(-1/2),minute,false); ctx.moveTo(200,50); ctx.arc(200,200,150,Math.PI*(-1/2),second,false); ctx.stroke(); requestAnimationFrame(step); } } }
嗯嗯,虽然指针是画出来了,都是我略感后悔,因为感觉太丑了。从审美学角度而言,带指针时钟就应该是个全圆轮廓的时钟。不知道读者有没有同感。我决定在第五步中把第四步回退了。
第五步:个性化、美化、自定义你的时钟。
html5的canvas画布,提供了多种样式属性,如线条颜色、线条粗细等等。
为了提高代码的可重用性,我们将画圆的代码抽象成一个函数。
function draw() { var canvas = document.getElementById('clock'); var currentTime = new Date(); var hour = (currentTime.getHours()%12) * Math.PI/6; var minute = currentTime.getMinutes() * Math.PI/30; var second = currentTime.getSeconds() * Math.PI/30; hour = hour - Math.PI * (1/2); minute = minute - Math.PI * (1/2); second = second - Math.PI * (1/2); if (canvas.getContext){ var ctx = canvas.getContext('2d'); var s = Math.PI / 1800; var m = s / 60; var h = m / 12; var rotate = requestAnimationFrame(step); function step(){ second = second + s; minute = minute + m; hour = hour + h; if(second >= Math.PI * (3/2)){ second = Math.PI * (-1/2); } if(minute >= Math.PI * (3/2)){ minute = Math.PI * (-1/2); } if(second >= Math.PI * (3/2)){ second = Math.PI * (-1/2); } ctx.clearRect(0, 0, 400 , 400); ctx.beginPath(); //绘制圆形轮廓 drawCircle(ctx, 100, hour, '#99ff00'); drawCircle(ctx, 120, minute, '#99ff66'); drawCircle(ctx, 140, second, '#66cc66'); requestAnimationFrame(step); } } } function drawCircle(ctx, radius ,endAngle, color){ ctx.beginPath(); ctx.moveTo(200,200-radius); ctx.strokeStyle = color; ctx.lineWidth = 20; ctx.arc(200,200,radius,Math.PI*(-1/2),endAngle,false); ctx.stroke(); }
第六步(后续添加):为时钟添加数字指数。