P5.js : 粒子頁面特效

P5.js可以給整個頁面帶來創新,不受限於畫布中作圖,而是把整個瀏覽器作爲畫布。

引入其CDN:

<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.0.0/p5.js"></script>

簡單使用:

 


 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>粒子特效</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.0.0/p5.js"></script>
    <style>
        body {
            margin: 0;
            background: #34495e;
            overflow: hidden;
        }
        h1 {
            color: #fff;
            font-size: 60px;
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%,-50%);
            text-align: center;
            margin: 0;
        }

    </style>
</head>
<body>
    
    <h1>I am YinleiCoder</h1>

    <script>
        // let p;
        const particles =  [];

        // p5js自動調用1次
        function setup() {
            // 創建畫布
            createCanvas(window.innerWidth, window.innerHeight);

            // p = new Particle();
            const particlesLength = Math.floor(window.innerWidth / 10);// 粒子數量

            for(let i=0;i<particlesLength;i++) {
                particles.push(new Particle());
            }
        }

        // p5js自動循環調用
        function draw() {
            background('#34495e');
            // p.customUpdate();
            // p.customDraw();
            particles.forEach((p,index) => {
                p.customUpdate();
                p.customDraw();
                p.checkParticles(particles.slice(index));
            });
        }

        // 粒子
        class Particle {
            constructor() {
                // 生成每個粒子的位置
                this.pos = createVector(random(width),random(height));
                // 粒子的大小
                this.size = 10;
                // 移動速度
                this.vel = createVector(random(-5,5),random(-5,5));
            }

            // 繪製圖形
            customDraw() {
                // 繪製單個粒子
                noStroke();
                fill('#1abc9c');
                circle(this.pos.x,this.pos.y,this.size);
            }

            // 更新動畫
            customUpdate(){
                this.pos.add(this.vel);
                this.customEdges();
            }

            // 檢測邊緣
            customEdges() {
                if(this.pos.x < 0 || this.pos.x > width) {
                    this.vel.x *= -1;
                }
                if(this.pos.y < 0 || this.pos.y > height) {
                    this.vel.y *= -1;
                }
            }

            // 粒子連線
            checkParticles(particles) {
                particles.forEach(particle => {
                    // 距離矩陣來限制範圍
                    const d = dist(this.pos.x, this.pos.y,particle.pos.x,particle.pos.y);

                    if(d < 50){
                        stroke('#3498db');
                        line(this.pos.x, this.pos.y,particle.pos.x,particle.pos.y);
                    }
                })
            }
        }
    </script>
</body>
</html>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章