您当前的位置: 首页 >  html

彭世瑜

暂无认证

  • 3浏览

    0关注

    2791博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

html:canvas画布绘图简单入门-刮刮乐-4

彭世瑜 发布时间:2022-01-01 12:13:35 ,浏览量:3

canvas示例系列:

  • html:canvas画布绘图简单入门-1
  • html:canvas画布绘图简单入门-2
  • html:canvas画布绘图简单入门-绘制时钟-3
  • html:canvas画布绘图简单入门-刮刮乐-4 在这里插入图片描述

在线示例:https://mouday.github.io/front-end-demo/canvas/canvas-scrape.html


        body {
            margin: 0;
            padding: 0;
            height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .box-wrap {
            position: relative;
            width: 300px;
            height: 150px;
            border: 1px solid #eeeeee;
        }

        .box {
            position: absolute;
            width: 100%;
            height: 100%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 25px;
            color: red;
            letter-spacing: 20px;
        }

        .canvas {
            position: absolute;
            width: 100%;
            height: 100%;
        }
    

    
    
        谢谢惠顾
        
    

    
        let canvas = document.querySelector('#canvas');
        let text = document.querySelector('#text');
        let ctx = canvas.getContext('2d');

        let clientWidth = canvas.clientWidth;
        let clientHeight = canvas.clientHeight;
        let width = canvas.width
        let height = canvas.height

        // 缩放比例
        let widthScale = width / clientWidth;
        let heightScale = height / clientHeight;

        // 绘制背景图
        ctx.fillStyle = 'gray';
        ctx.fillRect(0, 0, width, height);
        ctx.save();

        // 绘制文字
        ctx.translate(width / 2, height / 2);
        ctx.textAlign = "center";
        ctx.textBaseline = "middle";
        ctx.font = "50px 微软雅黑"
        ctx.fillStyle = 'white';
        ctx.fillText('刮开查看', 0, 0);
        // ctx.fill();
        ctx.restore();

        // 监听鼠标事件
        let isDraw = false;
        canvas.onmousedown = function (e) {
            // console.log(e);
            isDraw = true;
        }
        canvas.onmouseup = function (e) {
            // console.log(e);
            isDraw = false;
        }

        canvas.onmousemove = function (e) {
            if (!isDraw) {
                return;
            }
            // console.log(e);
            // ctx.fillStyle = 'white';

            // 在源图像之外显示目标图像。只有源图像之外的目标图像部分会被显示,源图像是透明的
            ctx.globalCompositeOperation = 'destination-out'
            // console.log(e.offsetX, e.offsetY);
            ctx.arc(e.offsetX * widthScale, e.offsetY * heightScale, 30, 0, Math.PI / 180 * 360);
            ctx.fill();
        }

        // 奖品设置
        let list = [{
                title: '一等奖',
                probability: 0.1
            },
            {
                title: '二等奖',
                probability: 0.2
            },
            {
                title: '三等奖',
                probability: 0.3
            }
        ]

        let randomNum = Math.random();
        console.log(randomNum);
        let randomNumUpperBound = 0;

        for (let item of list) {
            randomNumUpperBound += item.probability
            if (randomNum             
关注
打赏
1665367115
查看更多评论
0.0587s