2

I have a function that draw something on a canvas after triggering the if statement. I would like to know how to clear the canvas after it has been remove from the the coordinate(outside the if statement), I'm not sure how to go about this.

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
        <script type="text/javascript">
            function startCanvas() {
                var c = document.getElementById("myCanvas");
                var ctx = c.getContext("2d");

                var one = c.getContext("2d");
                var two = c.getContext("2d");
                var three = c.getContext("2d");
                var four = c.getContext("2d");
                var five = c.getContext("2d");
                var clearCanvas = false;

                var image = new Image();
                var imageone = new Image();

                image.onload = function () {
                    ctx.drawImage(image, 69, 50);

                    //draw a circle
                    one.beginPath();
                    one.arc(180, 90, 10, 0, Math.PI * 2, true);
                    one.closePath();
                    one.fill();

                    two.beginPath();
                    two.arc(155, 138, 10, 0, Math.PI * 2, true);
                    two.closePath();
                    two.fill();

                    three.beginPath();
                    three.arc(160, 180, 10, 0, Math.PI * 2, true);
                    three.closePath();
                    three.fill();

                    four.beginPath();
                    four.arc(257, 210, 10, 0, Math.PI * 2, true);
                    four.closePath();
                    four.fill();

                    five.beginPath();
                    five.arc(238, 235, 10, 0, Math.PI * 2, true);
                    five.closePath();
                    five.fill();
                };

                image.src = 'denmark.jpg';

                c.addEventListener(
                    'mousemove',
                    function (e) {
                        if ((e.x >= 170 && e.x <= 190) && (e.y >= 80 && e.y <= 100)) {
                            alert('this is a test');
                        }
                    },
                    false);

                c.addEventListener(
                    'mousemove',
                    function (e) {
                        if ((e.x >= 145 && e.x <= 190) && (e.y >= 128 && e.y <= 148)) {
                            ctx.font = "30px Arial";
                            ctx.fillText("Hello World", 400, 20);
                            ctx.drawImage(imageone, 400, 60);
                            imageone.src = 'alborg.jpg';
                        } else if ((e.x < 145 && e.x < 190) && (e.y > 128 && e.y < 148)) {
                            ctx.clearRect(0, 0, 400, 60);
                        }
                    },
                    false);

                c.addEventListener(
                    'mousemove',
                    function (e) {
                        if ((e.x >= 150 && e.x <= 190) && (e.y >= 170 && e.y <= 190)) {
                            alert('also work');
                        }
                    },
                    false);

                c.addEventListener(
                    'mousemove',
                    function (e) {
                        if ((e.x >= 247 && e.x <= 290) && (e.y >= 200 && e.y <= 220)) {
                            alert('ok');
                        }
                    },
                    false);

                c.addEventListener(
                    'mousemove',
                    function (e) {
                        if ((e.x >= 228 && e.x <= 290) && (e.y >= 225 && e.y <= 245)) {
                            alert('yes');
                        }
                    },
                    false);
            }
        </script>
    </head>
    <body onload="startCanvas()">
        <canvas id="myCanvas" width="600" height="600">Your browser does not support the HTML5 canvas tag.</canvas>
    </body>
</html>
Cerbrus
  • 70,800
  • 18
  • 132
  • 147

3 Answers3

3

use

ctx.clearRect(x, y, canvas_width, canvas_height);
//In your code x=0,y=0, canvas_width=400 and canvas_height=60

instead of

ctx.clearRect(400,60,0,0);

Update:-

Try This Update...

 else if ((e.x < 145 && e.x < 190) && (e.y > 128 && e.y < 148)) { 
 ctx.clearRect(0,0,400,60);
 one.clearRect(0,0,400,60);
 two.clearRect(0,0,400,60);
 three.clearRect(0,0,400,60);
 four.clearRect(0,0,400,60);
 five.clearRect(0,0,400,60);
}

Update

        c.addEventListener('mousemove',function (e) {
  if ((e.x >= 145 && e.x <= 190) && (e.y >= 128 && e.y <= 148)) {
   console.log("Hello"); 
      ctx.clearRect(0, 0, 400, 60);
      one.clearRect(0, 0, 400, 60);
      two.clearRect(0, 0, 400, 400);
      three.clearRect(0, 0, 400, 60);
      four.clearRect(0, 0, 400, 60);
  five.clearRect(0, 0, 400, 60);

  }  else if ((e.x < 145 && e.x < 190) && (e.y > 128 && e.y < 148)) {         
  console.log("hi");      
  }
  }, false);
