animated-bg.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. 'use strict';
  2. (function() {
  3. var requestAnimFrame = (function() {
  4. return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame ||
  5. function(callback) {
  6. window.setTimeout(callback, 1000 / 60);
  7. };
  8. })();
  9. var c = document.createElement('canvas');
  10. document.body.appendChild(c);
  11. var style = c.style;
  12. style.width = '100%';
  13. style.position = 'absolute';
  14. var ctx = c.getContext('2d');
  15. var x0, y0, w, h, dw;
  16. function init() {
  17. w = window.innerWidth;
  18. h = window.innerHeight;
  19. c.width = w;
  20. c.height = h;
  21. x0 = w / 2;
  22. y0 = h - 103;
  23. dw = Math.max(w, h, 1000) / 13;
  24. drawCircles();
  25. }
  26. window.onresize = init;
  27. function drawCicrle(radius) {
  28. ctx.beginPath();
  29. var color = Math.round(255 * (1- radius / Math.max(w, h)));
  30. ctx.strokeStyle = 'rgba(' + color + ',' + color + ',' + color + ',0.1)';
  31. ctx.arc(x0, y0, radius, 0, 2 * Math.PI);
  32. ctx.stroke();
  33. ctx.lineWidth = 2;
  34. }
  35. var step = 0;
  36. function drawCircles() {
  37. ctx.clearRect(0, 0, w, h);
  38. for (var i = 0; i < 8; i++) {
  39. drawCicrle(dw * i + step % dw);
  40. }
  41. step += 1;
  42. }
  43. var loading = true;
  44. function animate() {
  45. if (loading || step % dw < dw - 5) {
  46. requestAnimFrame(function() {
  47. drawCircles();
  48. animate();
  49. });
  50. }
  51. }
  52. window.anim = function(l) {
  53. loading = l;
  54. animate();
  55. };
  56. init();
  57. animate();
  58. }());