-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbig_cube_mouse_movement.html
More file actions
71 lines (61 loc) · 2.02 KB
/
big_cube_mouse_movement.html
File metadata and controls
71 lines (61 loc) · 2.02 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>3D Cursor Cube</title>
<style>
body {
margin: 0;
overflow: hidden;
cursor: none; /* Hide default cursor */
}
canvas {
display: block;
}
</style>
</head>
<body>
<!-- Three.js via CDN -->
<script src="https://cdn.jsdelivr.net/npm/three@0.158.0/build/three.min.js"></script>
<script>
// Setup scene, camera, renderer
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer({ alpha: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// Create wireframe cube
const geometry = new THREE.BoxGeometry(1, 1, 1);
const edges = new THREE.EdgesGeometry(geometry);
const material = new THREE.LineBasicMaterial({ color: 0x888888 }); // grey
const cube = new THREE.LineSegments(edges, material);
scene.add(cube);
camera.position.z = 5;
// Track mouse position
let mouseX = 0, mouseY = 0;
document.addEventListener('mousemove', (event) => {
mouseX = (event.clientX / window.innerWidth) * 2 - 1;
mouseY = -(event.clientY / window.innerHeight) * 2 + 1;
});
// Animate cube
function animate() {
requestAnimationFrame(animate);
// Smooth cube position toward cursor
const vector = new THREE.Vector3(mouseX, mouseY, 0.5).unproject(camera);
cube.position.lerp(vector, 0.2);
// Rotate cube
cube.rotation.x += 0.01;
cube.rotation.y += 0.01;
renderer.render(scene, camera);
}
animate();
// Handle window resize
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
</script>
</body>
</html>