webgl_loader_texture_lottie.html 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - lottie loader</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - lottie<br/></br>
  12. <input id="scrubber" type="range" value="0" style="width: 300px">
  13. </div>
  14. <script src="js/libs/lottie_canvas.js"></script>
  15. <script type="module">
  16. import * as THREE from '../build/three.module.js';
  17. import { LottieLoader } from './jsm/loaders/LottieLoader.js';
  18. let renderer, scene, camera;
  19. let mesh;
  20. init();
  21. animate();
  22. function init() {
  23. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 10 );
  24. camera.position.z = 3;
  25. scene = new THREE.Scene();
  26. scene.background = new THREE.Color( 0x222222 );
  27. const loader = new LottieLoader();
  28. loader.load( 'textures/lottie/24017-lottie-logo-animation.json', function ( texture ) {
  29. setupControls( texture.animation );
  30. const geometry = new THREE.BoxBufferGeometry();
  31. const material = new THREE.MeshBasicMaterial( { map: texture } );
  32. mesh = new THREE.Mesh( geometry, material );
  33. scene.add( mesh );
  34. } );
  35. renderer = new THREE.WebGLRenderer();
  36. renderer.setPixelRatio( window.devicePixelRatio );
  37. renderer.setSize( window.innerWidth, window.innerHeight );
  38. document.body.appendChild( renderer.domElement );
  39. //
  40. window.addEventListener( 'resize', onWindowResize, false );
  41. }
  42. function setupControls( animation ) {
  43. // Lottie animation API
  44. // https://airbnb.io/lottie/#/web
  45. // There are a few undocumented properties:
  46. // console.log( animation );
  47. const scrubber = document.getElementById( 'scrubber' );
  48. scrubber.max = animation.totalFrames;
  49. scrubber.addEventListener( 'pointerdown', function () {
  50. animation.pause();
  51. } );
  52. scrubber.addEventListener( 'pointerup', function () {
  53. animation.play();
  54. } );
  55. scrubber.addEventListener( 'input', function () {
  56. animation.goToAndStop( parseFloat( scrubber.value ), true );
  57. } );
  58. animation.addEventListener( 'enterFrame', function () {
  59. scrubber.value = animation.currentFrame;
  60. } );
  61. }
  62. function onWindowResize() {
  63. camera.updateProjectionMatrix();
  64. renderer.setSize( window.innerWidth, window.innerHeight );
  65. }
  66. //
  67. function animate() {
  68. requestAnimationFrame( animate );
  69. if ( mesh ) {
  70. mesh.rotation.x += 0.001;
  71. mesh.rotation.y += 0.01;
  72. }
  73. renderer.render( scene, camera );
  74. }
  75. </script>
  76. </body>
  77. </html>