先放一張效果圖: 下麵是源代碼: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style type="text/css"> div{ text-align: center; margin-top: 150 ...
先放一張效果圖:
下麵是源代碼:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style type="text/css"> div{ text-align: center; margin-top: 150px; } canvas{ border: 1px solid black; } </style> </head> <body> <div> <canvas width="500px" height="500px" id="canvas"></canvas> </div> </body> <script type="text/javascript"> var canvas = document.getElementById("canvas"); var ctx =canvas.getContext("2d"); var width = canvas.width; var height = canvas.height; var r = width/2; var a = Math.floor(Math.random()*256); var b = Math.floor(Math.random()*256); var c = Math.floor(Math.random()*256); function fun(){ ctx.save(); ctx.translate(r,r); ctx.beginPath(); ctx.arc(0,0,r-5,0,Math.PI*2,false); ctx.lineWidth = 10; var grd = ctx.createLinearGradient(-r,0,r,0) grd.addColorStop(0,"rgb("+a+","+b+","+c+")"); grd.addColorStop(1,"rgb("+c+","+b+","+a+")"); ctx.strokeStyle = grd; //ctx.strokeStyle = "rgb("+a+","+b+","+c+")"; ctx.stroke(); var num = [3,4,5,6,7,8,9,10,11,12,1,2]; ctx.textAlign = "center"; ctx.textBaseline = "middle"; for(var i =0; i < num.length; i++){ var rang = Math.PI*2/12*i; var x = Math.cos(rang)*(r-30); var y = Math.sin(rang)*(r-30); ctx.fillText(num[i],x,y); ctx.closePath(); } for(var j =0; j < 60; j++){ var rang1 = Math.PI*2/60*j; var x1 = Math.cos(rang1)*(r-20); var y1 = Math.sin(rang1)*(r-20); ctx.beginPath(); ctx.arc(x1,y1,3,0,Math.PI*2); if(j%5==0){ ctx.fillStyle = "#000000"; } else{ ctx.fillStyle = "#cccccc"; } ctx.fill(); ctx.closePath(); } } function drawHour(hour,minu){ ctx.save(); //保存當前的狀態 ctx.beginPath(); ctx.lineWidth = 6; var rad = Math.PI * 2 / 12 * hour; var rad_minu = Math.PI * 2 / 12 / 60 * minu; ctx.rotate(rad + rad_minu); ctx.moveTo(0,10); ctx.lineTo(0,-r/2); ctx.lineCap="round"; ctx.stroke(); ctx.restore(); } function drawMinu(minu,seco){ ctx.save(); ctx.beginPath(); ctx.lineWidth = 4; var rad = Math.PI * 2 / 60 * minu; var rad_seco = Math.PI * 2 / 60 / 60 * seco; ctx.rotate(rad + rad_seco); ctx.moveTo(0,10); ctx.lineTo(0,-r+50); ctx.lineCap="round"; ctx.stroke(); ctx.restore(); } function drawSeco(seco){ ctx.save(); var rad = Math.PI * 2 / 60 * seco; ctx.beginPath(); ctx.rotate(rad); ctx.fillStyle = "#f00"; ctx.moveTo(-2,20); ctx.lineTo(2,20); ctx.lineTo(1,-r+20); ctx.lineTo(-1,-r+20); ctx.closePath(); ctx.fill(); ctx.restore(); } function drawDot(){ ctx.beginPath(); ctx.fillStyle = "#fff"; ctx.arc(0,0,3,0,Math.PI*2); ctx.closePath(); ctx.fill(); } function draw(){ ctx.clearRect(0,0,width,height); var datenow = new Date(); var hour = datenow.getHours(); var minu = datenow.getMinutes(); var seco = datenow.getSeconds(); fun(); drawHour(hour,minu); drawMinu(minu,seco); drawSeco(seco); drawDot(); ctx.restore(); } draw(); setInterval(draw,1000); </script> </html>View Code
————
先建一個畫布
<canvas width="500px" height="500px" id="canvas"></canvas>
然後在script中獲取到它和它的的寬度、高度,同時定義表盤的半徑
var canvas = document.getElementById("canvas"); var ctx =canvas.getContext("2d"); var width = canvas.width; var height = canvas.height; var r = width/2;
表盤的圓心在畫布中央便於我們的後續操作。可是實際上畫布的原點是畫布的左上角,所以我們可以使用translate()方法重新映射原點坐標:
ctx.translate(r,r);
然後畫一個圓,我在這加了個漸變色。(arc()中的五個參數分別是arc(圓心x坐標,圓心y坐標,半徑,起始角,結束角,Boolean值))
ctx.beginPath();
ctx.arc(0,0,r-5,0,Math.PI*2,false);
ctx.lineWidth = 10;
var grd = ctx.createLinearGradient(-r,0,r,0)
grd.addColorStop(0,"rgb("+a+","+b+","+c+")");
grd.addColorStop(1,"rgb("+c+","+b+","+a+")");
ctx.strokeStyle = grd;
ctx.stroke();
定義一個數組來裝表盤上的數字,然後通過迴圈的方式將他們列印在表盤上,這裡最主要的就是要算出每個數字列印的坐標,我們可以先算出每兩個數字間的弧度,然後就可以根據相應的弧度算出每個數字的位置
var num = [3,4,5,6,7,8,9,10,11,12,1,2]; ctx.textAlign = "center"; ctx.textBaseline = "middle"; for(var i =0; i < num.length; i++){ var rang = Math.PI*2/12*i; var x = Math.cos(rang)*(r-30); var y = Math.sin(rang)*(r-30); ctx.fillText(num[i],x,y); ctx.closePath(); }
然後表盤上的刻度也是同樣的方法,整點的位置上的點我們可以用一點不同的顏色
for(var j =0; j < 60; j++){ var rang1 = Math.PI*2/60*j; var x1 = Math.cos(rang1)*(r-20); var y1 = Math.sin(rang1)*(r-20); ctx.beginPath(); ctx.arc(x1,y1,3,0,Math.PI*2); if(j%5==0){ ctx.fillStyle = "#000000"; } else{ ctx.fillStyle = "#cccccc"; } ctx.fill(); ctx.closePath(); }
然後是時針部分,繪製指針的時候,可以使用linecap屬性給兩端稍加一點弧度。(使用lineCap之後不能用closePath())
算出時針應該轉動的角度,然後利用rotate()方法轉動它。
function drawHour(hour,minu){ ctx.save(); ctx.beginPath(); ctx.lineWidth = 6; var rad = Math.PI * 2 / 12 * hour; var rad_minu = Math.PI * 2 / 12 / 60 * minu; ctx.rotate(rad + rad_minu); ctx.moveTo(0,10); ctx.lineTo(0,-r/2); ctx.lineCap="round"; ctx.stroke(); ctx.restore(); }
然後是分針和秒針
function drawMinu(minu,seco){ ctx.save(); ctx.beginPath(); ctx.lineWidth = 4; var rad = Math.PI * 2 / 60 * minu; var rad_seco = Math.PI * 2 / 60 / 60 * seco; ctx.rotate(rad + rad_seco); ctx.moveTo(0,10); ctx.lineTo(0,-r+50); ctx.lineCap="round"; ctx.stroke(); ctx.restore(); }
function drawSeco(seco){ ctx.save(); var rad = Math.PI * 2 / 60 * seco; ctx.beginPath(); ctx.rotate(rad); ctx.fillStyle = "#f00"; ctx.moveTo(-2,20); ctx.lineTo(2,20); ctx.lineTo(1,-r+20); ctx.lineTo(-1,-r+20); ctx.closePath(); ctx.fill(); ctx.restore(); }
為了使我們畫的時鐘能夠使用,我們需要獲取到當前的時間,並把時分秒分別賦給相應的變數後傳到相應的函數中,之後用定時器每隔一秒調用一次此函數,就能達到時鐘的效果
function draw(){ ctx.clearRect(0,0,width,height); var datenow = new Date(); var hour = datenow.getHours(); var minu = datenow.getMinutes(); var seco = datenow.getSeconds(); fun(); drawHour(hour,minu); drawMinu(minu,seco); drawSeco(seco); drawDot(); ctx.restore(); } draw(); setInterval(draw,1000);