h5 canvas 仿知乎動態粒子背景

本文首發於我的個人博客:http://cherryblog.site/ 
github項目地址:https://github.com/sunshine940326/canvas-nest

首先上效果圖: 
最終效果圖
實現起來也是很簡單的, 按照我的步驟一步一步來就可以了~

html代碼

首先要製作我們的頁面,用到的是html5的新標籤canvas;其實canvas就是我們需要用javascript腳本語言來繪圖的“畫布”,只是相當於一個容器呈現我們畫圖的結果,所以我們在頁面中需要創建一個充滿屏幕的canvas

<body>
   <canvas id="canvas"></canvas>
   <div class="text">仿知乎動態粒子效果背景</div>
</body>
  • 1
  • 2
  • 3
  • 4

是的,body中只有這兩行代碼就可以了,甚至可以只有一行代碼

css樣式

css樣式也沒有什麼好說的,只是要讓canvas充滿屏幕就可以了

html{height: 100%}
        body{margin: 0;height: 100%;
            background: #fff;}
        canvas{display: block;width: 100%;height: 100%;}
        .text{
            width: 100%;
            background: transparent;
            display: flex;
            justify-content: center;
            height: 100%;
            line-height: 100%;
            top: 0;
            position: absolute;
            top: 50%;
            font-size: 50px;
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

寫法不唯一,只要要你的canvas是充滿整個屏幕的就好,當然,你要是不需要充滿屏幕也可以~

js代碼

說完了html和css,那麼就剩js了,主要是通過js腳本來創建每個線段和粒子的~github上的例子中使用的是es6編寫的,不過在demo中也使用了gulp安裝babel可以將es6代碼轉化爲es5(所以索demo中同時有es6的代碼也有es5的代碼,大家按需下載~)主要的思路如下

  1. 設置單個粒子的隨機x,y座標和圓圈的半徑
  2. 使用canvas的api進行繪製粒子(圓圈)和粒子之前連線,設置一個範圍,在此範圍內的粒子圓心到圓心通過直線連接
  3. 讓粒子在屏幕範圍內移動
  4. 設置鼠標的交互事件,相當於以鼠標位置的x,y座標爲圓心,固定或隨機值爲半徑重新創建了一個粒子,並且也在一定範圍內也設置和其他粒子的連線(同第二步) 
    其實思路就以上五點,只不過我們需要了解canvas的api才能繪出我們想要的結果

設置單個粒子的隨機x,y座標和圓圈的半徑

//創建對象
    //以一個圓爲對象
    //設置隨機的 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() ;

    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

canvas 畫圓和畫直線

 //canvas 畫圓和畫直線
    //畫圓就是正常的用canvas畫一個圓
    //畫直線是兩個圓連線,爲了避免直線過多,給圓圈距離設置了一個值,距離很遠的圓圈,就不做連線處理
    drawCircle(ctx) {
        // beginPath() 方法開始一條路徑,或重置當前的路徑
        ctx.beginPath();   
        //arc() 方法使用一箇中心點和半徑,爲一個畫布的當前子路徑添加一條弧。
        ctx.arc(this.x, this.y, this.r, 0, 360)
        //closePath() 方法創建從當前點到開始點的路徑。
        ctx.closePath();
        //fillStyle()方法設置或返回用於填充繪畫的顏色、漸變或模式。
        ctx.fillStyle = 'rgba(204, 204, 204, 0.3)';
        //fill()方法  填充當前繪圖(路徑)
        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)
        //設置粒子圓心之間連線的範圍爲150
        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();
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

粒子移動

// 粒子移動
    // 圓圈移動的距離必須在屏幕範圍內
    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;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

完整js


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;

};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116

更多canvas的api

canvas現在可以寫出很多酷炫的效果,詳細的api請見:http://www.runoob.com/jsref/dom-obj-canvas.html

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