Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ontouchend ios返回错误的坐标_Javascript_Html_Mobile Safari_Html5 Canvas - Fatal编程技术网

Javascript ontouchend ios返回错误的坐标

Javascript ontouchend ios返回错误的坐标,javascript,html,mobile-safari,html5-canvas,Javascript,Html,Mobile Safari,Html5 Canvas,我一直在读一本非常有用的书《核心HTML5画布》,其中包括一个在相同功能中包含鼠标点击和触摸的示例。我的版本(与本书非常相似)如下: function windowToCanvas(x, y) { var bbox = canvas.getBoundingClientRect(); return { x: x - bbox.left * (canvas.width /bbox.width), y: y - bbox.top * (canvas.height / bbox.hei

我一直在读一本非常有用的书《核心HTML5画布》,其中包括一个在相同功能中包含鼠标点击和触摸的示例。我的版本(与本书非常相似)如下:

function windowToCanvas(x, y) {
    var bbox = canvas.getBoundingClientRect();

    return { x: x - bbox.left * (canvas.width /bbox.width), y: y - bbox.top * (canvas.height / bbox.height)};
};

canvas.ontouchstart = function(e) {
    e.preventDefault(e);
    MTStart(windowToCanvas(e.pageX, e.pageY));
};

canvas.ontouchmove = function(e) {
    e.preventDefault(e);
    MTMove(windowToCanvas(e.pageX, e.pageY));
};

canvas.ontouchend = function(e) {
    e.preventDefault(e);
    MTEnd(windowToCanvas(e.pageX, e.pageY));
};

canvas.onmousedown = function(e) {
    e.preventDefault(e);
    MTStart(windowToCanvas(e.pageX, e.pageY));
};

canvas.onmousemove = function(e) {
    e.preventDefault(e);
    MTMove(windowToCanvas(e.pageX, e.pageY));
};

canvas.onmouseup = function(e) {
    e.preventDefault(e);
    MTEnd(windowToCanvas(e.pageX, e.pageY));
};

function MTStart(location) {
    console.log("Mouse down");
    document.getElementById('message').innerHTML = 'MT Start x: ' + location.x + ', y: ' + location.y;
};

function MTMove(location) {

};

function MTEnd(location) {
    console.log("Mouse up");
    document.getElementById('message').innerHTML = 'MT End x: ' + location.x + ', y: ' + location.y;
};
这与鼠标配合得很好。然而,在iphone或ipad上运行safari时,ontouchstart似乎报告了正确的位置,而ontouchend则没有。Ontouchend将给出相同的坐标,无论我在何处触摸。我注意到,当且仅当我稍微滚动页面并触摸画布中的同一位置时,它返回的坐标似乎会改变

知道为什么ontouchstart和ontouchend会给出不同的位置值吗


谢谢

所以,我需要使用:

MTEnd(windowToCanvas(e.changedTouches[0].pageX, e.changedTouches[0].pageY));
但只适用于ontouchend,而不是ontouchstart