動態粒子背景效果_艾孜爾江撰

HTML代碼

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        html {
            height: 100%
        }
        
        body {
            margin: 0;
            height: 100%;
            background: #fff;
        }
        
        canvas {
            display: block;
            width: 100%;
            height: 100%;
        }
        
        .body_content {
            position: absolute;
            top: 30%;
            left: 20%;
            height: 20%;
            background: palevioletred;
            width: 20%;
        }
    </style>
</head>

<body>
    <canvas id="canvas"></canvas>
    <script src="./index.js"></script>

</body>

</html>

JS代碼

/**
 * Created by Alexander on 2020/5/30.
 */
class Circle {
    //創建對象
    //以一個圓爲對象
    //設置隨機的 x,y座標,r半徑,_mx,_my移動的距離
    //this.r是創建圓的半徑,參數越大半徑越大
    //this._mx,this._my是移動的距離,參數越大移動
    constructor(x, y) {
        this.x = x;
        this.y = y;
        this.r = Math.random() * 10;
        this._mx = Math.random();
        this._my = Math.random();

    }

    //canvas 畫圓和畫直線
    //畫圓就是正常的用canvas畫一個圓
    //畫直線是兩個圓連線,爲了避免直線過多,給圓圈距離設置了一個值,距離很遠的圓圈,就不做連線處理
    drawCircle(ctx) {
        ctx.beginPath();
        //arc() 方法使用一箇中心點和半徑,爲一個畫布的當前子路徑添加一條弧。
        ctx.arc(this.x, this.y, this.r, 0, 360)
        ctx.closePath();
        ctx.fillStyle = 'rgba(204, 204, 204, 0.3)';
        ctx.fill();
    }

    drawLine(ctx, _circle) {
        let dx = this.x - _circle.x;
        let dy = this.y - _circle.y;
        let d = Math.sqrt(dx * dx + dy * dy)
        if (d < 150) {
            ctx.beginPath();
            //開始一條路徑,移動到位置 this.x,this.y。創建到達位置 _circle.x,_circle.y 的一條線:
            ctx.moveTo(this.x, this.y); //起始點
            ctx.lineTo(_circle.x, _circle.y); //終點
            ctx.closePath();
            ctx.strokeStyle = 'rgba(204, 204, 204, 0.3)';
            ctx.stroke();
        }
    }

    // 圓圈移動
    // 圓圈移動的距離必須在屏幕範圍內
    move(w, h) {
        this._mx = (this.x < w && this.x > 0) ? this._mx : (-this._mx);
        this._my = (this.y < h && this.y > 0) ? this._my : (-this._my);
        this.x += this._mx / 2;
        this.y += this._my / 2;
    }
}
//鼠標點畫圓閃爍變動
class currentCirle extends Circle {
    constructor(x, y) {
        super(x, y);
    }

    drawCircle(ctx) {
        ctx.beginPath();
        //註釋內容爲鼠標焦點的地方圓圈半徑變化
        //this.r = (this.r < 14 && this.r > 1) ? this.r + (Math.random() * 2 - 1) : 2;
        this.r = 8;
        ctx.arc(this.x, this.y, this.r, 0, 360);
        ctx.closePath();
        //ctx.fillStyle = 'rgba(0,0,0,' + (parseInt(Math.random() * 100) / 100) + ')'
        ctx.fillStyle = 'rgba(255, 77, 54, 0.6)'
        ctx.fill();

    }
}
//更新頁面用requestAnimationFrame替代setTimeout
window.requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;

let canvas = document.getElementById('canvas');
let ctx = canvas.getContext('2d');
let w = canvas.width = canvas.offsetWidth;
let h = canvas.height = canvas.offsetHeight;
let circles = [];
let current_circle = new currentCirle(0, 0);

let draw = function() {
    ctx.clearRect(0, 0, w, h);
    for (let i = 0; i < circles.length; i++) {
        circles[i].move(w, h);
        circles[i].drawCircle(ctx);
        for (j = i + 1; j < circles.length; j++) {
            circles[i].drawLine(ctx, circles[j]);
        }
    }
    if (current_circle.x) {
        current_circle.drawCircle(ctx);
        for (var k = 1; k < circles.length; k++) {
            current_circle.drawLine(ctx, circles[k]);
        }
    }
    requestAnimationFrame(draw);
}

let init = function(num) {
    for (var i = 0; i < num; i++) {
        circles.push(new Circle(Math.random() * w, Math.random() * h));
    }
    draw();
}
window.addEventListener('load', init(60));
window.onmousemove = function(e) {
    e = e || window.event;
    current_circle.x = e.clientX;
    current_circle.y = e.clientY;
}
window.onmouseout = function() {
    current_circle.x = null;
    current_circle.y = null;
};

最終效果

動態粒子背景

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