向isPointinPath添加单击

时间:2014-02-19 18:56:12

标签: javascript html canvas

我在画布上有三个矩形。

当我的鼠标位于橙色矩形的路径中时,会出现绿色矩形。我想将它更改为当鼠标呈橙色 AND 点击时。

为了避免混淆,我从初始化函数中取出green(ctx),我将函数green(ctx)作为参考。它没有用处。

<!DOCTYPE html>

<!-- Created with Ai->Canvas Export Plug-In Version 1.0 (Mac) -->
<!-- By Mike Swanson (http://blogs.msdn.com/mswanson/)        -->
<!-- and MIX Online  (http://visitmix.com/)                   -->

<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.onmousemove = 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();
                ctx.restore();

            }

        }
    }

    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 green(ctx) {

      // green/Path
      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();
      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>

1 个答案:

答案 0 :(得分:0)

如果您只想在点击时更改颜色,只需替换:

canvas.onmousemove

canvas.onclick

A fiddle here (我希望我在这里正确理解你。)