描画中にカーソルで線を引く必要があります。mousemoveイベントを試しましたが、機能しませんでした。以下は私が現在持っているものです。
http://jsfiddle.net/m1erickson/qwd2a/
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var canvasMouseX;
var canvasMouseY;
var canvasOffset = $("#canvas").offset();
var offsetX = canvasOffset.left;
var offsetY = canvasOffset.top;
var storedLines = [];
ctx.strokeStyle = "orange";
ctx.font = '12px Arial';
$("#canvas").mousedown(function (e) {
handleMouseDown(e);
});
function handleMouseDown(e) {
canvasMouseX = parseInt(e.clientX - offsetX);
canvasMouseY = parseInt(e.clientY - offsetY);
// Put your mousedown stuff here
storedLines.push({
x: canvasMouseX,
y: canvasMouseY
});
var count = storedLines.length;
var X = canvasMouseX - (count < 10 ? 4 : 7);
ctx.strokeStyle = "orange";
ctx.fillStyle = "black";
ctx.lineWidth = 1;
ctx.beginPath();
ctx.arc(canvasMouseX, canvasMouseY, 8, 0, 2 * Math.PI, false);
ctx.fillText(storedLines.length, X, canvasMouseY + 4);
ctx.stroke();
}
$("#draw").click(function () {
ctx.strokeStyle = "red";
ctx.fillStyle = "blue";
ctx.lineWidth = 3;
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.beginPath();
ctx.moveTo(storedLines[0].x, storedLines[0].y);
for (var i = 0; i < storedLines.length; i++) {
ctx.lineTo(storedLines[i].x, storedLines[i].y);
}
ctx.closePath();
ctx.fill();
ctx.stroke();
storedLines = [];
});
$("#clear").click(function () {
ctx.clearRect(0, 0, canvas.width, canvas.height);
storedLines = [];
});