如何解决当从 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 举报,一经查实,本站将立刻删除。