animated-bg.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. var offset = h > 380 ? 100 : 65;
  22. x0 = w / 2;
  23. y0 = h - offset;
  24. dw = Math.max(w, h, 1000) / 13;
  25. drawCircles();
  26. }
  27. window.onresize = init;
  28. function drawCicrle(radius) {
  29. ctx.beginPath();
  30. var color = Math.round(255 * (1 - radius / Math.max(w, h)));
  31. ctx.strokeStyle = 'rgba(' + color + ',' + color + ',' + color + ',0.1)';
  32. ctx.arc(x0, y0, radius, 0, 2 * Math.PI);
  33. ctx.stroke();
  34. ctx.lineWidth = 2;
  35. }
  36. var step = 0;
  37. function drawCircles() {
  38. ctx.clearRect(0, 0, w, h);
  39. for (var i = 0; i < 8; i++) {
  40. drawCicrle(dw * i + step % dw);
  41. }
  42. step += 1;
  43. }
  44. var loading = true;
  45. function animate() {
  46. if (loading || step % dw < dw - 5) {
  47. requestAnimFrame(function() {
  48. drawCircles();
  49. animate();
  50. });
  51. }
  52. }
  53. window.anim = function(l) {
  54. loading = l;
  55. animate();
  56. };
  57. init();
  58. animate();
  59. }());