-
Notifications
You must be signed in to change notification settings - Fork 0
/
galaxy.html
121 lines (100 loc) · 2.99 KB
/
galaxy.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<style>
body {
margin: 0px;
overflow: hidden;
}
.ui {
position: absolute;
}
button {
margin: 20px;
height: 40px;
width: 80px;
}
canvas {
display: block;
/* fix necessary to remove space at bottom of canvas */
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/103/three.min.js"></script>
<script src="https://threejs.org/build/three.min.js"></script>
<script src="https://threejs.org/examples/js/controls/OrbitControls.js"></script>
</head>
<body>
<button style="top:0" id="fire" class="ui">Fire</button>
</body>
</html>
<script>
var button = document.getElementById("fire");
button.addEventListener("click", onButtonClick, false);
function onButtonClick(event) {
if (reverse) {
reverse = false;
} else {
reverse = true;
}
}
//variable declarations
let scene, camera, renderer, cube, sphere;
let element = [];
let x = 0;
let reverse = true;
let init = function () {
//create the scene
scene = new THREE.Scene();
scene.background = new THREE.Color(0x000000);
//setup camera
camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 500);
camera.position.set(0, 0, 100);
camera.lookAt(0, 0, 0);
//create box
createBox();
//render initialization
renderer = new THREE.WebGLRenderer();
renderer.render(scene, camera);
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
};
let createBox = function () {
var geometry = new THREE.BoxGeometry(0.1,0.1,0.1);
var material = new THREE.MeshBasicMaterial({ color: Math.random() * 0xD2001A });
cube = new THREE.Mesh(geometry, material);
cube.position.x = randomInRange(-30, 30);
cube.position.y = randomInRange(-30, 30);
cube.position.z = randomInRange(-50, 50);
if (reverse) {
x = x + 0.000001;
camera.rotation.z += x;
} else {
x = x - 0.000001;
camera.rotation.z -= x;
}
scene.add(cube);
element.push(cube);
};
let mainloop = function () {
let x = Math.random();
createBox();
renderer.render(scene, camera);
requestAnimationFrame(mainloop);
};
let randomInRange = function (from, to) {
let x = Math.random() * (to - from);
return (x + from) * 2;
};
let randomSize = function (from, to) {
let x = Math.random() * (to - from);
return (x + from);
};
let randomNumber = function (from, to) {
let x = Math.random() * (to - from);
return x;
};
init();
mainloop();
</script>