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

效果展示
刮刮乐.gif

<!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>

芹丸子
40 声望4 粉丝

所有文章都是自己的学习记录,如果对你有帮助我很荣幸,如果文章记录之处有什么不对不好的地方还请指教