-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmall_cube_mouse_movement.html
More file actions
78 lines (68 loc) · 2.07 KB
/
small_cube_mouse_movement.html
File metadata and controls
78 lines (68 loc) · 2.07 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Cursor-Following Wireframe Cube</title>
<style>
body {
margin: 0;
overflow: hidden;
cursor: none;
}
canvas {
display: block;
}
</style>
</head>
<body>
<script src="https://cdn.jsdelivr.net/npm/three@0.158.0/build/three.min.js"></script>
<script>
const scene = new THREE.Scene();
// Use orthographic camera so world units = pixels
let width = window.innerWidth;
let height = window.innerHeight;
const camera = new THREE.OrthographicCamera(
-width / 2, width / 2,
height / 2, -height / 2,
0.1, 1000
);
camera.position.z = 500;
const renderer = new THREE.WebGLRenderer({ alpha: true });
renderer.setSize(width, height);
document.body.appendChild(renderer.domElement);
// Create a wireframe cube
const geometry = new THREE.BoxGeometry(200, 200, 200); // 200px cube
const edges = new THREE.EdgesGeometry(geometry);
const material = new THREE.LineBasicMaterial({ color: 0x888888 }); // grey
const cube = new THREE.LineSegments(edges, material);
scene.add(cube);
let mouse = new THREE.Vector3(0, 0, 0);
document.addEventListener('mousemove', (event) => {
mouse.x = event.clientX - width / 2;
mouse.y = -(event.clientY - height / 2);
});
function animate() {
requestAnimationFrame(animate);
// Smoothly move cube toward cursor
cube.position.lerp(mouse, 0.15);
// Rotate cube
cube.rotation.x += 0.01;
cube.rotation.y += 0.01;
renderer.render(scene, camera);
}
animate();
// Handle window resize
window.addEventListener('resize', () => {
width = window.innerWidth;
height = window.innerHeight;
camera.left = -width / 2;
camera.right = width / 2;
camera.top = height / 2;
camera.bottom = -height / 2;
camera.updateProjectionMatrix();
renderer.setSize(width, height);
});
</script>
</body>
</html>