HTML5(问题):在iOS 11.3上进行交互/拖动时画布滚动. [英] Html5 (issue): canvas scrolling when interacting/dragging on iOS 11.3.

查看:117
本文介绍了HTML5(问题):在iOS 11.3上进行交互/拖动时画布滚动.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个HTML5应用程序,该应用程序充分利用了其画布.在iOS 11.3更新之后,我们开始遇到触摸问题.

I have an HTML5 app that makes extensive use of its canvas. We started experiencing issues with touch after the iOS 11.3 update.

实施后,我们确保明确告诉用户代理,不应该处理该事件. (即,我们添加了evnt.preventDefault()

When implemented, we made sure to explicitly tell the user agent that the event should not be handled. (ie. we added evnt.preventDefault()

我们还限制了画布,并确保禁用浏览器对所有平移和缩放手势的处理. (touch-action: none,尽管不是 Safari尚未正式支持此基本功能实施,这在iOS 11.3之前的任何浏览器上都有效)

We also restricted the canvas, and make sure to disable the browser handling of all panning and zooming gestures. (touch-action: none, although not Safari does not officially support this basic implementation, this does work on any browser prior iOS 11.3)

这并非特定于任何浏览器,但会在11.3设备之后的任何iOS设备上显示.

This is NOT specific to any browser, but it manifests itself on any iOS device after the 11.3 device.

可以使用以下JSFiddle复制它: https://jsfiddle.net/w542djdw/15/

It can be reproduced using this JSFiddle: https://jsfiddle.net/w542djdw/15/

任何建议将不胜感激.

Any recommendation would be greatly appreciated.

推荐答案

诀窍在于Safari 11.1(捆绑到iOS 11.3中)如何处理触摸事件.

The trick is in how Safari 11.1 (bundled into iOS 11.3) deals with touch events.

摘自他们的发行说明:

Web API:

Web APIs:

  • 更新了根文档触摸事件侦听器以使用被动模式,从而提高了滚动性能并减少了崩溃.

所以基本上要改变它:

// Prevent scrolling when touching the canvas
document.body.addEventListener("touchstart", function (e) {
    if (e.target == canvas) {
        e.preventDefault();
    }
}, false);
document.body.addEventListener("touchend", function (e) {
    if (e.target == canvas) {
        e.preventDefault();
    }
}, false);
document.body.addEventListener("touchmove", function (e) {
    if (e.target == canvas) {
        e.preventDefault();
    }
}, false);

对此:

// Prevent scrolling when touching the canvas
document.body.addEventListener("touchstart", function (e) {
    if (e.target == canvas) {
        e.preventDefault();
    }
}, { passive: false });
document.body.addEventListener("touchend", function (e) {
    if (e.target == canvas) {
        e.preventDefault();
    }
}, { passive: false });
document.body.addEventListener("touchmove", function (e) {
    if (e.target == canvas) {
        e.preventDefault();
    }
}, { passive: false });

阅读

passive:一个布尔值,如果为true,则表示侦听器指定的函数将永远不会调用preventDefault().如果被动侦听器确实调用了preventDefault(),则用户代理将不执行任何操作,只生成控制台警告.请参阅使用被动侦听器提高滚动性能以了解更多信息.

passive: A Boolean which, if true, indicates that the function specified by listener will never call preventDefault(). If a passive listener does call preventDefault(), the user agent will do nothing other than generate a console warning. See Improving scrolling performance with passive listeners to learn more.

这篇关于HTML5(问题):在iOS 11.3上进行交互/拖动时画布滚动.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