HTML and CSS Reference
In-Depth Information
handles = [],
numHandles = 3,
spring = 0.03,
friction = 0.9,
movingHandle = null;
for (var handle, i = 0; i < numHandles; i++) {
handle = new Ball(10, "#0000ff");
handle.x = Math.random() * canvas.width;
handle.y = Math.random() * canvas.height;
handles.push(handle);
}
canvas.addEventListener('mousedown', function () {
handles.forEach(function (handle) {
if (utils.containsPoint(handle.getBounds(), mouse.x, mouse.y)) {
movingHandle = handle;
}
});
}, false);
canvas.addEventListener('mouseup', function () {
if (movingHandle) {
movingHandle = null;
}
}, false);
canvas.addEventListener('mousemove', function () {
if (movingHandle) {
movingHandle.x = mouse.x;
movingHandle.y = mouse.y;
}
}, false);
function applyHandle (handle) {
var dx = handle.x - ball.x,
dy = handle.y - ball.y;
ball.vx += dx * spring;
ball.vy += dy * spring;
}
function drawHandle (handle) {
context.moveTo(ball.x, ball.y);
context.lineTo(handle.x, handle.y);
context.stroke();
handle.draw(context);
}
(function drawFrame () {
window.requestAnimationFrame(drawFrame, canvas);
context.clearRect(0, 0, canvas.width, canvas.height);
handles.forEach(applyHandle);
Search WWH ::




Custom Search