利用Canvas模仿百度貼吧客戶端loading小球的方法示例
前言
最近看到兩個好玩的 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 坐標(biāo)
var cy = canvas.height / 2 // 圓中心點 y 坐標(biāo)
function circle() {
ctx.beginPath()
ctx.arc(cx, cy, grid / 2, 0, 2 * Math.PI)
}
circle()
ctx.stroke()
</script>
</body>
</html>

這個 demo 只涉及 Canvas 最簡單的用法。
第一步:繪制藍(lán)色的“貼”字
使用 ctx.fillText,在圓的中心繪制一個藍(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)建了一個波浪形狀的路徑,和第三步不同的是,這里并沒有使用 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 坐標(biāo)
var cy = canvas.height / 2 // 圓中心點 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í)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章

html5結(jié)合Canvas實現(xiàn)發(fā)光進(jìn)度條loading動畫特效源碼
html5結(jié)合Canvas實現(xiàn)發(fā)光進(jìn)度條loading動畫特效源碼是一段實現(xiàn)了loading進(jìn)度條在加載的過程中持續(xù)發(fā)光的效果代碼,本段代碼適應(yīng)于所有網(wǎng)頁使用,有興趣的朋友們歡迎前來下2017-04-20
HTML5結(jié)合Canvas制作的圓形百分比進(jìn)度條Loading效果源碼
是一段實現(xiàn)了圓形百分比進(jìn)度條Loading效果代碼,本段代碼適應(yīng)于所有網(wǎng)頁使用,有需要的朋友們可以前來下載使用2016-06-15
HTML5基于Canvas實現(xiàn)超酷Loading動畫特效源碼
這次我們來看一款非??岬腖oading動畫加載效果,主要是利用幾何的特性來模擬的,它是一款旋轉(zhuǎn)的動畫效果。可實現(xiàn)伸縮旋轉(zhuǎn)的視覺效果2014-12-23
HTML5 Canvas繪制超級漂亮的發(fā)光Loading動畫
HTML5 Canvas相當(dāng)于一個畫板,你可以在Canvas繪制任意的東西,今天要分享一款利用HTML5 Canvas繪制的loading動畫效果,非常的漂亮2015-12-09
HTML5 Canvas實現(xiàn)的發(fā)光Loading動畫特效源碼
這是一款基于HTML5 Canvas實現(xiàn)的發(fā)光Loading加載動畫特效源碼。Loading旋轉(zhuǎn)圖標(biāo)是在canvas畫布上繪制的,整個loading動畫是發(fā)光3D的視覺效果2014-07-04






