HTML代碼css
<body> <div class="box"> <span id="hour"></span>: <span id="minute"></span>: <span id="second"></span> </div> <div> <canvas id="clock" width="200px" height="200px"></canvas> </div> </body>
css:canvas
*{margin: 0;padding: 0;} div{ text-align: center; } div.box{ margin: 50px auto; text-align: center; font-size: 30px; display: flex; align-content: center; justify-content: center; } span{ display: inline-block; width: 50px; height: 50px; line-height: 50px; border:2px solid #ccc; font-size: 20px; margin: 0px 20px; }
js代碼dom
var hourspan =document.getElementById("hour"); var minutespan =document.getElementById("minute"); var secondspan =document.getElementById("second"); var dom = document.getElementById("clock"); var ctx = dom.getContext("2d"); var width = ctx.canvas.width; var height = ctx.canvas.height; var r = width/2; function drawBackground(){ ctx.save(); //保存當前畫布樣式 ctx.translate(r,r);//切換中心點 //畫外圓 ctx.beginPath(); ctx.lineWidth=10; ctx.arc(0,0,r-5,0,2*Math.PI,false); ctx.stroke(); //畫數字 var hourNumber = [3,4,5,6,7,8,9,10,11,12,1,2]; ctx.textAlign = "center"; ctx.textBaseline = "middle"; hourNumber.forEach(function(item,index){ var rad = 2*Math.PI/12*index; var x = Math.cos(rad)*(r-30); var y = Math.sin(rad)*(r-30); ctx.fillText(item,x,y); }) //畫時刻 for(var i=0;i<60;i++){ var rad = 2*Math.PI/60*i; var x = Math.cos(rad)*(r-15); var y = Math.sin(rad)*(r-15); ctx.beginPath(); ctx.arc(x,y,2,0,Math.PI*2,false); if(i%5 == 0){ ctx.fillStyle = "#000"; ctx.fill(); }else{ ctx.fillStyle = "#ccc"; ctx.fill(); } } } //畫時針,分針,秒針 function drawHour(hour,minute,second){ ctx.save(); ctx.beginPath(); var rad = 2*Math.PI/12*hour+((2*Math.PI/12)*(minute+second/60)/60);//將分,秒換算成小時 ctx.rotate(rad); ctx.strokeStyle = "#000"; ctx.lineWidth = 4; ctx.lineCap = "round"; ctx.moveTo(0,10); ctx.lineTo(0,-r/2); ctx.stroke(); ctx.restore();//返回上一個畫布樣式 } function drawMinute(minute,second){ ctx.save(); ctx.beginPath(); var rad = 2*Math.PI/60*minute+(2*Math.PI/60)*(second/60);//將秒換算成分 ctx.rotate(rad); ctx.strokeStyle = "#000"; ctx.lineWidth = 6; ctx.lineCap = "round"; ctx.moveTo(0,15); ctx.lineTo(0,-(r-25)); ctx.stroke(); ctx.restore(); } function drawSecond(second){ ctx.save(); ctx.beginPath(); ctx.strokeStyle = "red"; var rad = 2*Math.PI/60*second; ctx.rotate(rad); ctx.lineWidth = 3; ctx.lineCap = "round"; ctx.moveTo(0,20); ctx.lineTo(0,-(r-15)); ctx.stroke(); ctx.restore(); } //畫圓心 function drawCicle(){ ctx.save(); ctx.beginPath(); ctx.fillStyle ="#fff"; ctx.arc(0,0,2,0,Math.PI*2,false); ctx.fill(); ctx.restore(); } //將全部事件封裝在一個函數中,爲製做成動態時鐘作準備 function timeActive(){ ctx.clearRect(0,0,width,height);//清空整個畫布 var date = new Date(); var hour = date.getHours(); var minute = date.getMinutes(); var second = date.getSeconds(); hourspan.innerText = hour; minutespan.innerText = minute; secondspan.innerText = second; drawBackground(); drawHour(hour,minute,second); drawMinute(minute,second); drawSecond(second); drawCicle(); ctx.restore(); } timeActive();//初始化畫布 setInterval('timeActive()',1000);
小生初出茅廬,但願各位海涵,看見網上的教學視頻後,感受挺有意思的,手敲了一遍。函數