canvas刮刮乐

1 篇文章 0 订阅
1 篇文章 0 订阅

1、主要使用clearRect来实现清除表层内容

2、在按下事件里边嵌套移动事件,将当前鼠标距离所在盒子水平距离和垂直方向距离分别作为clearRect的第一个和第二个参数。

直接上代码:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>刮刮乐</title>
	<style>
		.box{
			margin:0 auto;
			text-align: center;
		}
		#canvas{
			border:1px solid;
			z-index: 2;
		}
		.endState{
			color:red;
			width: 500px;
			height: 200px;
			background: orange;
		}
		.endState h2{
			color:purple;
			line-height: 200px;
		}
		.main{
			width: 500px;
			height: 200px;
			position: relative;
			margin:0 auto;
		}
		.main canvas,.main div{
			position: absolute;
			top:0;
			left: 0;
			width: 100%;
			height: 100%;
		}
	</style>
</head>
<body>
	<div class="box">
		<h2>欢迎刮奖</h2>
		<div class="main">
			<canvas width="500" height="200" id='canvas'></canvas>
			<div class="endState"><h2>恭喜您喜中一等奖</h2></div>
		</div>
	</div>
	<script>
		window.onload=function(){
			let canvas=document.getElementById('canvas');
			let ctx=canvas.getContext('2d');
			ctx.strokeStyle='blue';
			ctx.beginPath();
			ctx.moveTo(0,0);
			ctx.rect(0,0,600,300);
			ctx.fillStyle='#eee';
			ctx.fill();
			canvas.onmousedown=function(){
				canvas.onmousemove=function(e){
					ctx.clearRect(e.offsetX,e.offsetY,16,16);
				}
				document.onmouseup=function(){
					canvas.onmousemove=null;
					document.onmousedown=null;
				}
			}
		}
	</script>
</body>
</html>

效果图:

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值