佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > 利用Canvas模仿百度貼吧客戶端loading小球的方法示例

利用Canvas模仿百度貼吧客戶端loading小球的方法示例

熱門標(biāo)簽:團(tuán)購網(wǎng)站 企業(yè)做大做強(qiáng) 太平洋壽險(xiǎn)電話營銷 科大訊飛語音識別系統(tǒng) 網(wǎng)站排名優(yōu)化 網(wǎng)站文章發(fā)布 國美全國運(yùn)營中心 百度競價(jià)排名

前言

最近看到兩個(gè)好玩的 demo,效果圖如下:

今天趁著周末有空,用 H5 的 Canvas 仿了一下。這篇文章只實(shí)現(xiàn)第一個(gè)效果圖。
 

這是我實(shí)現(xiàn)的效果:

實(shí)現(xiàn)原理

實(shí)現(xiàn)原理是參考簡書的那篇文章,這里不再復(fù)述?,F(xiàn)在我們來一步一步實(shí)現(xiàn)這樣的效果。

第零步:畫一個(gè)圓

源碼如下:

運(yùn)行效果如下:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>百度貼吧客戶端Loading小球</title>
    <style>
        canvas {
            border: 1px solid #ccc;
        }
    </style>
</head>
<body>
<canvas id="canvas" width="500" height="500"></canvas>
<script>
    var canvas = document.getElementById('canvas')
    var ctx = canvas.getContext('2d')
    canvas.width = 500
    canvas.height = 500

    var grid = canvas.width / 4
    var cx = canvas.width / 2 // 圓中心點(diǎn) x 坐標(biāo)
    var cy = canvas.height / 2 // 圓中心點(diǎn) y 坐標(biāo)

    function circle() {
        ctx.beginPath()
        ctx.arc(cx, cy, grid / 2, 0, 2 * Math.PI)
    }

    circle()
    ctx.stroke()
</script>
</body>
</html>

這個(gè) demo 只涉及 Canvas 最簡單的用法。

第一步:繪制藍(lán)色的“貼”字

使用 ctx.fillText,在圓的中心繪制一個(gè)藍(lán)色的“帖”字。文字粗體、水平居中。

代碼如下:

function text(fillStyle) {
    var fontSize = size / 250 * 120
    ctx.font = 'bold ' + fontSize + 'px Arial'
    ctx.textAlign = 'center'
    ctx.fillStyle = fillStyle
    ctx.fillText('貼', cx, cy + fontSize * 0.3)
}

text('#29a3fe')

效果如下:

第二步:繪制藍(lán)色的波浪

var waveSize = size / 6 // 波浪大小
var x = 0 // 波浪位置偏移大小

function curve() {
    ctx.beginPath()
    ctx.moveTo(cx - size + x + size / 2, cy)
    ctx.quadraticCurveTo(cx - size + size / 4 + x + size / 2, cy - waveSize, cx - size + size / 2 + x + size / 2, cy)
    ctx.quadraticCurveTo(cx - size + size * 3 / 4 + x + size / 2, cy + waveSize, cx - size + size + x + size / 2, cy)

    ctx.quadraticCurveTo(cx + size / 4 + x + size / 2, cy - waveSize, cx + size / 2 + x + size / 2, cy)
    ctx.quadraticCurveTo(cx + size * 3 / 4 + x + size / 2, cy + waveSize, cx + size + x + size / 2, cy)
    ctx.lineTo(cx + size + x + size / 2, canvas.height)
    ctx.lineTo(cx - size + x + size / 2, canvas.height)
    ctx.lineTo(cx - size + x + size / 2, cy)
    ctx.closePath()
}

ctx.fillStyle = '#29a3fe'
curve()
ctx.fill()

效果如下:

第三步:繪制白色的“貼”字

curve()
ctx.clip()
text('#f00')

第一句代碼 curve() 創(chuàng)建了一個(gè)波浪形狀的路徑,和第三步不同的是,這里并沒有使用 ctx.fill() 填充路徑,而是使用了 ctx.clip() 裁剪路徑,這樣的話,后面繪制的路徑(包括文字)只有在剪裁區(qū)域內(nèi)才能顯示。

