代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<style type="text/css">
#cvs{
border: 1px solid palegreen;
position: absolute;
}
#out{
width: 200px;
height: 100px;
text-align: center;
line-height: 100px;
font-size: 30px;
position: absolute;
}
</style>
<body>
<div id="out">奖项</div>
<canvas id="cvs" width="200" height="100"></canvas> <!--画布 -->
</body>
<script type="text/javascript">
var cvs=document.getElementById("cvs");
var ctx=cvs.getContext("2d");
var arr=["汽车一辆",'现金1筐',"洗衣液一袋","谢谢惠顾","泡泡机"];
var i=Math.floor(Math.random()*arr.length) //随机数据
document.getElementById("out").innerHTML=arr[i];
ctx.beginPath();//开始绘画
ctx.fillStyle="#eee" //填充颜色
ctx.fillRect(0,0,200,100)
ctx.closePath() //关闭
cvs.onmousedown=function(){
document.onmousemove=function(e){
var x=e.clientX-cvs.offsetLeft; //获取鼠标位置
var y=e.clientY-cvs.offsetTop;
ctx.clearRect(x,y,20,20)
}
document.onmouseup=function(){
document.onmousedown=null;
document.onmousemove=null
}
}
</script>
</html>
效果如下: