In this repo i store all my websites, each in a different branch
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

186 linhas
5.9 KiB

  1. (function () {
  2. var width, height, largeHeader, canvas, ctx, points, target, animateHeader = true;
  3. // Main
  4. initHeader();
  5. initAnimation();
  6. addListeners();
  7. function initHeader() {
  8. width = window.innerWidth;
  9. height = window.innerHeight;
  10. target = { x: width / 2, y: height / 2 };
  11. largeHeader = document.getElementById('intro');
  12. largeHeader.style.height = height + 'px';
  13. canvas = document.getElementById('pollyfill-canvas');
  14. canvas.width = width;
  15. canvas.height = height;
  16. ctx = canvas.getContext('2d');
  17. // create points
  18. points = [];
  19. for (var x = 0; x < width; x = x + width / 20) {
  20. for (var y = 0; y < height; y = y + height / 20) {
  21. var px = x + Math.random() * width / 20;
  22. var py = y + Math.random() * height / 20;
  23. var p = { x: px, originX: px, y: py, originY: py };
  24. points.push(p);
  25. }
  26. }
  27. // for each point find the 5 closest points
  28. for (var i = 0; i < points.length; i++) {
  29. var closest = [];
  30. var p1 = points[i];
  31. for (var j = 0; j < points.length; j++) {
  32. var p2 = points[j]
  33. if (!(p1 == p2)) {
  34. var placed = false;
  35. for (var k = 0; k < 5; k++) {
  36. if (!placed) {
  37. if (closest[k] == undefined) {
  38. closest[k] = p2;
  39. placed = true;
  40. }
  41. }
  42. }
  43. for (var k = 0; k < 5; k++) {
  44. if (!placed) {
  45. if (getDistance(p1, p2) < getDistance(p1, closest[k])) {
  46. closest[k] = p2;
  47. placed = true;
  48. }
  49. }
  50. }
  51. }
  52. }
  53. p1.closest = closest;
  54. }
  55. // assign a circle to each point
  56. for (var i in points) {
  57. var c = new Circle(points[i], 2 + Math.random() * 2, 'rgba(255,255,255,0.3)');
  58. points[i].circle = c;
  59. }
  60. }
  61. // Event handling
  62. function addListeners() {
  63. if (!('ontouchstart' in window)) {
  64. window.addEventListener('mousemove', mouseMove);
  65. }
  66. window.addEventListener('scroll', scrollCheck);
  67. window.addEventListener('resize', resize);
  68. }
  69. function mouseMove(e) {
  70. var posx = posy = 0;
  71. if (e.pageX || e.pageY) {
  72. posx = e.pageX;
  73. posy = e.pageY;
  74. }
  75. else if (e.clientX || e.clientY) {
  76. posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
  77. posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
  78. }
  79. target.x = posx;
  80. target.y = posy;
  81. }
  82. function scrollCheck() {
  83. if (document.body.scrollTop > height) animateHeader = false;
  84. else animateHeader = true;
  85. }
  86. function resize() {
  87. width = window.innerWidth;
  88. height = window.innerHeight;
  89. largeHeader.style.height = height + 'px';
  90. canvas.width = width;
  91. canvas.height = height;
  92. }
  93. // animation
  94. function initAnimation() {
  95. animate();
  96. for (var i in points) {
  97. shiftPoint(points[i]);
  98. }
  99. }
  100. function animate() {
  101. if (animateHeader) {
  102. ctx.clearRect(0, 0, width, height);
  103. for (var i in points) {
  104. // detect points in range
  105. if (Math.abs(getDistance(target, points[i])) < 4000) {
  106. points[i].active = 0.3;
  107. points[i].circle.active = 0.6;
  108. } else if (Math.abs(getDistance(target, points[i])) < 20000) {
  109. points[i].active = 0.1;
  110. points[i].circle.active = 0.3;
  111. } else if (Math.abs(getDistance(target, points[i])) < 40000) {
  112. points[i].active = 0.02;
  113. points[i].circle.active = 0.1;
  114. } else {
  115. points[i].active = 0;
  116. points[i].circle.active = 0;
  117. }
  118. drawLines(points[i]);
  119. points[i].circle.draw();
  120. }
  121. }
  122. requestAnimationFrame(animate);
  123. }
  124. function shiftPoint(p) {
  125. TweenLite.to(p, 1 + 1 * Math.random(), {
  126. x: p.originX - 50 + Math.random() * 100,
  127. y: p.originY - 50 + Math.random() * 100, ease: Circ.easeInOut,
  128. onComplete: function () {
  129. shiftPoint(p);
  130. }
  131. });
  132. }
  133. // Canvas manipulation
  134. function drawLines(p) {
  135. if (!p.active) return;
  136. for (var i in p.closest) {
  137. ctx.beginPath();
  138. ctx.moveTo(p.x, p.y);
  139. ctx.lineTo(p.closest[i].x, p.closest[i].y);
  140. ctx.strokeStyle = 'rgba(255,255,255,' + p.active + ')';
  141. ctx.stroke();
  142. }
  143. }
  144. function Circle(pos, rad, color) {
  145. var _this = this;
  146. // constructor
  147. (function () {
  148. _this.pos = pos || null;
  149. _this.radius = rad || null;
  150. _this.color = color || null;
  151. })();
  152. this.draw = function () {
  153. if (!_this.active) return;
  154. ctx.beginPath();
  155. ctx.arc(_this.pos.x, _this.pos.y, _this.radius, 0, 2 * Math.PI, false);
  156. ctx.fillStyle = 'rgba(255,255,255,' + _this.active + ')';
  157. ctx.fill();
  158. };
  159. }
  160. // Util
  161. function getDistance(p1, p2) {
  162. return Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2);
  163. }
  164. })();