效果如下:
成都創(chuàng)新互聯(lián)公司于2013年成立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目成都網(wǎng)站制作、網(wǎng)站設(shè)計網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元商南做網(wǎng)站,已為上家服務(wù),為商南各地企業(yè)和個人服務(wù),聯(lián)系電話:13518219792
代碼如下:
<!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,//粒子距離鼠標點的距離
array : []//保存n個圓形粒子對象
}
//創(chuàng)建隨機顏色值
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(){
//圓形粒子隨機圓心坐標點
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>另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站chinadenli.net,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
本文題目:Canvas繪制粒子動畫背景-創(chuàng)新互聯(lián)
新聞來源:http://chinadenli.net/article10/cdcodo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計、企業(yè)建站、虛擬主機、網(wǎng)站設(shè)計公司、搜索引擎優(yōu)化、品牌網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容