實現強大的粒子線條效果

效果展示圖:


image.png

代碼:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>粒子線條canvas效果</title>
    <style>
    body{ background-color:#292929}
        #J_dotLine{
            display: block;
            margin: 0 auto;
        }
    </style>
</head>
<body>
    <!--
        **canvasAPI
            https://developer.mozilla.org/zh-CN/docs/Web/API/Canvas_API
        **大神教程
        1汰扭、http://www.cnblogs.com/axes/p/4960171.html
     -->
    <canvas id="J_dotLine" style="background-color: #F7F7F7;"></canvas>
    <script>
        function Dotline(option){
            this.opt = this.extend({
                dom:'J_dotLine',//畫布id
                cw:1000,//畫布寬
                ch:500,//畫布高
                ds:100,//點的個數
                r:0.5,//圓點半徑
                dis:100//觸發(fā)連線的距離
            },option);
            this.c = document.getElementById(this.opt.dom);//canvas元素id
            this.ctx = this.c.getContext('2d');
            this.c.width = this.opt.cw;//canvas寬
            this.c.height = this.opt.ch;//canvas高
            this.dotSum = this.opt.ds;//點的數量
            this.radius = this.opt.r;//圓點的半徑
            this.disMax = this.opt.dis*this.opt.dis;//點與點觸發(fā)連線的間距        
            this.dots = [];
            //requestAnimationFrame控制canvas動畫
            var RAF = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function(callback) {
                        window.setTimeout(callback, 1000 / 60);
                    };
            var _self = this;
            //增加鼠標效果
            var mousedot = {x:null,y:null,label:'mouse'};
            this.c.onmousemove = function(e){
                var e = e || window.event;
                mousedot.x = e.clientX - _self.c.offsetLeft;
                mousedot.y = e.clientY - _self.c.offsetTop;
            };
            this.c.onmouseout = function(e){
                mousedot.x = null;
                mousedot.y = null;
            }
            //控制動畫
            this.animate = function(){
                _self.ctx.clearRect(0, 0, _self.c.width, _self.c.height);
                _self.drawLine([mousedot].concat(_self.dots));
                RAF(_self.animate);
            };
        }
        //合并配置項东且,es6直接使用obj.assign();
        Dotline.prototype.extend = function(o,e){
            for(var key in e){
                if(e[key]){
                    o[key]=e[key]
                }
            }
            return o;
        };
        //畫點
        Dotline.prototype.addDots = function(){
            var dot;
            for(var i=0; i<this.dotSum; i++){//參數
                dot = {
                    x : Math.floor(Math.random()*this.c.width)-this.radius,
                    y : Math.floor(Math.random()*this.c.height)-this.radius,
                    ax : (Math.random() * 2 - 1) / 1.5,
                    ay : (Math.random() * 2 - 1) / 1.5
                }
                this.dots.push(dot);
            }
        };
        //點運動
        Dotline.prototype.move = function(dot){
            dot.x += dot.ax;
            dot.y += dot.ay;
            //點碰到邊緣返回
            dot.ax *= (dot.x>(this.c.width-this.radius)||dot.x<this.radius)?-1:1;
            dot.ay *= (dot.y>(this.c.height-this.radius)||dot.y<this.radius)?-1:1;
            //繪制點
            this.ctx.beginPath();
            this.ctx.arc(dot.x, dot.y, this.radius, 0, Math.PI*2, true);
            this.ctx.stroke();
        };
        //點之間畫線
        Dotline.prototype.drawLine = function(dots){
            var nowDot;
            var _that = this;
            //自己的思路:遍歷兩次所有的點珊泳,比較點之間的距離,函數的觸發(fā)放在animate里
            this.dots.forEach(function(dot){
                
                _that.move(dot);
                for(var j=0; j<dots.length; j++){
                    nowDot = dots[j];
                    if(nowDot===dot||nowDot.x===null||nowDot.y===null) continue;//continue跳出當前循環(huán)開始新的循環(huán)
                    var dx = dot.x - nowDot.x,//別的點坐標減當前點坐標
                        dy = dot.y - nowDot.y;
                    var dc = dx*dx + dy*dy;
                    if(Math.sqrt(dc)>Math.sqrt(_that.disMax)) continue;
                    // 如果是鼠標薯演,則讓粒子向鼠標的位置移動
                    if (nowDot.label && Math.sqrt(dc) >Math.sqrt(_that.disMax)/2) {
                        dot.x -= dx * 0.02;
                        dot.y -= dy * 0.02;
                    }
                    var ratio;
                    ratio = (_that.disMax - dc) / _that.disMax;

                    _that.ctx.beginPath();
                    _that.ctx.lineWidth = ratio / 2;
                    _that.ctx.strokeStyle = 'rgba(0,0,0,' + (ratio + 0.2) + ')';
                    _that.ctx.moveTo(dot.x, dot.y);
                    _that.ctx.lineTo(nowDot.x, nowDot.y);
                    _that.ctx.stroke();//不描邊看不出效果

                    //dots.splice(dots.indexOf(dot), 1);
                }
            });
        };
        //開始動畫
        Dotline.prototype.start = function(){
            var _that = this;
            this.addDots();
            setTimeout(function() {
                 _that.animate();
            }, 100);
        }
        //調用
        window.onload = function(){
            var dotline = new Dotline({
                dom:'J_dotLine',//畫布id
                cw:800,//畫布寬
                ch:500,//畫布高
                ds:50,//點的個數
                r:0.5,//圓點半徑
                dis:80//觸發(fā)連線的距離
            }).start();
        }
    </script>