為了和背景色區(qū)分開來,我把“貼”字改成紅色。

效果如下:

第四步:繪制運(yùn)動的波浪

function loop(){
    ctx.clearRect(0, 0, canvas.width, canvas.height)

    x -= 1.5
    x = x % size

    ctx.save()

    circle()
    ctx.stroke()
 
    ctx.fillStyle = '#29a3fe'
    curve()
    ctx.fill()

    ctx.restore()

    requestAnimationFrame(loop)
}
loop()

效果如下:

第五步:整合前面的內(nèi)容

效果如下:

第六步:剪裁圓形

把第零步的:

circle()
ctx.stroke()

改成:

circle()
ctx.clip()

這樣就能把圓形外面的形狀剪裁掉,然后就大功告成了。

最后,附上完整源碼:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        html,
        body {
            height: 100%;
        }
        canvas {
            border: 1px solid #ccc;
        }
    </style>
</head>
<body>
<canvas id="canvas" width="500" height="500"></canvas>
<script>
    var canvas = document.getElementById('canvas')
    var ctx = canvas.getContext('2d')
    canvas.width = 500
    canvas.height = 500

    var size = canvas.width / 4 // 圓的大小
    var cx = canvas.width / 2 // 圓中心點(diǎn) x 坐標(biāo)
    var cy = canvas.height / 2 // 圓中心點(diǎn) y 坐標(biāo)
    var waveSize = size / 6 // 波浪大小
    var x = 0 // 波浪位置偏移大小

    function circle() {
        ctx.beginPath()
        ctx.arc(cx, cy, size / 2, 0, 2 * Math.PI)
    }

    function curve() {
        ctx.beginPath()
        ctx.moveTo(cx - size + x + size / 2, cy)
        ctx.quadraticCurveTo(cx - size + size / 4 + x + size / 2, cy - waveSize, cx - size + size / 2 + x + size / 2, cy)
        ctx.quadraticCurveTo(cx - size + size * 3 / 4 + x + size / 2, cy + waveSize, cx - size + size + x + size / 2, cy)

        ctx.quadraticCurveTo(cx + size / 4 + x + size / 2, cy - waveSize, cx + size / 2 + x + size / 2, cy)
        ctx.quadraticCurveTo(cx + size * 3 / 4 + x + size / 2, cy + waveSize, cx + size + x + size / 2, cy)
        ctx.lineTo(cx + size + x + size / 2, canvas.height)
        ctx.lineTo(cx - size + x + size / 2, canvas.height)
        ctx.lineTo(cx - size + x + size / 2, cy)
        ctx.closePath()
    }

    function text(fillStyle) {
        var fontSize = size / 250 * 120
        ctx.font = 'bold ' + fontSize + 'px Arial'
        ctx.textAlign = 'center'
        ctx.fillStyle = fillStyle
        ctx.fillText('貼', cx, cy + fontSize * 0.3)
    }

    function loop(){
        ctx.clearRect(0, 0, canvas.width, canvas.height)

        x -= 1.5
        x = x % size

        ctx.save()

        circle()
        ctx.clip()

        text('#29a3fe')

        ctx.fillStyle = '#29a3fe'
        curve()
        ctx.fill()

        curve()
        ctx.clip()

        text('#fff')

        ctx.restore()

        requestAnimationFrame(loop)
    }
    loop()
</script>
</body>
</html>

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
 

標(biāo)簽:林芝 保定 赤峰 大同 延邊 泰州 萍鄉(xiāng)

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《利用Canvas模仿百度貼吧客戶端loading小球的方法示例》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    揭西县| 营山县| 南陵县| 元阳县| 涟水县| 临邑县| 泰顺县| 深水埗区| 瑞丽市| 乡城县| 周至县| 连州市| 阳原县| 江门市| 封开县| 正阳县| 黔西县| 报价| 广水市| 陵川县| 安康市| 正阳县| 崇左市| 上栗县| 个旧市| 昌都县| 普兰店市| 东乡县| 嵊州市| 上栗县| 隆化县| 静乐县| 双江| 宜君县| 潼南县| 会泽县| 蕲春县| 公主岭市| 车险| 临安市| 茶陵县|