canvas实现刮刮卡效果

基本api就直接放源码了

<!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>
        body {
            margin: 0;
            padding: 0;
        }
        
        #canvas {
            position: absolute;
            left: 0;
        }
    </style>
</head>

<body onl oad="draw()">
    <img src="./red.jpg" alt="">
    <canvas id="canvas" width="680" height="453">
        请更新版本!
    </canvas>

    <script>
        function draw() {
            var canvas = document.querySelector("#canvas");
            if (canvas.getContext) {
                var ctx = canvas.getContext("2d");
                ctx.beginPath();
                ctx.fillStyle = "gray";
                ctx.fillRect(0, 0, canvas.width, canvas.height)
                ctx.globalCompositeOperation = 'destination-out';
                ctx.lineWidth = 20;
                ctx.lineCap = 'round';
                canvas.onmousedown = function(e) {
                    var ev = e || window.event;
                    var x = ev.clientX;
                    var y = ev.clientY;
                    ctx.moveTo(x, y);
                    this.onmousemove = function(e) {
                        var ev = e || window.event;
                        var x = ev.clientX;
                        var y = ev.clientY;
                        ctx.lineTo(x, y);
                        ctx.stroke();
                    }
                    this.onmouseup = function(e) {
                        this.onmousemove = null;
                    }
                }
            }
        }
    </script>
</body>

</html>

希望对大家有帮助!!

上一篇:canvas画线


下一篇:sql生成随机不重复字符串 可指定长度