LottieLoader.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. /**
  10. * A loader for the Lottie texture animation format.
  11. *
  12. * The loader returns an instance of {@link CanvasTexture} to represent
  13. * the animated texture. Two additional properties are added to each texture:
  14. * - `animation`: The return value of `lottie.loadAnimation()` which is an object
  15. * with an API for controlling the animation's playback.
  16. * - `image`: The image container.
  17. *
  18. * ```js
  19. * const loader = new LottieLoader();
  20. * loader.setQuality( 2 );
  21. * const texture = await loader.loadAsync( 'textures/lottie/24017-lottie-logo-animation.json' );
  22. *
  23. * const geometry = new THREE.BoxGeometry();
  24. * const material = new THREE.MeshBasicMaterial( { map: texture } );
  25. * const mesh = new THREE.Mesh( geometry, material );
  26. * scene.add( mesh );
  27. * ```
  28. *
  29. * @augments Loader
  30. * @three_import import { LottieLoader } from 'three/addons/loaders/LottieLoader.js';
  31. */
  32. class LottieLoader extends Loader {
  33. /**
  34. * Constructs a new Lottie loader.
  35. *
  36. * @deprecated The loader has been deprecated and will be removed with r186. Use lottie-web instead and create your animated texture manually.
  37. * @param {LoadingManager} [manager] - The loading manager.
  38. */
  39. constructor( manager ) {
  40. super( manager );
  41. console.warn( 'THREE.LottieLoader: The loader has been deprecated and will be removed with r186. Use lottie-web instead and create your animated texture manually.' );
  42. }
  43. /**
  44. * Sets the texture quality.
  45. *
  46. * @param {number} value - The texture quality.
  47. */
  48. setQuality( value ) {
  49. this._quality = value;
  50. }
  51. /**
  52. * Starts loading from the given URL and passes the loaded Lottie asset
  53. * to the `onLoad()` callback.
  54. *
  55. * @param {string} url - The path/URL of the file to be loaded. This can also be a data URI.
  56. * @param {function(CanvasTexture)} onLoad - Executed when the loading process has been finished.
  57. * @param {onProgressCallback} onProgress - Executed while the loading is in progress.
  58. * @param {onErrorCallback} onError - Executed when errors occur.
  59. * @returns {CanvasTexture} The Lottie texture.
  60. */
  61. load( url, onLoad, onProgress, onError ) {
  62. const quality = this._quality || 1;
  63. const texture = new CanvasTexture();
  64. texture.minFilter = NearestFilter;
  65. texture.generateMipmaps = false;
  66. texture.colorSpace = SRGBColorSpace;
  67. const loader = new FileLoader( this.manager );
  68. loader.setPath( this.path );
  69. loader.setWithCredentials( this.withCredentials );
  70. loader.load( url, function ( text ) {
  71. const data = JSON.parse( text );
  72. // lottie uses container.offsetWidth and offsetHeight
  73. // to define width/height
  74. const container = document.createElement( 'div' );
  75. container.style.width = data.w + 'px';
  76. container.style.height = data.h + 'px';
  77. document.body.appendChild( container );
  78. const animation = lottie.loadAnimation( {
  79. container: container,
  80. animType: 'canvas',
  81. loop: true,
  82. autoplay: true,
  83. animationData: data,
  84. rendererSettings: { dpr: quality }
  85. } );
  86. texture.animation = animation;
  87. texture.image = animation.container;
  88. animation.addEventListener( 'enterFrame', function () {
  89. texture.needsUpdate = true;
  90. } );
  91. container.style.display = 'none';
  92. if ( onLoad !== undefined ) {
  93. onLoad( texture );
  94. }
  95. }, onProgress, onError );
  96. return texture;
  97. }
  98. }
  99. export { LottieLoader };