fireflies.js 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. function Animation(selector, option) {
  2. this.canvas = this.init(selector);
  3. this.fireflies = new Array(option.count).fill({}).map(function () {
  4. return new Firefly(option);
  5. });
  6. }
  7. Animation.prototype.init = function (canvas) {
  8. var resize = function () {
  9. canvas.width = window.innerWidth;
  10. canvas.height = window.innerHeight;
  11. }
  12. resize();
  13. window.addEventListener('resize', resize);
  14. return canvas;
  15. }
  16. Animation.prototype.draw = function () {
  17. var drawer = this.draw.bind(this);
  18. this.redraw();
  19. var reqId = window.requestAnimationFrame(drawer);
  20. return function () {
  21. console.log('------cancelAnimationFrame----123')
  22. cancelAnimationFrame(reqId);
  23. }
  24. }
  25. Animation.prototype.redraw = function () {
  26. var ctx = this.canvas.getContext('2d');
  27. ctx.clearRect(0, 0, window.innerWidth, window.innerHeight);
  28. this.fireflies.forEach(function (firefly) {
  29. firefly.fly();
  30. firefly.flicker();
  31. ctx.beginPath();
  32. ctx.arc(firefly.x, firefly.y, firefly.radius, 0, Math.PI * 2);
  33. ctx.closePath();
  34. ctx.fillStyle = firefly.color;
  35. ctx.shadowBlur = firefly.radius * 5;
  36. ctx.shadowColor = "yellow";
  37. ctx.fill();
  38. })
  39. }
  40. export default Animation;
  41. function Firefly(option) {
  42. this.x = random(window.innerWidth, option.radius, true);
  43. this.y = random(window.innerHeight, option.radius, true);
  44. this.radius = random(option.radius + 0.5, option.radius - 0.5);
  45. this.veer = false;
  46. this.angle = random(360, 0, true);
  47. this.rate = random(30 / 1000, 6 / 1000);
  48. this.speed = random(option.speed, option.speed / 8);
  49. this.opacity = random(1, 0.001);
  50. this.flare = this.opacity > 0.5;
  51. this.color = option.color;
  52. }
  53. Firefly.prototype.fly = function () {
  54. if (this.angle >= 360 || this.angle <= 0 || Math.random() * 360 < 6) {
  55. this.veer = !this.veer;
  56. }
  57. // this.speed -= this.veer ? -.01 : .01;
  58. this.angle -= this.veer ? -this.speed : this.speed;
  59. this.x += Math.sin((Math.PI / 180) * this.angle) * this.speed;
  60. this.y += Math.cos((Math.PI / 180) * this.angle) * this.speed;
  61. if (this.x < 0) this.x += window.innerWidth;
  62. if (this.x > window.innerWidth) this.x -= window.innerWidth;
  63. if (this.y < 0) this.y += window.innerHeight;
  64. if (this.y > window.innerHeight) this.y -= window.innerHeight;
  65. }
  66. Firefly.prototype.flicker = function () {
  67. if (this.opacity >= 1 || this.opacity <= 0.001) {
  68. this.flare = !this.flare;
  69. }
  70. this.opacity -= this.flare ? -this.rate : this.rate;
  71. this.color = setOpacity(this.color, this.opacity.toFixed(3));
  72. }
  73. function random(max, min, isInt) {
  74. return isInt
  75. ? Math.floor((Math.random() * (max - min) + min))
  76. : Number((Math.random() * (max - min) + min).toFixed(3));
  77. }
  78. function setOpacity(color, opacity) {
  79. var colors = color.match(/^rgba\((\d+),\s*(\d+),\s*(\d+),\s*(\S+)\)$/);
  80. return "rgba(" + colors[1] + ", " + colors[2] + ", " + colors[3] + ", " + opacity + ")";
  81. }