canvas-bg-5.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. export default function canvasBg() {
  2. var canvas = document.getElementById('canvas'),
  3. ctx = canvas.getContext('2d')
  4. canvas.width = window.innerWidth;
  5. canvas.height = window.innerHeight;
  6. ctx.lineWidth = .3;
  7. ctx.strokeStyle = (new Color(150)).style;
  8. var mousePosition = {
  9. x: 30 * canvas.width / 100,
  10. y: 30 * canvas.height / 100
  11. };
  12. var dots = {
  13. nb: 750,
  14. distance: 50,
  15. d_radius: 100,
  16. array: []
  17. };
  18. function colorValue(min) {
  19. return Math.floor(Math.random() * 255 + min);
  20. }
  21. function createColorStyle(r, g, b) {
  22. return 'rgba(' + r + ',' + g + ',' + b + ', 0.8)';
  23. }
  24. function mixComponents(comp1, weight1, comp2, weight2) {
  25. return (comp1 * weight1 + comp2 * weight2) / (weight1 + weight2);
  26. }
  27. function averageColorStyles(dot1, dot2) {
  28. var color1 = dot1.color,
  29. color2 = dot2.color;
  30. var r = mixComponents(color1.r, dot1.radius, color2.r, dot2.radius),
  31. g = mixComponents(color1.g, dot1.radius, color2.g, dot2.radius),
  32. b = mixComponents(color1.b, dot1.radius, color2.b, dot2.radius);
  33. return createColorStyle(Math.floor(r), Math.floor(g), Math.floor(b));
  34. }
  35. function Color(min) {
  36. min = min || 0;
  37. this.r = colorValue(min);
  38. this.g = colorValue(min);
  39. this.b = colorValue(min);
  40. this.style = createColorStyle(this.r, this.g, this.b);
  41. }
  42. function Dot() {
  43. this.x = Math.random() * canvas.width;
  44. this.y = Math.random() * canvas.height;
  45. this.vx = -.5 + Math.random();
  46. this.vy = -.5 + Math.random();
  47. this.radius = Math.random() * 2;
  48. this.color = new Color();
  49. // console.log(this);
  50. }
  51. Dot.prototype = {
  52. draw: function() {
  53. ctx.beginPath();
  54. ctx.fillStyle = this.color.style;
  55. ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
  56. ctx.fill();
  57. }
  58. };
  59. function createDots() {
  60. for (i = 0; i < dots.nb; i++) {
  61. dots.array.push(new Dot());
  62. }
  63. }
  64. function moveDots() {
  65. for (i = 0; i < dots.nb; i++) {
  66. var dot = dots.array[i];
  67. if (dot.y < 0 || dot.y > canvas.height) {
  68. dot.vx = dot.vx;
  69. dot.vy = -dot.vy;
  70. } else if (dot.x < 0 || dot.x > canvas.width) {
  71. dot.vx = -dot.vx;
  72. dot.vy = dot.vy;
  73. }
  74. dot.x += dot.vx;
  75. dot.y += dot.vy;
  76. }
  77. }
  78. function connectDots() {
  79. for (i = 0; i < dots.nb; i++) {
  80. for (j = 0; j < dots.nb; j++) {
  81. i_dot = dots.array[i];
  82. j_dot = dots.array[j];
  83. if ((i_dot.x - j_dot.x) < dots.distance && (i_dot.y - j_dot.y) < dots.distance && (i_dot.x -
  84. j_dot.x) > -dots.distance && (i_dot.y - j_dot.y) > -dots.distance) {
  85. if ((i_dot.x - mousePosition.x) < dots.d_radius && (i_dot.y - mousePosition.y) < dots
  86. .d_radius && (i_dot.x - mousePosition.x) > -dots.d_radius && (i_dot.y - mousePosition
  87. .y) > -dots.d_radius) {
  88. ctx.beginPath();
  89. ctx.strokeStyle = averageColorStyles(i_dot, j_dot);
  90. ctx.moveTo(i_dot.x, i_dot.y);
  91. ctx.lineTo(j_dot.x, j_dot.y);
  92. ctx.stroke();
  93. ctx.closePath();
  94. }
  95. }
  96. }
  97. }
  98. }
  99. function drawDots() {
  100. for (i = 0; i < dots.nb; i++) {
  101. var dot = dots.array[i];
  102. dot.draw();
  103. }
  104. }
  105. function animateDots() {
  106. ctx.clearRect(0, 0, canvas.width, canvas.height);
  107. moveDots();
  108. connectDots();
  109. drawDots();
  110. requestAnimationFrame(animateDots);
  111. }
  112. canvas.addEventListener('mousemove', function(e) {
  113. mousePosition.x = e.pageX;
  114. mousePosition.y = e.pageY;
  115. })
  116. // canvas.on('mousemove', function(e) {
  117. // mousePosition.x = e.pageX;
  118. // mousePosition.y = e.pageY;
  119. // });
  120. canvas.addEventListener('mouseleave', function(e) {
  121. mousePosition.x = canvas.width / 2;
  122. mousePosition.y = canvas.height / 2;
  123. })
  124. // canvas.on('mouseleave', function(e) {
  125. // mousePosition.x = canvas.width / 2;
  126. // mousePosition.y = canvas.height / 2;
  127. // });
  128. createDots();
  129. requestAnimationFrame(animateDots);
  130. }