canvas 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 = 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();
canvas particles的更多相关文章
- Particles.js基于Canvas画布创建粒子原子颗粒效果
文章目录 使用方法 自定义参数 相关链接 Particles.js是一款基于HTML5 Canvas画布的轻量级粒子动画插件,可以设置粒子的形状.旋转.分布.颜色等属性,还可以动态添加粒子,效果非常炫 ...
- 惊艳!9个不可思议的 HTML5 Canvas 应用试验
HTML5 <canvas> 元素给网页中的视觉展示带来了革命性的变化.Canvas 能够实现各种让人惊叹的视觉效果和高效的动画,在这以前是需要 Flash 支持或者 JavaScript ...
- JavaScript 加载动画Canvas 设计
var c = document.getElementById('c'), ctx = c.getContext('2d'), cw = c.width = 400, ch = c.height = ...
- canvas 的一些效果
<html> <head> <style> *{ margin: 0; padding: 0; } body{ background:green; } #div{ ...
- 弄个知乎的粒子动态背景_实践particles.js
好久没登录知乎,发现他们的登录页面粒子动态效果蛮炫的,查一下代码用了Particles.js基于Canvas画布创建粒子颗粒效果. 上图 上图: 感觉有比格,就照着弄了一个,玩玩. githu ...
- 用canvas 实现个图片三角化(LOW POLY)效果
之前无意中看到Ovilia 用threejs做了个LOW POLY,也就是图片平面三角化的效果,觉得很惊艳,然后就自己花了点时间尝试了一下. 我是没怎么用过threejs,所以就直接用canvas的2 ...
- 打造高大上的Canvas粒子(一)
HTML5 Canvas <canvas>标签定义图形,比如图表和其他图像,必须用脚本(javascript)绘制图形. 举例:绘制矩形 <script> var c = do ...
- Canvas实现文字粒子化,并且绕轴旋转(完善)
1. 之前有放过一个初始版本,但是因为在旋转的时候,有比较大的瑕疵,造成每个点运动到端点后,出现类似撞击的感觉. 2. 所以本文对旋转作了些调整,运用类似水平方向的圆周运动 a. HTML代码,定义c ...
- h5 canvas
概述 Canvas API(画布)用于在网页实时生成图像,并且可以操作图像内容,基本上它是一个可以用JavaScript操作的位图(bitmap). 使用前,首先需要新建一个canvas网页元素. & ...
随机推荐
- Python数组列表(List)
Python数组列表 数组是一种有序的集合,可以随时添加和删除其中的元素. 一.数组定义: 数组是最常用的Python数据类型,它可以作为一个方括号内的逗号分隔值出现. 数组的数据项不需要具有相同的类 ...
- memcache适用和不适用场景[转载]
适用memcached的业务场景:1)如果网站包含了访问量很大的动态网页,因而数据库的负载将会很高.由于大部分数据库请求都是读操作,那么memcached可以显著地减小数据库负载.2)如果数据库服务器 ...
- hdu - 1151 Air Raid(有向无环图的最小路径覆盖)
http://acm.hdu.edu.cn/showproblem.php?pid=1151 在一个城市里有n个地点和k条道路,道路都是单向的,并且不存在环.(DAG) 现在伞兵需要去n个地点视察,伞 ...
- 常用的delphi 第三方控件
Devexpress VCL 这个基本上覆盖了系统界面及数据库展示的方方面面,是做桌面系统必备的一套控件,目前的版本是2011.2.3, 支持win32 及win64. AutoUpgrader 这个 ...
- Minimum Depth of Binary Tree(二叉树DFS)
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- Redis Cluster集群搭建后,客户端的连接研究(Spring/Jedis)(待实践)
说明:无论是否已经搭建好集群,还是使用什么样的客户端去连接,都是必须把全部IP列表集成进去,然后随机往其中一个IP写. 这样做的好处: 1.随机IP写入之后,Redis Cluster代理层会自动根据 ...
- Spring集成Jedis(不依赖spring-data-redis)(单机/集群模式)(待实践)
Jedis是Redis的Java客户端,Spring将Jedis连接池作为一个Bean来配置.如果在Spring Data的官网上可以发现,Spring Data Redis已经将Jedis集成进去了 ...
- 2、Java并发性和多线程-多线程的优点
以下内容转自http://ifeve.com/benefits/: 尽管面临很多挑战,多线程有一些优点使得它一直被使用.这些优点是: 资源利用率更好 程序设计在某些情况下更简单 程序响应更快 资源利用 ...
- Tcl学习之--文件操作
Tcl中文件名称操作遵循Unix/Linux的命名规范. x/y/z表示x文件夹下的y 子文件夹及y以下的子文件夹z. ~admin/email则表示admin用户的email目录. l file ...
- java实现floyd统计天津地铁的网站距离
一:说明 (1)使用floyd实现各个网站的计算记录和路径 (2)网站获取和初始距离依据外部文件得到 (3)结果以外部文件的形式存储 (4)网站间转乘,觉得初始值也为1 (5)代码凝视比較具体,如有疑 ...