2012-07-25 7 views
1

안녕하십니까. 내 차트 구성을 html5로 변경했습니다. 이번에는 한 부분이나 모듈을 제외하고는 모든 것이 잘 수행되었습니다. 저는 캔버스의 직사각형을 클릭했을 때 경고 메시지를 표시하기 위해 인터넷을 통해 얻은 코드를 시도했습니다. 내가 모듈에 댓글을 달면 잘 동작하지만 마우스 클릭에는 영향을주지 않습니다. 누구든지 나를 도울 수 있습니까? 미리 감사드립니다. 여기 내 바이올린과 코드 [바이올린]입니다 : 여기 http://jsfiddle.net/s8kZs/마우스 클릭이 html5의 내 기둥 형 차트에서 작동하지 않습니다.

<!doctype html> 
    <html> 
     <head> 
      <script type="text/javascript"> 
      window.onload=function() { 
       var canvas=document.getElementById('mycanvas'); 
       var ctx=canvas.getContext('2d'); 
       var graphInfo=[{data:[120,130,140,160,180,100],color:'purple',label:["a","b","c","d","e","f"]},{data:[100,120,130,140,150,190],color:'green',label:["g","h","i","j","k","l"]}]; 
       var width=45; 
       function renderGrid(gridPixelSize, color) 
     { 
      ctx.save(); 
      ctx.lineWidth = 0.5; 
      ctx.strokeStyle = color; 
      for(var i = 20; i <= canvas.height-20; i = i + gridPixelSize) 
      { 
       ctx.beginPath(); 
       ctx.moveTo(20, i); 
       ctx.lineTo(canvas.width-20, i); 
       ctx.closePath(); 
       ctx.stroke(); 
      } 
      for(var j = 20; j <= canvas.width-20; j = j + gridPixelSize) 
      { 
       ctx.beginPath(); 
       ctx.moveTo(j, 20); 
       ctx.lineTo(j, canvas.height-20); 
       ctx.closePath(); 
       ctx.stroke(); 
      } 
      ctx.restore(); 
     } 
     renderGrid(10, "grey"); 

       ctx.beginPath(); 
       ctx.strokeStyle = 'black'; 
       ctx.moveTo(20, canvas.height-20); 
       ctx.lineTo(canvas.width-20, canvas.height-20); 
       ctx.closePath(); 
       ctx.stroke(); 

       ctx.beginPath(); 
     ctx.strokeStyle = 'black'; 
       ctx.moveTo(20, 20); 
       ctx.lineTo(20, canvas.height-20); 
       ctx.closePath(); 
       ctx.stroke(); 
       ctx.font = "bold 16px Arial"; 

     function getFunctionForTimeout(j){ 
        var i=0,currx=30,info=graphInfo[j],x5=j*5; 
        var fTimeout=function(){ 
         var h=Math.max(info.data[i]-x5,0); 
         var m=info.label[i]; 
         ctx.fillStyle='black' 
         ctx.fillRect(currx+(10*x5)+2,canvas.height-h-20,width+2,h-1); 
         ctx.fillStyle=info.color;     
         ctx.fillRect(currx+(10*x5),canvas.height-h-21,width,h);     
         ctx.fillText(m, currx+(10*x5)+20, canvas.height-5); 
         currx+=120; 
         i++; 
         /*var ctx = $('#mycanvas').get(0).getContext('2d'); 
         $('#mycanvas').click(function(e) { 
     var x = e.offsetX, 
      y = e.offsetY; 

     for(var k=0;k<j;k++) { 
      if(x > currx+(10*x5) 
      && x < canvas.height-h-21 
      && y > width 
      && y < h) { 
       alert('Rectangle ' + j + ' clicked'); 
      } 
     } 
    });*/ 
         if(i<info.data.length)setTimeout(fTimeout,2000); 
        }; 
        return fTimeout; 
     } 

       for(var j=graphInfo.length-1;j>=0;j--) { 
        setTimeout(getFunctionForTimeout(j),2000); 
       } 
      }; 
      </script> 
     </head> 
     <body> 
      <canvas id="mycanvas" height="400" width="800" style="border:1px solid #000000;"> 
     </body> 
    </html> 
+0

알림을 확인하는 if 조건에 약간의 문제가 있습니다 ... – GOK

+0

문제를 해결 했습니까? – GOK

답변

0

는 당신에게 당신이 캔버스에 클릭하는 마우스 위치를주는 코드입니다, 난 그냥 당신이 기대하고 경고를 위해 그것을 보정하는 코드를 변경하지만, 당신이 경고로 생성하는 당신의 상태에 약간 문제점이있다.

Njoy 마우스 위치를 클릭하고 경고합니다. 당신은 당신이 기대 같은 경고를 원하는 경우,

