JavaScript.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. 
  2. let max_particles = 100;
  3. let particles = [];
  4. let frequency = 100;
  5. let init_num = max_particles;
  6. let max_time = frequency * max_particles;
  7. let time_to_recreate = false;
  8. // Enable repopolate
  9. setTimeout(function () {
  10. time_to_recreate = true;
  11. }.bind(this), max_time)
  12. // Popolate particles
  13. popolate(max_particles);
  14. var tela = document.createElement('canvas');
  15. tela.width = $(window).width();
  16. tela.height = $(window).height();
  17. $("body").append(tela);
  18. var canvas = tela.getContext('2d');
  19. class Particle {
  20. constructor(canvas, options) {
  21. let colors = ["#feea00", "#a9df85", "#5dc0ad", "#ff9a00", "#fa3f20"]
  22. let types = ["full", "fill", "empty"]
  23. this.random = Math.random()
  24. this.canvas = canvas;
  25. this.progress = 0;
  26. this.x = ($(window).width() / 2) + (Math.random() * 200 - Math.random() * 200)
  27. this.y = ($(window).height() / 2) + (Math.random() * 200 - Math.random() * 200)
  28. this.w = $(window).width()
  29. this.h = $(window).height()
  30. this.radius = 1 + (8 * this.random)
  31. this.type = types[this.randomIntFromInterval(0, types.length - 1)];
  32. this.color = colors[this.randomIntFromInterval(0, colors.length - 1)];
  33. this.a = 0
  34. this.s = (this.radius + (Math.random() * 1)) / 10;
  35. //this.s = 12 //Math.random() * 1;
  36. }
  37. getCoordinates() {
  38. return {
  39. x: this.x,
  40. y: this.y
  41. }
  42. }
  43. randomIntFromInterval(min, max) {
  44. return Math.floor(Math.random() * (max - min + 1) + min);
  45. }
  46. render() {
  47. // Create arc
  48. let lineWidth = 0.2 + (2.8 * this.random);
  49. let color = this.color;
  50. switch (this.type) {
  51. case "full":
  52. this.createArcFill(this.radius, color)
  53. this.createArcEmpty(this.radius + lineWidth, lineWidth / 2, color)
  54. break;
  55. case "fill":
  56. this.createArcFill(this.radius, color)
  57. break;
  58. case "empty":
  59. this.createArcEmpty(this.radius, lineWidth, color)
  60. break;
  61. }
  62. }
  63. createArcFill(radius, color) {
  64. this.canvas.beginPath();
  65. this.canvas.arc(this.x, this.y, radius, 0, 2 * Math.PI);
  66. this.canvas.fillStyle = color;
  67. this.canvas.fill();
  68. this.canvas.closePath();
  69. }
  70. createArcEmpty(radius, lineWidth, color) {
  71. this.canvas.beginPath();
  72. this.canvas.arc(this.x, this.y, radius, 0, 2 * Math.PI);
  73. this.canvas.lineWidth = lineWidth;
  74. this.canvas.strokeStyle = color;
  75. this.canvas.stroke();
  76. this.canvas.closePath();
  77. }
  78. move() {
  79. this.x += Math.cos(this.a) * this.s;
  80. this.y += Math.sin(this.a) * this.s;
  81. this.a += Math.random() * 0.4 - 0.2;
  82. if (this.x < 0 || this.x > this.w - this.radius) {
  83. return false
  84. }
  85. if (this.y < 0 || this.y > this.h - this.radius) {
  86. return false
  87. }
  88. this.render()
  89. return true
  90. }
  91. calculateDistance(v1, v2) {
  92. let x = Math.abs(v1.x - v2.x);
  93. let y = Math.abs(v1.y - v2.y);
  94. return Math.sqrt((x * x) + (y * y));
  95. }
  96. }
  97. /*
  98. * Function to clear layer canvas
  99. * @num:number number of particles
  100. */
  101. function popolate(num) {
  102. for (var i = 0; i < num; i++) {
  103. setTimeout(
  104. function (x) {
  105. return function () {
  106. // Add particle
  107. particles.push(new Particle(canvas))
  108. };
  109. }(i)
  110. , frequency * i);
  111. }
  112. return particles.length
  113. }
  114. function clear() {
  115. // canvas.globalAlpha=0.04;
  116. canvas.fillStyle = '#111111';
  117. canvas.fillRect(0, 0, tela.width, tela.height);
  118. // canvas.globalAlpha=1;
  119. }
  120. function connection() {
  121. let old_element = null
  122. $.each(particles, function (i, element) {
  123. if (i > 0) {
  124. let box1 = old_element.getCoordinates()
  125. let box2 = element.getCoordinates()
  126. canvas.beginPath();
  127. canvas.moveTo(box1.x, box1.y);
  128. canvas.lineTo(box2.x, box2.y);
  129. canvas.lineWidth = 0.45;
  130. canvas.strokeStyle = "#3f47ff";
  131. canvas.stroke();
  132. canvas.closePath();
  133. }
  134. old_element = element
  135. })
  136. }
  137. /*
  138. * Function to update particles in canvas
  139. */
  140. function update() {
  141. clear();
  142. connection()
  143. particles = particles.filter(function (p) { return p.move() })
  144. // Recreate particles
  145. if (time_to_recreate) {
  146. if (particles.length < init_num) { popolate(1); }
  147. }
  148. requestAnimationFrame(update.bind(this))
  149. }
  150. update();