前端投屏pc/h5
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

187 lines
4.4 KiB

  1. function Star(id, x, y){
  2. this.id = id;
  3. this.x = x;
  4. this.y = y;
  5. this.r = Math.floor(Math.random()*2)+1;
  6. var alpha = (Math.floor(Math.random()*10)+1)/10/2;
  7. this.color = "rgba(255,255,255,"+alpha+")";
  8. }
  9. Star.prototype.draw = function() {
  10. ctx.fillStyle = this.color;
  11. ctx.shadowBlur = this.r * 2;
  12. ctx.beginPath();
  13. ctx.arc(this.x, this.y, this.r, 0, 2 * Math.PI, false);
  14. ctx.closePath();
  15. ctx.fill();
  16. }
  17. Star.prototype.move = function() {
  18. this.y -= .15;
  19. if (this.y <= -10) this.y = HEIGHT + 10;
  20. this.draw();
  21. }
  22. Star.prototype.die = function() {
  23. stars[this.id] = null;
  24. delete stars[this.id];
  25. }
  26. function Dot(id, x, y, r) {
  27. this.id = id;
  28. this.x = x;
  29. this.y = y;
  30. this.r = Math.floor(Math.random()*5)+1;
  31. this.maxLinks = 2;
  32. this.speed = .5;
  33. this.a = .5;
  34. this.aReduction = .005;
  35. this.color = "rgba(255,255,255,"+this.a+")";
  36. this.linkColor = "rgba(255,255,255,"+this.a/4+")";
  37. this.dir = Math.floor(Math.random()*140)+200;
  38. }
  39. Dot.prototype.draw = function() {
  40. ctx.fillStyle = this.color;
  41. ctx.shadowBlur = this.r * 2;
  42. ctx.beginPath();
  43. ctx.arc(this.x, this.y, this.r, 0, 2 * Math.PI, false);
  44. ctx.closePath();
  45. ctx.fill();
  46. }
  47. Dot.prototype.link = function() {
  48. if (this.id == 0) return;
  49. var previousDot1 = getPreviousDot(this.id, 1);
  50. var previousDot2 = getPreviousDot(this.id, 2);
  51. var previousDot3 = getPreviousDot(this.id, 3);
  52. if (!previousDot1) return;
  53. ctx.strokeStyle = this.linkColor;
  54. ctx.moveTo(previousDot1.x, previousDot1.y);
  55. ctx.beginPath();
  56. ctx.lineTo(this.x, this.y);
  57. if (previousDot2 != false) ctx.lineTo(previousDot2.x, previousDot2.y);
  58. if (previousDot3 != false) ctx.lineTo(previousDot3.x, previousDot3.y);
  59. ctx.stroke();
  60. ctx.closePath();
  61. }
  62. function getPreviousDot(id, stepback) {
  63. if (id == 0 || id - stepback < 0) return false;
  64. if (typeof dots[id - stepback] != "undefined") return dots[id - stepback];
  65. else return false;//getPreviousDot(id - stepback);
  66. }
  67. Dot.prototype.move = function() {
  68. this.a -= this.aReduction;
  69. if (this.a <= 0) {
  70. this.die();
  71. return
  72. }
  73. this.color = "rgba(255,255,255,"+this.a+")";
  74. this.linkColor = "rgba(255,255,255,"+this.a/4+")";
  75. this.x = this.x + Math.cos(degToRad(this.dir))*this.speed,
  76. this.y = this.y + Math.sin(degToRad(this.dir))*this.speed;
  77. this.draw();
  78. this.link();
  79. }
  80. Dot.prototype.die = function() {
  81. dots[this.id] = null;
  82. delete dots[this.id];
  83. }
  84. var canvas = document.getElementById('canvas'),
  85. ctx = canvas.getContext('2d'),
  86. WIDTH,
  87. HEIGHT,
  88. mouseMoving = false,
  89. mouseMoveChecker,
  90. mouseX,
  91. mouseY,
  92. stars = [],
  93. initStarsPopulation = 80,
  94. dots = [],
  95. dotsMinDist = 2,
  96. maxDistFromCursor = 50;
  97. setCanvasSize();
  98. init();
  99. function setCanvasSize() {
  100. WIDTH = document.documentElement.clientWidth,
  101. HEIGHT = document.documentElement.clientHeight;
  102. canvas.setAttribute("width", WIDTH);
  103. canvas.setAttribute("height", HEIGHT);
  104. }
  105. function init() {
  106. ctx.strokeStyle = "white";
  107. ctx.shadowColor = "white";
  108. for (var i = 0; i < initStarsPopulation; i++) {
  109. stars[i] = new Star(i, Math.floor(Math.random()*WIDTH), Math.floor(Math.random()*HEIGHT));
  110. //stars[i].draw();
  111. }
  112. ctx.shadowBlur = 0;
  113. animate();
  114. }
  115. function animate() {
  116. ctx.clearRect(0, 0, WIDTH, HEIGHT);
  117. for (var i in stars) {
  118. stars[i].move();
  119. }
  120. for (var i in dots) {
  121. dots[i].move();
  122. }
  123. drawIfMouseMoving();
  124. requestAnimationFrame(animate);
  125. }
  126. window.onmousemove = function(e){
  127. mouseMoving = true;
  128. mouseX = e.clientX;
  129. mouseY = e.clientY;
  130. clearInterval(mouseMoveChecker);
  131. mouseMoveChecker = setTimeout(function() {
  132. mouseMoving = false;
  133. }, 100);
  134. }
  135. function drawIfMouseMoving(){
  136. if (!mouseMoving) return;
  137. if (dots.length == 0) {
  138. dots[0] = new Dot(0, mouseX, mouseY);
  139. dots[0].draw();
  140. return;
  141. }
  142. var previousDot = getPreviousDot(dots.length, 1);
  143. var prevX = previousDot.x;
  144. var prevY = previousDot.y;
  145. var diffX = Math.abs(prevX - mouseX);
  146. var diffY = Math.abs(prevY - mouseY);
  147. if (diffX < dotsMinDist || diffY < dotsMinDist) return;
  148. var xVariation = Math.random() > .5 ? -1 : 1;
  149. xVariation = xVariation*Math.floor(Math.random()*maxDistFromCursor)+1;
  150. var yVariation = Math.random() > .5 ? -1 : 1;
  151. yVariation = yVariation*Math.floor(Math.random()*maxDistFromCursor)+1;
  152. dots[dots.length] = new Dot(dots.length, mouseX+xVariation, mouseY+yVariation);
  153. dots[dots.length-1].draw();
  154. dots[dots.length-1].link();
  155. }
  156. //setInterval(drawIfMouseMoving, 17);
  157. function degToRad(deg) {
  158. return deg * (Math.PI / 180);
  159. }