LottieLoader.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. */
  31. class LottieLoader extends Loader {
  32. /**
  33. * Sets the texture quality.
  34. *
  35. * @param {number} value - The texture quality.
  36. */
  37. setQuality( value ) {
  38. this._quality = value;
  39. }
  40. /**
  41. * Starts loading from the given URL and passes the loaded Lottie asset
  42. * to the `onLoad()` callback.
  43. *
  44. * @param {string} url - The path/URL of the file to be loaded. This can also be a data URI.
  45. * @param {function(CanvasTexture)} onLoad - Executed when the loading process has been finished.
  46. * @param {onProgressCallback} onProgress - Executed while the loading is in progress.
  47. * @param {onErrorCallback} onError - Executed when errors occur.
  48. * @returns {CanvasTexture} The Lottie texture.
  49. */
  50. load( url, onLoad, onProgress, onError ) {
  51. const quality = this._quality || 1;
  52. const texture = new CanvasTexture();
  53. texture.minFilter = NearestFilter;
  54. texture.generateMipmaps = false;
  55. texture.colorSpace = SRGBColorSpace;
  56. const loader = new FileLoader( this.manager );
  57. loader.setPath( this.path );
  58. loader.setWithCredentials( this.withCredentials );
  59. loader.load( url, function ( text ) {
  60. const data = JSON.parse( text );
  61. // lottie uses container.offsetWidth and offsetHeight
  62. // to define width/height
  63. const container = document.createElement( 'div' );
  64. container.style.width = data.w + 'px';
  65. container.style.height = data.h + 'px';
  66. document.body.appendChild( container );
  67. const animation = lottie.loadAnimation( {
  68. container: container,
  69. animType: 'canvas',
  70. loop: true,
  71. autoplay: true,
  72. animationData: data,
  73. rendererSettings: { dpr: quality }
  74. } );
  75. texture.animation = animation;
  76. texture.image = animation.container;
  77. animation.addEventListener( 'enterFrame', function () {
  78. texture.needsUpdate = true;
  79. } );
  80. container.style.display = 'none';
  81. if ( onLoad !== undefined ) {
  82. onLoad( texture );
  83. }
  84. }, onProgress, onError );
  85. return texture;
  86. }
  87. }
  88. export { LottieLoader };
粤ICP备19079148号