LottieLoader.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import {
  2. FileLoader,
  3. Loader,
  4. CanvasTexture,
  5. NearestFilter,
  6. SRGBColorSpace
  7. } from 'three';
  8. import lottie from '../libs/lottie_canvas.module.js';
  9. class LottieLoader extends Loader {
  10. setQuality( value ) {
  11. this._quality = value;
  12. }
  13. load( url, onLoad, onProgress, onError ) {
  14. const quality = this._quality || 1;
  15. const texture = new CanvasTexture();
  16. texture.minFilter = NearestFilter;
  17. texture.generateMipmaps = false;
  18. texture.colorSpace = SRGBColorSpace;
  19. const loader = new FileLoader( this.manager );
  20. loader.setPath( this.path );
  21. loader.setWithCredentials( this.withCredentials );
  22. loader.load( url, function ( text ) {
  23. const data = JSON.parse( text );
  24. // lottie uses container.offsetWidth and offsetHeight
  25. // to define width/height
  26. const container = document.createElement( 'div' );
  27. container.style.width = data.w + 'px';
  28. container.style.height = data.h + 'px';
  29. document.body.appendChild( container );
  30. const animation = lottie.loadAnimation( {
  31. container: container,
  32. animType: 'canvas',
  33. loop: true,
  34. autoplay: true,
  35. animationData: data,
  36. rendererSettings: { dpr: quality }
  37. } );
  38. texture.animation = animation;
  39. texture.image = animation.container;
  40. animation.addEventListener( 'enterFrame', function () {
  41. texture.needsUpdate = true;
  42. } );
  43. container.style.display = 'none';
  44. if ( onLoad !== undefined ) {
  45. onLoad( texture );
  46. }
  47. }, onProgress, onError );
  48. return texture;
  49. }
  50. }
  51. export { LottieLoader };
粤ICP备19079148号