-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.html
More file actions
144 lines (122 loc) · 4.39 KB
/
test.html
File metadata and controls
144 lines (122 loc) · 4.39 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>3D Rotating Cube</title>
<script src="https://cdn.jsdelivr.net/npm/three@0.160.0/build/three.min.js"></script>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
overflow: hidden;
touch-action: none;
background: #000;
}
canvas {
width: 100vw;
height: 100vh;
touch-action: none;
}
.instructions {
position: fixed;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
color: white;
font-family: Arial, sans-serif;
text-align: center;
background: rgba(0,0,0,0.5);
padding: 10px;
border-radius: 5px;
pointer-events: none;
}
</style>
</head>
<body>
<div class="instructions">Touch and drag to rotate</div>
<script>
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// Create cube
const geometry = new THREE.BoxGeometry(2, 2, 2);
const materials = [
new THREE.MeshBasicMaterial({ color: 0xff0000 }), // right
new THREE.MeshBasicMaterial({ color: 0x00ff00 }), // left
new THREE.MeshBasicMaterial({ color: 0x0000ff }), // top
new THREE.MeshBasicMaterial({ color: 0xffff00 }), // bottom
new THREE.MeshBasicMaterial({ color: 0xff00ff }), // front
new THREE.MeshBasicMaterial({ color: 0x00ffff }), // back
];
const cube = new THREE.Mesh(geometry, materials);
scene.add(cube);
camera.position.z = 5;
// Rotation handling
let isDragging = false;
let previousTouch = { x: 0, y: 0 };
let rotationSpeed = { x: 0, y: 0 };
// Touch events
document.addEventListener('touchstart', (e) => {
isDragging = true;
previousTouch.x = e.touches[0].clientX;
previousTouch.y = e.touches[0].clientY;
});
document.addEventListener('touchmove', (e) => {
if (!isDragging) return;
const deltaX = e.touches[0].clientX - previousTouch.x;
const deltaY = e.touches[0].clientY - previousTouch.y;
rotationSpeed.x = deltaY * 0.005;
rotationSpeed.y = deltaX * 0.005;
previousTouch.x = e.touches[0].clientX;
previousTouch.y = e.touches[0].clientY;
});
document.addEventListener('touchend', () => {
isDragging = false;
rotationSpeed.x *= 0.95;
rotationSpeed.y *= 0.95;
});
// Mouse events
document.addEventListener('mousedown', (e) => {
isDragging = true;
previousTouch.x = e.clientX;
previousTouch.y = e.clientY;
});
document.addEventListener('mousemove', (e) => {
if (!isDragging) return;
const deltaX = e.clientX - previousTouch.x;
const deltaY = e.clientY - previousTouch.y;
rotationSpeed.x = deltaY * 0.005;
rotationSpeed.y = deltaX * 0.005;
previousTouch.x = e.clientX;
previousTouch.y = e.clientY;
});
document.addEventListener('mouseup', () => {
isDragging = false;
});
// Handle window resize
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
// Animation loop
function animate() {
requestAnimationFrame(animate);
if (!isDragging) {
rotationSpeed.x *= 0.95;
rotationSpeed.y *= 0.95;
}
cube.rotation.x += rotationSpeed.x;
cube.rotation.y += rotationSpeed.y;
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>