您好,登錄后才能下訂單哦!
效果如下:
代碼如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <style> *{ margin:0px; padding:0px; } body{ background:#000; } canvas{ position:absolute; width:100%; height:100%; } </style> <body> <canvas id="canvas">您的瀏覽器不支持,請升級最新的版本!</canvas> <script>window.requestAnimFrame = ( function() { return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || function( callback ) { window.setTimeout( callback, 1000 / 60 ); }; })(); var can = document.getElementById("canvas"); var cxt = can.getContext("2d"); can.width = window.innerWidth; can.height = window.innerHeight; cxt.lineWidth = 0.3; //初始鏈接線條顯示位置 var mousePosition = { x : 30*can.width/100, y : 30*can.height/100 } //圓形粒子對象參數(shù) var dots = { n : 500,//圓形粒子個數(shù) distance : 50,//圓形粒子之間的距離 d_radius : 80,//粒子距離鼠標(biāo)點的距離 array : []//保存n個圓形粒子對象 } //創(chuàng)建隨機(jī)顏色值 function colorValue(min){ return Math.floor(Math.random()*255 + min); } function createColorStyle(r,g,b){ return "rgba("+r+","+g+","+b+", 1)"; } //混合兩個圓形粒子的顏色 function mixConnect(c1,r1,c2,r2){//圓的顏色 半徑 return (c1*r1+c2*r2)/(r1+r2); }; //生成線條的顏色 function lineColor(dot1,dot2){//獲取具體的圓的顏色再計算 var color1 = dot1.color, color2 = dot2.color; var r = mixConnect(color1.r,dot1.radius,color2.r,dot2.radius); var g = mixConnect(color1.g,dot1.radius,color2.g,dot2.radius); var b = mixConnect(color1.b,dot1.radius,color2.b,dot2.radius); return createColorStyle(Math.floor(r),Math.floor(g),Math.floor(b)); } //生成圓形粒子的顏色對象 function Color(min){ min = min || 0; this.r = colorValue(min); this.g = colorValue(min); this.b = colorValue(min); this.style = createColorStyle(this.r,this.g,this.b); } //創(chuàng)建圓形粒子對象 function Dot(){ //圓形粒子隨機(jī)圓心坐標(biāo)點 this.x = Math.random()*can.width; this.y = Math.random()*can.height; //x y 方向運(yùn)動的速度值 this.vx = -0.5 + Math.random(); this.vy = -0.5 + Math.random(); this.radius = Math.random()*5; //this.color = "#ff3333"; this.color = new Color(); } //繪制出圓形粒子 Dot.prototype.draw = function(){ cxt.beginPath(); cxt.fillStyle = this.color.style; cxt.arc(this.x,this.y,this.radius,0,Math.PI*2,false); cxt.fill(); } //添加圓形粒子 function createCircle(){ for (var i=0;i<dots.n ;i++ ) { dots.array.push(new Dot()); } } //繪制出圓形粒子 function drawDots(){ for (var i=0;i<dots.n ;i++ ) { var dot = dots.array[i]; dot.draw(); } } //drawDots(); //移動 function moveDots(){ for (var i=0;i<dots.n ;i++ ){ var dot = dots.array[i]; //當(dāng)圓形粒子對象碰壁的時候就反彈回來 if (dot.y < 0 || dot.y > can.height) { dot.vx = dot.vx; dot.vy = -dot.vy; }else if (dot.x < 0 || dot.x > can.width) { dot.vx = -dot.vx; dot.vy = dot.vy; } //給圓形粒子圓心坐標(biāo)加上速度值移動圓形粒子 dot.x += dot.vx; dot.y += dot.vy; } } //鏈接粒子對象 function connectDots(){ for (var i=0;i<dots.n ; i++) { for ( var j=0;j<dots.n ; j++) { iDot = dots.array[i]; jDot = dots.array[j]; if ((iDot.x - jDot.x) < dots.distance && (iDot.y - jDot.y) < dots.distance && (iDot.x - jDot.x) > -dots.distance && (iDot.y - jDot.y) > -dots.distance) { if ((iDot.x - mousePosition.x) < dots.d_radius && (iDot.y - mousePosition.y) < dots.d_radius && (iDot.x - mousePosition.x) > -dots.d_radius && (iDot.y - mousePosition.y) > -dots.d_radius) { cxt.beginPath(); //cxt.strokeStyle = "yellow"; cxt.strokeStyle = lineColor(iDot,jDot); cxt.moveTo(iDot.x,iDot.y); cxt.lineTo(jDot.x,jDot.y); cxt.closePath(); cxt.stroke(); } } } } } createCircle(); //讓圓形粒子不斷的移動 function animateDots(){ cxt.clearRect(0,0,can.width,can.height); moveDots(); connectDots() drawDots(); requestAnimFrame(animateDots); } animateDots(); can.onmousemove = function(ev){ var ev = ev || window.event; mousePosition.x = ev.pageX; mousePosition.y = ev.pageY; } can.onmouseout = function(){ mousePosition.x = can.width/2; mousePosition.y = can.height/2; } </script> </body> </html>
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持億速云!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。