前端实现刮刮乐效果

技术:HTML 、CSS、Canvas

talk is cheap,show me the code.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      canvas {
        position: absolute;
        top: 0;
        border-radius: 20px;
      }
      div {
        width: 300px;
        height: 200px;
        background-color: rgb(125, 56, 56);
        text-align: center;
        line-height: 200px;
        font-size: 66px;
        border-radius: 20px;
      }
    </style>
  </head>
  <body>
    <div>¥100</div>
    <script>
      const canvas = document.createElement("canvas");
      document.body.appendChild(canvas);
      canvas.height = 200;
      canvas.width = 300;
      const ctx = canvas.getContext("2d");

      ctx.beginPath();
      ctx.fillStyle = "#ccc";
      ctx.fillRect(0, 0, 300, 200);

      ctx.globalCompositeOperation = "destination-out";

      ctx.beginPath();
      ctx.lineWidth = 20;

      canvas.onmousedown = function (e) {
        ctx.moveTo(e.offsetX, e.offsetY);

        canvas.onmousemove = function (e) {
          ctx.lineTo(e.offsetX, e.offsetY);
          ctx.stroke();
        };
        canvas.onmouseout = function () {
          canvas.onmousemove = null;
        };
      };
    </script>
  </body>
</html>

效果如图:

效果仅需要基础知识,不涉及复杂逻辑。目的在于提供思路。后面会在评论区附上前端h5实现定制化取景框拍照。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值