先來個引子吧:JParticles
(JavaScript Particles 的縮寫)是一款基於 Canvas
的不依賴於其餘庫的輕量級 JavaScript
粒子運動特效庫。提供一些比較絢麗、實用的特效應用於 WEB
界面,但願能達到錦上添花的做用,給用戶帶來些許驚喜。git
做爲一個更新稍加不快的開源項目,JParticles 發佈 3.0 了喲,主要更新的點以下:github
下面咱們先來瞅瞅新增的遮罩功能帶來的酷酷的效果吧。chrome
常規遮罩最適用於純文字的 Logo,像 GitHub 的文字 Logo 就很棒,看看噻:segmentfault
代碼很簡潔,只須要在 mask 字段上設置 Logo 圖片地址就好了:svg
new JParticles.Wave('#demo', { num: 1, fillColor: '#14c8ff', crestHeight: 8, offsetTop: 0.5, crestCount: 2, fill: true, line: false, speed: 0.1, // 遮罩設置爲 Logo 圖片地址 mask: 'https://raw.githubusercontent.com/Barrior/assets/main/github-logo-text.svg', })
小提示:SVG 可使圖片在 Mac OS 等多倍屏幕的顯示下依然清晰,推薦。動畫
在線編輯代碼,體驗效果:https://codepen.io/barrior/pe...spa
感受也許這個名字可能適合吧「苦笑臉.jpg」。
幽靈遮罩(Ghost Mask):灰度化遮罩圖片做爲背景,再裁剪出原始彩色圖案。
該遮罩最適用於彩色的圖像的 Logo,像 Chrome 的 Logo 使用該加載效果就看起來特別棒了:rest
代碼很簡潔,只須要改變遮罩模式便可:日誌
new JParticles.Wave('#demo', { num: 2, line: false, fill: true, fillColor: 'rgba(0, 0, 0, 0.15)', offsetTop: 0.5, offsetLeft: [0, 1.5], crestHeight: 5, crestCount: 2, speed: 0.2, // 設置遮罩圖片 mask: 'https://raw.githubusercontent.com/Barrior/assets/main/chrome-logo.svg', // 設置遮罩模式爲「幽靈模式」 maskMode: 'ghost' })
在線編輯代碼,體驗效果:https://codepen.io/barrior/pen/GRrpermcode
Wave 是單純的效果,適用於展現實際進度值,而 WaveLoading 則是對它的封裝,用於平常的模擬進度的加載。
看看幽靈模式下的模擬加載是什麼效果吧:
代碼以下:
new JParticles.WaveLoading('#demo', { // Wave 效果的參數 num: 2, fillColor: 'rgba(0, 0, 0, 0.15)', offsetLeft: [0, 1.5], crestHeight: 5, crestCount: 2, speed: 0.22, mask: 'https://raw.githubusercontent.com/Barrior/assets/main/chrome-logo.svg', maskMode: 'ghost', // WaveLoading 特有的加載進度的參數 textColor: '#fff', // 進度文本顏色 textFormatter: '加載中...%d%', // 進度文本模板 duration: 8000, // 進度持續時長 })
在線編輯代碼,體驗效果:https://codepen.io/barrior/pen/WNRQWVr
如同更新日誌所述,也許在特定場景能夠起到點綴的做用吧,效果示例:
代碼以下:
new JParticles.Line('#demo', { // 最大傾斜角度 maxDegree: 70, // 最小傾斜角度 minDegree: 70, })
在線編輯代碼,體驗效果:https://codepen.io/barrior/pen/WNRQqXK