【问题标题】:html5 canvas in androidandroid中的html5画布
【发布时间】:2012-06-19 00:25:07
【问题描述】:

有没有什么特殊的方法可以让 Android 的浏览器响应点击/按下事件?我的代码在流行的浏览器中运行良好,但是当我使用我的 Android 平板电脑或 Android 手机时,节点在拖动/按下时不会移动。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title></title>
</head>
<style type="text/css">
html, body {
  width:  100%;
  height: 100%;
  margin: 0px;
}
#blurb_canvas {
  position: absolute; 
  top: 0px;
  left: 150px;
} 
#blurb_data_holder {
  height: 100%;
  width: 150px;
  top: 0px;
  left: 0px;
  overflow-y: auto;
} 
input.blurb_btn {
    background-color: #eaeaea;
    font-weight: bold;
    border: solid 1px #c5c5c5;
    cursor: pointer;
    width: 9em;
    height: 2em;
    padding: 5px;
} 
</style>
<body onLoad="init();">

    <table id="blurb_data_holder">
        <tr>
            <td align="center" valign="top">
            <input type="submit" title="" value="Data 1" id="data_1" class="blurb_btn" onClick="createBlurbNode(this.id);"/> <br />
            <input type="submit" title="" value="Data 2" id="data_2" class="blurb_btn" onClick="createBlurbNode(this.id);"/> <br />
            <input type="submit" title="" value="Data 3" id="data_3" class="blurb_btn" onClick="createBlurbNode(this.id);"/> <br />
            <input type="submit" title="" value="Data 4" id="data_4" class="blurb_btn" onClick="createBlurbNode(this.id);"/> <br />
            <input type="submit" title="" value="Data 5" id="data_5" class="blurb_btn" onClick="createBlurbNode(this.id);"/> <br />
            </td>
        </tr>
    </table>




    <canvas id="blurb_canvas">If you are able to see this message, your browser does not support HTML5's canvas feature.</canvas>


<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
    var canvasLay;
    var canvasContext;
    var blurbNodes;
    var x = 50;
    var y = 50;
    var movingObj = false;
    var movinganotherObj = false;
    var blurbSentences;

    function init(){
        canvasLay = document.getElementById('blurb_canvas');
        canvasContext = canvasLay.getContext('2d');
        canvasLay.width = window.innerWidth-150;
        canvasLay.height = window.innerHeight;

        blurbNodes = [];
        blurbNodes.push(new BlurbNode(300,75,60,1,1,'stuff here a'));
        blurbNodes.push(new BlurbNode(600,300,60,1,1,'stuff here b'));
        setInterval(drawScreen,25);
        canvasLay.addEventListener("mousedown", beginMovingObj, false);
        canvasLay.addEventListener("mousemove", moveObj, false);
        canvasLay.addEventListener("mouseup", stopMovingObj, false);
    }

    function createBlurbNode(text){
        blurbNodes.push(new BlurbNode(100,100,60,1,1,text));
    }

    function BlurbNode(positionx, positiony, radius, dx, dy, label){
        this.canvas = canvasLay;
        this.context = canvasContext;
        this.positionx = positionx;
        this.positiony = positiony;
        this.radius = radius;
        this.dx = dx;
        this.dy = dy;
        this.label = label;
        this.moving = false;
        this.hasChild = false;
        this.hasParent = false;
    }

    BlurbNode.prototype.Create = function(){
        this.context.fillStyle = '#ff0000';
        this.context.beginPath();
        this.context.arc(this.positionx, this.positiony, this.radius, 0, Math.PI*2, true); 
        this.context.closePath();
        this.context.fill();
        this.context.fillStyle = '#000000';
        this.context.fillText(this.label,this.positionx-20,this.positiony);
    };

    function drawLineConnection(parentCenterx, parentCentery, childCenterx, childCentery){
        canvasContext.beginPath();
        canvasContext.fillStyle = '#000000';
        canvasContext.moveTo(parentCenterx, parentCentery);
        canvasContext.lineTo(childCenterx, childCentery);
        canvasContext.stroke();
    }

    function drawScreen(){  
        canvasLay.width = window.innerWidth-150;
        canvasLay.height = window.innerHeight;
        canvasContext.clearRect(0,0,screen.width,screen.height);

        for(i = 0; i<blurbNodes.length; i++){

            if(movingObj){
                if(!movinganotherObj && (x > blurbNodes[i].positionx-50 && x < blurbNodes[i].positionx+50) && (y > blurbNodes[i].positiony && y < blurbNodes[i].positiony+50)){
                    blurbNodes[i].moving = true;
                    movinganotherObj = true;
                }

                if(blurbNodes[i].moving && x < (canvasLay.width-60) && y < (canvasLay.height-60) && x > (60) && y > (60)){
                    blurbNodes[i].positionx = x;
                    blurbNodes[i].positiony = y;
                }
            }else{
                blurbNodes[i].moving = false;
            }

            if(!blurbNodes[i].hasChild && !blurbNodes[i].hasParent && !blurbNodes[i].moving && blurbNodes[i].positiony < canvasLay.height-60){
                blurbNodes[i].positiony = blurbNodes[i].positiony + blurbNodes[i].dy;
            }
            blurbNodes[i].Create();


            for(j = 0; j<blurbNodes.length; j++){
                if(blurbNodes[i] != blurbNodes[j] && 
                        Math.abs(blurbNodes[i].positionx - blurbNodes[j].positionx) < 150 &&
                        Math.abs(blurbNodes[i].positiony - blurbNodes[j].positiony) < 150){
                    drawLineConnection(blurbNodes[i].positionx, blurbNodes[i].positiony, blurbNodes[j].positionx, blurbNodes[j].positiony);
                    blurbNodes[i].hasChild = true;
                    blurbNodes[j].hasParent = true;
                }
            }
        }
    }


    function beginMovingObj(e) {
        movingObj = true;
    }
    function stopMovingObj(e) {
        movingObj = false;
        movinganotherObj = false;
    }
    function moveObj(e) {
        if(movingObj){
            x = e.pageX-150;
            y = e.pageY;
        }
    }
