canvas做一个刮刮乐效果(复制拿走)

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">
<title>audio</title>
<style type="text/css">
    canvas {
        /* canvas 默认宽高 300*150 
        css设置的宽高,只能设置canvas对象在页面显示的大小。
        并不能增加画布内部的像素数
    */
        border: 1px solid black;
        /* width: 500px;
    height: 500px; */
        margin: 0 auto;
        position: absolute;
        left: 0;
        top: 0;
    }

    div {
        width: 500px;
        height: 500px;
        background: url("https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=4210473879,3554842544&fm=27&gp=0.jpg") no-repeat;
        background-size: 100% 100%;
        position: relative;
    }

    div p {
        font-size: 50px;
        line-height: 500px;
        text-align: center;
        width: 100%;
    }
</style>

</head>

<body>

<!-- 在标签上添加canvas像素值 -->
<div>
    <p>一等奖</p>
    <canvas id="canvasId" width="500" height="500">您的浏览器不支持canvas!</canvas>
</div>
<script type="text/javascript">
    var cv = document.getElementById("canvasId"),
        p = document.getElementsByTagName("p")[0];

    //设置中奖几率
    var num = 10000 * Math.random(),
        result = "谢谢惠顾";
    if (num < 1000) {
        result = "三等奖";
    }
    if (num < 50) {
        result = "二等奖";
    }
    if (num < 5) {
        result = "一等奖";
    }
    p.innerText = result;
    try {
        var ct = cv.getContext("2d");
        //绘制背景色
        ct.fillStyle = "#aaa";
        ct.fillRect(0, 0, 500, 500);
        //设置绘制状态
        ct.lineCap = "round";   //设置线条两端为圆弧
        ct.lineJoin = "round";   //设置线条转折为圆弧
        ct.lineWidth = 60;
        /*设置新绘制清除新绘制内容和原内容的重叠区域*/
        ct.globalCompositeOperation = "destination-out";
    } catch (e) {
        console.log(e);
    }
    //绘制曲线
    var flag = false,
        lx, ly;
    cv.onmousedown = function (e) {
        flag = true;
        lx = e.offsetX;
        ly = e.offsetY;
    }

    cv.onmousemove = function (e) {
        if (flag) {
            ct.beginPath();
            ct.moveTo(lx, ly);
            ct.lineTo(e.offsetX, e.offsetY);
            ct.stroke();
            ct.closePath();

            //更新坐标
            lx = e.offsetX;
            ly = e.offsetY;
        }
    }

    cv.onmouseup = function (e) {
        flag = false;
    }
</script>

</html>

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容