Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
83 lines (74 sloc) 3.06 KB
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Triangles</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.</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,
angleX, angleY;
//the letter 'A', using 11 points
points[0] = new Point3d( -50, -250, 100);
points[1] = new Point3d( 50, -250, 100);
points[2] = new Point3d( 200, 250, 100);
points[3] = new Point3d( 100, 250, 100);
points[4] = new Point3d( 50, 100, 100);
points[5] = new Point3d( -50, 100, 100);
points[6] = new Point3d(-100, 250, 100);
points[7] = new Point3d(-200, 250, 100);
points[8] = new Point3d( 0, -150, 100);
points[9] = new Point3d( 50, 0, 100);
points[10] = new Point3d( -50, 0, 100);
points.forEach(function (point) {
point.setVanishingPoint(vpX, vpY);
point.setCenter(0, 0, 200);
});
//create 11 triangle objects from our points
triangles[0] = new Triangle(points[0], points[1], points[8], "#ffcccc");
triangles[1] = new Triangle(points[1], points[9], points[8], "#ffcccc");
triangles[2] = new Triangle(points[1], points[2], points[9], "#ffcccc");
triangles[3] = new Triangle(points[2], points[4], points[9], "#ffcccc");
triangles[4] = new Triangle(points[2], points[3], points[4], "#ffcccc");
triangles[5] = new Triangle(points[4], points[5], points[9], "#ffcccc");
triangles[6] = new Triangle(points[9], points[5], points[10], "#ffcccc");
triangles[7] = new Triangle(points[5], points[6], points[7], "#ffcccc");
triangles[8] = new Triangle(points[5], points[7], points[10], "#ffcccc");
triangles[9] = new Triangle(points[0], points[10], points[7], "#ffcccc");
triangles[10] = new Triangle(points[0], points[8], points[10], "#ffcccc");
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>