濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > HTML5實(shí)現(xiàn)可縮放時(shí)鐘代碼

HTML5實(shí)現(xiàn)可縮放時(shí)鐘代碼

熱門(mén)標(biāo)簽:電銷(xiāo)機(jī)器人電話用什么卡 江蘇智能電銷(xiāo)機(jī)器人哪家好 當(dāng)涂高德地圖標(biāo)注 黃島區(qū)地圖標(biāo)注 鎮(zhèn)江智能外呼系統(tǒng)有效果嗎 成都智能外呼系統(tǒng)平臺(tái) 南寧點(diǎn)撥外呼系統(tǒng)哪家公司做的好 四川點(diǎn)撥外呼系統(tǒng) 云南大理400電話申請(qǐng)官方

廢話不多說(shuō)了,直接給大家貼代碼了,具體代碼如下所示:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>canvas_time</title>
    <style type="text/css">
    div {
        text-align: center;
        margin-top: 250px;
    }
    #clock {
        border: 1px solid #cccccc;
    }
    </style>
</head>
<body>
    <div>
        <canvas id="clock" height="600px" width="600px"></canvas>
    </div>
    <script type="text/JavaScript" >
var dom = document.getElementById('clock');
var ctx = dom.getContext('2d');
var width = ctx.canvas.width;
var height = ctx.canvas.height;
var r = width / 2;
var rem = width/200;
function drawBackground() {
    ctx.save(); //存儲(chǔ)當(dāng)前環(huán)境變量;
    ctx.translate(r, r); //重置坐標(biāo)到r,r
    ctx.beginPath(); // 起始一條路徑
    ctx.lineWidth = 10*rem; //設(shè)置線寬10;
    ctx.arc(0, 0, r - ctx.lineWidth /2, 0, 2 * Math.PI, false);
    ctx.stroke();
    var hourNumbers = [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2]; //定義數(shù)組
    ctx.font = 18*rem+"px Arial";
    ctx.textAlign = "center";
    ctx.textBaseline = "middle";
       hourNumbers.forEach(function(number, i) {
        var rad = 2 * Math.PI / 12 * i;
        var x = Math.cos(rad) * (r - 30*rem);
        var y = Math.sin(rad) * (r - 30*rem);
        ctx.fillText(number, x, y);
    });
    for (var i = 0; i < 60; i++) {
        var rad = 2 * Math.PI / 60 * i;
        var x = Math.cos(rad) * (r - 18*rem);
        var y = Math.sin(rad) * (r - 18*rem);
        ctx.beginPath();
        if (i % 5 === 0) {
            ctx.arc(x, y, 2*rem, 0, 2 * Math.PI, false);
            ctx.fillStyle = "#000";
        } else {
            ctx.arc(x, y, 2*rem, 0, 2 * Math.PI, false);
            ctx.fillStyle = "#ccc";
        }
        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 = 6*rem;
        ctx.lineCap = "round";
        ctx.moveTo(0, 10*rem);
        ctx.lineTo(0, -r / 2);
        ctx.stroke();
        ctx.restore();
    }
    function drawMinute(minute) {
        ctx.save();
        ctx.beginPath();
        var rad = 2 * Math.PI / 60 * minute;
        ctx.rotate(rad);
        ctx.lineWidth = 3*rem;
        ctx.lineCap = "round";
        ctx.moveTo(0, 10*rem);
        ctx.lineTo(0, -r + 30*rem);
        ctx.stroke();
        ctx.restore();
    }
    function drawSecond(second) {
        ctx.save();
        ctx.beginPath();
        ctx.fillStyle = 'red';
        var rad = 2 * Math.PI / 60 * second;
        ctx.rotate(rad);
        ctx.moveTo(-2*rem, 20*rem);
        ctx.lineTo(2*rem, 20*rem);
        ctx.lineTo(1, -r + 16*rem);
        ctx.lineTo(-1, -r + 16*rem);
        ctx.fill();
        ctx.restore();
    }
    function drawDot() {
        ctx.beginPath();
        ctx.fillStyle = '#fff';
        ctx.arc(0, 0, 3*rem, 0, 2 * Math.PI, false);
        ctx.fill();
    }
    function draw01() {
        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);
        drawMinute(minute);
        drawSecond(second);
        drawDot();
        ctx.restore();
    }
    draw01();
    setInterval(draw01, 1000);
</script>
</body>
</html>

總結(jié)

以上所述是小編給大家介紹的HTML5實(shí)現(xiàn)可縮放時(shí)鐘代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

標(biāo)簽:十堰 淮安 南京 廣西 酒泉 咸寧 佳木斯 西寧

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《HTML5實(shí)現(xiàn)可縮放時(shí)鐘代碼》,本文關(guān)鍵詞  HTML5,實(shí)現(xiàn),可,縮放,時(shí)鐘,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《HTML5實(shí)現(xiàn)可縮放時(shí)鐘代碼》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于HTML5實(shí)現(xiàn)可縮放時(shí)鐘代碼的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    乌鲁木齐县| 苍山县| 义乌市| 金湖县| 农安县| 荔浦县| 兴业县| 德江县| 永仁县| 当阳市| 曲沃县| 称多县| 襄樊市| 南宫市| 竹溪县| 林芝县| 白河县| 宣汉县| 肇州县| 琼结县| 青阳县| 宁明县| 江安县| 洱源县| 平武县| 卫辉市| 休宁县| 吕梁市| 宁强县| 兰州市| 五指山市| 陆川县| 安图县| 手游| 昂仁县| 灌南县| 临澧县| 萝北县| 远安县| 四川省| 岗巴县|