var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d'); var Grewer = {
init: function() {
this.getWindowSize();
canvas.width = this.w;
canvas.height = this.h;
this.num = 50;
this.range = 100;
this.arr = [];
this.add(); },
getWindowSize: function() {
//获取窗口宽度
if (window.innerWidth) { //兼容火狐,谷歌,safari等浏览器
this.w = window.innerWidth;
} else if ((document.body) && (document.body.clientWidth)) { //兼容IE浏览器
this.w = document.body.clientWidth;
} //获取窗口高度
if (window.innerHeight) {
this.h = window.innerHeight;
} else if ((document.body) && (document.body.clientHeight)) {
this.h = document.body.clientHeight;
}
},
update: function(obj) {
obj.x += obj.vx;
obj.y += obj.vy; if (obj.x < 0 || obj.x > this.w) {
obj.vx *= -1;
}
if (obj.y < 0 || obj.y > this.h) {
obj.vy *= -1;
}
},
add: function() { var i = this.num;
while (i--) {
var particles = {
x: (Math.random() * this.w) | 0,
y: (Math.random() * this.h) | 0,
vx: (Math.random() - .5) * 1,
vy: (Math.random() - .5) * 1,
}
this.arr.push(particles);
}
},
checkDist: function(a, b, dist) {
var x = a.x - b.x,
y = a.y - b.y; return x * x + y * y <= dist * dist;
},
print: function(obj) {
ctx.beginPath();
ctx.arc(obj.x, obj.y, 2, 0, 2 * Math.PI);
ctx.fillStyle = '#ddd';
ctx.fill();
} }
var G = Object.create(Grewer);
G.init();
var Ganim = function() {
window.requestAnimationFrame(Ganim); ctx.fillStyle = '#fff';
ctx.fillRect(0, 0, G.w, G.h); var length = G.num;
for (var i = 0; i < length; i++) {
var o1 = G.arr[i]
G.update(o1);
G.print(o1); for (var j = i + 1; j < length; ++j) { var o2 = G.arr[j];
if (G.checkDist(o1, o2, G.range)) {
ctx.strokeStyle = '#ddd';
ctx.beginPath();
ctx.moveTo(o1.x, o1.y);
ctx.lineTo(o2.x, o2.y);
ctx.stroke();
}
} }
}
Ganim();

旧版本

demo:https://grewer.github.io/JsDemo/particles/

github:https://github.com/Grewer/JsDemo/tree/master/particles

var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d'); var Grewer = {
init: function() {
this.getWindowSize();
canvas.width = this.w;
canvas.height = this.h;
this.num = 70;
this.range = 80;
this.arr = [];
this.add();
},
getWindowSize: function() {
//获取窗口宽度
if (window.innerWidth) { //兼容火狐,谷歌,safari等浏览器
this.w = window.innerWidth;
} else if ((document.body) && (document.body.clientWidth)) { //兼容IE浏览器
this.w = document.body.clientWidth;
} //获取窗口高度
if (window.innerHeight) {
this.h = window.innerHeight;
} else if ((document.body) && (document.body.clientHeight)) {
this.h = document.body.clientHeight;
}
},
update: function(obj) {
obj.x += obj.vx;
obj.y += obj.vy; if (obj.x < 0 || obj.x > this.w) {
obj.vx *= -1;
}
if (obj.y < 0 || obj.y > this.h) {
obj.vy *= -1;
}
},
add: function() { var i = this.num;
while (i--) {
var particles = {
x: (Math.random() * this.w) | 0,
y: (Math.random() * this.h) | 0,
vx: (Math.random() - .5) * 1,
vy: (Math.random() - .5) * 1,
r: ((Math.random() * 2) | 0) + 1
}
this.arr.push(particles);
}
},
checkDist: function(a, b, dist) {
var x = a.x - b.x,
y = a.y - b.y; return x * x + y * y <= dist * dist;
},
print: function(obj) {
ctx.beginPath();
ctx.arc(obj.x, obj.y, obj.r, 0, 2 * Math.PI);
ctx.fillStyle = '#cccccc';
ctx.fill();
} }
var G = Object.create(Grewer);
G.init();
var Ganim = function() {
window.requestAnimationFrame(Ganim); ctx.fillStyle = '#fff';
ctx.fillRect(0, 0, G.w, G.h); var length = G.arr.length;
for (var i = 0; i < length; i++) {
var o1 = G.arr[i]
G.update(o1);
G.print(o1); for (var j = i + 1; j < length; ++j) { var o2 = G.arr[j];
if (G.checkDist(o1, o2, G.range)) {
ctx.strokeStyle = '#cccccc';
ctx.beginPath();
ctx.moveTo(o1.x, o1.y);
ctx.lineTo(o2.x, o2.y);
ctx.stroke();
}
} }
}
G.arr.push({
x: 1,
y: 1,
vx: 0,
vy: 0,
r: 1
})
document.addEventListener('mousemove', function(e) {
G.arr[G.num].x = e.clientX;
G.arr[G.num].y = e.clientY;
}, false)
Ganim();

最新文章

  1. java.lang.UnsatisfiedLinkError: Couldn&#39;t load BaiduMapSDK 的解决方法
  2. C# 结构体
  3. memache session
  4. 关于oracle出现ORA-06143:连接未打开 解决方案
  5. Java使用memcached
  6. careercup-排序和查找 11.3
  7. HDU 5938 Four Operations 【贪心】(2016年中国大学生程序设计竞赛(杭州))
  8. 上传XML文件字符编码问题
  9. C++对象数组操作误区
  10. HDU 2553 n皇后问题(回溯法)
  11. PowerDesigner 生成带凝视SQL 各个版本号通用10(12、15)
  12. 8.2.1.7 Use of Index Extensions 使用索引扩展
  13. python 调用shell命令的方法
  14. Hive 行列转换
  15. console.log 用法
  16. Postgresql中string转换成timestamp类型
  17. Celery 分布式任务队列快速入门
  18. Linux网络文件系统的实现与调试
  19. Virtual Memory is deprecated in Redis 2.4
  20. SAP MM 物料主数据采购视图中的字段&#39;Var. OUn&#39;的作用?

热门文章

  1. Leetcode 187.重复的DNA序列
  2. 58. Spring Boot国际化(i18n)【从零开始学Spring Boot】
  3. Node.js &amp; Unix/Linux &amp; NVM
  4. 【01染色法判断二分匹配+匈牙利算法求最大匹配】HDU The Accomodation of Students
  5. 国内程序员的十大疑问之一:为什么老外不愿意用MyBatis?
  6. cdq分治入门--BZOJ1492: [NOI2007]货币兑换Cash
  7. 找宝箱 (bfs)
  8. Codeforces 629D Babaei and Birthday Cake(树状数组优化dp)
  9. SOJ 3531_Number Pyramids
  10. Spring基础入门(一)