1+ <!DOCTYPE html>
2+ < html lang ="en ">
3+ < head >
4+ < meta charset ="UTF-8 " />
5+ < meta name ="viewport " content ="width=device-width, initial-scale=1.0 "/>
6+ < title > Cursor-Following Wireframe Cube</ title >
7+ < style >
8+ body {
9+ margin : 0 ;
10+ overflow : hidden;
11+ cursor : none;
12+ }
13+ canvas {
14+ display : block;
15+ }
16+ </ style >
17+ </ head >
18+ < body >
19+ < script src ="https://cdn.jsdelivr.net/npm/three@0.158.0/build/three.min.js "> </ script >
20+ < script >
21+ const scene = new THREE . Scene ( ) ;
22+
23+ // Use orthographic camera so world units = pixels
24+ let width = window . innerWidth ;
25+ let height = window . innerHeight ;
26+ const camera = new THREE . OrthographicCamera (
27+ - width / 2 , width / 2 ,
28+ height / 2 , - height / 2 ,
29+ 0.1 , 1000
30+ ) ;
31+ camera . position . z = 500 ;
32+
33+ const renderer = new THREE . WebGLRenderer ( { alpha : true } ) ;
34+ renderer . setSize ( width , height ) ;
35+ document . body . appendChild ( renderer . domElement ) ;
36+
37+ // Create a wireframe cube
38+ const geometry = new THREE . BoxGeometry ( 200 , 200 , 200 ) ; // 200px cube
39+ const edges = new THREE . EdgesGeometry ( geometry ) ;
40+ const material = new THREE . LineBasicMaterial ( { color : 0x888888 } ) ; // grey
41+ const cube = new THREE . LineSegments ( edges , material ) ;
42+ scene . add ( cube ) ;
43+
44+ let mouse = new THREE . Vector3 ( 0 , 0 , 0 ) ;
45+ document . addEventListener ( 'mousemove' , ( event ) => {
46+ mouse . x = event . clientX - width / 2 ;
47+ mouse . y = - ( event . clientY - height / 2 ) ;
48+ } ) ;
49+
50+ function animate ( ) {
51+ requestAnimationFrame ( animate ) ;
52+
53+ // Smoothly move cube toward cursor
54+ cube . position . lerp ( mouse , 0.15 ) ;
55+
56+ // Rotate cube
57+ cube . rotation . x += 0.01 ;
58+ cube . rotation . y += 0.01 ;
59+
60+ renderer . render ( scene , camera ) ;
61+ }
62+
63+ animate ( ) ;
64+
65+ // Handle window resize
66+ window . addEventListener ( 'resize' , ( ) => {
67+ width = window . innerWidth ;
68+ height = window . innerHeight ;
69+ camera . left = - width / 2 ;
70+ camera . right = width / 2 ;
71+ camera . top = height / 2 ;
72+ camera . bottom = - height / 2 ;
73+ camera . updateProjectionMatrix ( ) ;
74+ renderer . setSize ( width , height ) ;
75+ } ) ;
76+ </ script >
77+ </ body >
78+ </ html >
0 commit comments