課程
/前端開(kāi)發(fā)
/Html5
/Canvas 繪制時(shí)鐘
可以轉(zhuǎn)動(dòng),但是是按照時(shí)針來(lái)轉(zhuǎn)的,不是秒針?
2017-03-12
源自:Canvas 繪制時(shí)鐘 4-1
正在回答
下面的代碼是按照秒針rotate:
function drawHour(hour,minute,second){ ? ?ctx.save(); ? ?ctx.beginPath(); ?
?var rad=2*Math.PI/12*hour; ? ?
var srad=2*Math.PI/60/60*second;
????var mrad=2*Math.PI/12/60*minute+srad; ????
ctx.rotate(rad+mrad); ? ?
ctx.lineWidth=6; ? ?ctx.lineCap='round'; ? ?ctx.moveTo(0,10*rem); ? ?ctx.lineTo(0,-r/2); ? ?ctx.stroke(); ? ?ctx.restore(); }
function drawMinute(minute,second){ ? ?ctx.save(); ?
?var rad=2*Math.PI/60*minute; ?
?var srad=2*Math.PI/60/60*second; ?
?ctx.rotate(rad+srad); ?
??ctx.beginPath(); ? ?ctx.lineWidth=3; ? ?ctx.lineCap='round'; ? ?ctx.moveTo(0,20*rem); ? ?ctx.lineTo(0,-r/4*3); ? ?ctx.stroke(); ? ?ctx.restore(); }
function drawSecond(second){ ? ?ctx.save(); ? ?ctx.fillStyle="#c12345" ? ?var rad=2*Math.PI/60*second; ? ?ctx.rotate(rad); ? ?ctx.beginPath(); ? ?ctx.moveTo(-2,20*rem); ? ?ctx.lineTo(2,20*rem); ? ?ctx.lineTo(1,-r+18*rem); ? ?ctx.lineTo(-1,-r+18*rem); ? ?ctx.fill(); ? ?ctx.restore(); } function drawDot(){ ? ?ctx.beginPath(); ? ?ctx.fillStyle="#fff"; ? ?ctx.arc(0,0,3,0,2*Math.PI,false); ? ?ctx.fill(); } function draw(){ ? ?ctx.save(); ? ?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);
qq_笑的狠牽強(qiáng)_0 提問(wèn)者
舉報(bào)
canvas畫(huà)出漂亮的時(shí)鐘,通過(guò)本教程能重新掌握一些幾何知識(shí)
1 回答按照改變秒針的方式改變顏色,但是分針,時(shí)針都不了可以改顏色!這是為什么?
1 回答她會(huì)動(dòng),但是要刷新秒針才會(huì)動(dòng)
3 回答為什么畫(huà)秒針的時(shí)候,半徑是2,
1 回答分針的旋轉(zhuǎn)角度不對(duì)
2 回答指針的旋轉(zhuǎn)角度
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號(hào)-11 京公網(wǎng)安備11010802030151號(hào)
購(gòu)課補(bǔ)貼聯(lián)系客服咨詢(xún)優(yōu)惠詳情
慕課網(wǎng)APP您的移動(dòng)學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號(hào)
2017-03-15
下面的代碼是按照秒針rotate:
function drawHour(hour,minute,second){ ? ?ctx.save(); ? ?ctx.beginPath(); ?
?var rad=2*Math.PI/12*hour; ? ?
var srad=2*Math.PI/60/60*second;
????var mrad=2*Math.PI/12/60*minute+srad; ????
ctx.rotate(rad+mrad); ? ?
ctx.lineWidth=6; ? ?ctx.lineCap='round'; ? ?ctx.moveTo(0,10*rem); ? ?ctx.lineTo(0,-r/2); ? ?ctx.stroke(); ? ?ctx.restore(); }
function drawMinute(minute,second){ ? ?ctx.save(); ?
?var rad=2*Math.PI/60*minute; ?
?var srad=2*Math.PI/60/60*second; ?
?ctx.rotate(rad+srad); ?
??ctx.beginPath(); ? ?ctx.lineWidth=3; ? ?ctx.lineCap='round'; ? ?ctx.moveTo(0,20*rem); ? ?ctx.lineTo(0,-r/4*3); ? ?ctx.stroke(); ? ?ctx.restore(); }
function drawSecond(second){ ? ?ctx.save(); ? ?ctx.fillStyle="#c12345" ? ?var rad=2*Math.PI/60*second; ? ?ctx.rotate(rad); ? ?ctx.beginPath(); ? ?ctx.moveTo(-2,20*rem); ? ?ctx.lineTo(2,20*rem); ? ?ctx.lineTo(1,-r+18*rem); ? ?ctx.lineTo(-1,-r+18*rem); ? ?ctx.fill(); ? ?ctx.restore(); } function drawDot(){ ? ?ctx.beginPath(); ? ?ctx.fillStyle="#fff"; ? ?ctx.arc(0,0,3,0,2*Math.PI,false); ? ?ctx.fill(); } function draw(){ ? ?ctx.save(); ? ?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);