canvas時鐘

var oc = document.getElementById("c1");

var ogc = oc.getContext("2d");

function toDraw() {

var x= 200;

var y= 200;

var r= 150;

ogc.clearRect(0,0,oc.width,oc.height);//每次清空畫布

? ? var oDate= new Date();

var oHours= oDate.getHours();

var oMin= oDate.getMinutes();

var oSec= oDate.getSeconds();

//

? ? var oHoursValue= (-90 + oHours* 30 + oMin/ 2 + oSec/ 120)* Math.PI/ 180;

var oMinValue= (-90 + oMin* 6 + oSec/ 10)* Math.PI/ 180;

var oSecValue= (-90 + oSec* 6)* Math.PI/ 180;

// 繪制表盤? 每格角度 360/60=6°? 角度*π/180 = 弧度

? ? ogc.beginPath();

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

ogc.moveTo(x, y);

ogc.arc(x, y, r,6 * i* Math.PI/ 180,6 * (i+ 1)* Math.PI/ 180,false);

}

ogc.closePath();

ogc.stroke();

ogc.fillStyle= 'white';

ogc.beginPath();

ogc.moveTo(x, y);

ogc.arc(x, y, r* 19 / 20,0,360 * Math.PI/ 180,false);

ogc.closePath();

ogc.fill();

ogc.beginPath();

ogc.lineWidth= 3;

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

ogc.moveTo(x, y);

ogc.arc(x, y, r,30 * i* Math.PI/ 180,30 * (i+ 1)* Math.PI/ 180,false);

}

ogc.closePath();

ogc.stroke();

ogc.fillStyle= 'white';

ogc.beginPath();

ogc.moveTo(x, y);

ogc.arc(x, y, r* 18 / 20,0,360 * Math.PI/ 180,false);

ogc.closePath();

ogc.fill();

ogc.lineWidth= 5;

ogc.beginPath();

ogc.moveTo(x, y);

ogc.arc(x, y, r* 10 / 20, oHoursValue, oHoursValue,false);

ogc.closePath();

ogc.stroke();

ogc.lineWidth= 3;

ogc.beginPath();

ogc.moveTo(x, y);

ogc.arc(x, y, r* 15 / 20, oMinValue, oMinValue,false);

ogc.closePath();

ogc.stroke();

ogc.lineWidth= 1;

ogc.beginPath();

ogc.moveTo(x, y);

ogc.arc(x, y, r* 19 / 20, oSecValue, oSecValue,false);

ogc.closePath();

ogc.stroke();

}

setInterval(function () {

toDraw();

},1000);

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

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

  • 先來看一下效果圖 下面是代碼 哈哈哈 <!DOCTYPE html> 時鐘 #clock{/border...
    小飛俠zzr閱讀 247評論 0 0
  • 一:canvas簡介 1.1什么是canvas? ①:canvas是HTML5提供的一種新標簽 ②:HTML5 ...
    GreenHand1閱讀 4,883評論 2 32
  • 打開想寫點什么,有很多很多想法又不知道去寫些什么。 寫日記這個東西來說有很多很多的好處,但是貴在堅持。我自身有很大...
    1李一晨閱讀 369評論 0 0
  • 腳踩在淤泥里 但心要向光明
    靖源小王子閱讀 120評論 0 0
  • 我現(xiàn)在是個活在三線城市的普通女孩,而且沒有多牢靠的工作。上了四年大學還跟個打工妹一樣,甚至混的不如同齡的打工妹。這...
    我愛吃魚吃火鍋閱讀 277評論 0 1

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