<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
margin:0;
padding:0;
}
#top{
background:transparent;
}
#bottom{
position:absolute;
top:0;
width:500px;
height:200px;
z-index:-1;
background:wheat;
}
</style>
</head>
<body>
<canvas id="bottom" width="500" height="200"></canvas>
<canvas id="top" width="500" height="200"></canvas>
<script>
var obj = {
findEl: function(name){
return document.querySelector(name);
},
init() {
let canvasBottom = this.findEl('#bottom');
let canvasTop = this.findEl("#top");
let ctx1 = canvasBottom.getContext("2d");
let ctx2 = canvasTop.getContext("2d");
let tag = false; //为true时表示鼠标按下,开始清除上面的蒙板,鼠标抬起设置为false,表示操作结束
ctx2.fillStyle = "red";
ctx2.fillRect(0,0,canvasBottom.clientWidth,canvasBottom.clientHeight);
ctx1.font="12px 微软雅黑";
// 设置画布中文字居中显示,需要设置textAlign为center,还需要将fillText的第二个参数设置为画布容器的一半
ctx1.textAlign = "center";
ctx1.fillText("恭喜您,中奖100000000000000元",250,50);
canvasTop.addEventListener("mousedown",function(e){
ctx2.beginPath()
tag = true
})
canvasTop.addEventListener("mousemove",function(e){
if (!tag) {
return
}
ctx2.clearRect(e.offsetX,e.offsetY,100,100)
})
canvasTop.addEventListener("mouseup",function(e){
ctx2.closePath()
tag = false
})
}
}
obj.init()
// 实现思路:
//1.准备两个画布,一个用于放中奖信息,另一个是上面的遮盖蒙板
//2.将中奖信息的画布放置于蒙板层下
//3.将蒙板的背景色设置成透明,然后将当前蒙板填充上别的颜色
//4.监听从鼠标按下到鼠标抬起这个过程从而将蒙板上填充的颜色清除掉
</script>
</body>
</html>