使用canvas繪制走動(dòng)的鐘表

效果圖:

Paste_Image.png

思路:
1.設(shè)置畫(huà)布
2.利用ctx.arc繪制出大圓
3.利用for循環(huán)繪制鐘表的刻度值(小時(shí)每個(gè)刻度間隔30度,分鐘每個(gè)刻度間隔6度),注意設(shè)置畫(huà)布的原點(diǎn)位置
4.設(shè)置時(shí)針、分針、秒針
5.獲取當(dāng)期時(shí)間,并設(shè)置對(duì)應(yīng)的小時(shí)、分鐘、秒的時(shí)間
6.設(shè)置指針走動(dòng):
6.1清屏
6.2重新繪制鐘表的大圓和刻度值
6.3利用ctx.rotate設(shè)置時(shí)分秒針的旋轉(zhuǎn)
另:寫(xiě)完了就發(fā)上來(lái)了,代碼沒(méi)經(jīng)過(guò)優(yōu)化,請(qǐng)各位大神諒解。

直接上代碼
html:

<canvas id="lCanvas" width="900px" height="600px"></canvas>

css:

<style type="text/css">
    * {
        margin: 0;
        padding: 0;
        list-style: none;
    }

    #lCanvas {
        margin-left: 250px;
        border: 1px solid #000;
    }
</style>

javascript:

<script>
    var canvas = document.getElementById("lCanvas");
    //設(shè)置上下文
    var ctx = canvas.getContext("2d");

    //清屏
    function clear() {
        ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
    }

    //鐘表刻度設(shè)置
    function Scale() {
        //大圓
        ctx.beginPath();
        ctx.arc(450, 300, 260, 0, 2 * Math.PI, false);
        ctx.strokeStyle = "#FFC0CB";
        ctx.lineWidth = 10;
        ctx.stroke();
        //刻度
        var hours = 12;
        var mins = 60;
        for (var i = 0; i < hours; i++) {
            ctx.save();
            ctx.beginPath();
            ctx.strokeStyle = "black";
            ctx.translate(canvas.width * 0.5, canvas.height * 0.5);
            ctx.rotate(i * 30 * Math.PI / 180);
            ctx.moveTo(-5,-190);
            ctx.lineTo(5,-190)
            ctx.lineWidth = 25;
            ctx.stroke();
            ctx.restore();
        }
        for (var i = 0; i < mins; i++) {
            ctx.save();
            ctx.beginPath();
            ctx.strokeStyle = "black";
            ctx.translate(canvas.width * 0.5, canvas.height * 0.5);
            ctx.rotate(i * 6 * Math.PI / 180);
            ctx.moveTo(-2,-195);
            ctx.lineTo(2,-195);
            ctx.lineWidth = 10;
            ctx.stroke();
            ctx.restore();
        }
        //中心圓
        ctx.save();
        ctx.beginPath();
        ctx.translate(canvas.width * 0.5, canvas.height * 0.5);
        ctx.arc(0, 0, 10, 0, 2 * Math.PI);
        ctx.fillStyle = 'black';
        ctx.fill();
        ctx.restore();
    }

    //走動(dòng)
    function timeRun() {
        setInterval(function () {
            //清屏
            clear();
            //畫(huà)時(shí)鐘
            Scale();
            //獲取時(shí)間
            var date = new Date();
            var sec= date.getSeconds();
            var min = date.getMinutes() + sec / 60;
            var hour = date.getHours() + min / 60;
            hour = hour > 12 ? hour - 12 : hour;

            //時(shí)針
            ctx.save();
            ctx.beginPath();
            ctx.translate(canvas.width * 0.5, canvas.height * 0.5);
            ctx.rotate(hour * 30 * Math.PI / 180);
            ctx.strokeStyle = '#000';
            ctx.moveTo(0,-130);
            ctx.lineTo(0,5);
            ctx.lineWidth = 12;
            ctx.stroke();
            ctx.restore();

            //分針
            ctx.save();
            ctx.beginPath();
            ctx.translate(canvas.width * 0.5, canvas.height * 0.5);
            ctx.rotate(min * 6 * Math.PI / 180);
            ctx.strokeStyle = '#ccc';
            ctx.moveTo(0,-150);
            ctx.lineTo(0,5);
            ctx.lineWidth = 8;
            ctx.stroke();
            ctx.restore();

            //秒針
            ctx.save();
            ctx.beginPath();
            ctx.translate(canvas.width * 0.5, canvas.height * 0.5);
            ctx.rotate(sec * 6 * Math.PI / 180);
            ctx.strokeStyle = 'red';
            ctx.moveTo(0,-200);
            ctx.lineTo(0,3);
            ctx.lineWidth = 3;
            ctx.stroke();
            ctx.restore();
        }, 1000);
    }

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

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

  • 一:canvas簡(jiǎn)介 1.1什么是canvas? ①:canvas是HTML5提供的一種新標(biāo)簽 ②:HTML5 ...
    GreenHand1閱讀 4,881評(píng)論 2 32
  • Cnavas繪制時(shí)鐘 背景圖的繪制(大圓、數(shù)字、小圓點(diǎn)),掌握基礎(chǔ)知識(shí):圓的繪制(arc方法),關(guān)于圓的弧度的計(jì)算...
    Iris_mao閱讀 2,569評(píng)論 7 26
  • 一、canvas簡(jiǎn)介 1.1 什么是canvas?(了解) 是HTML5提供的一種新標(biāo)簽 Canvas是一個(gè)矩形區(qū)...
    Looog閱讀 4,040評(píng)論 3 40
  • 在iOS中隨處都可以看到絢麗的動(dòng)畫(huà)效果,實(shí)現(xiàn)這些動(dòng)畫(huà)的過(guò)程并不復(fù)雜,今天將帶大家一窺ios動(dòng)畫(huà)全貌。在這里你可以看...
    每天刷兩次牙閱讀 8,693評(píng)論 6 30
  • “身份證可以造假, 結(jié)婚證可以藏起來(lái), 承諾可以翻臉不認(rèn)人, 名字公安局可以改。 但刻進(jìn)身體的刺青不騙人, 沒(méi)洗掉...
    低俗青年制造所閱讀 1,070評(píng)論 0 2

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