function nowTime(context){ myDate = new Date(); myHour = myDate.getHours(); (myHour >= 12){ myHour = myHour-12; } var myMin = myDate.getMinutes(); var mySec = myDate.getSeconds(); hourArc; (myHour < 3){ hourArc = 2*Math.PI-(3-myHour)*Math.PI/6; }else{ hourArc = (myHour-3)*Math.PI/6; } (myHour){ case 0: hourArc = Math.PI*3/2;break; case 3: hourArc = 0;break; case 6: hourArc = Math.PI/2;break; case 9: hourArc = Math.PI;break; } //调用drawTime函数,在表盘上画出小时针 drawTime(context,hourArc,60); minArc; (myMin < 15){ minArc = 2*Math.PI-(15-myMin)*Math.PI/30; }else{ minArc = (myMin-15)*Math.PI/30; } (myMin){ case 0: minArc = Math.PI*3/2;break; case 15: minArc = 0;break; case 30: minArc = Math.PI/2;break; case 45: minArc = Math.PI;break; } //调用drawTime函数,在表盘上画出分钟针 drawTime(context,minArc,80); secArc; (mySec < 15){ secArc = 2*Math.PI-(15-mySec)*Math.PI/30; }else{ secArc = (mySec-15)*Math.PI/30; } (mySec){ case 0: secArc = Math.PI*3/2;break; case 14: secArc = 0;break; case 29: secArc = Math.PI/2;break; case 44: secArc = Math.PI;break; } //调用drawTime函数,在表盘上画出小时针 drawTime(context,secArc,80,"red"); //设置一个超时调用函数,每一秒超时调用nowTime更新时钟 setTimeout(function(){ //调用画新的指针前,当然应该要清除下原来的时针吧,用clearTime函数,真的好用! clearTime(context); //把闲杂指针清除了,再画一次当前的指针吧~ nowTime(context); },1000); }
那么,我们具体是怎么操作上下文画出指针的呢?我也不知道,所以,我们今天就到此结束吧~
开玩笑啦,嘿嘿,take it easy(一定要假装被我整到哈)
接下来是drawTime函数,它一共有四个参数(context,theArc,theLength,color="#000"),context一眼看穿是画布的上下文,theArc则是我们要旋转画布的角度,theLength代表指针的长度,color则是指针的颜色。