在点击或触摸事件上绘制

时间:2012-12-08 20:12:45

标签: javascript html5 html5-canvas

im learnig javascript并尝试开发一个页面来绘制。更改颜色和线宽已经到位。现在它正在绘制,但就在我移动鼠标时。我还想在移动时添加点击。所以我也可以画点。我已将drawCircle函数放在函数stopDraw中,但这不能正常工作。有什么想法吗?

第二个问题是,我想画一个没有笔画的圆圈。但是一旦我改变了lineWidth,我的圆圈也会一下子。请帮忙......

// Setup event handlers

cb_canvas = document.getElementById("cbook");

cb_lastPoints = Array();

if (cb_canvas.getContext) {
    cb_ctx = cb_canvas.getContext('2d');
    cb_ctx.lineWidth = 14;
    cb_ctx.strokeStyle = "#0052f8";
    cb_ctx.lineJoin="round";
    cb_ctx.lineCap="round"
    cb_ctx.beginPath();

    cb_canvas.onmousedown = startDraw;
    cb_canvas.onmouseup = stopDraw;
    cb_canvas.ontouchstart = startDraw;
    cb_canvas.ontouchend = stopDraw;
    cb_canvas.ontouchmove = drawMouse;
}

function startDraw(e) {
if (e.touches) {
    // Touch event
    for (var i = 1; i <= e.touches.length; i++) {
        cb_lastPoints[i] = getCoords(e.touches[i - 1]); // Get info for finger #1
    }
}
else {
    // Mouse event
    cb_lastPoints[0] = getCoords(e);
    cb_canvas.onmousemove = drawMouse;
}

return false;
}

// Called whenever cursor position changes after drawing has started
function stopDraw(e) {
drawCircle(e);
e.preventDefault();
cb_canvas.onmousemove = null;
}

//Draw circle
function drawCircle(e) {
var canvasOffset = canvas.offset();
var canvasX = Math.floor(e.pageX-canvasOffset.left);
var canvasY = Math.floor(e.pageY-canvasOffset.top);
//var canvasPos = getCoords(e);

cb_ctx.beginPath();
cb_ctx.arc(canvasX, canvasY, cb_ctx.lineWidth/2, 0, Math.PI*2, true);
cb_ctx.fillStyle = cb_ctx.strokeStyle;
cb_ctx.fill();

}

function drawMouse(e) {
cb_ctx.beginPath();
if (e.touches) {
    // Touch Enabled
    for (var i = 1; i <= e.touches.length; i++) {
        var p = getCoords(e.touches[i - 1]); // Get info for finger i
    cb_lastPoints[i] = drawLine(cb_lastPoints[i].x, cb_lastPoints[i].y, p.x, p.y);
    }
}
else {
    // Not touch enabled
    var p = getCoords(e);
    cb_lastPoints[0] = drawLine(cb_lastPoints[0].x, cb_lastPoints[0].y, p.x, p.y);
}
cb_ctx.stroke();
cb_ctx.closePath();
//cb_ctx.beginPath();

return false;
}

// Draw a line on the canvas from (s)tart to (e)nd
function drawLine(sX, sY, eX, eY) {
cb_ctx.moveTo(sX, sY);
cb_ctx.lineTo(eX, eY);
return { x: eX, y: eY };
}

// Get the coordinates for a mouse or touch event
function getCoords(e) {
if (e.offsetX) {
    return { x: e.offsetX, y: e.offsetY };
}
else if (e.layerX) {
    return { x: e.layerX, y: e.layerY };
}
else {
    return { x: e.pageX - cb_canvas.offsetLeft, y: e.pageY - cb_canvas.offsetTop };
}
}

1 个答案:

答案 0 :(得分:0)

您的drawCircle函数包含在画布的onclick事件中。它应该是

function drawCircle(e) {
    var canvasOffset = canvas.offset();
    var canvasX = Math.floor(e.pageX-canvasOffset.left);
    var canvasY = Math.floor(e.pageY-canvasOffset.top);
    cb_ctx.beginPath();
    cb_ctx.lineWidth = 0;
    cb_ctx.arc(canvasX,canvasY,cb_ctx.lineWidth/2,0,Math.PI*2,true);
    cb_ctx.fillStyle = cb_ctx.strokeStyle;
    cb_ctx.fill();
}

所以你必须从停止绘图中传递事件对象

function stopDraw(e) {
    drawCircle(e); //notice the change here
    e.preventDefault();
    cb_canvas.onmousemove = null;
}

请注意,您还要将圆的半径设置为0,这可能是它未显示的另一个原因。

cb_ctx.lineWidth = 0;
cb_ctx.arc(canvasX,canvasY,cb_ctx.lineWidth/2,0,Math.PI*2,true);

0除以2始终为零。如果你不想围绕圆圈描边,就不要调用stroke()。 drawCircle函数没有引起这种情况,因为有一个beginPath()调用。我怀疑是因为你在drawMouse中调用stroke()之前没有使用beginPath()。