This repository has been archived by the owner on Dec 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 247
/
12-move-cube-2.html
103 lines (94 loc) · 3.57 KB
/
12-move-cube-2.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
102
103
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Move Cube 2</title>
<link rel="stylesheet" href="../include/style.css">
</head>
<body>
<header>
Example from <a href="http://amzn.com/1430236655?tag=html5anim-20"><em>Foundation HTML5 Animation with JavaScript</em></a>
</header>
<canvas id="canvas" width="400" height="400"></canvas>
<aside>Move mouse on canvas element, and press arrow keys to move cube.</aside>
<script src="../include/utils.js"></script>
<script src="./classes/point3d.js"></script>
<script src="./classes/triangle.js"></script>
<script>
window.onload = function () {
var canvas = document.getElementById('canvas'),
context = canvas.getContext('2d'),
mouse = utils.captureMouse(canvas),
points = [],
triangles = [],
fl = 250,
vpX = canvas.width / 2,
vpY = canvas.height / 2,
offsetX = 0,
offsetY = 0,
angleX, angleY;
//front four corners
points[0] = new Point3d(-100, -100, -100);
points[1] = new Point3d( 100, -100, -100);
points[2] = new Point3d( 100, 100, -100);
points[3] = new Point3d(-100, 100, -100);
//back four corners
points[4] = new Point3d(-100, -100, 100);
points[5] = new Point3d( 100, -100, 100);
points[6] = new Point3d( 100, 100, 100);
points[7] = new Point3d(-100, 100, 100);
points.forEach(function (point) {
point.setVanishingPoint(vpX, vpY);
point.setCenter(0, 0, 200);
});
//front
triangles[0] = new Triangle(points[0], points[1], points[2], "#6666cc");
triangles[1] = new Triangle(points[0], points[2], points[3], "#6666cc");
//top
triangles[2] = new Triangle(points[0], points[5], points[1], "#66cc66");
triangles[3] = new Triangle(points[0], points[4], points[5], "#66cc66");
//back
triangles[4] = new Triangle(points[4], points[6], points[5], "#cc6666");
triangles[5] = new Triangle(points[4], points[7], points[6], "#cc6666");
//bottom
triangles[6] = new Triangle(points[3], points[2], points[6], "#cc66cc");
triangles[7] = new Triangle(points[3], points[6], points[7], "#cc66cc");
//right
triangles[8] = new Triangle(points[1], points[5], points[6], "#66cccc");
triangles[9] = new Triangle(points[1], points[6], points[2], "#66cccc");
//left
triangles[10] = new Triangle(points[4], points[0], points[3], "#cccc66");
triangles[11] = new Triangle(points[4], points[3], points[7], "#cccc66");
window.addEventListener('keydown', function (event) {
if (event.keyCode === 37) { //left
offsetX -= 5;
} else if (event.keyCode === 39) { //right
offsetX += 5;
} else if (event.keyCode === 38) { //up
offsetY -= 5;
} else if (event.keyCode === 40) { //down
offsetY += 5;
}
points.forEach(function (point) {
point.setCenter(offsetX, offsetY, 200);
});
}, false);
function move (point) {
point.rotateX(angleX);
point.rotateY(angleY);
}
function draw (triangle) {
triangle.draw(context);
}
(function drawFrame () {
window.requestAnimationFrame(drawFrame, canvas);
context.clearRect(0, 0, canvas.width, canvas.height);
angleX = (mouse.y - vpY) * 0.0005;
angleY = (mouse.x - vpX) * 0.0005;
points.forEach(move);
triangles.forEach(draw);
}());
};
</script>
</body>
</html>