Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE HTML>
<html>
<head>
    <style>
        body {
            margin: 0px;
            padding: 0px;
        }
        #myCanvas {
            border: 1px solid #9C9898;
        }
    </style>
    <script>
        var targetX = 500, targetY = 200, targetRadius = 50;
      
        function writeMessage(canvas, message) {
            var context = canvas.getContext('2d');
            context.clearRect(0, 0, canvas.width, canvas.height);
            context.font = '18pt Calibri';
            context.fillStyle = 'black';
            context.fillText(message, 10, 25);
        }
        function getMousePos(canvas, evt) {
            // get canvas position
            var obj = canvas;
            var top = 0;
            var left = 0;
            while (obj && obj.tagName != 'BODY') {
                top += obj.offsetTop;
                left += obj.offsetLeft;
                obj = obj.offsetParent;
            }
            // return relative mouse position
            var mouseX = evt.clientX - left + window.pageXOffset;
            var mouseY = evt.clientY - top + window.pageYOffset;
            return {
                x:mouseX,
                y:mouseY
            };
        }
      
      function drawObstacles(canvas, context) {
        /*
        context.beginPath();
        context.moveTo(canvas.width/2.0, 0);
        context.lineTo(canvas.width/2.0, (canvas.height/2.0)-50);
        context.moveTo(canvas.width/2.0, (canvas.height/2.0)+50);
        context.lineTo(canvas.width/2.0, canvas.height);
        context.stroke();*/
        context.fillRect(100,100, 200, 200);
      }
      
      function drawPlayer(canvas, context, x, y) {
        context.fillRect(x,y, 20,20);
      }
      
      function rectsOverlap(x0, y0, w0, h0, x2, y2, w2, h2) {
        if ((x0 > (x2 + w2)) || ((x0 + w0) < x2))
            return false;
      
        if ((y0 > (y2 + h2)) || ((y0 + h0) < y2))
            return false;
        return true;
      }
      
      function circRectsOverlap(x0, y0, w0, h0, cx, cy, r) {
          var testX=cx; 
          var testY=cy; 
          
          if (testX < x0) testX=x0; 
          if (testX > (x0+w0)) testX=(x0+w0); 
          if (testY < y0) testY=y0; 
          if (testY > (y0+h0)) testY=(y0+h0); 
 
          return (((cx-testX)*(cx-testX)+(cy-testY)*(cy-testY))<r*r); 
      }
      
      function drawTarget(canvas, context, x, y, r, fillColor) {
               
                context.save();
                context.beginPath();
                context.arc(x, y, r, 0, 2*Math.PI, false);
                context.fillStyle=fillColor;
                context.fill();
                context.lineWidth = 5;
                context.strokeStyle = "black";
             
                context.stroke();
                context.restore();
      }
        window.onload = function () {
            var canvas = document.getElementById('myCanvas');
            var context = canvas.getContext('2d');
            var started = false, previousMousePos;
          
            function checkArrowKeys(e){
              var arrs= [], key= window.event? event.keyCode: e.keyCode;
              arrs[37]= 'left';
              arrs[38]= 'up';
              arrs[39]= 'right';
              arrs[40]= 'down';
              if(arrs[key]) alert(arrs[key]);
           }
           document.onkeydown=checkArrowKeys;
          
            canvas.addEventListener('mousemove', function (evt) {
                var mousePos = getMousePos(canvas, evt);
                var message = "Mouse position: " + mousePos.x + "," + mousePos.y;
                // TRY TO UNCOMMENT THIS LINE !
                //writeMessage(canvas, message);
                // Let's draw some lines that follow the mouse pos
                if (!started) {
                    previousMousePos = mousePos;
                    started = true;
                } else {
                    context.clearRect(0,0, canvas.width, canvas.height);
                  drawObstacles(canvas, context);
                  drawPlayer(canvas, context, mousePos.x, mousePos.y);
                 
                  // Collision test avec l'obstacle rectangulaire
                  if(rectsOverlap(mousePos.x, mousePos.y, 20, 20, 100, 100, 200, 200)) {
                    context.fillStyle = 'red';
                  } else { 
                    context.fillStyle ='black';
                  }
                  // Test pour voir si on a atteint la cible
                  if(circRectsOverlap(mousePos.x, mousePos.y, 20, 20, 
                                      targetX, targetY, targetRadius)) {                                         
                      drawTarget(canvas, context, targetX, targetY, targetRadius, 'yellow');                    
                  } else {                    
                     drawTarget(canvas, context, targetX, targetY, targetRadius, "#8ED6FF");
                  }
                  
                    previousMousePos = mousePos;
                }
            }, false);
        };
    </script>
</head>
<body onmousedown="return false;">
<canvas id="myCanvas" width="578" height="400">
</canvas>
</body>
</html>
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
anonymouspro
0viewers