2012-06-15 7 views
1

Android의 브라우저가 클릭/프레스 이벤트에 응답하도록하는 특별한 방법이 있습니까? 내 코드는 인기있는 브라우저에서 잘 작동하지만 Android 태블릿이나 Android 휴대 전화를 사용하면 노드를 드래그/밀 때 이동하지 않습니다.android의 html5 canvas

<!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> 
+1

jsfiddle을 사용하면 코드가 무엇인지, 어떻게 작동하는지 알 수 있습니다. 내가 무엇을 고치려고하는지 알 때 항상 도움이됩니다. http://www.jsfiddle.net – Xitcod13

+0

다음은 jsfiddle 링크입니다. http://jsfiddle.net/qWgNj/ – Donnie

답변

1

여기,이 작품 :

당신이 정말로 무엇을 놓치고 있는지

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

은 'touchstart', '하는 TouchMove'하고 마우스 이벤트를 칭찬하는 'touchend'이벤트입니다.

당신은 실제로 내가했던 것보다 더 잘 구성해야합니다. 그것은 예제를 얻고 작업하는 가장 빠른 코드 일뿐입니다.

+0

감사합니다. 이것은 실제로 작동합니다. 유일한 문제는 응답이 정말 느리다는 것입니다. 왜 이런 생각이 들지? 안드로이드 타블렛이나 안드로이드 폰에서 사용해 보셨습니까? – Donnie

+0

일반적으로 html5는 Android 기기에서 느립니다. 어쩌면 프레임 속도? – Donnie

0

필자는 최근에 그림을 그리기 위해 일부 코드를 수정했으며 적절한 이벤트 속성을 찾기 위해 몇 가지 장소를 살펴야했습니다. 다음 코드는 나를 위해 작동합니다. 잘하면 그것은 당신을 도울 것입니다.

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; 
... 
+0

이 코드를 캔버스에 연결하는 방법에 대한 자세한 내용을 코드에 제공 할 수 있습니까? 리스너 기능입니까? – Donnie

+0

귀하의 문제는 귀하의 moveObj 방법이 좋은 x와 y를 얻지 못한다는 것을 짐작하고 있습니다. 내 코드의 첫 번째 부분을 추가하여 좋은 이벤트가 있는지 확인하십시오. (나의 ev == 당신의 e). 또한 마우스 이벤트 이외에도 터치 시작, 터치 이동, 터치 등을들을 필요가 있습니다. –

+0

문제가 x, y 좌표가 잘못되어 있다고 생각하지 않습니다. 내 안드로이드 기기에서 캔버스를 클릭하면 전체 캔버스가 이미지 또는 버튼과 같이 화면에 내장 된 것처럼 강조 표시됩니다. 하지만 조금 후에 조언을 받고 확인해 보겠습니다. – Donnie