</script>

</body>
</html>

【问题讨论】:

  • 我建议使用 jsfiddle,这样我们都可以看到你的代码是关于什么的,以及它是如何工作的。当我知道我要解决什么问题时,它总是对我有帮助。 jsfiddle.net
  • 这里是 jsfiddle 链接:jsfiddle.net/qWgNj

标签: android html canvas clicklistener


【解决方案1】:

这里,这行得通:

http://jsfiddle.net/simonsarris/W45jt/

您真正缺少的是用于补充鼠标事件的“touchstart”、“touchmove”和“touchend”事件。

你真的应该比我组织得更好,这只是启动示例并运行的最快的代码。

【讨论】:

  • 谢谢。这实际上有效。现在唯一的问题是响应真的很慢。有什么想法为什么会这样?你在安卓平板或安卓手机上试过吗?
  • 一般来说,我猜 html5 在 Android 设备上很慢?也许是帧速率?
【解决方案2】:

我最近修改了一些用于绘图的代码,并且需要在一些地方寻找正确的事件属性。以下代码适用于我。希望对您有所帮助。

drawStart = function(ev) {
        if(ev.originalEvent.touches && ev.originalEvent.touches.length) {
            ev = ev.originalEvent.touches[0];
        } else if(ev.originalEvent.changedTouches && ev.originalEvent.changedTouches.length) {
            ev = ev.originalEvent.changedTouches[0];
        }

        // Calculate the current mouse X, Y coordinates with canvas offset
        var x, y;
        x = ev.pageX - $(doodle.canvas).offset().left;
        y = ev.pageY - $(doodle.canvas).offset().top;
...

【讨论】:

  • 您能否在代码中详细说明如何将其连接到画布?这是一个监听函数吗?
  • 我猜你的问题是你的 moveObj 方法没有得到好的 x 和 y。添加我的代码的第一部分,以确保您有一个好的事件可以从中获取它们。 (我的 ev == 你的 e)。除了鼠标事件之外,您可能还需要监听 touchstart、touchmove、touchend。
  • 我认为问题不在于获取错误的 x,y 坐标。当我在我的 android 设备上单击画布时,整个画布会突出显示,就好像它像图像或按钮一样嵌入在屏幕中。但我会听取您的建议,稍后再检查。
猜你喜欢
  • 2011-07-31
  • 2012-07-18
  • 2015-06-22
  • 1970-01-01
  • 2013-10-10
  • 2011-10-24
  • 2012-09-30
  • 2011-09-01
  • 2011-03-04
相关资源
最近更新 更多