-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreejs.html
More file actions
181 lines (145 loc) · 4.54 KB
/
Copy paththreejs.html
File metadata and controls
181 lines (145 loc) · 4.54 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Ryan's World - ThreeJS</title>
<script src="https://kit.fontawesome.com/f5be8e1e8e.js" crossorigin="anonymous"></script>
<style>
@import url('https://fonts.googleapis.com/css?family=Montserrat:400,700&display=swap');
body {
margin: 0;
height: 100vh;
font-family: 'Montserrat';
}
a {
position: absolute;
top: 3%;
left: 3%;
font-size: 1.2rem;
font-weight: bold;
cursor: pointer;
}
canvas {
display: block;
}
#text {
position: absolute;
top: 50vh;
left: 50vw;
transform: translate(-50%, -50%);
width: auto;
border: 5px solid black;
padding: .5em;
text-align: center;
}
#text h1 {
font-size: 7rem;
text-transform: uppercase;
/* line-height: 1em; */
}
#text p {
font-size: 2em
}
@media (max-width: 650px) {
#text h1 {
font-size: 4em;
}
}
</style>
</head>
<body>
<a onclick="goBack()"><i class="fas fa-arrow-left"></i> Go Back</a>
<div id="text">
<h1>ThreeJS Rules!</h1>
<p><b>To have fun:</b> click in a cube!</p>
</div>
<script>
function goBack() {
window.history.back();
}
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/102/three.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/2.1.2/TweenMax.min.js"></script>
<script>
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.z = 5;
var renderer = new THREE.WebGLRenderer({
antialias: true
});
renderer.setClearColor('#e5e5e5');
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
window.addEventListener('resize', () => {
renderer.setSize(window.innerWidth, window.innerHeight);
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
})
var raycaster = new THREE.Raycaster();
var mouse = new THREE.Vector2();
var geometry = new THREE.BoxGeometry(1, 1, 1);
var material = new THREE.MeshLambertMaterial({
color: 0xF7F7F7
});
// var mesh = new THREE.Mesh(geometry, material);
// mesh.position.set(2, 2, -2);
// mesh.rotation.set(45, 0, 2);
// scene.add(mesh);
meshX = -10;
for (var j = 0; j < 15; ++j) {
var mesh = new THREE.Mesh(geometry, material);
mesh.position.x = (Math.random() - 0.5) * 10;
mesh.position.y = (Math.random() - 0.5) * 10;
mesh.position.z = (Math.random() - 0.5) * 10;
scene.add(mesh);
meshX++;
}
var light = new THREE.PointLight(0xFFFFFF, 1, 1000);
light.position.set(0, 0, 0);
scene.add(light);
var light = new THREE.PointLight(0xFFFFFF, 2, 1000);
light.position.set(0, 0, 25);
scene.add(light);
// This function refreshs de page everytime the window change it's size
var render = function () {
requestAnimationFrame(render);
renderer.render(scene, camera);
};
function onMouseMove(event) {
event.preventDefault();
mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
raycaster.setFromCamera(mouse, camera);
var intersects = raycaster.intersectObjects(scene.children, true);
for (var i = 0; i < intersects.length; i++) {
// intersects[i].object.material.color.set(0xff0000);
this.tl = new TimelineMax();
const form = intersects[i].object;
// console.log(form);
const formPositionX = form.position.x;
const formRotationY = form.rotation.y;
const power = Math.floor(Math.random() * 10);
const direction = Math.pow(-1, power);
// console.log(direction, power);
this.tl.to(form.scale, 1, {
x: 2,
ease: Expo.easeOut
}).to(form.scale, .5, {
x: 1,
ease: Expo.easeOut
}).to(form.position, .5, {
x: formPositionX + direction * (Math.random() * 5),
ease: Expo.easeOut
}).to(form.rotation, .5, {
y: formRotationY + direction * (Math.PI * .5),
ease: Expo.easeOut
}, "-=1.5");
}
}
window.addEventListener('click', onMouseMove);
render();
</script>
</body>
</html>