html粒子连接,HTML5/Canvas 粒子连接

JavaScript

语言:

JaveScriptBabelCoffeeScript

确定

/* Canvas Variables */

var canvas = document.querySelector('canvas'),

ctx = canvas.getContext('2d'),

w = canvas.width = 720,

h = canvas.height = 720,

timing = 0,

centerX = w / 2,

centerY = h / 2;

/* Function Returning : Random between min and max */

var randInt = function(min, max) {

return (~~(Math.random() * max) - min);

}

/* Function Returning Cart Distance */

var distance = function(ax, ay, bx, by) {

return (~~(Math.sqrt((ax - bx) * (ax - bx) + (ay - by) * (ay - by))));

}

/* Particles Variables */

var numParticles = 50;

var tabParticles = [];

var staticParticles = [];

function Particle(i) {

this.diameter = 2;

this.positionX = canvas.width / 2 + Math.cos(i * (Math.PI * 2) / numParticles) * 200;

this.positionY = canvas.height / 2 + Math.sin(i * (Math.PI * 2) / numParticles) * 200;

this.velocityX = Math.cos(i * (Math.PI * 2) / numParticles);

this.velocityY = Math.sin(i * (Math.PI * 2) / numParticles);

this.speedX = 10;

this.speedY = 2;

}

function Static(i) {

this.positionX = canvas.width / 2 + Math.cos(i * (Math.PI * 2) / numParticles) * 200;

this.positionY = canvas.height / 2 + Math.sin(i * (Math.PI * 2) / numParticles) * 200;

}

/* Static Particles */

for (var i = 0; i < numParticles; i++) {

staticParticles.push(new Static(i));

}

for (var i = 0; i < numParticles; i++) {

tabParticles.push(new Particle(i));

}

function update() {

for (var i = 0; i < numParticles; i++) {

tabParticles[i].positionX += tabParticles[i].velocityX * tabParticles[i].speedX;

tabParticles[i].positionY += tabParticles[i].velocityY * tabParticles[i].speedY;

if (tabParticles[i].positionX >= canvas.width) {

tabParticles[i].velocityX *= -1;

//tabParticles[i].positionX = 0;

} else if (tabParticles[i].positionX <= 0) {

tabParticles[i].velocityX *= -1;

//tabParticles[i].positionX = canvas.width;

}

if (tabParticles[i].positionY >= canvas.height) {

tabParticles[i].velocityY *= -1;

//tabParticles[i].positionY = 0;

} else if (tabParticles[i].positionY <= 0) {

//tabParticles[i].velocityY *= -1;

tabParticles[i].positionY = canvas.height;

}

}

}

var d = 0;

function draw() {

ctx.save();

ctx.fillStyle = 'rgba(95,18,150,1)';

ctx.globalCompositeOperation = "source-over";

ctx.fillRect(0, 0, w, h);

/* */

for (var i = 0; i < numParticles; i++) {

for (var k = 1; k < numParticles; k++) {

if (c = distance(tabParticles[i].positionX, tabParticles[i].positionY, tabParticles[k].positionX, tabParticles[k].positionY) < 150) {

ctx.beginPath();

ctx.lineWidth = 10;

ctx.moveTo(tabParticles[i].positionX, tabParticles[i].positionY);

ctx.lineTo(tabParticles[k].positionX, tabParticles[k].positionY);

ctx.lineWidth = 1;

//ctx.strokeStyle = 'rgba('+i*5+','+93+',125,'+0.4+')';

ctx.strokeStyle = 'rgba(' + i * 5 + ',' + 3 + ',255,' + c / 2 + ')';

ctx.stroke();

}

}

ctx.beginPath();

ctx.arc(

tabParticles[i].positionX,

tabParticles[i].positionY,

tabParticles[i].diameter * 2,

0, 2 * Math.PI, false);

ctx.fillStyle = 'rgba(63,193,255,0.9)';

ctx.fill();

ctx.closePath();

ctx.beginPath();

ctx.arc(

tabParticles[i].positionX,

tabParticles[i].positionY,

tabParticles[i].diameter * 3,

0, 2 * Math.PI, false);

ctx.strokeStyle = 'gold';

ctx.stroke();

ctx.closePath();

}

ctx.restore();

}

window.requestAnimFrame = (function() {

return window.requestAnimationFrame ||

window.webkitRequestAnimationFrame ||

window.mozRequestAnimationFrame ||

function(callback) {

window.setTimeout(callback, 1000 / 60);

};

})();

(function loop() {

update();

draw();

requestAnimFrame(loop);

})();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值