JS庫(kù)之ParticlesJS使用簡(jiǎn)介
particles.js
A lightweight JavaScript library for creating particles.
一個(gè)輕量級(jí)的創(chuàng)建粒子背景的 JavaScript 庫(kù)
我們先來(lái)看一下效果圖:
標(biāo)準(zhǔn)版:
星空版:
泡泡版:
下雪版:
利用這個(gè)我們可以做些什么呢?
我覺(jué)得這個(gè)比較適合用在無(wú)背景的頁(yè)面,或者說(shuō)找不到合適的圖片來(lái)做背景,那么,我們都可以使用這個(gè)。
比如說(shuō):
或者
嗯,效果都挺不錯(cuò)的。
那么,下面將介紹如何使用 particles.js 。
particlesJS 開(kāi)源在Github上:https://github.com/VincentGarreau/particles.js
這個(gè)項(xiàng)目中有提供demo,可以直接下載這個(gè)項(xiàng)目,打開(kāi)demo里面的index.html文件,即可看到效果。
那么,如果我們要構(gòu)建自己的項(xiàng)目,該如何引入文件呢?
建議如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>particles.js</title> <meta name="description" content="particles.js is a lightweight JavaScript library for creating particles."> <meta name="author" content="Vincent Garreau" /> <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"> <link rel="stylesheet" media="screen" href="css/style.css" rel="external nofollow" > </head> <body> <div id="particles-js"></div> <!-- scripts --> <script src="js/particles.js"></script> <script src="js/app.js"></script> </body> </html>
particles.js 是它的庫(kù),我們肯定是要引入的,app.js 是參數(shù)配置文件,我們也要引入,而 demo 中的 stats.js 就沒(méi)有必要引入了。
style.css 我們也可以引入,背景顏色是在css中設(shè)置的。
基于這個(gè)模板,我們可以在上面添加我們想要實(shí)現(xiàn)的功能,比如說(shuō)注冊(cè)登錄功能,需要注意的是:
用 div 來(lái)封裝我們要實(shí)現(xiàn)的功能代碼塊,并且在 css 中為這個(gè) div 設(shè)置絕對(duì)定位。
下面介紹參數(shù)配置文件 app.js 文件的使用:
particles.number.value : 粒子的數(shù)量
particles.number.density : 粒子的稀密程度
particles.number.density.enable : 啟用粒子的稀密程度 (true 或 false)
particles.number.density.value_area : 每一個(gè)粒子占據(jù)的空間(啟用粒子密度,才可用)
particles.color.value : 粒子的顏色 (支持16進(jìn)制”#b61924”,rgb”{r:182, g:25, b:36}”,hsl,以及random)
particles.shape.type: 粒子的形狀 (”circle” “edge” “triangle” “polygon” “star” “image”)
particles.opacity.value: 粒子的透明度
particles.size.anim.enable: 是否啟用粒子速度(true/false)
particles.size.anim.speed: 粒子動(dòng)畫(huà)頻率
particles.size.anim.sync: 粒子運(yùn)行速度與動(dòng)畫(huà)是否同步
particles.move.speed: 粒子移動(dòng)速度
大家可以根據(jù)這些配置文件,配置自己喜歡的背景出來(lái),下面提供兩份完整配置文件 app.js。
配置文件一(經(jīng)典背景):
{ "particles": { "number": { "value": 80, "density": { "enable": true, "value_area": 800 } }, "color": { "value": "#ffffff" }, "shape": { "type": "polygon", "stroke": { "width": 0, "color": "#000000" }, "polygon": { "nb_sides": 5 }, "image": { "src": "img/github.svg", "width": 100, "height": 100 } }, "opacity": { "value": 0.5, "random": false, "anim": { "enable": false, "speed": 1, "opacity_min": 0.1, "sync": false } }, "size": { "value": 3, "random": true, "anim": { "enable": false, "speed": 40, "size_min": 0.1, "sync": false } }, "line_linked": { "enable": true, "distance": 150, "color": "#ffffff", "opacity": 0.4, "width": 1 }, "move": { "enable": true, "speed": 6, "direction": "none", "random": false, "straight": false, "out_mode": "out", "bounce": false, "attract": { "enable": false, "rotateX": 600, "rotateY": 1200 } } }, "interactivity": { "detect_on": "canvas", "events": { "onhover": { "enable": true, "mode": "repulse" }, "onclick": { "enable": true, "mode": "push" }, "resize": true }, "modes": { "grab": { "distance": 400, "line_linked": { "opacity": 1 } }, "bubble": { "distance": 400, "size": 40, "duration": 2, "opacity": 8, "speed": 3 }, "repulse": { "distance": 200, "duration": 0.4 }, "push": { "particles_nb": 4 }, "remove": { "particles_nb": 2 } } }, "retina_detect": false }
配置文件二(星空背景):
{ "particles": { "number": { "value": 160, "density": { "enable": true, "value_area": 800 } }, "color": { "value": "#ffffff" }, "shape": { "type": "circle", "stroke": { "width": 0, "color": "#000000" }, "polygon": { "nb_sides": 5 }, "image": { "src": "img/github.svg", "width": 100, "height": 100 } }, "opacity": { "value": 1, "random": true, "anim": { "enable": true, "speed": 1, "opacity_min": 0, "sync": false } }, "size": { "value": 3, "random": true, "anim": { "enable": false, "speed": 4, "size_min": 0.3, "sync": false } }, "line_linked": { "enable": false, "distance": 150, "color": "#ffffff", "opacity": 0.4, "width": 1 }, "move": { "enable": true, "speed": 1, "direction": "none", "random": true, "straight": false, "out_mode": "out", "bounce": false, "attract": { "enable": false, "rotateX": 600, "rotateY": 600 } } }, "interactivity": { "detect_on": "canvas", "events": { "onhover": { "enable": true, "mode": "bubble" }, "onclick": { "enable": true, "mode": "repulse" }, "resize": true }, "modes": { "grab": { "distance": 400, "line_linked": { "opacity": 1 } }, "bubble": { "distance": 250, "size": 0, "duration": 2, "opacity": 0, "speed": 3 }, "repulse": { "distance": 400, "duration": 0.4 }, "push": { "particles_nb": 4 }, "remove": { "particles_nb": 2 } } }, "retina_detect": true }
總結(jié)
以上所述是小編給大家介紹的ParticlesJS使用簡(jiǎn)介,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
javascript設(shè)計(jì)模式 – 工廠模式原理與應(yīng)用實(shí)例分析
這篇文章主要介紹了javascript設(shè)計(jì)模式 – 工廠模式,結(jié)合實(shí)例形式分析了javascript工廠模式基本概念、原理、定義、應(yīng)用場(chǎng)景及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2020-04-04javascript 動(dòng)態(tài)添加事件代碼
往往我們需要在 JS 中動(dòng)態(tài)添加事件,這就涉及到瀏覽器兼容性問(wèn)題了,以下談及的幾種方法,我們也常?;旌鲜褂?。2008-11-11js實(shí)現(xiàn)頁(yè)面轉(zhuǎn)發(fā)功能示例代碼
本文為大家介紹的是使用js實(shí)現(xiàn)頁(yè)面轉(zhuǎn)發(fā),具體實(shí)現(xiàn)如下,感興趣的朋友可以參考下,希望對(duì)大家有所幫助2013-08-08JS解決IOS中拍照?qǐng)D片預(yù)覽旋轉(zhuǎn)90度BUG的問(wèn)題
下面小編就為大家?guī)?lái)一篇JS解決IOS中拍照?qǐng)D片預(yù)覽旋轉(zhuǎn)90度BUG的問(wèn)題。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09js獲取數(shù)組最后一位元素的五種方法及執(zhí)行效率對(duì)比
js獲取數(shù)組最后一位元素的五種方法代碼示例,使用console.time和console.timeEnd測(cè)量javascript腳本程序執(zhí)行效率對(duì)比2023-08-08基于JavaScript制作一個(gè)簡(jiǎn)單的天氣應(yīng)用
隨著Web開(kāi)發(fā)技術(shù)的不斷發(fā)展,JavaScript已經(jīng)成為前端開(kāi)發(fā)中不可或缺的一部分,下面我們就來(lái)看看如何使用JavaScript實(shí)現(xiàn)一個(gè)簡(jiǎn)單的天氣應(yīng)用吧2024-01-01wap手機(jī)圖片滑動(dòng)切換特效無(wú)css3元素js腳本編寫(xiě)
手機(jī)圖片滑動(dòng)切換,網(wǎng)上有很多這樣的例子,但都借助于其他組件,讓代碼混亂的不行,本例無(wú)css3元素js腳本編寫(xiě),需要的朋友可以參考下2014-07-07