Create script.js
Browse files
script.js
ADDED
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// Scene Setup
|
2 |
+
const scene = new THREE.Scene();
|
3 |
+
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
|
4 |
+
const renderer = new THREE.WebGLRenderer({
|
5 |
+
canvas: document.querySelector('#bg'),
|
6 |
+
alpha: true // Make canvas background transparent
|
7 |
+
});
|
8 |
+
|
9 |
+
renderer.setPixelRatio(window.devicePixelRatio);
|
10 |
+
renderer.setSize(window.innerWidth, window.innerHeight);
|
11 |
+
camera.position.setZ(30);
|
12 |
+
|
13 |
+
// --- OBJECTS ---
|
14 |
+
|
15 |
+
// Central Torus Knot (the glowing frame)
|
16 |
+
const geometry = new THREE.TorusKnotGeometry(18, 1, 300, 20, 2, 3);
|
17 |
+
const material = new THREE.MeshStandardMaterial({
|
18 |
+
color: 0x00ffff, // Cyan color
|
19 |
+
metalness: 0.9,
|
20 |
+
roughness: 0.2,
|
21 |
+
emissive: 0x005555 // Gives it a subtle inner glow
|
22 |
+
});
|
23 |
+
const torusKnot = new THREE.Mesh(geometry, material);
|
24 |
+
scene.add(torusKnot);
|
25 |
+
|
26 |
+
// --- LIGHTS ---
|
27 |
+
|
28 |
+
const pointLight = new THREE.PointLight(0xffffff, 1.5); // A bright white light
|
29 |
+
pointLight.position.set(20, 20, 20);
|
30 |
+
|
31 |
+
const ambientLight = new THREE.AmbientLight(0x00ffff, 0.3); // Ambient cyan light
|
32 |
+
scene.add(pointLight, ambientLight);
|
33 |
+
|
34 |
+
// --- HELPERS (Optional: for development) ---
|
35 |
+
// const lightHelper = new THREE.PointLightHelper(pointLight);
|
36 |
+
// const gridHelper = new THREE.GridHelper(200, 50);
|
37 |
+
// scene.add(lightHelper, gridHelper);
|
38 |
+
|
39 |
+
// --- STARFIELD ---
|
40 |
+
|
41 |
+
function addStar() {
|
42 |
+
const starGeometry = new THREE.SphereGeometry(0.25, 24, 24);
|
43 |
+
const starMaterial = new THREE.MeshStandardMaterial({ color: 0xffffff });
|
44 |
+
const star = new THREE.Mesh(starGeometry, starMaterial);
|
45 |
+
|
46 |
+
const [x, y, z] = Array(3).fill().map(() => THREE.MathUtils.randFloatSpread(200));
|
47 |
+
star.position.set(x, y, z);
|
48 |
+
scene.add(star);
|
49 |
+
}
|
50 |
+
|
51 |
+
Array(400).fill().forEach(addStar); // Add 400 stars
|
52 |
+
|
53 |
+
// --- MOUSE CONTROLS ---
|
54 |
+
|
55 |
+
let mouseX = 0;
|
56 |
+
let mouseY = 0;
|
57 |
+
document.addEventListener('mousemove', (event) => {
|
58 |
+
// Normalize mouse position (-1 to 1)
|
59 |
+
mouseX = (event.clientX / window.innerWidth) * 2 - 1;
|
60 |
+
mouseY = -(event.clientY / window.innerHeight) * 2 + 1;
|
61 |
+
});
|
62 |
+
|
63 |
+
|
64 |
+
// --- ANIMATION LOOP ---
|
65 |
+
|
66 |
+
function animate() {
|
67 |
+
requestAnimationFrame(animate);
|
68 |
+
|
69 |
+
// Object rotation
|
70 |
+
torusKnot.rotation.x += 0.001;
|
71 |
+
torusKnot.rotation.y += 0.0005;
|
72 |
+
torusKnot.rotation.z += 0.001;
|
73 |
+
|
74 |
+
// Camera parallax effect based on mouse movement
|
75 |
+
camera.position.x += (mouseX * 5 - camera.position.x) * 0.02;
|
76 |
+
camera.position.y += (mouseY * 5 - camera.position.y) * 0.02;
|
77 |
+
camera.lookAt(scene.position);
|
78 |
+
|
79 |
+
|
80 |
+
renderer.render(scene, camera);
|
81 |
+
}
|
82 |
+
|
83 |
+
// Handle window resizing
|
84 |
+
window.addEventListener('resize', () => {
|
85 |
+
camera.aspect = window.innerWidth / window.innerHeight;
|
86 |
+
camera.updateProjectionMatrix();
|
87 |
+
renderer.setSize(window.innerWidth, window.innerHeight);
|
88 |
+
});
|
89 |
+
|
90 |
+
animate(); // Start the animation
|