效果如下:

代碼如下:
<!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}//圓形粒子對象參數var dots = { n : 500,//圓形粒子個數 distance : 50,//圓形粒子之間的距離 d_radius : 80,//粒子距離鼠標點的距離 array : []//保存n個圓形粒子對象}//創建隨機顏色值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);}//創建圓形粒子對象function Dot(){ //圓形粒子隨機圓心坐標點 this.x = Math.random()*can.width; this.y = Math.random()*can.height; //x y 方向運動的速度值 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];  //當圓形粒子對象碰壁的時候就反彈回來  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;  }  //給圓形粒子圓心坐標加上速度值移動圓形粒子  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>以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持武林網!
新聞熱點
疑難解答