其他分享
首页 > 其他分享> > canvas实现刮刮卡效果

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>

希望对大家有帮助!!

标签:function,canvas,效果,ctx,刮刮卡,getContext,var,ev
来源: https://www.cnblogs.com/y-y-y-y/p/12195157.html