LottieLoader.js 1.4 KB

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