canvas刮刮乐效果

虽然对于canvas不是很了解,但是写这个效果确实是很常用的,看到这个资料的时候相对于来说这个代码还是比较简单的,所以这里先保存下来

效果展示

<!DOCTYPE html>

<html lang="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>Document</title>

<style>

* {

margin: 0;

padding: 0;

}

img {

width: 400px;

height: 300px;

left: 200px;

position: absolute;

z-index: -1;

}

canvas {

margin-left: 200px;

}

</style>

</head>

<body>

<img src="http://n.sinaimg.cn/photo/transform/700/w1000h500/20200317/512d-iqyryku6022748.jpg" alt="">

<canvas id="canvas" width="400" height="300"></canvas>

</body>

</html>

<script type="text/javascript">

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>

以上是 canvas刮刮乐效果 的全部内容, 来源链接: utcz.com/a/14674.html

回到顶部