Permalink
Switch branches/tags
Nothing to show
Find file Copy path
f0a01ed Dec 23, 2011
1 contributor

Users who have contributed to this file

56 lines (48 sloc) 1.63 KB
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Object Hit Test</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>
<textarea id="log"></textarea>
<aside>Drag circle with mouse.</aside>
<script src="../include/utils.js"></script>
<script src="./classes/ball.js"></script>
<script>
window.onload = function () {
var canvas = document.getElementById('canvas'),
context = canvas.getContext('2d'),
log = document.getElementById('log'),
mouse = utils.captureMouse(canvas),
ballA = new Ball(),
ballB = new Ball();
ballA.x = canvas.width / 2;
ballA.y = canvas.height / 2;
(function drawFrame () {
window.requestAnimationFrame(drawFrame, canvas);
context.clearRect(0, 0, canvas.width, canvas.height);
ballB.x = mouse.x;
ballB.y = mouse.y;
if (utils.intersects(ballA.getBounds(), ballB.getBounds())) {
log.value = "Hit!";
} else {
log.value = '';
}
ballA.draw(context);
ballB.draw(context);
//draw bounding boxes
var boundsA = ballA.getBounds(),
boundsB = ballB.getBounds();
context.strokeRect(boundsA.x, boundsA.y, boundsA.width, boundsA.height);
context.strokeRect(boundsB.x, boundsB.y, boundsB.width, boundsB.height);
}());
};
</script>
</body>
</html>