Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>canvas-test</title>
  6. <style>
  7. body {
  8. margin: 0;
  9. padding: 0;
  10. }
  11. #canvas {
  12. background-color: black;
  13. display: block;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <canvas id="canvas"></canvas>
  19. <script src="app.js"></script>
  20. </body>
  21. </html>
  22. // Generated by CoffeeScript 1.9.3
  23. (function () {
  24. var canvas, ctx, track_mouse, mouse = {},
  25. draw, body, H, W, particlemotion, Particle, particles = [],
  26. randSpeed, particlesNum;
  27.  
  28. body = document.querySelector("body");
  29. canvas = document.querySelector("#canvas");
  30. H = window.innerHeight;
  31. W = window.innerWidth;
  32. canvas.height = H;
  33. canvas.width = W;
  34. particlesNum = 15; //粒子数
  35. ctx = canvas.getContext("2d");
  36.  
  37. // 鼠标监听
  38. track_mouse = function (e) {
  39. e = e || window.event;
  40. mouse.x = e.pageX || 1;
  41. mouse.y = e.pageY || 1;
  42. };
  43. body.addEventListener('mousemove', track_mouse, false);
  44.  
  45. // 随机方法
  46. randSpeed = function (speed) {
  47. return Math.round(Math.random()) ? Math.round(Math.random() * 9 + 1) : -Math.round(Math.random() * 9 + 1);
  48. };
  49.  
  50. // 粒子构造器
  51. Particle = function () {
  52. this.x = Math.round(Math.random() * W);//粒子初始x坐标
  53. this.y = Math.round(Math.random() * H);//粒子初始y坐标
  54. this.r = Math.round(Math.random() * 10 + 5);//粒子半径
  55. this.clr = this.r * 5.5;//绕鼠标转动半径
  56. this.rgb = {};//随机产生颜色
  57. this.rgb.r = Math.round(Math.random() * 255);
  58. this.rgb.g = Math.round(Math.random() * 255);
  59. this.rgb.b = Math.round(Math.random() * 255);
  60. this.speed_x = randSpeed();//x方向速度
  61. this.speed_y = randSpeed();//y方向速度
  62. this.speed_z = Math.random() + 0.5;//转动速度
  63. this.beginDeg = 0;
  64. // 默认移动方式
  65. this.move = function () {
  66. if (this.x < 0) {
  67. this.x = 0;
  68. this.speed_x = -this.speed_x;
  69. }
  70. if (this.x > W) {
  71. this.x = W;
  72. this.speed_x = -this.speed_x;
  73. }
  74. if (this.y < 0) {
  75. this.y = 0;
  76. this.speed_y = -this.speed_y;
  77. }
  78. if (this.y > H) {
  79. this.y = H;
  80. this.speed_y = -this.speed_y;
  81. }
  82. this.x += this.speed_x;
  83. this.y += this.speed_y;
  84. this.beginDeg = 0;
  85. };
  86.  
  87. // 绕鼠标转动
  88. this.moveWithMouse = function (dt, x) {
  89. var rad = Math.PI / 180;
  90. this.x = mouse.x + dt * Math.cos(this.beginDeg * rad);
  91.  
  92. this.y = mouse.y + dt * Math.sin(this.beginDeg * rad);
  93. if (this.beginDeg > 360 || this.beginDeg < -360) {
  94. this.beginDeg = 0.5;
  95. }
  96. if (x < 0 && this.speed_z > 0) {
  97. this.speed_z = -this.speed_z;
  98. }
  99. this.beginDeg += this.speed_z;
  100. };
  101. };
  102.  
  103. for (var i = 0; i < particlesNum; i++) {
  104. particles.push(new Particle());
  105. }
  106.  
  107. draw = function (mouse_x, mouse_y, r, rgb) {
  108. var rgbcolor = "rgb(" + rgb.r + "," + rgb.g + "," + rgb.b + "," + ")";
  109. ctx.fillStyle = "rgb(" + rgb.r + "," + rgb.g + "," + rgb.b + ")";
  110. ctx.beginPath();
  111. ctx.arc(mouse_x, mouse_y, r, 0, 2 * Math.PI);
  112. ctx.closePath();
  113. ctx.fill();
  114. };
  115.  
  116. particlemotion = function () {
  117. ctx.clearRect(0, 0, W, H);
  118. for (i = 0; i < particlesNum; i++) {
  119. draw(particles[i].x, particles[i].y, particles[i].r, particles[i].rgb);
  120. var x = particles[i].x - mouse.x,
  121. dx = Math.abs(x),
  122. y = particles[i].y - mouse.y,
  123. dy = Math.abs(y),
  124. dt = Math.sqrt(dx * dx + dy * dy);
  125. if (dt < particles[i].clr) {
  126. if (particles[i].beginDeg === 0) {
  127. particles[i].beginDeg = Math.acos(x / particles[i].clr);
  128. }
  129. particles[i].moveWithMouse(dt, x);
  130. } else {
  131. particles[i].move();
  132. }
  133. }
  134. };
  135. setInterval(function () {
  136. particlemotion();
  137. }, 5);
  138.  
  139. }).call(this);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement