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 是縮放比例, 將原來的畫布放大1.5倍
  • 畫圓弧 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');
    },
    // 渲染表盤
    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)過的角度對(duì)應(yīng)的時(shí)針旋轉(zhuǎn)過的角度
        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)系作者
  • 序言:七十年代末洲鸠,一起剝皮案震驚了整個(gè)濱河市绢淀,隨后出現(xiàn)的幾起案子覆履,更是在濱河造成了極大的恐慌,老刑警劉巖伟众,帶你破解...
    沈念sama閱讀 212,542評(píng)論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異先紫,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)仑鸥,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,596評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來疮胖,“玉大人硕糊,你說我怎么就攤上這事县遣。” “怎么了?”我有些...
    開封第一講書人閱讀 158,021評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)箕速。 經(jīng)常有香客問我,道長(zhǎng)字柠,這世上最難降的妖魔是什么窑业? 我笑而不...
    開封第一講書人閱讀 56,682評(píng)論 1 284
  • 正文 為了忘掉前任搀擂,我火速辦了婚禮喷市,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘腹备。我一直安慰自己,他們只是感情好惧互,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,792評(píng)論 6 386
  • 文/花漫 我一把揭開白布稻据。 她就那樣靜靜地躺著匆赃,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上何荚,一...
    開封第一講書人閱讀 49,985評(píng)論 1 291
  • 那天妥衣,我揣著相機(jī)與錄音税手,去河邊找鬼。 笑死候齿,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的慌盯。 我是一名探鬼主播,決...
    沈念sama閱讀 39,107評(píng)論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼乃摹!你這毒婦竟也來了孵睬?” 一聲冷哼從身側(cè)響起伶跷,我...
    開封第一講書人閱讀 37,845評(píng)論 0 268
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤蹈集,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后靖诗,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,299評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,612評(píng)論 2 327
  • 正文 我和宋清朗相戀三年位衩,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了糖驴。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片辙谜。...
    茶點(diǎn)故事閱讀 38,747評(píng)論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出润绵,到底是詐尸還是另有隱情躺孝,我是刑警寧澤植袍,帶...
    沈念sama閱讀 34,441評(píng)論 4 333
  • 正文 年R本政府宣布,位于F島的核電站籽懦,受9級(jí)特大地震影響于个,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜暮顺,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,072評(píng)論 3 317
  • 文/蒙蒙 一厅篓、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧捶码,春花似錦贷笛、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,828評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至,卻和暖如春汇荐,著一層夾襖步出監(jiān)牢的瞬間洞就,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,069評(píng)論 1 267
  • 我被黑心中介騙來泰國(guó)打工掀淘, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留旬蟋,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 46,545評(píng)論 2 362
  • 正文 我出身青樓革娄,卻偏偏與公主長(zhǎng)得像倾贰,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子拦惋,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,658評(píng)論 2 350

推薦閱讀更多精彩內(nèi)容

  • 一:canvas簡(jiǎn)介 1.1什么是canvas匆浙? ①:canvas是HTML5提供的一種新標(biāo)簽 ②:HTML5 ...
    GreenHand1閱讀 4,674評(píng)論 2 32
  • 圓弧context.arc(x, y, radius, start,end, boolean)x:圓心的x坐標(biāo)y:...
    蒲公英_前端開發(fā)者閱讀 373評(píng)論 0 0
  • 一、canvas簡(jiǎn)介 1.1 什么是canvas厕妖?(了解) 是HTML5提供的一種新標(biāo)簽 Canvas是一個(gè)矩形區(qū)...
    Looog閱讀 3,940評(píng)論 3 40
  • 一首尼、基礎(chǔ)介紹和畫直線 大多數(shù)現(xiàn)代瀏覽器都是支持Canvas的,比如 Firefox, safari, chrome...
    空谷悠閱讀 830評(píng)論 0 1
  • ## 功能 在網(wǎng)頁(yè)中實(shí)時(shí)生成圖像,并操作圖像內(nèi)容(通俗就是在網(wǎng)頁(yè)中畫畫) 1.canvas畫布可以制作在線繪圖工具...
    a8f90e785c29閱讀 549評(píng)論 0 0