Spaces:
Running
Running
Update index.html
Browse files- index.html +163 -19
index.html
CHANGED
@@ -1,19 +1,163 @@
|
|
1 |
-
<!
|
2 |
-
<html>
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
</
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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>Rubik's Cube</title>
|
7 |
+
<style>
|
8 |
+
body { margin: 0; overflow: hidden; }
|
9 |
+
canvas { width: 100%; height: 100%; }
|
10 |
+
</style>
|
11 |
+
</head>
|
12 |
+
<body>
|
13 |
+
<div id="controls">
|
14 |
+
<button onclick="rotate('U')">U</button>
|
15 |
+
<button onclick="rotate('U\'')">U'</button>
|
16 |
+
<button onclick="rotate('D')">D</button>
|
17 |
+
<button onclick="rotate('D\'')">D'</button>
|
18 |
+
<button onclick="rotate('L')">L</button>
|
19 |
+
<button onclick="rotate('L\'')">L'</button>
|
20 |
+
<button onclick="rotate('R')">R</button>
|
21 |
+
<button onclick="rotate('R\'')">R'</button>
|
22 |
+
<button onclick="rotate('F')">F</button>
|
23 |
+
<button onclick="rotate('F\'')">F'</button>
|
24 |
+
<button onclick="rotate('B')">B</button>
|
25 |
+
<button onclick="rotate('B\'')">B'</button>
|
26 |
+
<button onclick="scramble()">Scramble</button>
|
27 |
+
<button onclick="unscramble()">Unscramble</button>
|
28 |
+
</div>
|
29 |
+
<script type="module">
|
30 |
+
import * as THREE from 'https://cdn.skypack.dev/[email protected]';
|
31 |
+
import { OrbitControls } from 'https://cdn.skypack.dev/[email protected]/examples/jsm/controls/OrbitControls.js';
|
32 |
+
|
33 |
+
const scene = new THREE.Scene();
|
34 |
+
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
|
35 |
+
const renderer = new THREE.WebGLRenderer();
|
36 |
+
renderer.setSize(window.innerWidth, window.innerHeight);
|
37 |
+
document.body.appendChild(renderer.domElement);
|
38 |
+
|
39 |
+
const controls = new OrbitControls(camera, renderer.domElement);
|
40 |
+
camera.position.z = 5;
|
41 |
+
|
42 |
+
const cubeSize = 0.9;
|
43 |
+
const colors = {
|
44 |
+
'U': 0xFFFFFF, 'D': 0xFFFF00, 'L': 0xFFA500,
|
45 |
+
'R': 0x00FF00, 'F': 0x0000FF, 'B': 0xFF00FF
|
46 |
+
};
|
47 |
+
|
48 |
+
// Create cubies
|
49 |
+
const cubies = [];
|
50 |
+
for (let x = -1; x <= 1; x++) {
|
51 |
+
for (let y = -1; y <= 1; y++) {
|
52 |
+
for (let z = -1; z <= 1; z++) {
|
53 |
+
const geometry = new THREE.BoxGeometry(cubeSize, cubeSize, cubeSize);
|
54 |
+
const material = new THREE.MeshStandardMaterial({
|
55 |
+
color: 0x000000,
|
56 |
+
emissive: 0x000000
|
57 |
+
});
|
58 |
+
|
59 |
+
const cubie = new THREE.Mesh(geometry, material);
|
60 |
+
cubie.position.set(x * (cubeSize + 0.01), y * (cubeSize + 0.01), z * (cubeSize + 0.01));
|
61 |
+
|
62 |
+
// Paint faces
|
63 |
+
for (let face of ['U', 'D', 'L', 'R', 'F', 'B']) {
|
64 |
+
if ((face === 'U' && y === 1) || (face === 'D' && y === -1) ||
|
65 |
+
(face === 'L' && x === -1) || (face === 'R' && x === 1) ||
|
66 |
+
(face === 'F' && z === 1) || (face === 'B' && z === -1)) {
|
67 |
+
cubie.geometry.faces[getFaceIndex(face)].color.setHex(colors[face]);
|
68 |
+
}
|
69 |
+
}
|
70 |
+
cubie.geometry.colorsNeedUpdate = true;
|
71 |
+
cubies.push({ mesh: cubie, originalPosition: cubie.position.clone(), originalRotation: cubie.rotation.clone() });
|
72 |
+
scene.add(cubie);
|
73 |
+
}
|
74 |
+
}
|
75 |
+
}
|
76 |
+
|
77 |
+
// Light setup
|
78 |
+
const ambientLight = new THREE.AmbientLight(0x404040);
|
79 |
+
scene.add(ambientLight);
|
80 |
+
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.5);
|
81 |
+
directionalLight.position.set(5, 5, 5);
|
82 |
+
scene.add(directionalLight);
|
83 |
+
|
84 |
+
// Cube rotation logic
|
85 |
+
const moveMap = {
|
86 |
+
'U': { axis: 'y', layer: 1, direction: 1, affectedCubies: cubies.filter(c => c.mesh.position.y === cubeSize) },
|
87 |
+
'U\'': { axis: 'y', layer: 1, direction: -1, affectedCubies: cubies.filter(c => c.mesh.position.y === cubeSize) },
|
88 |
+
// ... other moves similarly defined
|
89 |
+
};
|
90 |
+
|
91 |
+
let movesHistory = [];
|
92 |
+
|
93 |
+
function rotate(move) {
|
94 |
+
const { axis, layer, direction, affectedCubies } = moveMap[move];
|
95 |
+
const pivot = new THREE.Object3D();
|
96 |
+
scene.add(pivot);
|
97 |
+
for (let cubie of affectedCubies) {
|
98 |
+
pivot.attach(cubie.mesh);
|
99 |
+
}
|
100 |
+
const targetRotation = Math.PI / 2 * direction;
|
101 |
+
let currentRotation = 0;
|
102 |
+
|
103 |
+
function animate() {
|
104 |
+
if (currentRotation < targetRotation) {
|
105 |
+
currentRotation += 0.1;
|
106 |
+
pivot.rotation[axis] = currentRotation;
|
107 |
+
requestAnimationFrame(animate);
|
108 |
+
} else {
|
109 |
+
pivot.rotation[axis] = targetRotation;
|
110 |
+
for (let cubie of affectedCubies) {
|
111 |
+
cubie.mesh.applyMatrix4(pivot.matrix);
|
112 |
+
scene.attach(cubie.mesh);
|
113 |
+
}
|
114 |
+
scene.remove(pivot);
|
115 |
+
movesHistory.push(move);
|
116 |
+
}
|
117 |
+
}
|
118 |
+
animate();
|
119 |
+
}
|
120 |
+
|
121 |
+
function scramble() {
|
122 |
+
const moves = ['U', 'U\'', 'D', 'D\'', 'L', 'L\'', 'R', 'R\'', 'F', 'F\'', 'B', 'B\''];
|
123 |
+
for (let i = 0; i < 20; i++) {
|
124 |
+
rotate(moves[Math.floor(Math.random() * moves.length)]);
|
125 |
+
}
|
126 |
+
}
|
127 |
+
|
128 |
+
let unscrambling = false;
|
129 |
+
function unscramble() {
|
130 |
+
if (!unscrambling) {
|
131 |
+
unscrambling = true;
|
132 |
+
const interval = setInterval(() => {
|
133 |
+
if (movesHistory.length > 0) {
|
134 |
+
let lastMove = movesHistory.pop();
|
135 |
+
let inverse = lastMove.replace("'", "") + (lastMove.includes('\'') ? '' : '\'');
|
136 |
+
rotate(inverse);
|
137 |
+
} else {
|
138 |
+
clearInterval(interval);
|
139 |
+
unscrambling = false;
|
140 |
+
}
|
141 |
+
}, 500);
|
142 |
+
}
|
143 |
+
}
|
144 |
+
|
145 |
+
function animate() {
|
146 |
+
requestAnimationFrame(animate);
|
147 |
+
renderer.render(scene, camera);
|
148 |
+
}
|
149 |
+
animate();
|
150 |
+
|
151 |
+
function getFaceIndex(face) {
|
152 |
+
switch(face) {
|
153 |
+
case 'U': return 4; // top
|
154 |
+
case 'D': return 5; // bottom
|
155 |
+
case 'L': return 0; // left
|
156 |
+
case 'R': return 1; // right
|
157 |
+
case 'F': return 2; // front
|
158 |
+
case 'B': return 3; // back
|
159 |
+
}
|
160 |
+
}
|
161 |
+
</script>
|
162 |
+
</body>
|
163 |
+
</html>
|