backgroud.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*背景*/
  2. window.onload = function () {
  3. //定义body的margin由默认值8px->0px
  4. document.body.style.margin = "0";
  5. document.body.style.background = "255,255,255";
  6. //创建canvas画布
  7. document.body.appendChild(document.createElement('canvas'));
  8. var canvas = document.querySelector('canvas'),
  9. ctx = canvas.getContext('2d') //ctx返回一个在canvas上画图的api/dom
  10. canvas.width = window.innerWidth;
  11. canvas.height = window.innerHeight;
  12. canvas.style.position = 'fixed';
  13. ctx.lineWidth = .3;
  14. ctx.strokeStyle = (new Color(150)).style;
  15. //定义鼠标覆盖范围
  16. var mousePosition = {
  17. x: 30 * canvas.width / 100,
  18. y: 30 * canvas.height / 100
  19. };
  20. var dots = {
  21. nb: 1000,//Dot的总数
  22. distance: 50,
  23. d_radius: 100,
  24. array: []
  25. };
  26. //创建颜色类,Color类返回字符串型rgba(*,*,*,.8)
  27. function mixComponents(comp1, weight1, comp2, weight2) {
  28. return (comp1 * weight1 + comp2 * weight2) / (weight1 + weight2);
  29. }
  30. function averageColorStyles(dot1, dot2) {
  31. var color1 = dot1.color,
  32. color2 = dot2.color;
  33. var r = mixComponents(color1.r, dot1.radius, color2.r, dot2.radius),
  34. g = mixComponents(color1.g, dot1.radius, color2.g, dot2.radius),
  35. b = mixComponents(color1.b, dot1.radius, color2.b, dot2.radius);
  36. return createColorStyle(Math.floor(r), Math.floor(g), Math.floor(b));
  37. }
  38. function colorValue(min) {
  39. return Math.floor(Math.random() * 255 + min);
  40. }
  41. function createColorStyle(r, g, b) {
  42. return 'rgba(' + r + ',' + g + ',' + b + ', 0.8)';
  43. }
  44. function Color(min) {
  45. min = min || 0;
  46. this.r = colorValue(min);
  47. this.g = colorValue(min);
  48. this.b = colorValue(min);
  49. this.style = createColorStyle(this.r, this.g, this.b);
  50. }
  51. //创建Dot类以及一系列方法
  52. function Dot() {
  53. this.x = Math.random() * canvas.width;
  54. this.y = Math.random() * canvas.height;
  55. this.vx = -.5 + Math.random();
  56. this.vy = -.5 + Math.random();
  57. this.radius = Math.random() * 2;
  58. this.color = new Color();
  59. }
  60. Dot.prototype = {
  61. draw: function () {
  62. ctx.beginPath();
  63. ctx.fillStyle = this.color.style;
  64. ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
  65. ctx.fill();
  66. }
  67. };
  68. function moveDots() {//Dot对象的移动
  69. for (i = 0; i < dots.nb; i++) {
  70. var dot = dots.array[i];
  71. if (dot.y < 0 || dot.y > canvas.height) {
  72. dot.vx = dot.vx;
  73. dot.vy = - dot.vy;
  74. }
  75. else if (dot.x < 0 || dot.x > canvas.width) {
  76. dot.vx = - dot.vx;
  77. dot.vy = dot.vy;
  78. }
  79. dot.x += dot.vx;
  80. dot.y += dot.vy;
  81. }
  82. }
  83. function connectDots() {//DOt对象的连接
  84. for (i = 0; i < dots.nb; i++) {
  85. for (j = i; j < dots.nb; j++) {
  86. i_dot = dots.array[i];
  87. j_dot = dots.array[j];
  88. if ((i_dot.x - j_dot.x) < dots.distance && (i_dot.y - j_dot.y) < dots.distance && (i_dot.x - j_dot.x) > - dots.distance && (i_dot.y - j_dot.y) > - dots.distance) {
  89. if ((i_dot.x - mousePosition.x) < dots.d_radius && (i_dot.y - mousePosition.y) < dots.d_radius && (i_dot.x - mousePosition.x) > - dots.d_radius && (i_dot.y - mousePosition.y) > - dots.d_radius) {
  90. ctx.beginPath();
  91. ctx.strokeStyle = averageColorStyles(i_dot, j_dot);
  92. ctx.moveTo(i_dot.x, i_dot.y);
  93. ctx.lineTo(j_dot.x, j_dot.y);
  94. ctx.stroke();//绘制定义的路线
  95. ctx.closePath();//创建从当前点回到起始点的路径
  96. }
  97. }
  98. }
  99. }
  100. }
  101. function createDots() {//创建nb个Dot对象
  102. for (i = 0; i < dots.nb; i++) {
  103. dots.array.push(new Dot());
  104. }
  105. }
  106. function drawDots() {//引用Dot原型链,使用draw方法,在canvas上画出Dot对象
  107. for (i = 0; i < dots.nb; i++) {
  108. var dot = dots.array[i];
  109. dot.draw();
  110. }
  111. }
  112. function animateDots() {
  113. ctx.clearRect(0, 0, canvas.width, canvas.height);//清除画布,否则线条会连在一起
  114. moveDots();
  115. connectDots();
  116. drawDots();
  117. requestAnimationFrame(animateDots);
  118. }
  119. createDots();//使用创建Dot类函数
  120. requestAnimationFrame(animateDots);//使用canvas独有的60Hz刷新屏幕画布的方法
  121. document.querySelector('canvas').addEventListener('mousemove', function (e) {
  122. mousePosition.x = e.pageX;
  123. mousePosition.y = e.pageY;
  124. })
  125. document.querySelector('canvas').addEventListener('mouseleave', function (e) {//鼠标离开时,连接自动返回到画布中心
  126. mousePosition.x = canvas.width / 2;
  127. mousePosition.y = canvas.height / 2;
  128. })
  129. }
  130. /*背景end*/