濮阳杆衣贸易有限公司

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

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

熱門標簽:濟南辦理400電話 鶴壁手機自動外呼系統(tǒng)怎么安裝 威海營銷外呼系統(tǒng)招商 中紳電銷智能機器人 跟電銷機器人做同事 農(nóng)村住宅地圖標注 鄭州電銷外呼系統(tǒng)違法嗎 漳州人工外呼系統(tǒng)排名 ai電銷機器人連接網(wǎng)關(guān)

前言

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

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

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

實現(xiàn)原理

實現(xiàn)原理是參考簡書的那篇文章,這里不再復(fù)述?,F(xiàn)在我們來一步一步實現(xià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 // 圓中心點 x 坐標
    var cy = canvas.height / 2 // 圓中心點 y 坐標

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

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

這個 demo 只涉及 Canvas 最簡單的用法。

第一步:繪制藍色的“貼”字

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

代碼如下:

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')

效果如下:

第二步:繪制藍色的波浪

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)建了一個波浪形狀的路徑,和第三步不同的是,這里并沒有使用 ctx.fill() 填充路徑,而是使用了 ctx.clip() 裁剪路徑,這樣的話,后面繪制的路徑(包括文字)只有在剪裁區(qū)域內(nèi)才能顯示。

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

效果如下:

第四步:繪制運動的波浪

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 // 圓中心點 x 坐標
    var cy = canvas.height / 2 // 圓中心點 y 坐標
    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í)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
 

標簽:咸陽 營口 甘南 文山 蘇州 萍鄉(xiāng) 紅河 惠州

巨人網(wǎng)絡(luò)通訊聲明:本文標題《利用Canvas模仿百度貼吧客戶端loading小球的方法示例》,本文關(guān)鍵詞  利用,Canvas,模仿,百度貼吧,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《利用Canvas模仿百度貼吧客戶端loading小球的方法示例》相關(guān)的同類信息!
  • 本頁收集關(guān)于利用Canvas模仿百度貼吧客戶端loading小球的方法示例的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    嘉定区| 丹巴县| 福州市| 中卫市| 常熟市| 驻马店市| 曲阜市| 璧山县| 大竹县| 天台县| 永年县| 寿阳县| 雷波县| 竹山县| 基隆市| 景谷| 河西区| 北流市| 石景山区| 洪泽县| 泉州市| 隆昌县| 留坝县| 呼玛县| 鸡东县| 新建县| 敦化市| 五河县| 郎溪县| 静乐县| 紫阳县| 多伦县| 宁强县| 新田县| 游戏| 江门市| 赤峰市| 新干县| 双峰县| 两当县| 察哈|