<!DOCTYPE html>
<html lang="ch-en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>刮刮乐</title>
</head>
<body>
<canvas width="300" height="150" id="canvas"></canvas>
<div class="prize"></div>
</body>
<script>
window.onload = () => {
var canvas = document.querySelector('#canvas');
var ctx = canvas.getContext('2d');
//绘制矩形
ctx.fillStyle = '#c0c0c0';
ctx.fillRect(0,0,300,150);
canvas.onmousedown = () => { //按下的事件
canvas.onmousemove = (e2) => {
ctx.clearRect(e2.clientX, e2.clientY, 10, 10); //清除
}
}
canvas.onmouseup = (e) => { //松开事件
canvas.onmousemove = null; //清除事件 事件清除直接用null
}
//中奖信息
var arr = ["奖品1","奖品2","奖品3","奖品4"];
var i = Math.floor(Math.random()*arr.length);
document.getElementsByClassName("prize")[0].innerText = arr[i];
}
</script>
<style>
.prize {
position: absolute;
width: 300px;
height: 150px;
text-align: center;
line-height: 150px;
font-size: 30px;
color: red;
}
#canvas{
border: 1px solid #000;
position: absolute;
z-index: 2;
}
</style>
</html>