0

我开始研究画布并遇到了一个问题,在画布的帮助下使用鼠标 onmousedown、onmousemove、onmouseup 进行绘图,iPad 使用了 touchstart、touchmove、touchend,但触摸板的代码不起作用。不工作的代码我注释掉了,不胜感激真道的帮助和指导。

ipad中画canvas的代码不起作用 canvas.ontouchstart,canvas.touchstart,canvas.touchmove,canvas.touchend

 function unit(){
        canvasOffset = canvas.getBoundingClientRect();
        ctx.lineJoin = "round";
        canvas.onmousedown = function(e) {
            drawing = true;
            ctx.beginPath();
        }
        //canvas.ontouchstart = function(e) {
        //  if (e.touches) e = e.touches[0];
        //  return false;
        //}
        //canvas.touchstart = function(e) {
        //      drawing = true;
        //      ctx.beginPath();
        //}
        canvas.onmousemove = function(e) {
            if (drawing) {
                var mousePosition = getMousePosition(e);
              ctx.lineTo(mousePosition[0], mousePosition[1]);
                        myColor =      document.getElementById('myColor').style.background;
                        ctx.strokeStyle = myColor;
                ctx.stroke();
            }
        };
        //canvas.touchmove = function(e) {
        //      if (drawing) {
        //              var mousePosition = getMousePosition(e);
        //              ctx.lineTo(mousePosition[0], mousePosition[1]);
        //              myColor = document.getElementById('myColor').style.background;
        //              ctx.strokeStyle = myColor;
        //              ctx.stroke();
        //      }
        //};
        canvas.onmouseup = function(e) {
            drawing = false;
            ctx.closePath();
        }
        //canvas.touchend = function(e) {
        //      drawing = false;
        //      ctx.closePath();
        //}
        function getMousePosition(mouseEvent) {
            return [Math.floor(event.offsetX), Math.floor(event.offsetY)];
        }
}
4

0 回答 0