成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

Canvas 使用及應(yīng)用

jone5679 / 848人閱讀

摘要:的基本使用方法它是一個(gè)行內(nèi)塊元素默認(rèn)大小是,不能在里給他設(shè)置樣式,只能在標(biāo)簽內(nèi)寫(xiě)它的屬性。刮刮卡效果使用的圖形合成的屬性可以實(shí)現(xiàn)圖片合成的效果。具體應(yīng)用于刮刮卡。

Canvas

canvas 是 HTML5 當(dāng)中我最喜歡的所有新特性中我最喜歡的一個(gè)標(biāo)簽了。因?yàn)樗珡?qiáng)大了,各種有意思的特效都可以實(shí)現(xiàn)。

1. canvas 的基本使用方法
- 它是一個(gè)行內(nèi)塊元素
- 默認(rèn)大小是 300 x 150,不能在 css 里給他設(shè)置樣式,只能在標(biāo)簽內(nèi)寫(xiě)它的屬性。如 width = 400,height = 300
- 獲取畫(huà)布
    var canvas = document。querySelector("canvas")
- 獲取畫(huà)筆(上下文)
    var ctx = canvas.getContext("2d")
    
    
2. canvas 繪制基本的圖形
填充矩形
ctx.fillRect(0,0,100,100)
fill:跟填充有關(guān)
Rect: 描繪一個(gè)矩形

填充圖形設(shè)置樣式
ctx.fillStyle = "green"

描邊矩形
ctx.strokeRect(100,100,100,100)

描邊圖形設(shè)置樣式
ctx.strokeStyle = "white"
ctx.lineWidth = 100

清除整個(gè)畫(huà)布
ctx.clearRect(0,0,canvas.width,canvas.height)

畫(huà)線段
ctx.moveTo(100,100)
ctx.lineTo(100,100)

描邊
ctx.stroke()
填充
ctx.fill()-

起始點(diǎn)和結(jié)束點(diǎn)連接
ctx.closePath()

ctx.save()開(kāi)頭
    ......
ctx.restore()結(jié)尾
3. 畫(huà)布時(shí)鐘

使用畫(huà)布我們可以畫(huà)一個(gè)時(shí)鐘,包括刻度和時(shí)針,每一秒走的刻度可以用 Data 對(duì)象通過(guò)定時(shí)器來(lái)時(shí)時(shí)更新。

var canvas = document.querySelector("canvas");
    var ctx = canvas.getContext("2d");




    function move() {
        ctx.save()
            ctx.translate(300,300)
            //  初始化一些公共的樣式
            ctx.lineCap = "round"
            ctx.strokeStyle = "black"
            ctx.lineWidth = 8
            ctx.scale(0.5,0.5)

            // 畫(huà)外面的圓
            ctx.save();
                ctx.beginPath();
                ctx.strokeStyle = "gold";
                ctx.arc(0,0,150,0,2*Math.PI);
                ctx.stroke();
            ctx.restore();

            // 畫(huà)里面的刻度
            ctx.save()
                ctx.beginPath();
                for (var i=0; i < 12; i++) {
                    ctx.moveTo(0,-125);
                    ctx.lineTo(0,-140);
                    ctx.stroke()
                    ctx.rotate(30*Math.PI/180)
                }
            ctx.restore()

            // 分針刻度
            ctx.save()
                ctx.lineWidth = 3
                for (var i = 0; i < 60; i++) {
                    if (i % 5 != 0){
                        ctx.beginPath()
                        ctx.moveTo(0,-135);
                        ctx.lineTo(0,-140);
                        ctx.stroke()
                    }
                    ctx.rotate(6*Math.PI/180)
                }
            ctx.restore()
            // 當(dāng)前時(shí)間
            var date = new Date()
            var s = date.getSeconds()
            var min = date.getMinutes() + s/60
            var h = date.getHours() + min/60

            // 時(shí)針
            ctx.save()
                ctx.rotate(30*h*Math.PI/180)
                ctx.lineWidth = 14
                ctx.beginPath()
                ctx.moveTo(0,-80)
                ctx.lineTo(0,20)
                ctx.stroke()
            ctx.restore()

            // 分針
            ctx.save()
                ctx.lineWidth = 10
                ctx.rotate(6*min*Math.PI/180)
                ctx.beginPath()
                ctx.rotate(-30*Math.PI/180)
                ctx.moveTo(0,-120)
                ctx.lineTo(0,30)
                ctx.stroke()
            ctx.restore()

            //秒針
            ctx.save()
                ctx.lineWidth = 6
                ctx.strokeStyle = "pink"
                ctx.fillStyle = "pink"
                ctx.rotate(6*s*Math.PI/180)

                ctx.beginPath()
                ctx.arc(0,0,10,0,2*Math.PI)
                ctx.fill()

                ctx.beginPath()
                ctx.moveTo(0,-125)
                ctx.lineTo(0,30)
                ctx.stroke()

                ctx.beginPath()
                ctx.arc(0,-135,10,0,2*Math.PI)
                ctx.stroke()
            ctx.restore()
        ctx.restore()
    }

    setInterval(function () {
        ctx.clearRect(0,0,canvas.width,canvas.height)
        move()
    },1000)

