4 <title>three.js webgl - animation - basic</title>
6 <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
10 font-family:Monospace;
14 background-color: #000;
21 top: 0px; width: 100%;
33 <div id="container"></div>
36 <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - animation - basic use
39 <script src="../build/three.js"></script>
40 <script src="js/Detector.js"></script>
41 <script src="js/libs/stats.min.js"></script>
45 if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
48 var scene, camera, renderer, mixer;
55 scene = new THREE.Scene();
57 var container = document.getElementById( 'container' );
61 camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 1000 );
62 camera.position.set( 50, 50, 100 );
63 camera.lookAt( scene.position );
67 var axisHelper = new THREE.AxisHelper( 10 );
68 scene.add( axisHelper );
72 var geometry = new THREE.BoxBufferGeometry( 5, 5, 5 );
73 var material = new THREE.MeshBasicMaterial( { color: 0xffffff } );
74 var mesh = new THREE.Mesh( geometry, material );
77 // create a keyframe track (i.e. a timed sequence of keyframes) for each animated property
78 // Note: the keyframe track type should correspond to the type of the property being animated
81 var positionKF = new THREE.VectorKeyframeTrack( '.position', [ 0, 1, 2 ], [ 0, 0, 0, 30, 0, 0, 0, 0, 0 ] );
84 var scaleKF = new THREE.VectorKeyframeTrack( '.scale', [ 0, 1, 2 ], [ 1, 1, 1, 2, 2, 2, 1, 1, 1 ] );
87 // Rotation should be performed using quaternions, using a QuaternionKeyframeTrack
88 // Interpolating Euler angles (.rotation property) can be problematic and is currently not supported
90 // set up rotation about x axis
91 var xAxis = new THREE.Vector3( 1, 0, 0 );
93 var qInitial = new THREE.Quaternion().setFromAxisAngle( xAxis, 0 );
94 var qFinal = new THREE.Quaternion().setFromAxisAngle( xAxis, Math.PI );
95 var quaternionKF = new THREE.QuaternionKeyframeTrack( '.quaternion', [ 0, 1, 2 ], [ qInitial.x, qInitial.y, qInitial.z, qInitial.w, qFinal.x, qFinal.y, qFinal.z, qFinal.w, qInitial.x, qInitial.y, qInitial.z, qInitial.w ] );
98 var colorKF = new THREE.ColorKeyframeTrack( '.material.color', [ 0, 1, 2 ], [ 1, 0, 0, 0, 1, 0, 0, 0, 1 ], THREE.InterpolateDiscrete );
100 // create an animation sequence with the tracks
101 // If a negative time value is passed, the duration will be calculated from the times of the passed tracks array
102 var clip = new THREE.AnimationClip( 'Action', 3, [ scaleKF, positionKF, quaternionKF, colorKF ] );
104 // setup the AnimationMixer
105 mixer = new THREE.AnimationMixer( mesh );
107 // create a ClipAction and set it to play
108 var clipAction = mixer.clipAction( clip );
113 renderer = new THREE.WebGLRenderer();
114 renderer.setPixelRatio( window.devicePixelRatio );
115 renderer.setSize( window.innerWidth, window.innerHeight );
116 container.appendChild( renderer.domElement );
121 container.appendChild( stats.dom );
125 clock = new THREE.Clock();
129 window.addEventListener( 'resize', onWindowResize, false );
133 function onWindowResize() {
135 camera.aspect = window.innerWidth / window.innerHeight;
136 camera.updateProjectionMatrix();
138 renderer.setSize( window.innerWidth, window.innerHeight );
144 requestAnimationFrame( animate );
152 var delta = clock.getDelta();
156 mixer.update( delta );
160 renderer.render( scene, camera );