bg.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. class Circle {
  2. constructor(x, y) {
  3. this.x = x;
  4. this.y = y;
  5. this.r = Math.random() * 10 ;
  6. this._mr = Math.random();
  7. this._mx = (Math.random() - 0.5) * 2;
  8. this._my = (Math.random() - 0.5) * 2 ;
  9. this.ax = 0;
  10. this.ay = 0;
  11. }
  12. drawCircle(ctx) {
  13. ctx.beginPath(); // 开始绘制
  14. ctx.arc(this.x, this.y, this.r, 0, 2 * Math.PI); // 画弧线
  15. ctx.closePath(); // 结束绘制
  16. ctx.fillStyle = 'rgba(204, 204, 204, 0.3)';
  17. ctx.fill();
  18. }
  19. drawLine(ctx, _circle) {
  20. let dx = this.x - _circle.x; // 差距
  21. let dy = this.y - _circle.y; // 差距
  22. let d = Math.sqrt(dx * dx + dy * dy); // 勾股距离
  23. if (d < 180) {
  24. ctx.beginPath();
  25. //开始一条路径,移动到位置 this.x,this.y。创建到达位置 _circle.x,_circle.y 的一条线:
  26. ctx.moveTo(this.x, this.y); //起始点
  27. ctx.lineTo(_circle.x, _circle.y); //终点
  28. ctx.closePath();
  29. ctx.strokeStyle = 'rgba(204, 204, 204, ' + String(1 - (d/180)) + ')';
  30. ctx.stroke();
  31. }
  32. }
  33. set_a(ax, ay){
  34. this.ax += ax;
  35. this.ax /= 2;
  36. this.ay += ay;
  37. this.ay /= 2;
  38. }
  39. set_a_first(){
  40. if (Math.abs(this._mx) > 3) {
  41. this.ax = 0;
  42. this._mx = (Math.random() - 0.5) * 2;
  43. }
  44. if (Math.abs(this._my) > 3) {
  45. this.ay = 0;
  46. this._my = (Math.random() - 0.5) * 2;
  47. }
  48. }
  49. move(w, h) {
  50. this._mx += this.ax;
  51. this._my += this.ay;
  52. this._mx = (this.x < w && this.x > 1) ? this._mx : (-this._mx);
  53. this._my = (this.y < h && this.y > 1) ? this._my : (-this._my);
  54. this._mr = (this.r < 10 && this.r > 1) ? this._mr : (-this._mr);
  55. this.x += this._mx / 2;
  56. this.y += this._my / 2;
  57. this.r += this._mr / 2;
  58. if (this.r <= 0){
  59. this._mr = -this._mr;
  60. this.r = -this.r;
  61. }
  62. }}
  63. class currentCirle extends Circle {
  64. constructor(x, y) {
  65. super(x, y);
  66. this.r = 8;
  67. this.R = 30;
  68. this.rad = 0;
  69. this.circle = [];
  70. }
  71. add() {
  72. console.log('add');
  73. document.getElementsByTagName('title').item(0).innerHTML = 'loading...';
  74. this.circle.push('rgba(204, 204, 204, 0.8)');
  75. }
  76. remove() {
  77. console.log('remove');
  78. this.circle.shift();
  79. if (this.circle.length === 0){
  80. document.getElementsByTagName('title').item(0).innerHTML = 'Hello';
  81. }
  82. }
  83. drawLine(ctx, _circle) {
  84. let dx = this.x - _circle.x; // 差距
  85. let dy = this.y - _circle.y; // 差距
  86. let d = Math.sqrt(dx * dx + dy * dy); // 勾股距离
  87. if (d < 180){
  88. ctx.beginPath();
  89. ctx.moveTo(this.x, this.y); //起始点
  90. ctx.lineTo(_circle.x, _circle.y); //终点
  91. ctx.closePath();
  92. ctx.strokeStyle = 'rgba(204, 0, 0, ' + String(1 - (d / 180)) + ')';
  93. ctx.stroke();
  94. _circle.set_a(dx / 1800, dy / 1800);
  95. }else{
  96. _circle.set_a_first()
  97. }
  98. }
  99. drawCircle(ctx) {
  100. let l = this.circle.length;
  101. for (let i of this.circle){
  102. let dx = this.R * Math.cos(this.rad);
  103. let dy = this.R * Math.sin(this.rad);
  104. ctx.beginPath(); // 开始绘制
  105. ctx.arc(this.x + dx, this.y + dy, this.r, 0, 2 * Math.PI); // 画弧线
  106. ctx.closePath(); // 结束绘制
  107. ctx.fillStyle = i;
  108. ctx.fill();
  109. this.rad += 2 * Math.PI * (1 / l);
  110. if (this.rad >= 2 * Math.PI){
  111. this.rad -= 2 * Math.PI}
  112. }
  113. this.rad += Math.PI * (15 / 180);
  114. }
  115. }
  116. //更新页面用requestAnimationFrame替代setTimeout
  117. window.requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
  118. let canvas = document.getElementById('canvas');
  119. let ctx = canvas.getContext('2d');
  120. let w = canvas.width = canvas.offsetWidth;
  121. let h = canvas.height = canvas.offsetHeight;
  122. let circles = [];
  123. let current_circle = new currentCirle(0, 0); // 鼠标位置
  124. let update_times = 0;
  125. let draw = function () {
  126. ctx.clearRect(0, 0, w, h); // 清空
  127. for (let i = 0; i < circles.length; i++) {
  128. circles[i].move(w, h);
  129. circles[i].drawCircle(ctx);
  130. for (j = i + 1; j < circles.length; j++) {
  131. circles[i].drawLine(ctx, circles[j])
  132. }
  133. }
  134. if (current_circle.x) {
  135. current_circle.drawCircle(ctx);
  136. for (let k = 1; k < circles.length; k++) {
  137. current_circle.drawLine(ctx, circles[k])
  138. }
  139. }else{
  140. for (let k = 1; k < circles.length; k++) {
  141. current_circle.set_a_first()
  142. }
  143. }
  144. update_times += 1;
  145. if (update_times >= 100) {
  146. circles.push(new Circle(Math.random() * w, Math.random() * h));
  147. circles.shift();
  148. update_times = 0
  149. }
  150. requestAnimationFrame(draw)
  151. };
  152. let init = function (num) {
  153. for (var i = 0; i < num; i++) {
  154. circles.push(new Circle(Math.random() * w, Math.random() * h));
  155. }
  156. draw();
  157. };
  158. window.addEventListener('load', init(220));
  159. window.onmousemove = function (e) { // 鼠标进入范围
  160. // e = e || window.event;
  161. current_circle.x = e.clientX;
  162. current_circle.y = e.clientY;
  163. };
  164. window.onmouseout = function () { // 鼠标移出反围
  165. current_circle.x = null;
  166. current_circle.y = null;
  167. };