1. var canvas = document.getElementById('canvas');
  2. var ctx = canvas.getContext('2d');
  3.  
  4. var Grewer = {
  5. init: function() {
  6. this.getWindowSize();
  7. canvas.width = this.w;
  8. canvas.height = this.h;
  9. this.num = 50;
  10. this.range = 100;
  11. this.arr = [];
  12. this.add();
  13.  
  14. },
  15. getWindowSize: function() {
  16. //获取窗口宽度
  17. if (window.innerWidth) { //兼容火狐,谷歌,safari等浏览器
  18. this.w = window.innerWidth;
  19. } else if ((document.body) && (document.body.clientWidth)) { //兼容IE浏览器
  20. this.w = document.body.clientWidth;
  21. }
  22.  
  23. //获取窗口高度
  24. if (window.innerHeight) {
  25. this.h = window.innerHeight;
  26. } else if ((document.body) && (document.body.clientHeight)) {
  27. this.h = document.body.clientHeight;
  28. }
  29. },
  30. update: function(obj) {
  31. obj.x += obj.vx;
  32. obj.y += obj.vy;
  33.  
  34. if (obj.x < 0 || obj.x > this.w) {
  35. obj.vx *= -1;
  36. }
  37. if (obj.y < 0 || obj.y > this.h) {
  38. obj.vy *= -1;
  39. }
  40. },
  41. add: function() {
  42.  
  43. var i = this.num;
  44. while (i--) {
  45. var particles = {
  46. x: (Math.random() * this.w) | 0,
  47. y: (Math.random() * this.h) | 0,
  48. vx: (Math.random() - .5) * 1,
  49. vy: (Math.random() - .5) * 1,
  50. }
  51. this.arr.push(particles);
  52. }
  53. },
  54. checkDist: function(a, b, dist) {
  55. var x = a.x - b.x,
  56. y = a.y - b.y;
  57.  
  58. return x * x + y * y <= dist * dist;
  59. },
  60. print: function(obj) {
  61. ctx.beginPath();
  62. ctx.arc(obj.x, obj.y, 2, 0, 2 * Math.PI);
  63. ctx.fillStyle = '#ddd';
  64. ctx.fill();
  65. }
  66.  
  67. }
  68. var G = Object.create(Grewer);
  69. G.init();
  70. var Ganim = function() {
  71. window.requestAnimationFrame(Ganim);
  72.  
  73. ctx.fillStyle = '#fff';
  74. ctx.fillRect(0, 0, G.w, G.h);
  75.  
  76. var length = G.num;
  77. for (var i = 0; i < length; i++) {
  78. var o1 = G.arr[i]
  79. G.update(o1);
  80. G.print(o1);
  81.  
  82. for (var j = i + 1; j < length; ++j) {
  83.  
  84. var o2 = G.arr[j];
  85. if (G.checkDist(o1, o2, G.range)) {
  86. ctx.strokeStyle = '#ddd';
  87. ctx.beginPath();
  88. ctx.moveTo(o1.x, o1.y);
  89. ctx.lineTo(o2.x, o2.y);
  90. ctx.stroke();
  91. }
  92. }
  93.  
  94. }
  95. }
  96. Ganim();

