提交 887a0d60 编写于 作者: M Mr.doob

Added LottieLoader

上级 e99f3e5e
......@@ -119,6 +119,7 @@
"webgl_loader_texture_hdr",
"webgl_loader_texture_ktx",
"webgl_loader_texture_ktx2",
"webgl_loader_texture_lottie",
"webgl_loader_texture_pvrtc",
"webgl_loader_texture_rgbm",
"webgl_loader_texture_tga",
......
此差异已折叠。
import {
CanvasTexture,
Loader,
LoadingManager
} from '../../../src/Three';
export class LottieLoader extends Loader {
constructor( manager?: LoadingManager );
load( url: string, onLoad: ( texture: CanvasTexture ) => void, onProgress?: ( event: ProgressEvent ) => void, onError?: ( event: ErrorEvent ) => void ) : void;
}
import {
FileLoader,
Loader,
CanvasTexture
} from "../../../build/three.module.js";
var LottieLoader = function ( manager ) {
Loader.call( this, manager );
};
LottieLoader.prototype = Object.assign( Object.create( Loader.prototype ), {
constructor: LottieLoader,
load: function ( url, onLoad, onProgress, onError ) {
const texture = new CanvasTexture();
const loader = new FileLoader( this.manager );
loader.setPath( this.path );
loader.setWithCredentials( this.withCredentials );
loader.load( url, function ( text ) {
const data = JSON.parse( text );
// bodymoving uses container.offetWidth and offsetHeight
// to define width/height
const container = document.createElement( 'div' );
container.style.width = data.w + 'px';
container.style.height = data.h + 'px';
document.body.appendChild( container );
const animation = bodymovin.loadAnimation( {
container: container,
animType: 'canvas',
loop: true,
autoplay: true,
animationData: data,
rendererSettings: { dpr: 1 }
} );
texture.animation = animation;
texture.image = animation.container;
animation.addEventListener( 'enterFrame', function () {
texture.needsUpdate = true;
} );
container.style.display = 'none';
if ( onLoad !== undefined ) {
onLoad( texture );
}
}, onProgress, onError );
return texture;
}
} );
export { LottieLoader };
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - lottie loader</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link type="text/css" rel="stylesheet" href="main.css">
</head>
<body>
<div id="info">
<a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - lottie<br/></br>
<input id="scrubber" type="range" value="0" style="width: 300px">
</div>
<script src="js/libs/lottie_canvas.js"></script>
<script type="module">
import * as THREE from '../build/three.module.js';
import { LottieLoader } from './jsm/loaders/LottieLoader.js';
let renderer, scene, camera;
let mesh;
init();
animate();
function init() {
camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 10 );
camera.position.z = 3;
scene = new THREE.Scene();
scene.background = new THREE.Color( 0x222222 );
//
const loader = new LottieLoader();
loader.load( 'textures/lottie/24017-lottie-logo-animation.json', function ( texture ) {
setupControls( texture.animation );
const geometry = new THREE.BoxBufferGeometry();
const material = new THREE.MeshBasicMaterial( { map: texture } );
mesh = new THREE.Mesh( geometry, material );
scene.add( mesh );
} );
//
renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
//
window.addEventListener( 'resize', onWindowResize, false );
}
function setupControls( animation ) {
// console.log( animation );
const scrubber = document.getElementById( 'scrubber' );
scrubber.max = animation.totalFrames;
scrubber.addEventListener( 'pointerdown', function () {
animation.pause();
} );
scrubber.addEventListener( 'pointerup', function () {
animation.play();
} );
scrubber.addEventListener( 'input', function () {
animation.goToAndStop( parseFloat( scrubber.value ), true );
} );
animation.addEventListener( 'enterFrame', function () {
scrubber.value = animation.currentFrame;
} );
}
function onWindowResize() {
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
//
function animate() {
requestAnimationFrame( animate );
if ( mesh ) {
mesh.rotation.x += 0.001;
mesh.rotation.y += 0.01;
}
renderer.render( scene, camera );
}
</script>
</body>
</html>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册