sky.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*(function() {
  2. // Main
  3. initHeader();
  4. initAnimation();
  5. addListeners();
  6. })();*/
  7. var width, height, largeHeader, canvas, ctx, points, target, animateHeader = true;
  8. export function initHeader() {
  9. width = window.innerWidth;
  10. height = window.innerHeight;
  11. target = {x: width/2, y: height/2};
  12. // largeHeader = document.getElementById('large-header');
  13. // largeHeader.style.height = height+'px';
  14. canvas = document.getElementById('demo-canvas');
  15. canvas.width = width;
  16. canvas.height = height;
  17. ctx = canvas.getContext('2d');
  18. // create points
  19. points = [];
  20. for(var x = 0; x < width; x = x + width/20) {
  21. for(var y = 0; y < height; y = y + height/20) {
  22. var px = x + Math.random()*width/20;
  23. var py = y + Math.random()*height/20;
  24. var p = {x: px, originX: px, y: py, originY: py };
  25. points.push(p);
  26. }
  27. }
  28. // for each point find the 5 closest points
  29. for(var i = 0; i < points.length; i++) {
  30. var closest = [];
  31. var p1 = points[i];
  32. for(var j = 0; j < points.length; j++) {
  33. var p2 = points[j]
  34. if(!(p1 == p2)) {
  35. var placed = false;
  36. for(var k = 0; k < 5; k++) {
  37. if(!placed) {
  38. if(closest[k] == undefined) {
  39. closest[k] = p2;
  40. placed = true;
  41. }
  42. }
  43. }
  44. for(var k = 0; k < 5; k++) {
  45. if(!placed) {
  46. if(getDistance(p1, p2) < getDistance(p1, closest[k])) {
  47. closest[k] = p2;
  48. placed = true;
  49. }
  50. }
  51. }
  52. }
  53. }
  54. p1.closest = closest;
  55. }
  56. // assign a circle to each point
  57. for(var i in points) {
  58. var c = new Circle(points[i], 2+Math.random()*2, 'rgba(255,255,255,0.3)');
  59. points[i].circle = c;
  60. }
  61. }
  62. // Event handling
  63. export function addListeners() {
  64. if(!('ontouchstart' in window)) {
  65. window.addEventListener('mousemove', mouseMove);
  66. }
  67. window.addEventListener('scroll', scrollCheck);
  68. window.addEventListener('resize', resize);
  69. }
  70. function mouseMove(e) {
  71. var posx = 0;
  72. var posy = 0;
  73. if (e.pageX || e.pageY) {
  74. posx = e.pageX;
  75. posy = e.pageY;
  76. }
  77. else if (e.clientX || e.clientY) {
  78. posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
  79. posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
  80. }
  81. target.x = posx;
  82. target.y = posy;
  83. }
  84. function scrollCheck() {
  85. if(document.body.scrollTop > height) animateHeader = false;
  86. else animateHeader = true;
  87. }
  88. function resize() {
  89. width = window.innerWidth;
  90. height = window.innerHeight;
  91. largeHeader.style.height = height+'px';
  92. canvas.width = width;
  93. canvas.height = height;
  94. }
  95. // animation
  96. export function initAnimation() {
  97. animate();
  98. for(var i in points) {
  99. shiftPoint(points[i]);
  100. }
  101. }
  102. function animate() {
  103. if(animateHeader) {
  104. ctx.clearRect(0,0,width,height);
  105. for(var i in points) {
  106. // detect points in range
  107. if(Math.abs(getDistance(target, points[i])) < 4000) {
  108. points[i].active = 0.3;
  109. points[i].circle.active = 0.6;
  110. } else if(Math.abs(getDistance(target, points[i])) < 20000) {
  111. points[i].active = 0.1;
  112. points[i].circle.active = 0.3;
  113. } else if(Math.abs(getDistance(target, points[i])) < 40000) {
  114. points[i].active = 0.02;
  115. points[i].circle.active = 0.1;
  116. } else {
  117. points[i].active = 0;
  118. points[i].circle.active = 0;
  119. }
  120. drawLines(points[i]);
  121. points[i].circle.draw();
  122. }
  123. }
  124. requestAnimationFrame(animate);
  125. }
  126. function shiftPoint(p) {
  127. TweenLite.to(p, 1+1*Math.random(), {x:p.originX-50+Math.random()*100,
  128. y: p.originY-50+Math.random()*100, ease:Circ.easeInOut,
  129. onComplete: function() {
  130. shiftPoint(p);
  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(156,217,249,'+ 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(156,217,249,'+ _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. }