background.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. var refreshDuration = 50000;
  2. var refreshTimeout;
  3. var numPointsX;
  4. var numPointsY;
  5. var unitWidth;
  6. var unitHeight;
  7. var points;
  8. function onLoad()
  9. {
  10. var svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
  11. svg.setAttribute('width',window.innerWidth);
  12. svg.setAttribute('height',window.innerHeight);
  13. //document.querySelector('#bg').appendChild(svg);
  14. var unitSize = (window.innerWidth+window.innerHeight)/20;
  15. numPointsX = Math.ceil(window.innerWidth/unitSize)+1;
  16. numPointsY = Math.ceil(window.innerHeight/unitSize)+1;
  17. unitWidth = Math.ceil(window.innerWidth/(numPointsX-1));
  18. unitHeight = Math.ceil(window.innerHeight/(numPointsY-1));
  19. points = [];
  20. for(var y = 0; y < numPointsY; y++) {
  21. for(var x = 0; x < numPointsX; x++) {
  22. points.push({x:unitWidth*x, y:unitHeight*y, originX:unitWidth*x, originY:unitHeight*y});
  23. }
  24. }
  25. randomize();
  26. for(var i = 0; i < points.length; i++) {
  27. if(points[i].originX != unitWidth*(numPointsX-1) && points[i].originY != unitHeight*(numPointsY-1)) {
  28. var topLeftX = points[i].x;
  29. var topLeftY = points[i].y;
  30. var topRightX = points[i+1].x;
  31. var topRightY = points[i+1].y;
  32. var bottomLeftX = points[i+numPointsX].x;
  33. var bottomLeftY = points[i+numPointsX].y;
  34. var bottomRightX = points[i+numPointsX+1].x;
  35. var bottomRightY = points[i+numPointsX+1].y;
  36. var rando = Math.floor(Math.random()*2);
  37. for(var n = 0; n < 2; n++) {
  38. var polygon = document.createElementNS(svg.namespaceURI, 'polygon');
  39. if(rando==0) {
  40. if(n==0) {
  41. polygon.point1 = i;
  42. polygon.point2 = i+numPointsX;
  43. polygon.point3 = i+numPointsX+1;
  44. polygon.setAttribute('points',topLeftX+','+topLeftY+' '+bottomLeftX+','+bottomLeftY+' '+bottomRightX+','+bottomRightY);
  45. } else if(n==1) {
  46. polygon.point1 = i;
  47. polygon.point2 = i+1;
  48. polygon.point3 = i+numPointsX+1;
  49. polygon.setAttribute('points',topLeftX+','+topLeftY+' '+topRightX+','+topRightY+' '+bottomRightX+','+bottomRightY);
  50. }
  51. } else if(rando==1) {
  52. if(n==0) {
  53. polygon.point1 = i;
  54. polygon.point2 = i+numPointsX;
  55. polygon.point3 = i+1;
  56. polygon.setAttribute('points',topLeftX+','+topLeftY+' '+bottomLeftX+','+bottomLeftY+' '+topRightX+','+topRightY);
  57. } else if(n==1) {
  58. polygon.point1 = i+numPointsX;
  59. polygon.point2 = i+1;
  60. polygon.point3 = i+numPointsX+1;
  61. polygon.setAttribute('points',bottomLeftX+','+bottomLeftY+' '+topRightX+','+topRightY+' '+bottomRightX+','+bottomRightY);
  62. }
  63. }
  64. polygon.setAttribute('fill','rgba(0,0,0,'+(Math.random()/25)+')');
  65. var animate = document.createElementNS('http://www.w3.org/2000/svg','animate');
  66. animate.setAttribute('fill','freeze');
  67. animate.setAttribute('attributeName','points');
  68. animate.setAttribute('dur',refreshDuration+'ms');
  69. animate.setAttribute('calcMode','linear');
  70. polygon.appendChild(animate);
  71. svg.appendChild(polygon);
  72. }
  73. }
  74. }
  75. //refresh();
  76. }
  77. function randomize() {
  78. for(var i = 0; i < points.length; i++) {
  79. if(points[i].originX != 0 && points[i].originX != unitWidth*(numPointsX-1)) {
  80. points[i].x = points[i].originX + Math.random()*unitWidth-unitWidth/2;
  81. }
  82. if(points[i].originY != 0 && points[i].originY != unitHeight*(numPointsY-1)) {
  83. points[i].y = points[i].originY + Math.random()*unitHeight-unitHeight/2;
  84. }
  85. }
  86. }
  87. function refresh() {
  88. randomize();
  89. for(var i = 0; i < document.querySelector('#bg svg').childNodes.length; i++) {
  90. var polygon = document.querySelector('#bg svg').childNodes[i];
  91. var animate = polygon.childNodes[0];
  92. if(animate.getAttribute('to')) {
  93. animate.setAttribute('from',animate.getAttribute('to'));
  94. }
  95. animate.setAttribute('to',points[polygon.point1].x+','+points[polygon.point1].y+' '+points[polygon.point2].x+','+points[polygon.point2].y+' '+points[polygon.point3].x+','+points[polygon.point3].y);
  96. animate.beginElement();
  97. }
  98. refreshTimeout = setTimeout(function() {refresh();}, refreshDuration);
  99. }
  100. function onResize() {
  101. document.querySelector('#bg svg').remove();
  102. clearTimeout(refreshTimeout);
  103. onLoad();
  104. }
  105. window.onload = onLoad;
  106. window.onresize = onResize;