微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

鼠标单击(或触摸)画布上的事件会导致使用HTML5,Phonegap和Android的选择

我正在使用easlejs phonegap进行 HTML5游戏,并且遇到问题,每当您在画布上点击/ touch / mousedown时,屏幕会闪烁.

下面是我创建的非常简单的代码来测试问题,看看它是否与easlejs有关.从代码中可以看出,它与easlejs无关,只是一个html5 / phonegap问题.

你可以看到我也试过没有选择的CSS样式没有运气.

<!doctype html>
<html>
<head>
    <title></title>
    <style type="text/css">
        #canvas
        {
            user-select: none;
            -webkit-user-select: none;
            -moz-user-select: none;
        }
    </style>
</head>
<body>
    <canvas  id="canvas" width="320" height="480"></canvas>
    <script type="text/javascript">
        var canvas = document.getElementById("canvas");

        canvas.addEventListener("mousedown",function(e)
        {
            var ctx = canvas.getContext("2d");
            var x = Math.random() * 320;
            var y = Math.random() * 480;
            var w = Math.random() * 100;
            var h = Math.random() * 100;

            ctx.fillStyle = "#FF0000";

            ctx.fillRect(x,y,w,h);
        },false);
    </script>
</body>
</html>

解决方法

编辑:发现EaselJS仍然有触摸显示选择的错误.要解决这个问题,我在阅读了这篇文章之后,在画布中添加一个CSS属性https://stackoverflow.com/a/7981302/66044

修复是:
-webkit-tap-highlight-color:transparent;

能够在本文的帮助下解决这个问题:
How to handle touch events in IOS and Android?

这是工作代码.修复程序是处理touchstart / end事件来处理点击.不再经历橙色选择阶段.

在2.2模拟器和我的设备(三星Captivate运行Cyanogenmod ICS)中进行了测试

<!doctype html>
<html>
<head>
    <title></title>
</head>
<body>
    <canvas  id="canvas" width="320" height="480"></canvas>
    <script type="text/javascript" src="cordova-1.7.0.js"></script>
    <script type="text/javascript">
        var canvas = document.getElementById("canvas");

        // FIX: Cancel touch end event and handle click via touchstart
        document.addEventListener("touchend",function(e) { e.preventDefault(); },false);
        canvas.addEventListener("touchstart",h);

            // FIX: Cancel the event
            e.preventDefault();
        },false);
    </script>
</body>
</html>

原文地址:https://www.jb51.cc/html5/168644.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。