HTML and CSS Reference
In-Depth Information
var dx = xpos - segment.x,
dy = ypos - segment.y;
segment.rotation = Math.atan2(dy, dx);
var w = segment.getPin().x - segment.x,
h = segment.getPin().y - segment.y;
return {
x: xpos - w,
y: ypos - h
};
}
function position (segmentA, segmentB) {
segmentA.x = segmentB.getPin().x;
segmentA.y = segmentB.getPin().y;
}
(function drawFrame () {
window.requestAnimationFrame(drawFrame, canvas);
context.clearRect(0, 0, canvas.width, canvas.height);
var target = reach(segment0, mouse.x, mouse.y);
reach(segment1, target.x, target.y);
position(segment0, segment1);
segment0.draw(context);
segment1.draw(context);
}());
};
</script>
</body>
</html>
Building off this example, it's easy to create an array to hold as many segments as you like. The next
exercise does just that, 06-multi-segment-reach.html :
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Multi Segment Reach</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<canvas id="canvas" width="400" height="400"></canvas>
<script src="utils.js"></script>
<script src="segment.js"></script>
<script>
window.onload = function () {
var canvas = document.getElementById('canvas'),
context = canvas.getContext('2d'),
mouse = utils.captureMouse(canvas),
Search WWH ::




Custom Search