Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
49 lines (40 loc) · 1.4 KB
/
Copy pathindex.js
File metadata and controls
49 lines (40 loc) · 1.4 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
import * as THREE from "https://cdn.jsdelivr.net/npm/three@0.160.0/build/three.module.js";
// Scene & Camera
let scene = new THREE.Scene();
scene.background = new THREE.Color("white"); // light sky blue
let camera = new THREE.PerspectiveCamera(
60,
window.innerWidth / window.innerHeight,
0.1,
100
);
camera.position.z = 4;
scene.add(camera);
// Cube
let box = new THREE.BoxGeometry(1, 1, 1,2);
let material = new THREE.MeshStandardMaterial({ color: "red" });
let mesh = new THREE.Mesh(box, material);
scene.add(mesh);
// Light (so we can see materials properly)
let light = new THREE.DirectionalLight(0xffffff, 2);
light.position.set(2, 2, 5);
scene.add(light);
// Renderer
const canvas = document.querySelector("canvas");
let renderer = new THREE.WebGLRenderer({ canvas: canvas });
renderer.setSize(window.innerWidth, window.innerHeight);
// Track mouse movement
let mouse = { x: 0, y: 0 };
window.addEventListener("mousemove", (event) => {
mouse.x = (event.clientX / window.innerWidth) * 2 ; // range: -1 to 1
mouse.y = (event.clientY / window.innerHeight) ; // range: -1 to 1
});
// Animate
function animate() {
window.requestAnimationFrame(animate);
// Rotate cube based on mouse position
mesh.rotation.x = mouse.x * Math.PI; // left-right movement
mesh.rotation.y = mouse.y * Math.PI; // up-down movement
renderer.render(scene, camera);
}
animate();