web前端-Canvas時(shí)鐘

技術(shù)點(diǎn):

  • 狀態(tài)保存 context.save()
  • 狀態(tài)恢復(fù) context.restore()
  • 旋轉(zhuǎn) context.rotate(弧度)
  • 平移 context.translate(x,y) x,y 是需要移動(dòng)到的目標(biāo)位置坐標(biāo)
  • 縮放 context.scale(1.5,1.5) 1.5,1.5 是縮放比例, 將原來(lái)的畫(huà)布放大1.5倍
  • 畫(huà)圓弧 context.arc(x,y,r,初始弧度, 最終弧度)
  • 清空矩形內(nèi)容context.clearRect(x,y,width,height)
  • 時(shí)間(時(shí),分,秒)的角度換算

點(diǎn)擊此處查看Demo

效果圖如下


CanvasClock

上代碼

<canvas id="clock" width="400" height="400"></canvas>
canvas {
        border: 1px solid #000;
        display: block;
        margin: 100px auto;
    }
function Clock(options) {
    this._init(options);
}
Clock.prototype = {
    constructor: Clock,
    _init: function(options) {
        options = options || {};
        this.width = options.width || 400;
        this.height = options.height || 400;
        this.r = options.r || 180;
        this.clockR = options.r - 20 || 160;
        this.ctx = options.ctx || document.getElementById('clock').getContext('2d');
    },
    // 渲染表盤(pán)
    renderClock: function() {

        // 保存狀態(tài)
        this.ctx.save();

        // 繪制外面的圈
        this.ctx.arc(0, 0, this.r, 0, 2 * Math.PI);
        this.ctx.strokeStyle = '#408000';
        this.ctx.lineWidth = 10;
        this.ctx.stroke();

        // 恢復(fù)狀態(tài)
        this.ctx.restore();
    },
    renderPoint: function() {
        this.ctx.save();
        this.ctx.beginPath();
        this.ctx.fillStyle = '#FF6666';
        this.ctx.arc(0, 0, 8, 0, 2 * Math.PI);
        this.ctx.fill();
        this.ctx.restore();
    },
    // 渲染刻度
    renderScale: function() {
        // 保存狀態(tài)
        this.ctx.save();
        // 繪制刻度
        this.ctx.beginPath();
        for (var i = 0; i < 60; i++) {
            var x = Math.cos(i * 6 * Math.PI / 180) * this.clockR;
            var y = Math.sin(i * 6 * Math.PI / 180) * this.clockR;
            this.ctx.beginPath();
            var r = 3;
            var text = 1;
            if (i % 5 == 0) {
                r = 5;
                this.ctx.save();
                var v = parseInt(i / 5);
                text = v == 0 ? 12 : v;
                this.ctx.textAlign = 'center';
                this.ctx.textBaseline = 'middle';
                this.ctx.font = '18px Monaco';
                this.ctx.fillStyle = '#000';
                var xx = Math.cos(i * 6 * Math.PI / 180) * (this.clockR + 15);
                var yy = Math.sin(i * 6 * Math.PI / 180) * (this.clockR + 15);
                this.ctx.translate(xx, yy);
                this.ctx.rotate(90 * Math.PI / 180);
                this.ctx.fillText(text, 0, 0);
                this.ctx.restore();
            }
            this.ctx.beginPath();
            this.ctx.arc(x, y, r, 0, 2 * Math.PI);
            this.ctx.fillStyle = '#FF8000';
            this.ctx.fill();
        }
        // 恢復(fù)狀態(tài)
        this.ctx.restore();
    },
    // 渲染時(shí)針
    renderHour: function(dateTime) {
        this.ctx.save();
        var hour = dateTime.getHours();
        var minute = dateTime.getMinutes();
        var second = dateTime.getSeconds();
        // 每秒6度
        var deg = ((hour % 12) * 30 * Math.PI / 180);
        
        // 計(jì)算分針和秒針旋轉(zhuǎn)過(guò)的角度對(duì)應(yīng)的時(shí)針旋轉(zhuǎn)過(guò)的角度
        var otherDeg = ((minute * 6 + 6 / 60 * second) / 60 * 5) * Math.PI / 180;
        deg += otherDeg;
        this.ctx.rotate(deg);
        this.ctx.beginPath();
        this.ctx.moveTo(-15, 0);
        this.ctx.lineTo(90, 0);
        this.ctx.strokeStyle = '#0094ff';
        this.ctx.lineWidth = 8;
        this.ctx.stroke();
        this.ctx.restore();
    },
    // 渲染分針
    renderMiute: function(dateTime) {
        this.ctx.save();
        var minute = dateTime.getMinutes();
        var second = dateTime.getSeconds();
        // 每秒6度
        var deg = (minute * 6 * Math.PI / 180) + (6 / 60 * second * Math.PI / 180);

        this.ctx.rotate(deg);
        this.ctx.beginPath();
        this.ctx.moveTo(-20, 0);
        this.ctx.lineTo(120, 0);
        this.ctx.strokeStyle = '#333333';
        this.ctx.lineWidth = 5;
        this.ctx.stroke();
        this.ctx.restore();
    },
    // 渲染秒針
    renderSecond: function(dateTime) {
        this.ctx.save();
        var second = dateTime.getSeconds();
        // 每秒6度
        var deg = second * 6 * Math.PI / 180;

        this.ctx.rotate(deg);
        this.ctx.beginPath();
        this.ctx.moveTo(-30, 0);
        this.ctx.lineTo(150, 0);
        this.ctx.lineWidth = 2;
        this.ctx.strokeStyle = '#FF6666'
        this.ctx.stroke();
        this.ctx.restore();
    },
    render: function(dateTime) {
        this.ctx.save();
        this.ctx.lineCap = 'round';
        this.ctx.clearRect(0, 0, this.width, this.height);
        this.ctx.beginPath();
        this.ctx.translate(this.width / 2, this.width / 2);
        this.ctx.rotate(-Math.PI / 2);
        this.renderClock();
        this.renderScale();
        this.renderHour(dateTime);
        this.renderMiute(dateTime);
        this.renderSecond(dateTime);
        this.renderPoint();
        this.ctx.restore();
    }
}

var clock = new Clock();
clock.render(new Date());
setInterval(function() {
    clock.render(new Date());
}, 1000)
最后編輯于
?著作權(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,883評(píng)論 2 32
  • 圓弧context.arc(x, y, radius, start,end, boolean)x:圓心的x坐標(biāo)y:...
    蒲公英_前端開(kāi)發(fā)者閱讀 488評(píng)論 0 0
  • 一、canvas簡(jiǎn)介 1.1 什么是canvas?(了解) 是HTML5提供的一種新標(biāo)簽 Canvas是一個(gè)矩形區(qū)...
    Looog閱讀 4,052評(píng)論 3 40
  • 一、基礎(chǔ)介紹和畫(huà)直線 大多數(shù)現(xiàn)代瀏覽器都是支持Canvas的,比如 Firefox, safari, chrome...
    空谷悠閱讀 957評(píng)論 0 1
  • ## 功能 在網(wǎng)頁(yè)中實(shí)時(shí)生成圖像,并操作圖像內(nèi)容(通俗就是在網(wǎng)頁(yè)中畫(huà)畫(huà)) 1.canvas畫(huà)布可以制作在線繪圖工具...
    a8f90e785c29閱讀 607評(píng)論 0 0

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