You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
2.4 KiB

1 year ago
  1. // JS is for dynamically creating and moving the birds across the screen.
  2. // The actual bird flapping and flight wave is CSS animation.
  3. // Adjust these options here to customize the scene.
  4. let options = {
  5. delay: 500,
  6. speedRange: [2, 5],
  7. angleRange: [-30, 30],
  8. sizeRange: [15, 30],
  9. };
  10. let bird = document.createElement('span');
  11. bird.className = 'bird';
  12. let particles = [];
  13. let length = 12;
  14. let isLeave = false;
  15. init();
  16. function init() {
  17. for (let i = 0; i < length; i++) {
  18. let particle = initParticle();
  19. particle.move();
  20. particles.push(particle);
  21. }
  22. }
  23. function initPos() {
  24. var top = $('.d1').offset().top + 50;
  25. var bottom = $('.d1').height() / 1.8 + top;
  26. return [rand(50, window.innerWidth / 2), rand(top, bottom)];
  27. }
  28. function initParticle() {
  29. let newBird = bird.cloneNode();
  30. const size = rand(options.sizeRange[0], options.sizeRange[1]);
  31. newBird.style.width = size + 'px';
  32. newBird.style.height = size / 5 + 'px';
  33. document.querySelector('.animate-bg').appendChild(newBird);
  34. let pos = initPos();
  35. return new Particle(newBird, {
  36. speed: rand(options.speedRange[0], options.speedRange[1]),
  37. angle: rand(options.angleRange[0], options.angleRange[1]),
  38. pos: pos,
  39. });
  40. }
  41. window.requestAnimationFrame(draw);
  42. function draw() {
  43. particles.forEach((particle, i, arr) => {
  44. if (particle.element.style.display == 'none') {
  45. particle.element.style.display = 'inline-block';
  46. particle.pos = initPos();
  47. }
  48. if (particle.pos[0] > window.innerWidth || particle.pos[1] > window.innerHeight || particle.pos[0] < 0 - window.innerWidth || particle.pos[1] < 0 - window.innerHeight) {
  49. particle.element.style.display = 'none';
  50. } else {
  51. particle.move();
  52. }
  53. });
  54. window.requestAnimationFrame(draw);
  55. }
  56. function Particle(element, options) {
  57. this.size = 1;
  58. this.speed = 1;
  59. this.angle = 90;
  60. this.pos = [0, 0];
  61. this.element = element;
  62. this.constructor = function (options) {
  63. for (let i in options) {
  64. this[i] = options[i];
  65. }
  66. };
  67. this.move = function () {
  68. var radians = (this.angle * Math.PI) / 180;
  69. this.pos[0] += Math.cos(radians) * this.speed;
  70. this.pos[1] += Math.sin(radians) * this.speed;
  71. // console.log(this.pos)
  72. this.draw();
  73. };
  74. this.draw = function () {
  75. this.element.style.left = this.pos[0] + 'px';
  76. this.element.style.top = this.pos[1] + 'px';
  77. };
  78. this.constructor(options);
  79. }
  80. function rand(min, max) {
  81. return Math.random() * (max - min) + min;
  82. }