简体   繁体   English

用于触摸设备的画布绘图应用

[英]Canvas drawing app for touch devices

Hallo stackoverflooow community, I have a little problem with my small drawing app. 哈罗stackoverflooow社区,我的小型绘图应用程序存在一些问题。 It works perfect on desktop but not on touch devices. 它可以完美地在台式机上运行,​​但不适用于触摸设备。 The App just make a point when I touch the display. 当我触摸显示器时,该应用程序就很重要。 I hope somebody can help me. 我希望有人能帮助我。

var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');

var radius = 5;
context.lineWidth = radius*2;
var press = false;

var putPoint = function(e){
    if(press){
    context.beginPath();
    context.arc(e.offsetX, e.offsetY, radius, 0, 2*Math.PI);
    context.fill();
    }
}

var start = function(e){
    press = true;
    putPoint(e);
    context.beginPath();
    context.moveTo(e.offsetX, e.offsetY);
}

var move = function(e){
    context.lineTo(e.offsetX, e.offsetY);
    context.stroke();
    putPoint(e);
    context.beginPath();
    context.moveTo(e.offsetX, e.offsetY);
}

var stop = function(){
press = false;
context.beginPath();
}

//mouse
canvas.addEventListener('mousedown', start);
canvas.addEventListener('mousemove', move);
canvas.addEventListener('mouseup', stop);

//touch
canvas.addEventListener('touchstart', start);
canvas.addEventListener('touchmove', move);
canvas.addEventListener('touchend', stop);

` `

The problem with misbehaving touch events generally comes from the differences between the way that touch events and mouse events work. 触摸事件行为异常的问题通常来自触摸事件和鼠标事件的工作方式之间的差异。 Mouse events only occur one at a time, since there's only one mouse pointer. 一次只能发生一个鼠标事件,因为只有一个鼠标指针。 Touch events on the other hand, need to deal with multi-touch events. 另一方面,触摸事件需要处理多点触摸事件。 Eg one finger goes down, it moves and then another goes down. 例如,一根手指下降,它移动,然后另一根下降。 We've now got 2 touchstart events, and 1 touch move event. 现在,我们有2个touchstart事件和1个touch move事件。 It gets pretty complicated pretty fast. 它很快变得非常复杂。

I forget where I got it from now, (sorry original author) but here's the code that I use in one of my applications. 我忘了从现在开始得到的信息(抱歉,原始作者),但这是我在一个应用程序中使用的代码。 The function is attached to all three types of touch events - touchstart, touchmove and touchend. 该功能附加到所有三种类型的触摸事件-touchstart,touchmove和touchend。 It doesn't deal with multi-touch, simply ignoring any events that happen when more than one finger is touching the input device. 它不涉及多点触摸,只是忽略了多个手指触摸输入设备时发生的任何事件。 It then creates a synthetic mouse-event, which is then passed onto the normal mouse event handlers. 然后,它创建一个合成的鼠标事件,然后将其传递给普通的鼠标事件处理程序。

function onCanvasTouch(evt)
{
    if (evt.touches.length > 1 )
        return;

    if ((evt.type == "touchend" && evt.touches.length > 0))
    {
        evt.preventDefault();
        return;
    }

    evt.preventDefault();

    var newEvt = document.createEvent("MouseEvents");
    var type = null;
    var touch = null;
    switch (evt.type)
    {
        case "touchstart":
            type = "mousedown";
            touch = evt.changedTouches[0];
            break;
        case "touchmove":
            type = "mousemove";
            touch = evt.changedTouches[0];
            break;
        case "touchend":
            type = "mouseup";
            touch = evt.changedTouches[0];
            break;
    }

    newEvt.initMouseEvent(
                            type, true, true, evt.target, 0,
                            touch.screenX, touch.screenY, touch.clientX, touch.clientY,
                            evt.ctrlKey, evt.altKey, evt.shiftKey, evt.metaKey, 0, null
                        );

    evt.target.dispatchEvent(newEvt);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM