-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
98 lines (77 loc) · 2.54 KB
/
app.js
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
import * as THREE from 'three';
const threeContainer = document.querySelector('#three-container');
const mainContent = document.querySelector('#main-content');
let isOnMain = false;
mainContent.addEventListener('mouseenter', () => (isOnMain = true));
mainContent.addEventListener('mouseleave', () => (isOnMain = false));
let mouseX = 0;
// let mouseY = 0;
// bg rsc
const path = './assets/bg-textures/';
const format = '.webp';
const urls = [
path + 'nx' + format,
path + 'px' + format,
path + 'py' + format,
path + 'ny' + format,
path + 'nz' + format,
path + 'pz' + format,
];
// scene
const textureCube = new THREE.CubeTextureLoader().load(urls);
const scene = new THREE.Scene();
scene.background = textureCube;
// camera
const camera = new THREE.PerspectiveCamera(
100,
mainContent.clientWidth / mainContent.clientHeight,
0.01,
1000
);
camera.position.z = 5;
// renderer
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(mainContent.clientWidth, mainContent.clientHeight);
threeContainer.appendChild(renderer.domElement);
const vector = new THREE.Vector4(0, 1, 0, 0);
function render() {
if (
isOnMain ||
/Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent)
) {
camera.position.x += (-mouseX - camera.position.x) * 0.01;
// camera.position.y += (-mouseY - camera.position.y) * 0.1;
camera.lookAt(scene.position);
}
renderer.render(scene, camera);
}
function animate() {
requestAnimationFrame(animate);
// renderer.render(scene, camera);
render();
}
function onDocumentMouseMove(e) {
mouseX = (e.clientX - window.innerWidth / 2) / 100;
// mouseY = (e.clientY - window.innerHeight / 2) / 100;
}
function onDocumentTouchMove(e) {
mouseX = (e.touches[0].clientX - window.innerWidth / 2) / 100;
// mouseY = (e.clientY - window.innerHeight / 2) / 100;
}
function onWindowResize() {
camera.aspect = mainContent.clientWidth / mainContent.clientHeight;
camera.updateProjectionMatrix();
renderer.setSize(mainContent.clientWidth, mainContent.clientHeight);
}
document.addEventListener('mousemove', onDocumentMouseMove);
document.addEventListener('touchmove', onDocumentTouchMove);
window.addEventListener('resize', onWindowResize);
animate();
/* Search input interaction */
const searchBar = document.querySelector('#search-bar');
const searchBarLabel = document.querySelector('#search-bar-label');
searchBar.addEventListener('blur', function () {
this.value
? searchBarLabel.classList.add('isComplete')
: searchBarLabel.classList.remove('isComplete');
});