靜止的圖像如下圖。

刮刮卡效果

使用 canvas 的圖形合成的屬性可以實(shí)現(xiàn)圖片合成的效果。具體應(yīng)用于刮刮卡。
globalCompositeOperation屬性設(shè)置或返回如何將一個(gè)源(新的)圖像繪制到目標(biāo)(已有)的圖像上
源圖像 = 您打算放置到畫(huà)布上的繪圖
目標(biāo)圖像 = 您已經(jīng)放置在畫(huà)布上的繪圖

var  canvas = document.querySelector("canvas")
    var ctx = getCtx()
    log(ctx)
    ctx.fillStyle = "yellow"
    ctx.fillRect(0,0,400,400)

    ctx.globalCompositeOperation = "destination-out";

    // 鼠標(biāo)按下
    canvas.onmousedown = function (event) {
        ctx.beginPath()
        ctx.arc(event.clientX - canvas.offsetLeft,event.clientY - canvas.offsetTop,
            20,0,2*Math.PI)
        ctx.fill()
        // 鼠標(biāo)移動(dòng)
        document.onmousemove = function (event) {
            ctx.beginPath()
            ctx.arc(event.clientX - canvas.offsetLeft,event.clientY - canvas.offsetTop,
            20,0,2*Math.PI)
            ctx.fill()
        }

        // 鼠標(biāo)抬起
        document.onmouseup = function () {
            document.onmousemove = document.onmouseup = null
        }
        return false
    }

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/53309.html

相關(guān)文章

  • canvas應(yīng)用一:簡(jiǎn)易時(shí)鐘

    摘要:時(shí)鐘的實(shí)現(xiàn)主要是應(yīng)用上下文的簡(jiǎn)單變換文本添加及周期性調(diào)用方法。在繪制表盤及時(shí)針過(guò)程注意使用及方法添加用以保存或返回上一個(gè)畫(huà)布設(shè)置屬性。思路編寫(xiě)兩個(gè)構(gòu)造函數(shù),分別代表表盤和時(shí)針,最后利用函數(shù)加以實(shí)現(xiàn)。 寫(xiě)在之前 canvas 元素中提供了看似簡(jiǎn)單的繪圖方法,但仔細(xì)挖掘,可以以此做出非常復(fù)雜而漂亮的圖形。隨著 API 的逐漸完善,我相信自己能進(jìn)行更多有意思的嘗試。 時(shí)鐘的 canvas ...

    whinc 評(píng)論0 收藏0
  • canvas應(yīng)用一:簡(jiǎn)易時(shí)鐘

    摘要:時(shí)鐘的實(shí)現(xiàn)主要是應(yīng)用上下文的簡(jiǎn)單變換文本添加及周期性調(diào)用方法。在繪制表盤及時(shí)針過(guò)程注意使用及方法添加用以保存或返回上一個(gè)畫(huà)布設(shè)置屬性。思路編寫(xiě)兩個(gè)構(gòu)造函數(shù),分別代表表盤和時(shí)針,最后利用函數(shù)加以實(shí)現(xiàn)。 寫(xiě)在之前 canvas 元素中提供了看似簡(jiǎn)單的繪圖方法,但仔細(xì)挖掘,可以以此做出非常復(fù)雜而漂亮的圖形。隨著 API 的逐漸完善,我相信自己能進(jìn)行更多有意思的嘗試。 時(shí)鐘的 canvas ...

    MobService 評(píng)論0 收藏0
  • 基于canvas實(shí)現(xiàn)的高性能、跨平臺(tái)的股票圖表庫(kù)--clchart

    摘要:什么是是一個(gè)基于創(chuàng)建的簡(jiǎn)單高性能和跨平臺(tái)的股票數(shù)據(jù)可視化開(kāi)源項(xiàng)目。支持以及和等平臺(tái)。而針對(duì)股票等有價(jià)證劵特定的圖表庫(kù)有和等項(xiàng)目,這些圖表庫(kù)對(duì)股票繪圖已經(jīng)做了一些非常專業(yè)的處理及優(yōu)化了,但是他們均基于來(lái)繪圖。 什么是 ClChart? ClChart是一個(gè)基于canvas創(chuàng)建的簡(jiǎn)單、高性能和跨平臺(tái)的股票數(shù)據(jù)可視化開(kāi)源項(xiàng)目。支持PC、webApp以及React Native和Weex等平臺(tái)...

    baihe 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<