canvas制作刮刮卡

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			#ggk {
				width: 400px;
				height: 100px;
				position: relative;
			}

			#gk .nr {
				width: 400px;
				height: 100px;
				position: absolute;
				left: 0;
				top: 0;
				font-size: 30px;
				text-align: center;
				line-height: 100px;
			}

			#gk #canvas {
				width: 400px;
				height: 100px;
				position: absolute;
				left: 0;
				top: 0;
			}
		</style>
	</head>
	<body>
		<div id="gk">
			<div class="nr">谢谢会顾</div>
			<canvas id="canvas" width="400" height="100"></canvas>
		</div>
	</body>
	<script type="text/javascript">
		let canvas = document.querySelector('#canvas')
		let ggkDom = document.querySelector('#gk')

		let ctx = canvas.getContext('2d')

		ctx.fillStyle = 'darkgray'
		ctx.fillRect(0, 0, 400, 100)
		ctx.font = "20px 微软雅黑"
		ctx.fillStyle = '#FFF'
		ctx.fillText('刮刮卡', 180, 50)
		// 设置isDraw= true,即为允许绘制
		var isGua = false;
		canvas.onmousedown = function() {
			isGua = true
		}
		// 开始移动时候绘制刮奖面积圆形,将原图像内的目标内容给清除掉
		canvas.onmousemove = function(e) {
			
			if (isGua) {
				let x=e.pageX-ggkDom.offsetLeft;
				let y = e.pageY - ggkDom.offsetTop
				ctx.globalCompositeOperation = 'destination-out'
				ctx.arc(x, y, 20, 0, 2 * Math.PI)
				ctx.fill()
			}
		}

		canvas.onmouseup = function() {
			isGua = false

		}
	</script>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值