canvas畫時(shí)鐘

html:

<canvas id="#clock" width="63px" height="63px"></canvas>


js:

var dom = $("#clock");

var ctx = dom[0].getContext("2d");

console.log(ctx.canvas.width);

var width = ctx.canvas.width;

var height = ctx.canvas.height;

var r = width/2;

var rem = width/200;

function drawAround() {

ctx.save();

ctx.beginPath();

ctx.translate(r,r);

ctx.lineWidth = 2;

ctx.arc(0,0,r-1,0,2*Math.PI,false);

ctx.strokeStyle = "#FFF";

ctx.stroke();

for (var i = 0; i < 60; i++) {

var rad = 2 * Math.PI/60 * i;

var x = Math.cos(rad) * (r-6);

var y = Math.sin(rad) * (r-6);

ctx.beginPath();

if (i % 5 != 0) {

ctx.fillStyle = "rgba(255,255,255,0)";

}

else if (i % 3 != 0) {

ctx.fillStyle = "#FFF";

}

else{

ctx.fillStyle = "#F00";

}

ctx.arc(x,y,2,0,2*Math.PI,false);

ctx.fill();

}

}

function drawHour(hour,minute) {

ctx.save();

ctx.beginPath();

var rad = 2 * Math.PI/12 * hour;

var mrad = 2 * Math.PI/12/60 * minute;

ctx.rotate(rad + mrad);

ctx.lineWidth = 4;

ctx.lineCap = "round";

ctx.fillStyle = "#FFF";

ctx.moveTo(0,3);

ctx.lineTo(0,-14);

ctx.stroke();

ctx.restore();

}

function drawMinute(minute) {

ctx.save();

ctx.beginPath();

var rad = 2 * Math.PI/60 * minute;

ctx.rotate(rad);

ctx.lineWidth = 3;

ctx.lineCap = "round";

ctx.moveTo(0,5);

ctx.lineTo(0,-r+14);

ctx.stroke();

ctx.restore();

}

function drawSecond(second) {

ctx.save();

ctx.beginPath();

var rad = 2 * Math.PI/60 * second;

ctx.rotate(rad);

ctx.fillStyle = "#F00";

ctx.moveTo(-2,10);

ctx.lineTo(2,10);

ctx.lineTo(0,-r+10);

ctx.lineTo(-1,-r+10);

ctx.fill();

ctx.restore();

}

function drawDot() {

ctx.beginPath();

ctx.fillStyle = "#FFF";

ctx.arc(0,0,3,0,2*Math.PI,false);

}

function draw() {

ctx.clearRect(0,0,width,height);

var now = new Date();

var hour = now.getHours();

var minute = now.getMinutes();

var second = now.getSeconds();

drawAround();

drawSecond(second);

drawMinute(minute);

drawHour(hour,minute);

drawDot();

ctx.restore();

}

draw();

setInterval(draw,1000);

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Cnavas繪制時(shí)鐘 背景圖的繪制(大圓、數(shù)字、小圓點(diǎn)),掌握基礎(chǔ)知識:圓的繪制(arc方法),關(guān)于圓的弧度的計(jì)算...
    Iris_mao閱讀 2,565評論 7 26
  • 一:canvas簡介 1.1什么是canvas? ①:canvas是HTML5提供的一種新標(biāo)簽 ②:HTML5 ...
    GreenHand1閱讀 4,877評論 2 32
  • 一、canvas簡介 1.1 什么是canvas?(了解) 是HTML5提供的一種新標(biāo)簽 Canvas是一個(gè)矩形區(qū)...
    Looog閱讀 4,034評論 3 40
  • 一、canvas簡介 1.1 什么是canvas?(了解) 是HTML5提供的一種新標(biāo)簽 Canvas是一個(gè)矩形區(qū)...
    J_L_L閱讀 1,683評論 0 4
  • 啥是canvas? HTML5 標(biāo)簽用于繪制圖像(通過腳本,通常是 JavaScript)。不過, 元素本身...
    kiaizi閱讀 847評論 0 4

友情鏈接更多精彩內(nèi)容