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

前言
最近看到兩個(gè)好玩的 demo,效果圖如下:
今天趁著周末有空,用 H5 的 Canvas 仿了一下。這篇文章只實(shí)現(xiàn)第一個(gè)效果圖。
這是我實(shí)現(xiàn)的效果:
實(shí)現(xiàn)原理
實(shí)現(xiàn)原理是參考簡(jiǎn)書的那篇文章,這里不再?gòu)?fù)述?,F(xiàn)在我們來(lái)一步一步實(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 最簡(jiǎn)單的用法。
第一步:繪制藍(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è)波浪形狀的路徑,和第三步不同的是,這里并沒(méi)有使用 ctx.fill()
填充路徑,而是使用了 ctx.clip()
裁剪路徑,這樣的話,后面繪制的路徑(包括文字)只有在剪裁區(qū)域內(nèi)才能顯示。
為了和背景色區(qū)分開(kāi)來(lái),我把“貼”字改成紅色。
效果如下:
第四步:繪制運(yùn)動(dòng)的波浪
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é)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
html5結(jié)合Canvas實(shí)現(xiàn)發(fā)光進(jìn)度條loading動(dòng)畫特效源碼
html5結(jié)合Canvas實(shí)現(xiàn)發(fā)光進(jìn)度條loading動(dòng)畫特效源碼是一段實(shí)現(xiàn)了loading進(jìn)度條在加載的過(guò)程中持續(xù)發(fā)光的效果代碼,本段代碼適應(yīng)于所有網(wǎng)頁(yè)使用,有興趣的朋友們歡迎前來(lái)下2017-04-20HTML5結(jié)合Canvas制作的圓形百分比進(jìn)度條Loading效果源碼
是一段實(shí)現(xiàn)了圓形百分比進(jìn)度條Loading效果代碼,本段代碼適應(yīng)于所有網(wǎng)頁(yè)使用,有需要的朋友們可以前來(lái)下載使用2016-06-15HTML5基于Canvas實(shí)現(xiàn)超酷Loading動(dòng)畫特效源碼
這次我們來(lái)看一款非??岬腖oading動(dòng)畫加載效果,主要是利用幾何的特性來(lái)模擬的,它是一款旋轉(zhuǎn)的動(dòng)畫效果。可實(shí)現(xiàn)伸縮旋轉(zhuǎn)的視覺(jué)效果2014-12-23HTML5 Canvas繪制超級(jí)漂亮的發(fā)光Loading動(dòng)畫
HTML5 Canvas相當(dāng)于一個(gè)畫板,你可以在Canvas繪制任意的東西,今天要分享一款利用HTML5 Canvas繪制的loading動(dòng)畫效果,非常的漂亮2015-12-09HTML5 Canvas實(shí)現(xiàn)的發(fā)光Loading動(dòng)畫特效源碼
這是一款基于HTML5 Canvas實(shí)現(xiàn)的發(fā)光Loading加載動(dòng)畫特效源碼。Loading旋轉(zhuǎn)圖標(biāo)是在canvas畫布上繪制的,整個(gè)loading動(dòng)畫是發(fā)光3D的視覺(jué)效果2014-07-04