</body>
</html>

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末跨扮,一起剝皮案震驚了整個濱河市衡创,隨后出現的幾起案子,更是在濱河造成了極大的恐慌璃氢,老刑警劉巖狮辽,帶你破解...
    沈念sama閱讀 218,036評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現場離奇詭異椰苟,居然都是意外死亡树叽,警方通過查閱死者的電腦和手機,發(fā)現死者居然都...
    沈念sama閱讀 93,046評論 3 395
  • 文/潘曉璐 我一進店門须误,熙熙樓的掌柜王于貴愁眉苦臉地迎上來仇轻,“玉大人,你說我怎么就攤上這事篷店。” “怎么了方淤?”我有些...
    開封第一講書人閱讀 164,411評論 0 354
  • 文/不壞的土叔 我叫張陵蹄殃,是天一觀的道長。 經常有香客問我讳苦,道長带膜,這世上最難降的妖魔是什么膝藕? 我笑而不...
    開封第一講書人閱讀 58,622評論 1 293
  • 正文 為了忘掉前任咐扭,我火速辦了婚禮,結果婚禮上袜爪,老公的妹妹穿的比我還像新娘。我一直安慰自己饿敲,他們只是感情好逛绵,可當我...
    茶點故事閱讀 67,661評論 6 392
  • 文/花漫 我一把揭開白布术浪。 她就那樣靜靜地躺著寿酌,像睡著了一般。 火紅的嫁衣襯著肌膚如雪醇疼。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,521評論 1 304
  • 那天倔毙,我揣著相機與錄音乙濒,去河邊找鬼。 笑死颁股,一個胖子當著我的面吹牛,可吹牛的內容都是我干的诉儒。 我是一名探鬼主播,決...
    沈念sama閱讀 40,288評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼忱反,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了缭受?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,200評論 0 276
  • 序言:老撾萬榮一對情侶失蹤韭畸,失蹤者是張志新(化名)和其女友劉穎蔓搞,沒想到半個月后,有當地人在樹林里發(fā)現了一具尸體喂分,經...
    沈念sama閱讀 45,644評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,837評論 3 336
  • 正文 我和宋清朗相戀三年甘萧,在試婚紗的時候發(fā)現自己被綠了梆掸。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,953評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡怪得,死狀恐怖卑硫,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情欢伏,我是刑警寧澤,帶...
    沈念sama閱讀 35,673評論 5 346
  • 正文 年R本政府宣布财岔,位于F島的核電站河爹,受9級特大地震影響,放射性物質發(fā)生泄漏咸这。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,281評論 3 329
  • 文/蒙蒙 一酿雪、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧指黎,春花似錦、人聲如沸醋安。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,889評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至团秽,卻和暖如春叭首,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背焙格。 一陣腳步聲響...
    開封第一講書人閱讀 33,011評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人损肛。 一個月前我還...
    沈念sama閱讀 48,119評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像治拿,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子劫谅,可洞房花燭夜當晚...
    茶點故事閱讀 44,901評論 2 355

推薦閱讀更多精彩內容