最近看到兩個好玩的demo,效果圖如下:
今天趁著周末有空,用H5 的Canvas 仿了一下。這篇文章只實現第一個效果圖。
這是我實現的效果:實現原理實現原理是參考簡書的那篇文章,這裡不再复述。現在我們來一步一步實現這樣的效果。
第零步:畫一個圓源碼如下:
運行效果如下:
<!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()創建了一個波浪形狀的路徑,和第三步不同的是,這裡並沒有使用ctx.fill()填充路徑,而是使用了ctx.clip()裁剪路徑,這樣的話,後面繪製的路徑(包括文字)只有在剪裁區域內才能顯示。
為了和背景色區分開來,我把貼字改成紅色。
效果如下:
第四步:繪製運動的波浪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()效果如下:
第五步:整合前面的內容效果如下:
第六步:剪裁圓形把第零步的:
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武林網的支持。