0

这是我的代码:

var can = $('#signature')[0],
    ctx = can.getContext('2d'),
    mousePressed = false,
    mouseX = 0,
    mouseY = 0;
can.addEventListener('touchmove', onTouchMove, false);
can.addEventListener('touchstart', onTouchStart, false);
can.addEventListener('touchend', onMouseUp, false);
// Tablet
function onTouchMove(event){
    if (mousePressed) {
        event.preventDefault();
        mouseX = (event.targetTouches[0].pageX) - can.offsetLeft;
        mouseY = (event.targetTouches[0].pageY) - can.offsetTop;
        ctx.lineTo(mouseX, mouseY);
        ctx.stroke();
    }
}

function onTouchStart(event){
    mousePressed = true;
    mouseX = (event.targetTouches[0].pageX) - can.offsetLeft;
    mouseY = (event.targetTouches[0].pageY) - can.offsetTop;
    ctx.beginPath();
    ctx.moveTo(mouesX, mouseY);
}

function onMouseUp(event){
    mousePressed = false;
}
// Desktop
can.addEventListener('mousemove', onMouseMove, false);
can.addEventListener('mousedown', onMouseDown, false);
can.addEventListener('mouseup', onMouseUp, false);

function onMouseMove(event) {
    if (mousePressed) {
        event.preventDefault();
        mouseX = event.clientX - can.offsetLeft;
        mouseY = event.clientY - can.offsetTop;
        ctx.lineTo(mouseX, mouseY);
        ctx.stroke();
    }
}

function onMouseDown(event) {
    mousePressed = true;
    mouseX = event.clientX - can.offsetLeft;
    mouseY = event.clientY - can.offsetTop;
    ctx.beginPath();
    ctx.moveTo(mouseX, mouseY);
}

$('#clearsig')[0].addEventListener('click', clearSignature, false);

function clearSignature() {
    ctx.clearRect(0, 0, can.width, can.height);
}

的HTML:

<canvas id="signature" width="800" height="350" style="border:1px solid #000;"></canvas>

在桌面上,一切都用鼠标画得很好,我没有问题。然而,在我的银河选项卡 3 上,当我在画布上绘图时,线条会画出锯齿形图案。不知道为什么会发生这种情况或如何解决它。

4

1 回答 1

0

not sure if you've found an answer for this but I had a similar issue and finally managed to solve it. Drawing lines on canvas on galaxy tab 2 and 3, also galaxy note 10.1(2012 edition, no problem on 2014 edition) can create jagged lines. Same applies for Izzey's jsfiddle. If your issue is the same as mine then the device is picking both mousemove and touchmove.. It basically draws the line twice with messed up mousemove values. Have a look at this http://jbkflex.wordpress.com/2012/07/12/javascript-touch-event-or-mouse-event-detect-and-handle-according-to-device/ It worked for me. I really hope this helps.. :)

于 2014-07-10T14:49:34.487 回答