简体   繁体   中英

Clearing shape in a canvas

When this page opens, there are two shapes: one orange and one tan rectangle. Right now when the orange rectangle is clicked, a new green one appears. When the mouse is clicked outside of the orange rectangle, I would like the green one to go away. This would be done in the else where i have tried ctx.clear();

<!DOCTYPE html>

<html lang="en">
 <head>
  <meta charset="UTF-8" />
  <title>testclick</title>
  <script>

    function init() {

      var canvas = document.getElementById("canvas");
      var ctx = canvas.getContext("2d");

      tan(ctx);

      orange(ctx);

      canvas.onclick = function (e)
        {
            var canvas = e.target;
            var ctx = canvas.getContext('2d');

            // This gets the mouse coordinates (relative to the canvas)
            var mouseX  =  e.clientX - canvas.getBoundingClientRect().left;
            var mouseY  =  e.clientY - canvas.getBoundingClientRect().top;


            // Replay the rectangle path (no need to fill() it) and test it
            ctx.beginPath();
            ctx.moveTo(663.3, 254.3);
            ctx.lineTo(516.0, 254.3);
            ctx.lineTo(516.0, 176.7);
            ctx.lineTo(663.3, 176.7);
            ctx.lineTo(663.3, 254.3);

            if (ctx.isPointInPath(mouseX, mouseY)) {
                ctx.save();
                ctx.beginPath();
                ctx.moveTo(417.3, 320.7);
                ctx.lineTo(113.3, 320.7);
                ctx.lineTo(113.3, 0.0);
                ctx.lineTo(417.3, 0.0);
                ctx.lineTo(417.3, 320.7);
                ctx.closePath();
                ctx.fillStyle = "rgb(60, 127, 60)";
                ctx.fill();
            } else {
                ctx.clear();
                ctx.moveTo(417.3, 320.7);
                ctx.lineTo(113.3, 320.7);
                ctx.lineTo(113.3, 0.0);
                ctx.lineTo(417.3, 0.0);
                ctx.lineTo(417.3, 320.7);
            }

        }
    }

    function tan(ctx) {

      // tan/Path
      ctx.save();
      ctx.beginPath();
      ctx.moveTo(393.3, 422.7);
      ctx.lineTo(0.0, 422.7);
      ctx.lineTo(0.0, 52.7);
      ctx.lineTo(393.3, 52.7);
      ctx.lineTo(393.3, 422.7);
      ctx.closePath();
      ctx.fillStyle = "rgb(255, 238, 191)";
      ctx.fill();
      ctx.restore();
    }

    function orange(ctx) {

      // orange/Path
      ctx.save();
      ctx.beginPath();
      ctx.moveTo(663.3, 254.3);
      ctx.lineTo(516.0, 254.3);
      ctx.lineTo(516.0, 176.7);
      ctx.lineTo(663.3, 176.7);
      ctx.lineTo(663.3, 254.3);
      ctx.closePath();
      ctx.fillStyle = "rgb(240, 89, 41)";
      ctx.fill();
      ctx.restore();
    }


  </script>
 </head>
 <body onload="init()">
   <canvas id="canvas" width="664" height="423"></canvas>
 </body>
</html>

There is no clear() method on context. Try with:

ctx.clearRect(x, y, width, height);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM