首页 > 解决方案 > 为虽然屏幕(TouchEvent)进行擦除工作

问题描述

我做了一个简单的游戏,你必须擦除彩色图层才能显示图像。但它只适用于我的桌面,不适用于带屏幕(iphone 或 ipad)的东西。我知道我必须将 MouseEvent 替换为 TouchEvent,但我不知道怎么做,因为我是编码初学者。我希望有人可以帮助我!

#canvas {
    background-image: url("img/image.jpg");
    background-repeat: no-repeat;
    width: 800px;
    height: 800px;
}
<!DOCTYPE html>
<html lang="en">

<head>

  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

  <title>Ellen Langendam</title>
  <link href="style.css" rel="stylesheet">

</head>

<body>

<div id="canvas"></div>

<script>

(function() {
    // Creates a new canvas element and appends it as a child
    // to the parent element, and returns the reference to
    // the newly created canvas element

    function createCanvas(parent, width, height) {
        var canvas = {};
        canvas.node = document.createElement('canvas');
        canvas.context = canvas.node.getContext('2d');
        canvas.node.width = width || 100;
        canvas.node.height = height || 100;
        parent.appendChild(canvas.node);
        return canvas;
    }

    function init(container, width, height, fillColor) {
        var canvas = createCanvas(container, width, height);
        var ctx = canvas.context;
        // define a custom fillCircle method
        ctx.fillCircle = function(x, y, radius, fillColor) {
            this.fillStyle = fillColor;
            this.beginPath();
            this.moveTo(x, y);
            this.arc(x, y, radius, 0, Math.PI * 10, false);
            this.fill();
        };
        ctx.clearTo = function(fillColor) {
            ctx.fillStyle = fillColor;
            ctx.fillRect(0, 0, width, height);
        };
        ctx.clearTo(fillColor || "#ddd");

        // bind mouse events
        canvas.node.onmousemove = function(e) {
            if (!canvas.isDrawing) {
               return;
            }
            var x = e.pageX - this.offsetLeft;
            var y = e.pageY - this.offsetTop;
            var radius = 40; // or whatever
            var fillColor = '#ff0000';
            ctx.globalCompositeOperation = 'destination-out';
            ctx.fillCircle(x, y, radius, fillColor);
        };
        canvas.node.onmousedown = function(e) {
            canvas.isDrawing = true;
        };
        canvas.node.onmouseup = function(e) {
            canvas.isDrawing = false;
        };
    }

    var container = document.getElementById('canvas');
    init(container, 800, 800, '#99ff99');

})();

</script>

</body>

</html>

标签: javascripthtmlmouseeventtouch-eventerase

解决方案


注册触摸动作,ontouchmove然后通过触摸点进行交互,event.touches并使用每个项目的属性来绘制你的圆圈,就像你在onmousemove.

我还建议转向 using letor consttype of variables 而不是varasvar可能会导致不幸的问题,请参阅:使用“let”和“var”有什么区别?

function createCanvas(parent, width, height) {
    var canvas = {};
    canvas.node = document.createElement('canvas');
    canvas.context = canvas.node.getContext('2d');
    canvas.node.width = width || 100;
    canvas.node.height = height || 100;
    parent.appendChild(canvas.node);
    return canvas;
}

function init(container, width, height, fillColor) {
    var canvas = createCanvas(container, width, height);
    var ctx = canvas.context;
    // define a custom fillCircle method
    ctx.fillCircle = function(x, y, radius, fillColor) {
        this.fillStyle = fillColor;
        this.beginPath();
        this.moveTo(x, y);
        this.arc(x, y, radius, 0, Math.PI * 10, false);
        this.fill();
    };
    ctx.clearTo = function(fillColor) {
        ctx.fillStyle = fillColor;
        ctx.fillRect(0, 0, width, height);
    };
    ctx.clearTo(fillColor || "#ddd");

    // bind mouse events
    canvas.node.onmousemove = function(e) {
        if (!canvas.isDrawing) {
           return;
        }
        var x = e.pageX - this.offsetLeft;
        var y = e.pageY - this.offsetTop;
        var radius = 40; // or whatever
        var fillColor = '#ff0000';
        ctx.globalCompositeOperation = 'destination-out';
        ctx.fillCircle(x, y, radius, fillColor);
    };
    canvas.node.onmousedown = function(e) {
        canvas.isDrawing = true;
    };
    canvas.node.onmouseup = function(e) {
        canvas.isDrawing = false;
    };
    
    canvas.node.ontouchmove = function(event) {
      for(let index = 0; index < event.touches.length; index++) {
        const touch = event.touches[index];
        
        const x = touch.pageX - this.offsetLeft;
        const y = touch.pageY - this.offsetTop;
        
        const radius = 40; // or whatever
        const fillColor = '#ff0000';
        
        ctx.globalCompositeOperation = 'destination-out';
        ctx.fillCircle(x, y, radius, fillColor);
      }
    };
}

var container = document.getElementById('canvas');
init(container, 800, 800, '#99ff99');
#canvas {
    background-image: url("img/image.jpg");
    background-repeat: no-repeat;
    width: 800px;
    height: 800px;
}
<div id="canvas"></div>


推荐阅读