-
Notifications
You must be signed in to change notification settings - Fork 2
/
rubiks.html
101 lines (93 loc) · 2.21 KB
/
rubiks.html
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
<html>
<head>
<title>Rubik's Cube</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0">
<script type="text/javascript" src='js/rubiks.js'></script>
<script type="text/javascript" src='js/solver.js'></script>
<script type="text/javascript" src='js/flat.js'></script>
<style type="text/css">
body{
background:#fafafa;
}
</style>
</head>
<body>
<canvas id="cube" style='position:absolute;left:15px;top:15px;'>HTML5 CANVAS</canvas>
<div id='controls'></div>
<div id='flat-cube'></div>
<script type="text/javascript">
var cube, cube2;
var flatCube, controls;
/**********************************
* Main Loop
**********************************/
var run = function () {
cube.tick();
cube.render();
if(cube2){
cube2.tick();
cube2.render();
}
// ---- animation loop ----
requestAnimationFrame(run);
}
var oldWidth = null;
var reset = function () {
var down = false;
var parent = document.getElementById('cube').parentElement;
var width = Math.min(parent.offsetWidth/2-30, parent.offsetHeight/5*3);
if(width < 250){
width = window.innerWidth/2-30;
}
if(width < 250){
width = window.innerWidth - 30;
down = true;
}
oldWidth = width;
cube = new RubiksCube('cube', width);
flatCube = new FlatCube('flat-cube', width, down);
controls = new RubiksCubeControls('controls', cube, width);
cube.flatCube = flatCube;
flatCube.cube = cube;
};
var init = function () {
reset();
run();
}
window.addEventListener('load', function () {
init();
function loop(){
if(cube.rotating){
setTimeout(loop,3000);
return;
}
scramble(20);
solve();
loop();
}
}, false);
window.addEventListener('resize', function(){
var down = false;
var parent = document.getElementById('cube').parentElement;
var width = Math.min(parent.offsetWidth/2-30, parent.offsetHeight/5*3);
if(width < 250){
width = window.innerWidth/2-30;
}
if(width < 250){
width = window.innerWidth - 30;
down = true;
}
if(width == oldWidth){
return;
}
oldWidth = width;
cube.updateSize(width);
controls.setWidth(width);
flatCube = new FlatCube('flat-cube', width, down);
cube.flatCube = flatCube;
flatCube.cube = cube;
cube.update();
flatCube.update();
});
</script>
</html>