当从 Math.random() 生成正方形时,如何使正方形对象在鼠标光标位于其顶部时消失

问题描述

我想让我用 Math.random() 函数制作的方块在我的鼠标移到它们上面时消失。我尝试了很多方法,但似乎都做对了。

如果光标在上面,我只想删除一个正方形 这是我到目前为止的代码

    function draw(){  
      var dom = document.getElementById("myCanvas");
      if (dom.getContext) {
          var ctx = dom.getContext('2d');
          var width  = window.innerWidth;
          var height = window.innerHeight;

          dom.width=width; // assign width to canvas
          dom.height=height; //assign height to canvas

          //use so squares aren't cut off 
          positionW=window.innerWidth - 110;
          positionH=window.innerHeight - 110;

        function makeSquares(n){
          for(let i = 0; i < n; i++){

            var x = Math.random()* positionW; 
            var y = Math.random()* positionH;

            //make squares colourful
            ctx.globalAlpha=0.5; //opacity
            var color = '#'+ Math.round(0xffffff * Math.random()).toString(16);
            ctx.fillStyle = color;

            //draw rectangle
            ctx.fillRect(x,y,100,100);
          }
        }
      //initialize 
        makeSquares(100);

        }
      }

      draw();

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)