Canvas 繪制粒子動(dòng)畫(huà)背景
更新時(shí)間:2017年02月15日 09:25:20 作者:曾阿牛
本文主要分享了Canvas 繪制粒子動(dòng)畫(huà)背景的示例代碼。具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧
效果如下:
代碼如下:
<!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">您的瀏覽器不支持,請(qǐng)升級(jí)最新的版本!</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 } //圓形粒子對(duì)象參數(shù) var dots = { n : 500,//圓形粒子個(gè)數(shù) distance : 50,//圓形粒子之間的距離 d_radius : 80,//粒子距離鼠標(biāo)點(diǎn)的距離 array : []//保存n個(gè)圓形粒子對(duì)象 } //創(chuàng)建隨機(jī)顏色值 function colorValue(min){ return Math.floor(Math.random()*255 + min); } function createColorStyle(r,g,b){ return "rgba("+r+","+g+","+b+", 1)"; } //混合兩個(gè)圓形粒子的顏色 function mixConnect(c1,r1,c2,r2){//圓的顏色 半徑 return (c1*r1+c2*r2)/(r1+r2); }; //生成線條的顏色 function lineColor(dot1,dot2){//獲取具體的圓的顏色再計(jì)算 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)); } //生成圓形粒子的顏色對(duì)象 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)建圓形粒子對(duì)象 function Dot(){ //圓形粒子隨機(jī)圓心坐標(biāo)點(diǎn) this.x = Math.random()*can.width; this.y = Math.random()*can.height; //x y 方向運(yùn)動(dòng)的速度值 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(); //移動(dòng) function moveDots(){ for (var i=0;i<dots.n ;i++ ){ var dot = dots.array[i]; //當(dāng)圓形粒子對(duì)象碰壁的時(shí)候就反彈回來(lá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; } //給圓形粒子圓心坐標(biāo)加上速度值移動(dòng)圓形粒子 dot.x += dot.vx; dot.y += dot.vy; } } //鏈接粒子對(duì)象 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(); //讓圓形粒子不斷的移動(dòng) 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>
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!
相關(guān)文章
fastadmin如何讓后臺(tái)的日期顯示成年月日格式(推薦)
FastAdmin是一款基于ThinkPHP5+Bootstrap的極速后臺(tái)開(kāi)發(fā)框架,本文給大家介紹fastadmin的后臺(tái)時(shí)間戳字段如何顯示成年月日的日期格式,感興趣的朋友跟隨小編一起看看吧2023-10-10Javascript實(shí)現(xiàn)飛動(dòng)廣告效果的方法
這篇文章主要介紹了Javascript實(shí)現(xiàn)飛動(dòng)廣告效果的方法,可實(shí)現(xiàn)廣告窗口的浮動(dòng)顯示效果,且廣告窗口具有關(guān)閉功能,需要的朋友可以參考下2015-05-05小程序按鈕避免多次調(diào)用接口和點(diǎn)擊方案實(shí)現(xiàn)(不用showLoading)
這篇文章主要介紹了小程序按鈕避免多次調(diào)用接口和點(diǎn)擊方案實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04詳解weex默認(rèn)webpack.config.js改造
本篇文章主要介紹了詳解weex默認(rèn)webpack.config.js改造,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01javascript中強(qiáng)制執(zhí)行toString()具體實(shí)現(xiàn)
Javascript通常會(huì)根據(jù)方法或運(yùn)算符的需要而自動(dòng)把值轉(zhuǎn)成所需的類(lèi)型,這可能導(dǎo)致各種錯(cuò)誤,接下來(lái)為大家介紹下javascript如何強(qiáng)制執(zhí)行toString(),感興趣的朋友可以參考下哈2013-04-04