課程
/前端開發(fā)
/Html5
/Canvas 繪制時鐘
???????
2017-02-18
源自:Canvas 繪制時鐘 4-2
正在回答
得看下你的代碼,在draw()函數(shù)里面的ctx.restore()之前的函數(shù)(drawSecond())里面的開始不需要ctx.save(),后面也不需要ctx.restore(),我的一開始就是因為設(shè)置秒針的函數(shù)的開頭設(shè)置了個ctx.save(),右下角四分之一沒了,因為你在設(shè)置秒針的開頭設(shè)置一個ctx.save()會導(dǎo)致draw里面的清除函數(shù)的畫布原點,變成秒針的原點,也就是圓的中心點,今天才剛開始學(xué)canvas,也不知道我理解的對不對,反正你可以試試
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(); //畫小時數(shù) ????var??hoursNumbers=[3,4,5,6,7,8,9,10,11,12,1,2]; ????ctx.font="18px?Arial"; ????ctx.textAlign="center"; ????ctx.textBaseline="middle"; ????hoursNumbers.forEach(function?(number,i){ ????????var?rad?=2*Math.PI/12*i; ????????var?x=Math.cos(rad)*(r-30); ????????var?y=Math.sin(rad)*(r-30); ????????ctx.fillText(number,x,y); ????}); //畫分鐘數(shù) ????for?(var?i?=?0;?i?<?60;?i++)?{ ????????var?rad?=2*Math.PI/60*i; ????????var?x?=?Math.cos(rad)*(r-18); ????????var?y?=Math.sin(rad)*(r-18); ????????ctx.beginPath(); ????????if(i%5===0){ ????????????ctx.fillStyle="#000"; ????????????ctx.arc(x,y,2,0,2*Math.PI,false); ????????}else{ ????????????ctx.fillStyle="#ccc"; ????????????ctx.arc(x,y,2,0,2*Math.PI,false); ????????}?? ????????ctx.fill(); ????} } //時針 function?drawHour(hour,minute,second){ ????ctx.save(); ????ctx.beginPath(); ????var?rad=2*Math.PI/12*hour; ????var?mrad=2*Math.PI/12/60*minute; ????var?srad=2*Math.PI/12/60/60*second; ????ctx.rotate(rad+mrad+srad); ????ctx.lineWidth=6; ????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; ????var?srad=2*Math.PI/60/60*second; ????ctx.rotate(rad+srad); ????ctx.lineWidth=3; ????ctx.lineCap="round"; ????ctx.moveTo(0,10); ????ctx.lineTo(0,-r+30); ????ctx.stroke(); ????ctx.restore(); } //秒針 function?drawSecond(second){ ????ctx.save(); ????ctx.beginPath(); ????var?rad=2*Math.PI/60*second; ????ctx.rotate(rad); ????ctx.lineWidth=1; ????ctx.moveTo(0,10); ????ctx.lineTo(0,-r+18); ????ctx.strokeStyle="red"; ????ctx.stroke();???? ????ctx.restore(); } //圓點 function?drawDot(){ ????ctx.beginPath(); ????ctx.fillStyle="#fff"; ????ctx.arc(0,0,3,0,2*Math.PI,false); ????ctx.fill(); } ??? 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,second); ???drawMinute(minute,second); ???drawSecond(second);? ???drawDot();? ???ctx.restore(); } draw(); setInterval(draw,1000);
舉報
canvas畫出漂亮的時鐘,通過本教程能重新掌握一些幾何知識
3 回答第一步就錯了,只有方框,但是檢查了好多次跟老師的一樣啊
2 回答代碼如下,但是第一次HTML和css我自己寫的就出來了圓,這次按照視頻里的就不行
1 回答為什么ctx.clearRect(0,0,width,height);要放在第一行?
3 回答為什么加了清除,還是重復(fù)的再畫呢?
1 回答為什么沒有出來圓?js的代碼為什么沒有用到,已經(jīng)應(yīng)用在body里了啊
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學(xué)習伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號
2017-02-19
得看下你的代碼,在draw()函數(shù)里面的ctx.restore()之前的函數(shù)(drawSecond())里面的開始不需要ctx.save(),后面也不需要ctx.restore(),我的一開始就是因為設(shè)置秒針的函數(shù)的開頭設(shè)置了個ctx.save(),右下角四分之一沒了,因為你在設(shè)置秒針的開頭設(shè)置一個ctx.save()會導(dǎo)致draw里面的清除函數(shù)的畫布原點,變成秒針的原點,也就是圓的中心點,今天才剛開始學(xué)canvas,也不知道我理解的對不對,反正你可以試試
2017-03-19