JavaScript html5 canvas繪制時(shí)鐘效果
來源:易賢網(wǎng) 閱讀:630 次 日期:2016-08-04 15:07:54
溫馨提示:易賢網(wǎng)小編為您整理了“JavaScript html5 canvas繪制時(shí)鐘效果”,方便廣大網(wǎng)友查閱!

本文實(shí)例講述了JavaScript+html5 canvas繪制時(shí)鐘效果。分享給大家供大家參考,具體如下:

HTML部分:

<!DOCTYPE html>

<html lang="en">

<head>

 <meta charset="UTF-8">

 <meta name="viewport" content="width=device-width, initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">

 <title>canvas繪圖</title>

</head>

<body onload="init()">

<canvas id="canvas" width="200px" height="200px"></canvas>

</body>

</html>

JavaScript部分:

function init(){

 var canvas = document.getElementById("canvas"),

  context = canvas.getContext("2d");

 setInterval(function(){draw(canvas, context)},1000);

}

function draw(canvas, context){

 var x = canvas.width,

  y = canvas.height,

  r = Math.min(x/2, y/2);

 context.clearRect(0, 0, x, y); //清除繪畫歷史

 //繪畫鐘框

 context.fillStyle = "#f1f1f1";

 drawCircle(context, x, y, r);

 //繪畫文字

 var tx = x/2,ty = y/2,tr = 0.8*r;

 context.font = "bold 12px 微軟雅黑";

 context.fillStyle = "#000";

 drawText(context, "1", tx + 0.5*tr,ty - 0.866*tr);

 drawText(context, "2", tx + 0.866*tr, ty - 0.5*tr);

 drawText(context, "3", tx + tr, ty);

 drawText(context, "4", tx + 0.866*tr, ty + 0.5*tr);

 drawText(context, "5", tx + 0.5*tr, ty + 0.866*tr);

 drawText(context, "6", tx, ty + tr);

 drawText(context, "7", tx - 0.5*tr, ty + 0.866*tr);

 drawText(context, "8", tx - 0.866*tr, ty + 0.5*tr);

 drawText(context, "9", tx - tr, ty);

 drawText(context, "10", tx - 0.866*tr, ty - 0.5*tr);

 drawText(context, "11", tx - 0.5*tr, ty - 0.866*tr);

 drawText(context, "12", tx, ty - tr);

 //獲取當(dāng)前時(shí)間

 var date = new Date(),

  h = date.getHours(),

  m = date.getMinutes(),

  s = date.getSeconds(),

  angleH = (360/12)*Math.PI/180,

  angleM = (360/60)*Math.PI/180

 context.strokeSyle = "#000";

  //繪制時(shí)刻度

  drawScale(context, x, y, r, angleH, -0.88*r, -0.96*r, 3);

  //繪制分刻度

  drawScale(context, x, y, r, angleM, -0.93*r, -0.96*r, 1);

 //繪畫時(shí)分秒針

 drawCircle(context, x, y, 3);

 drawNeedle(context, x, y, r, h*angleH + m*angleM/12, -0.5*r);

 drawNeedle(context, x, y, r, m*angleM + s*angleM/60, -0.6*r);

 drawNeedle(context, x, y, r, s*angleM, -0.75*r);

}

//繪畫圓

function drawCircle(context, x, y, r){

 context.save();

 context.beginPath();

 context.arc(x/2, y/2, r, 0, Math.PI*2, 0);

 context.fill();

 context.closePath();

 context.restore();

}

//繪畫文字方法

function drawText(context, text, x, y){

 context.save();

 x -= (context.measureText(text).width/2);

 y += 4;

 context.translate(x, y);

 context.fillText(text, 0, 0);

 context.restore();

}

//繪制刻度方法

function drawScale(context, x, y, r, rotate, start, end, lineWidth){

 context.save();

 context.beginPath();

 context.translate(x/2,y/2);

 context.lineWidth = lineWidth;

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

  context.rotate(rotate);

  context.moveTo(0, start); 

  context.lineTo(0, end);

 }

 context.closePath();

 context.stroke();

 context.restore();

}

//繪畫時(shí)分秒針方法

function drawNeedle(context, x, y, r, rotate, line){

 context.save();

 context.translate(x/2,y/2);

 context.beginPath();

 context.rotate(rotate);

 context.moveTo(0, 0.1*r);

 context.lineTo(0, line);

 context.closePath();

 context.stroke();

 context.restore();

}

希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。

更多信息請(qǐng)查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:JavaScript html5 canvas繪制時(shí)鐘效果
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 加入群交流 | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65317125(9:00—18:00) 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)