A line:
ctx.beginPath(); ctx.moveTo(10, 10); ctx.lineTo(100, 100); ctx.stroke() ctx.closePath(); api.mousemove(function(x,y) { console.log(x,y); });
A draggable ball:
function DragBall(x,y) { if ( !(this instanceof arguments.callee) ) return new DragBall(x, y); var x = 0; var y = 0; var mousedown = false; var rad = 10; var that = this; this.draw = function() { api.circle(x, y, rad); } api.mousemove(function(xx, yy) { if (mousedown) { console.log("moving to ", xx, yy); x = xx; y = yy; that.draw(); } }); api.mousedown(function() { mousedown = true; }); api.mouseup(function() { mousedown = false; }); this.draw(); } d = new DragBall(30, 30); $(canvas).mousedown(function(evt) { mousedown = true; }); $(canvas).mouseup(function(evt) { mousedown = false; }); function drawball(x, y) { ctx.beginPath(); ctx.arc(x, y, 10, 0, Math.PI*2, true); ctx.closePath(); ctx.fill(); }