<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>刮刮卡</title> <style> *{ margin: 0; padding: 0; } img{ width: 400px; height: 300px; ...
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>刮刮卡</title>
<style>
*{
margin: 0;
padding: 0;
}
img{
width: 400px;
height: 300px;
position: absolute;
left: 200px;
z-index: -1;
}
canvas{
margin-left: 200px;
}
</style>
</head>
<body>
<img src="img/gailun.jpg" alt="">
<canvas id="canvas" width="400" height="300"></canvas>
<script>
var canvas = document.getElementById("canvas");
var context = canvas.getContext('2d');
//畫蒙布
context.beginPath();
context.fillStyle= 'grey'
context.fillRect(0,0,400,300);
//滑鼠按下開刮
canvas.onmousedown=function(){
canvas.onmousemove = function(){
//獲取滑鼠坐標
var x = event.clientX;
var y = event.clientY;
//destination-out 顯示原來的不在後來區域的部分
context.globalCompositeOperation = "destination-out";
context.beginPath();
context.arc(x-200,y,30,0,Math.PI*2);
context.fill();
}
}
//滑鼠抬起不刮開
canvas.onmouseup=function(){
canvas.onmousemove = function(){
}
}
</script>
</body>
</html>