-
Notifications
You must be signed in to change notification settings - Fork 10
/
keyframe.html
165 lines (137 loc) · 4.89 KB
/
keyframe.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<!DOCTYPE html>
<html>
<head>
<title>Appendix C - KeyFrame</title>
<!-- tweening functions: http://sole.github.io/tween.js/examples/03_graphs.html -->
<style>
body{
margin: 0;
overflow: hidden;
}
#stats { /* Align stats top-left */
position: absolute;
left: 0px;
top: 0px;
}
</style>
</head>
<body>
<!-- JavaScript libraries -->
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/three.js/r67/three.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/stats.js/r11/Stats.min.js"></script>
<script src="../examples/assets/libs/TrackballControls.js"></script>
<script src="../examples/assets/libs/tween.min.js"></script>
<script src="../examples/assets/libs/keyframe.js"></script>
<!-- Javascript code that runs our Three.js examples -->
<script>
// once everything is loaded, we run our Three.js stuff.
$(function () {
var stats = initStats();
// create a scene, that will hold all our elements such as objects, cameras and lights.
var scene = new THREE.Scene();
// create a camera, which defines where we're looking at.
var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
// create a render and set the size
var renderer = new THREE.WebGLRenderer({antialias: true});
// create trackball controls
var trackballControls = new THREE.TrackballControls(camera);
renderer.setClearColor(new THREE.Color(0xEEEEEE, 1.0));
renderer.setSize(window.innerWidth, window.innerHeight);
// create the ground plane
var planeGeometry = new THREE.PlaneGeometry(60,20);
var planeMaterial = new THREE.MeshLambertMaterial({color: 0xffffff});
plane = new THREE.Mesh(planeGeometry,planeMaterial);
// rotate and position the plane
plane.rotation.x=-0.5*Math.PI;
plane.position.set(15,0,0);
// add the plane to the scene
scene.add(plane);
// create a cube
var cubeGeometry = new THREE.BoxGeometry(4,4,4);
var cubeMaterial = new THREE.MeshLambertMaterial({color: 0xff0000});
var cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
// position the cube
cube.position.set(-4,3,0);
// add the cube to the scene
scene.add(cube);
var sphereGeometry = new THREE.SphereGeometry(4,20,20);
var sphereMaterial = new THREE.MeshLambertMaterial({color: 0x7777ff});
var sphere = new THREE.Mesh(sphereGeometry,sphereMaterial);
// position the sphere
sphere.position.set(20,4,2);
// add the sphere to the scene
scene.add(sphere);
// position and point the camera to the center of the scene
camera.position.set(-30,40,30);
camera.up = new THREE.Vector3(0,1,0);
camera.lookAt(scene.position);
// add subtle ambient lighting
var ambientLight = new THREE.AmbientLight(0x0c0c0c);
scene.add(ambientLight);
// add spotlight for the shadows
var spotLight = new THREE.SpotLight( 0xffffff );
spotLight.position.set(-40,60,-10);
scene.add(spotLight);
// add axis helper
var axisHelper = new THREE.AxisHelper(3);
scene.add(axisHelper);
// add the output of the renderer to the html element
$('body').append(renderer.domElement);
//define animations
var animator = null;
var duration = 4; // sec
var loopAnimation = true;
function initAnimator() {
animator = new KF.KeyFrameAnimator();
animator.init({
interps:
[
{
keys:[0, .2, .8, 1],
values:[
{ z : 0, x: 0},
{ z : Math.PI, x: 0 },
{ z : 5*Math.PI / 4, x : 0 },
{ z : Math.PI * 2, x: Math.PI * 2 },
],
target: cube.rotation
},
{
keys:[0, .3, .8, 1],
values:[
{ z : 0 },
{ z : -3 },
{ z : 5 },
{ z : 6 },
],
target: cube.position
},
],
loop: loopAnimation,
duration: duration * 1000,
easing: TWEEN.Easing.Bounce.Out
});
}
initAnimator();
// call the render function
render();
animator.start();
function render() {
stats.update();
trackballControls.update();
KF.update();
// render using requestAnimationFrame
requestAnimationFrame(render);
renderer.render(scene, camera);
}
function initStats() {
var stats = new Stats();
stats.setMode(0); // 0: fps, 1: ms
$('body').append(stats.domElement);
return stats;
}
});
</script>
</body>
</html>