<!doctype html> 
<html> 
    <head> 
     <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script> 
     <script type="text/javascript"> 
     window.onload=function() { 
      var canvas=document.getElementById('mycanvas'); 
      var ctx=canvas.getContext('2d'); 
      var graphInfo=[{data:[120,130,140,160,180,100],color:'purple',label:["a","b","c","d","e","f"]},{data:[100,120,130,140,150,190],color:'green',label:["g","h","i","j","k","l"]}]; 
      var width=45; 
      function renderGrid(gridPixelSize, color) 
      { 
       ctx.save(); 
       ctx.lineWidth = 0.5; 
       ctx.strokeStyle = color; 
       for(var i = 20; i <= canvas.height-20; i = i + gridPixelSize) 
       { 
        ctx.beginPath(); 
        ctx.moveTo(20, i); 
        ctx.lineTo(canvas.width-20, i); 
        ctx.closePath(); 
        ctx.stroke(); 
       } 
       for(var j = 20; j <= canvas.width-20; j = j + gridPixelSize) 
       { 
        ctx.beginPath(); 
        ctx.moveTo(j, 20); 
        ctx.lineTo(j, canvas.height-20); 
        ctx.closePath(); 
        ctx.stroke(); 
       } 
       ctx.restore(); 
      } 
       renderGrid(10, "grey"); 

         ctx.beginPath(); 
         ctx.strokeStyle = 'black'; 
         ctx.moveTo(20, canvas.height-20); 
         ctx.lineTo(canvas.width-20, canvas.height-20); 
         ctx.closePath(); 
         ctx.stroke(); 

         ctx.beginPath(); 
       ctx.strokeStyle = 'black'; 
         ctx.moveTo(20, 20); 
         ctx.lineTo(20, canvas.height-20); 
         ctx.closePath(); 
         ctx.stroke(); 
         ctx.font = "bold 16px Arial"; 

      function getFunctionForTimeout(j){ 
       var i=0,currx=30,info=graphInfo[j],x5=j*5; 
       var fTimeout=function(){ 
        var h=Math.max(info.data[i]-x5,0); 
        var m=info.label[i]; 
        var ctx = $('#mycanvas').get(0).getContext('2d'); 
        ctx.fillStyle='black' 
        ctx.fillRect(currx+(10*x5)+2,canvas.height-h-20,width+2,h-1); 
        ctx.fillStyle=info.color;      
        ctx.fillRect(currx+(10*x5),canvas.height-h-21,width,h);      
        ctx.fillText(m, currx+(10*x5)+20, canvas.height-5); 
        currx+=120; 
        i++; 

        $('#mycanvas').click(function(e) { 
         var canvas = document.getElementById("myCanvas"); 
         var mousePos = getMousePos(canvas, e); 
         var message = "Mouse position: " + mousePos.x + "," + mousePos.y; 
         var x = mousePos.x; 
         var y = mousePos.y; 
         alert(message); 

         for(var k=0;k<j;k++) { 
          if(x > currx+(10*x5) 
          && x < canvas.height-h-21 
          && y > width 
          && y < h) { 
           alert('Rectangle ' + j + ' clicked'); 
          } 
         } 
        }); 
        if(i<info.data.length)setTimeout(fTimeout,2000); 
       }; 
       return fTimeout; 
      }  

      for(var j=graphInfo.length-1;j>=0;j--) { 
       setTimeout(getFunctionForTimeout(j),2000); 
      } 
      function getMousePos(canvas, evt) { 
       var rect = canvas.getBoundingClientRect(), root = document.documentElement; 

      // return relative mouse position 
      var mouseX = evt.clientX - rect.top - root.scrollTop; 
      var mouseY = evt.clientY - rect.left - root.scrollLeft; 
       return { 
        x: mouseX, 
        y: mouseY 
       }; 
       } 
     }; 
     </script> 
    </head> 
    <body> 
     <canvas id="mycanvas" height="400" width="800" style="border:1px solid #000000;"> 
    </body> 
</html> 

그냥 같은에 대한 getMousePos의 변화를() 체크 ... 당신이 내 위치 경고하여 얻을 것이다 경우 조건을 변경 u는

건배를 필요! !! !! Njoy !!!

+0

감사합니다. 어떻게 canvas에서 직사각형 만 감지 할 수 있도록 코드를 만들 수 있습니까 ??? –

+1

var rect = canvas.getBoundingClientRect(), root = document.documentElement; \t \t \t \t // 상대 마우스 위치를 반환 \t \t \t \t var에 mouseX = evt.clientX - rect.top - root.scrollTop; \t \t \t \t var mouseY = evt.clientY - rect.left - root.scrollLeft; – GOK

+1

getMousePos()에서 필요한 변경 사항을 확인하십시오. – GOK

관련 문제