国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > HTML > 正文

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

2024-08-26 00:19:52
字體:
來源:轉載
供稿:網友

前言

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

客戶端,百度貼吧,canvas,loading

客戶端,百度貼吧,canvas,loading

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

這是我實現的效果:

客戶端,百度貼吧,canvas,loading

實現原理

實現原理是參考簡書的那篇文章,這里不再復述。現在我們來一步一步實現這樣的效果。

第零步:畫一個圓

源碼如下:

運行效果如下:

<!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>

客戶端,百度貼吧,canvas,loading

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

效果如下:

客戶端,百度貼吧,canvas,loading

第二步:繪制藍色的波浪

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

效果如下:

客戶端,百度貼吧,canvas,loading

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

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

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

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

效果如下:

客戶端,百度貼吧,canvas,loading

第四步:繪制運動的波浪

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

效果如下:

客戶端,百度貼吧,canvas,loading

第五步:整合前面的內容

效果如下:

客戶端,百度貼吧,canvas,loading

第六步:剪裁圓形

把第零步的:

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>

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對VeVb武林網的支持。
 


注:相關教程知識閱讀請移步到HTML教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 兴海县| 景宁| 武功县| 修文县| 克什克腾旗| 邵武市| 泸西县| 桃园市| 什邡市| 万州区| 哈尔滨市| 会东县| 东兰县| 怀宁县| 镇坪县| 望奎县| 罗平县| 大石桥市| 台前县| 广水市| 甘南县| 峨边| 张掖市| 大关县| 深泽县| 常宁市| 大石桥市| 封丘县| 牙克石市| 新津县| 青河县| 依兰县| 怀化市| 武定县| 云安县| 大余县| 宁城县| 衡阳市| 屯昌县| 平和县| 屏山县|