Scarecrow
  • 4,057
  • 3
  • 30
  • 56
1

clearRect takes four arguments: x, y, width and height. Also, is it ctx or context? Use the right variable.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
1

To clear the canvas of your "Hello World" and "alborg.jpg", use this: ctx.clearRect(400,0,200,600)

Example code:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>

<script type="text/javascript">
    function startCanvas() {
        var c = document.getElementById("myCanvas");
        var ctx = c.getContext("2d");

        var one = c.getContext("2d");
        var two = c.getContext("2d");
        var three = c.getContext("2d");
        var four = c.getContext("2d");
        var five = c.getContext("2d");
        var clearCanvas = false;

        var image = new Image();
        var imageone = new Image();

        image.onload = function () {
            ctx.drawImage(image, 69, 50);

            //draw a circle
            one.beginPath();
            one.arc(180, 90, 10, 0, Math.PI * 2, true);
            one.closePath();
            one.fill();

            two.beginPath();
            two.arc(155, 138, 10, 0, Math.PI * 2, true);
            two.closePath();
            two.fill();

            three.beginPath();
            three.arc(160, 180, 10, 0, Math.PI * 2, true);
            three.closePath();
            three.fill();

            four.beginPath();
            four.arc(257, 210, 10, 0, Math.PI * 2, true);
            four.closePath();
            four.fill();

            five.beginPath();
            five.arc(238, 235, 10, 0, Math.PI * 2, true);
            five.closePath();
            five.fill();
        };

        image.src = 'coffee.png';


     c.addEventListener('mousemove',
     function (e) {
         if ((e.x >= 170 && e.x <= 190) && (e.y >= 80 && e.y <= 100)) {
             alert('this is a test');
         }
     }
     , false);


     c.addEventListener('mousemove',
     function (e) {
      if ((e.x >= 145 && e.x <= 190) && (e.y >= 128 && e.y <= 148)) {
          ctx.font = "30px Arial";
          ctx.fillText("Hello World", 400, 20);
          ctx.drawImage(imageone, 400, 60);
          imageone.src = 'house-icon.png';
      } else if ((e.x < 145 && e.x < 190) && (e.y > 128 && e.y < 148)) {
      //
      //
      // HERE IS THE CHANGE TO MAKE YOUR CODE WORK !!!
      //
      // Use this to clear only the "Hello World" and "alborg.jpg"
      //
      //
                ctx.clearRect(400,0,200,600);
      //
      //
      //
            }
        }
        , false);


      c.addEventListener('mousemove',
      function (e) {
           if ((e.x >= 150 && e.x <= 190) && (e.y >= 170 && e.y <= 190)) {
               alert('also work');
           }
       }
       , false);

       c.addEventListener('mousemove',
          function (e) {
              if ((e.x >= 247 && e.x <= 290) && (e.y >= 200 && e.y <= 220)) {
                  alert('ok');
              }
          }
          , false);

        c.addEventListener('mousemove',
        function (e) {
            if ((e.x >= 228 && e.x <= 290) && (e.y >= 225 && e.y <= 245)) {
                alert('yes');
            }
        }
        , false);

    }
</script>
</head>
<body onload="startCanvas()">
<canvas id="myCanvas" width="600" height="600";">
Your browser does not support the HTML5 canvas tag.</canvas>
</body>
</html>
markE
  • 102,905
  • 11
  • 164
  • 176
  • thank you i been trying to figure this out all night – Devendra Danny Ramdayal Feb 15 '13 at 12:18
  • I had to do ctx.clearRect(0,0,canvas.width,canvas.height); ctx.beginPath(); – elrobis Mar 07 '14 at 16:35
  • @elrobis Did you really downvote my answer because it didn't fit your different situation...how rude! – markE Mar 07 '14 at 17:01
  • Actually I just removed my initial up-vote because I thought the answer below was more project agnostic. – elrobis Mar 07 '14 at 18:38
  • ...now that I think about it, my desktop mouse is horrible and sometimes the left button sticks. It's possible that I unintentionally double-clicked the down vote---at the time it seemed that something screwy happened. I didn't mean to criticize your answer as much as promote the one below it. Anyway to offset the unintended results? – elrobis Mar 07 '14 at 18:40
  • Yeah you're right---it's showing as a down-vote. When I try to undo it, SO says my answer is locked until you edit your post. If you make some change I'll undo the down-vote. :/ – elrobis Mar 07 '14 at 18:45
  • @markE, ok I removed the downvote. Sorry for the nonsense. – elrobis Apr 02 '14 at 15:27