Skip to content

Commit

Permalink
add an easing example
Browse files Browse the repository at this point in the history
  • Loading branch information
yomotsu committed Apr 8, 2020
1 parent 98528d3 commit 9e313ed
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 4 deletions.
118 changes: 118 additions & 0 deletions examples/easing.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>=^.^=</title>
<link rel="stylesheet" href="./style.css">
</head>
<body>
<div class="info">
<a href="https://github.com/yomotsu/camera-controls">GitHub repo</a><br>
<button onclick="cameraControls.reset( true )">reset</button><br>
<button onclick="rotate45()">rotate 45 deg <b>relatively</b> with easing in 1 sec</button><br>
<button onclick="rotate0To120()">rotate 0 to 120 deg with easing in 3 sec</button><br>
</div>

<script src="https://unpkg.com/three@0.115.0/build/three.min.js"></script>
<script src="https://unpkg.com/@tweenjs/tween.js@18.5.0/dist/tween.umd.js"></script>
<script src="../dist/camera-controls.js"></script>
<script>
CameraControls.install( { THREE: THREE } );

const width = window.innerWidth;
const height = window.innerHeight;
const clock = new THREE.Clock();
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera( 60, width / height, 0.01, 100 );
camera.position.set( 0, 0, 5 );
const renderer = new THREE.WebGLRenderer( { antialias: true, stencil: false } );
renderer.setSize( width, height );
document.body.appendChild( renderer.domElement );

const cameraControls = new CameraControls( camera, renderer.domElement );

const mesh = new THREE.Mesh(
new THREE.BoxGeometry( 1, 1, 1 ),
new THREE.MeshBasicMaterial( { color: 0xff0000, wireframe: true } )
);
scene.add( mesh );

const gridHelper = new THREE.GridHelper( 50, 50 );
gridHelper.position.y = - 1;
scene.add( gridHelper );



const rotate45 = function () {

// save the start angle
let startAzimuthAngle = cameraControls.azimuthAngle;
new TWEEN.Tween( cameraControls )
.to( { azimuthAngle: 45 * THREE.Math.DEG2RAD + startAzimuthAngle }, 1000 )
.easing( TWEEN.Easing.Bounce.Out )
.onStart( function() {

// disable user control while the animation
cameraControls.enabled = false;

} )
.onComplete( function() {

cameraControls.enabled = true;

} )
.start();

}


const rotate0To120 = function () {

cameraControls.azimuthAngle = 0;

new TWEEN.Tween( cameraControls )
.to( { azimuthAngle: 120 * THREE.Math.DEG2RAD }, 3000 )
.easing( TWEEN.Easing.Exponential.Out )
.onStart( function() {

// disable user control while the animation
cameraControls.enabled = false;

} )
.onComplete( function() {

cameraControls.enabled = true;

} )
.start();

}


renderer.render( scene, camera );

( function anim () {

const delta = clock.getDelta();
const elapsed = clock.getElapsedTime();
const updated = cameraControls.update( delta );

// if ( elapsed > 30 ) { return; }

requestAnimationFrame( anim );

TWEEN.update( elapsed * 1000 );

if ( updated ) {

renderer.render( scene, camera );
console.log( 'rendered' );

}

} )();
</script>

</body>
</html>
2 changes: 1 addition & 1 deletion examples/path-animation.html
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@

requestAnimationFrame( anim );

TWEEN.update();
TWEEN.update( elapsed * 1000 );

if ( updated ) {

Expand Down
7 changes: 4 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ A camera control for three.js, similar to THREE.OrbitControls yet supports smoot
- [combined gestures](https://yomotsu.github.io/camera-controls/examples/combined-gestures.html)
- [keyboard events](https://yomotsu.github.io/camera-controls/examples/keyboard.html)
- [collision](https://yomotsu.github.io/camera-controls/examples/collision.html)
- [rotate with duration and easing](https://yomotsu.github.io/camera-controls/examples/easing.html) (with [tween.js](https://github.com/tweenjs/tween.js))
- [path animation](https://yomotsu.github.io/camera-controls/examples/path-animation.html) (with [tween.js](https://github.com/tweenjs/tween.js))


Expand Down Expand Up @@ -85,13 +86,13 @@ See [the demo](https://github.com/yomotsu/camera-movement-comparison#dolly-vs-zo
| Name | Type | Default | Description |
| ------------------------- | --------- | ----------- | ----------- |
| `.enabled` | `boolean` | `true` | Whether or not the controls are enabled. |
| `.distance` | `number` | N/A | Readonly. Get the current distance. |
| `.distance` | `number` | N/A | current distance. |
| `.minDistance` | `number` | `0` | Minimum distance for dolly. |
| `.maxDistance` | `number` | `Infinity` | Maximum distance for dolly. |
| `.polarAngle` | `number` | N/A | Readonly. Get the current polarAngle in radians. |
| `.polarAngle` | `number` | N/A | current polarAngle in radians. |
| `.minPolarAngle` | `number` | `0` | In radians. |
| `.maxPolarAngle` | `number` | `Math.PI` | In radians. |
| `.azimuthAngle` | `number` | N/A | Readonly. Get the current azimuthAngle in radians. |
| `.azimuthAngle` | `number` | N/A | current azimuthAngle in radians. |
| `.minAzimuthAngle` | `number` | `-Infinity` | In radians. |
| `.maxAzimuthAngle` | `number` | `Infinity` | In radians. |
| `.boundaryFriction` | `number` | `0.0` | Friction ratio of the boundary. |
Expand Down

0 comments on commit 9e313ed

Please sign in to comment.