旧版本

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

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

  1. var canvas = document.getElementById('canvas');
  2. var ctx = canvas.getContext('2d');
  3.  
  4. var Grewer = {
  5. init: function() {
  6. this.getWindowSize();
  7. canvas.width = this.w;
  8. canvas.height = this.h;
  9. this.num = 70;
  10. this.range = 80;
  11. this.arr = [];
  12. this.add();
  13. },
  14. getWindowSize: function() {
  15. //获取窗口宽度
  16. if (window.innerWidth) { //兼容火狐,谷歌,safari等浏览器
  17. this.w = window.innerWidth;
  18. } else if ((document.body) && (document.body.clientWidth)) { //兼容IE浏览器
  19. this.w = document.body.clientWidth;
  20. }
  21.  
  22. //获取窗口高度
  23. if (window.innerHeight) {
  24. this.h = window.innerHeight;
  25. } else if ((document.body) && (document.body.clientHeight)) {
  26. this.h = document.body.clientHeight;
  27. }
  28. },
  29. update: function(obj) {
  30. obj.x += obj.vx;
  31. obj.y += obj.vy;
  32.  
  33. if (obj.x < 0 || obj.x > this.w) {
  34. obj.vx *= -1;
  35. }
  36. if (obj.y < 0 || obj.y > this.h) {
  37. obj.vy *= -1;
  38. }
  39. },
  40. add: function() {
  41.  
  42. var i = this.num;
  43. while (i--) {
  44. var particles = {
  45. x: (Math.random() * this.w) | 0,
  46. y: (Math.random() * this.h) | 0,
  47. vx: (Math.random() - .5) * 1,
  48. vy: (Math.random() - .5) * 1,
  49. r: ((Math.random() * 2) | 0) + 1
  50. }
  51. this.arr.push(particles);
  52. }
  53. },
  54. checkDist: function(a, b, dist) {
  55. var x = a.x - b.x,
  56. y = a.y - b.y;
  57.  
  58. return x * x + y * y <= dist * dist;
  59. },
  60. print: function(obj) {
  61. ctx.beginPath();
  62. ctx.arc(obj.x, obj.y, obj.r, 0, 2 * Math.PI);
  63. ctx.fillStyle = '#cccccc';
  64. ctx.fill();
  65. }
  66.  
  67. }
  68. var G = Object.create(Grewer);
  69. G.init();
  70. var Ganim = function() {
  71. window.requestAnimationFrame(Ganim);
  72.  
  73. ctx.fillStyle = '#fff';
  74. ctx.fillRect(0, 0, G.w, G.h);
  75.  
  76. var length = G.arr.length;
  77. for (var i = 0; i < length; i++) {
  78. var o1 = G.arr[i]
  79. G.update(o1);
  80. G.print(o1);
  81.  
  82. for (var j = i + 1; j < length; ++j) {
  83.  
  84. var o2 = G.arr[j];
  85. if (G.checkDist(o1, o2, G.range)) {
  86. ctx.strokeStyle = '#cccccc';
  87. ctx.beginPath();
  88. ctx.moveTo(o1.x, o1.y);
  89. ctx.lineTo(o2.x, o2.y);
  90. ctx.stroke();
  91. }
  92. }
  93.  
  94. }
  95. }
  96. G.arr.push({
  97. x: 1,
  98. y: 1,
  99. vx: 0,
  100. vy: 0,
  101. r: 1
  102. })
  103. document.addEventListener('mousemove', function(e) {
  104. G.arr[G.num].x = e.clientX;
  105. G.arr[G.num].y = e.clientY;
  106. }, false)
  107. 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. conflunce安装配置

    下载 下载Confluence-v5.4.4.zip包,其中包含   atlassian-confluence-5.4.4-x64.bin #程序二进制文件 confluence5.x-crack.z ...

  2. bzoj 4991 [Usaco2017 Feb]Why Did the Cow Cross the Road III(cdq分治,树状数组)

    题目描述 Farmer John is continuing to ponder the issue of cows crossing the road through his farm, intro ...

  3. 【NOIP2017练习】函数变换(DP,dfs)

    题意: 思路: 极限步数大概不会超过30 ; ..max,..]of longint; eul:..max]of longint; cas,v,n,k,i,ans,j:longint; functio ...

  4. 【IntelliJ】IntelliJ IDEA常用设置及快捷键以及自定义Live templates

    IntelliJ IDEA是一款非常优秀的JAVA编辑器,初学都可会对其中的一些做法感到很别扭,刚开始用的时候我也感到很不习惯,在参考了网上一些文章后在这里把我的一些经验写出来,希望初学者能快速适应它 ...

  5. 【小记事】电脑命令行开WiFi

    1.设置WiFi名称和密码 在命令行输入: netsh wlan set hostednetwork mode=allow WiFi名称 key=密码 2.开启WiFi 在命令行输入: netsh w ...

  6. Spring Boot为我们准备了最佳的数据库连接池方案,只需要在属性文件(例如application.properties)中配置需要的连接池参数即可。

    Spring Boot为我们准备了最佳的数据库连接池方案,只需要在属性文件(例如application.properties)中配置需要的连接池参数即可.

  7. spark开发环境配置

    以后spark,mapreduce,mpi可能三者集于同一平台,各自的侧重点有所不用,相当于云计算与高性能计算的集合,互补,把spark的基础看了看,现在把开发环境看看,主要是看源码,最近Apache ...

  8. CentOS 7 防火墙开启了哪些服务和端口?

    过滤封包功能的 netfilter 已经内建在 CentOS 7 的内核,但是配置 netfilter 的界面程式 firewalld 却未必有安装,不论是否已经安装,都可以执行下面的安装指令: yu ...

  9. AutoCAD如何方便截图放到Word文档,改成白底黑字

    将模型视图切换到布局2即可   比如下图所示的效果   先回到模型视图把所有线条颜色都改成白色,然后添加适当的标注(比如要受力分析,则在CAD中绘制箭头也很方便的),文字说明.然后切换到布局2就OK ...

  10. Codeforces Round #313 B. Gerald is into Art(简单题)

    B. Gerald is into Art time limit per test 2 seconds memory limit per test 256 megabytes input standa ...