HTML and CSS Reference
In-Depth Information
Dragging More Segments
Now you can add as many segments as you want. In the next example, add five segments, named
segment0 through segment4 , and store them in an array. Each segment is passed as an argument to the
drag function. Here's the code (document 04-multi-segment-drag.html ):
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Multi Segment Drag</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),
segments = [],
numSegments = 5;
while (numSegments--) {
segments.push(new Segment(50, 10));
}
function drag (segment, xpos, ypos) {
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;
segment.x = xpos - w;
segment.y = ypos - h;
}
function move (segment, i) {
if (i !== 0) {
drag(segment, segments[i-1].x, segments[i-1].y);
}
}
function draw (segment, i) {
segment.draw(context);
}
(function drawFrame () {
 
Search WWH ::




Custom Search