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的更多相关文章

  1. Particles.js基于Canvas画布创建粒子原子颗粒效果

    文章目录 使用方法 自定义参数 相关链接 Particles.js是一款基于HTML5 Canvas画布的轻量级粒子动画插件,可以设置粒子的形状.旋转.分布.颜色等属性,还可以动态添加粒子,效果非常炫 ...

  2. 惊艳!9个不可思议的 HTML5 Canvas 应用试验

    HTML5 <canvas> 元素给网页中的视觉展示带来了革命性的变化.Canvas 能够实现各种让人惊叹的视觉效果和高效的动画,在这以前是需要 Flash 支持或者 JavaScript ...

  3. JavaScript 加载动画Canvas 设计

    var c = document.getElementById('c'), ctx = c.getContext('2d'), cw = c.width = 400, ch = c.height = ...

  4. canvas 的一些效果

    <html> <head> <style> *{ margin: 0; padding: 0; } body{ background:green; } #div{ ...

  5. 弄个知乎的粒子动态背景_实践particles.js

    好久没登录知乎,发现他们的登录页面粒子动态效果蛮炫的,查一下代码用了Particles.js基于Canvas画布创建粒子颗粒效果. 上图 上图:   感觉有比格,就照着弄了一个,玩玩.   githu ...

  6. 用canvas 实现个图片三角化(LOW POLY)效果

    之前无意中看到Ovilia 用threejs做了个LOW POLY,也就是图片平面三角化的效果,觉得很惊艳,然后就自己花了点时间尝试了一下. 我是没怎么用过threejs,所以就直接用canvas的2 ...

  7. 打造高大上的Canvas粒子(一)

    HTML5 Canvas <canvas>标签定义图形,比如图表和其他图像,必须用脚本(javascript)绘制图形. 举例:绘制矩形 <script> var c = do ...

  8. Canvas实现文字粒子化,并且绕轴旋转(完善)

    1. 之前有放过一个初始版本,但是因为在旋转的时候,有比较大的瑕疵,造成每个点运动到端点后,出现类似撞击的感觉. 2. 所以本文对旋转作了些调整,运用类似水平方向的圆周运动 a. HTML代码,定义c ...

  9. h5 canvas

    概述 Canvas API(画布)用于在网页实时生成图像,并且可以操作图像内容,基本上它是一个可以用JavaScript操作的位图(bitmap). 使用前,首先需要新建一个canvas网页元素. & ...

随机推荐

  1. Automation 的 ReportFlow

    ReportFlow: // click the Grid icon and switch to grid page public void changeToGrid() // click the A ...

  2. hdu 4465 求期望(C(m,n)太大用log优化)

    /* 坑啊 数学函数的运用log处理,exp还原 tle好长时间,一直用g++交,最后把别人正确的代码交上也是tle,用c++交一遍ac 题意:有两个数量为n的糖果,一个人开始吃,吃到最后有一堆剩余为 ...

  3. 安装eclipse插件,很慢终于找到了解决的方法

    1 .除非你需要,否则不要选择"联接到所有更新站点" 在安装对话框里有一个小复选框,其标示为"在安装过程中联接到所有更新站点从而找到所需的软件."从表面上看,这 ...

  4. 【HDOJ6148】Valley Numer(数位DP)

    题意: 1≤T≤200 ● 1≤length(N)≤100 思路: 设f[i,j,k,l]为第i位为j,前i位是否贴上限(0/1),递减或递增(0/1)方案数 g[i,j,k]为不到n位,第i位为j, ...

  5. win10笔记本相机打开黑屏无法打开笔记本相机

    打开注册表编辑器(WINDOWS图标+R,或者右击左下角微软图标选择“运行(R)”),在出现的窗口输入regedit并回车(Enter)确定   进入HKEY_LOCAL_MACHINE \ SOFT ...

  6. Nginx与HAProxy的区别

    对于做软负载,我们都知道主流的方案有LVS.Haproxy.Nginx!那么对于Haproxy和Nginx,我们如何选择呢?回答这个问题之前,我根据个人使用经验来讲下它们的特点! Haproxy特点 ...

  7. SAS编程基础 - 菜鸟入门常用操作

    1. SAS9.4添加和取消注释的快捷键? Ctrl+/:添加注释 Ctrl+Shift+/:取消注释 2. 如何强制终止程序运行? 看到那个圆圈里带叹号的图标了吗?没错,就是它 - 中断! 3. 如 ...

  8. CentOS 7防火墙服务FirewallD指南

    CentOS 7防火墙服务FirewallD指南 作者:chszs,未经博主同意不得转载.经许可的转载需注明作者和博客主页:http://blog.csdn.net/chszs 防火墙是一种位于内部网 ...

  9. STL中常用的c++语法

    函数调用操作(c++语法中的左右小括号)可以被重载,STL的特殊版本都以仿函数形式呈现.如果对某个class进行operator()重载,它就成为一个仿函数. 仿函数(functor),就是使一个类的 ...

  10. HDU 4983 Goffi and GCD(数论)

    HDU 4983 Goffi and GCD 思路:数论题.假设k为2和n为1.那么仅仅可能1种.其它的k > 2就是0种,那么事实上仅仅要考虑k = 1的情况了.k = 1的时候,枚举n的因子 ...