canvas.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. $(main);
  2. function main(){
  3. const PHI = (1 + Math.sqrt(5)) / 2, // 1.618033988749895
  4. maxGeneration = (navigator.userAgent.toLowerCase().indexOf('firefox') > -1) ? 5 : 6,
  5. frameDuration = 1000 / 60,
  6. duration = 3000,
  7. rotationSpeed = 0.3,
  8. totalIterations = Math.floor(duration / frameDuration),
  9. maxBaseSize = 100,
  10. baseSizeSpeed = 0.02;
  11. debugger;
  12. var canvas = document.getElementById("canvas"),
  13. ctx = canvas.getContext("2d"),
  14. canvasWidth = document.documentElement.clientWidth,
  15. canvasHeight = document.documentElement.clientHeight,
  16. shapes = [],
  17. sizeVariation,
  18. iteration = 0,
  19. animationDirection = 1,
  20. sizeVariationRange = .15,
  21. baseRotation = 0,
  22. baseSize = 50,
  23. c1 = 43,
  24. c1S = 1,
  25. c2 = 205,
  26. c2S = 1,
  27. c3 = 255,
  28. c3S = 1;
  29. canvas.setAttribute("width", canvasWidth);
  30. canvas.setAttribute("height", canvasHeight);
  31. function Shape(gen, x, y, size, rotation) {
  32. this.generation = gen;
  33. this.size = size;
  34. this.rotation = -rotation;
  35. this.start = {
  36. x: x,
  37. y: y
  38. };
  39. this.end = {
  40. x_1: this.start.x + Math.cos(degToRad(this.rotation)) * this.size,
  41. y_1: this.start.y + Math.sin(degToRad(this.rotation)) * this.size,
  42. x_2: this.start.x + Math.cos(degToRad(this.rotation + 360 / 3)) * this.size,
  43. y_2: this.start.y + Math.sin(degToRad(this.rotation + 360 / 3)) * this.size,
  44. x_3: this.start.x +
  45. Math.cos(degToRad(this.rotation + 360 / 3 * 2)) * this.size,
  46. y_3: this.start.y + Math.sin(degToRad(this.rotation + 360 / 3 * 2)) * this.size
  47. };
  48. this.init();
  49. }
  50. Shape.prototype.init = function() {
  51. if (this.generation < maxGeneration) {
  52. var gen = this.generation + 1,
  53. newSize = this.size * sizeVariation,
  54. newRotation = this.rotation;
  55. shapes.push(
  56. new Shape(gen, this.end.x_1, this.end.y_1, newSize, newRotation)
  57. );
  58. shapes.push(
  59. new Shape(gen, this.end.x_2, this.end.y_2, newSize, newRotation)
  60. );
  61. shapes.push(
  62. new Shape(gen, this.end.x_3, this.end.y_3, newSize, newRotation)
  63. );
  64. }
  65. this.draw();
  66. };
  67. Shape.prototype.draw = function() {
  68. ctx.beginPath();
  69. ctx.moveTo(this.start.x, this.start.y);
  70. ctx.lineTo(this.end.x_1, this.end.y_1);
  71. ctx.moveTo(this.start.x, this.start.y);
  72. ctx.lineTo(this.end.x_2, this.end.y_2);
  73. ctx.moveTo(this.start.x, this.start.y);
  74. ctx.lineTo(this.end.x_3, this.end.y_3);
  75. //ctx.closePath();
  76. ctx.strokeStyle =
  77. "rgba(" + c1 + "," + c2 + "," + c3 + "," + 1 / this.generation / 5 + ")";
  78. ctx.stroke();
  79. //ctx.fill();
  80. };
  81. function animate() {
  82. //ctx.clearRect(0, 0, canvasWidth, canvasHeight);
  83. ctx.globalCompositeOperation = "source-over";
  84. ctx.fillStyle = "rgba(0,0,0,.1)";
  85. ctx.fillRect(0, 0, canvasWidth, canvasHeight);
  86. ctx.globalCompositeOperation = "lighter";
  87. shapes = [];
  88. shapes.push(
  89. new Shape(0, canvasWidth / 2, canvasHeight / 2, baseSize, baseRotation)
  90. );
  91. changeColor();
  92. iteration++;
  93. if (baseSize < maxBaseSize) baseSize += baseSizeSpeed;
  94. baseRotation += rotationSpeed;
  95. sizeVariation = easeInOutSine(
  96. iteration,
  97. 1 - sizeVariationRange * animationDirection,
  98. sizeVariationRange * 2 * animationDirection,
  99. totalIterations
  100. );
  101. if (iteration >= totalIterations) {
  102. iteration = 0;
  103. animationDirection *= -1;
  104. }
  105. requestAnimationFrame(animate);
  106. }
  107. function degToRad(deg) {
  108. return Math.PI / 180 * deg;
  109. }
  110. function easeInOutSine(
  111. currentIteration,
  112. startValue,
  113. changeInValue,
  114. totalIterations
  115. ) {
  116. return (
  117. changeInValue /
  118. 2 *
  119. (1 - Math.cos(Math.PI * currentIteration / totalIterations)) +
  120. startValue
  121. );
  122. }
  123. function changeColor() {
  124. if (c1 == 0 || c1 == 255) c1S *= -1;
  125. if (c2 == 0 || c2 == 255) c2S *= -1;
  126. if (c3 == 0 || c3 == 255) c3S *= -1;
  127. c1 += 1 * c1S;
  128. c2 += 1 * c2S;
  129. c3 += 1 * c3S;
  130. }
  131. ctx.globalCompositeOperation = "lighter";
  132. animate();
  133. window.addEventListener("resize", function() {
  134. canvasWidth = document.documentElement.clientWidth;
  135. canvasHeight = document.documentElement.clientHeight;
  136. canvas.setAttribute("width", canvasWidth);
  137. canvas.setAttribute("height", canvasHeight);
  138. ctx.strokeStyle = "rgba(66,134,240,.3)";
  139. ctx.globalCompositeOperation = "lighter";
  140. });
  141. }