-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
137 lines (103 loc) · 3.27 KB
/
index.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
<!DOCTYPE HTML>
<html>
<head>
<title>Spiraling Down</title>
<style>
body {
margin: 0px;
padding: 0px;
}
</style>
</head>
<body>
<div id="container"></div>
<script src="js/three.js"></script>
<script >
var camera, scene, renderer;
var spheres = [],
NUM_SPHERES = 7,
delay = 10,
nextTime = 0,
isMouseDown = false,
initialCameraPosition = 1000,
startTime,
time;
init();
animate();
function init() {
// renderer
renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// set background to white
renderer.setClearColor( 0xffffff, 1);
// camera
camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 20000);
// scene
scene = new THREE.Scene();
// create spheres
for (var i = 0; i < NUM_SPHERES; i++) {
spheres[i] = new THREE.Mesh(new THREE.SphereGeometry(10, 10, 10), new THREE.MeshLambertMaterial({
color: 'red'
}));
scene.add(spheres[i]);
}
// directional lighting
var directionalLight = new THREE.DirectionalLight(0xffffff);
directionalLight.position.set(1, 1, 1).normalize();
scene.add(directionalLight);
// initialize camera position
camera.position.z = initialCameraPosition;
// set the time animation starts
startTime = Date.now();
requestAnimationFrame(watcher);
// add event listeners
document.addEventListener('mousedown', function(e){mouseDownHandler(e);});
document.addEventListener('mouseup', function(e){mouseUpHandler(e);});
function mouseDownHandler(e) {
e.preventDefault();
time = Date.now();
isMouseDown = true;
};
function mouseUpHandler(e) {
e.preventDefault();
time = 0;
isMouseDown = false;
console.log("mousedown is: " + isMouseDown);
}
function watcher(loopTime) {
requestAnimationFrame(watcher);
if (loopTime < nextTime) {return;}
nextTime = loopTime + delay;
if (isMouseDown) {
camera.position.z -= 250;
console.log("camera: " + camera.position.z);
}
}
}
function animate() {
// update
time = Date.now();
for (var i = 1; i <= NUM_SPHERES; i++) {
spheres[i - 1].position.x = Math.cos(time * 0.01 / i) * i * 20;
spheres[i - 1].position.y = Math.sin(time * 0.01 / i) * i * 20;
if (isMouseDown || spheres[i - 1].position.z >= (camera.position.z - initialCameraPosition)) {
spheres[i - 1].position.z -= (spheres.length - i + 1) * 15;
console.log("spheres:" + spheres[1].position.z);
}
}
if (isMouseDown || spheres[spheres.length - 1].position.z >= (camera.position.z - initialCameraPosition)) {
spheres[spheres.length - 1].position.z -= 30;
}
// render
renderer.render(scene, camera);
// request new frame
requestAnimationFrame(function(){
animate();
});
}
// start animation
animate();
</script>
</body>
</html>