Canvas刮刮乐效果

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
 
<body>
    <div id="container" style="width: 100%; height: 600px; position: relative">
      <canvas id="canvas" width="300" height="200"></canvas>
      <img id="cover" src="./Images/2.jpeg" alt="cover" width="300" height="200" style="position: absolute; left: 0; z-index: -1;">
    </div>
</body>
<script>


  const canvas = document.getElementById("canvas");
  const coverCtx = canvas.getContext("2d");

  const image = new Image();
  image.src = "./Images/1.jpeg";
  image.onload = function() {
    coverCtx.drawImage(image, 0, 0, canvas.width, canvas.height);
  };

  let isMouseDown = false;
  const clearArea = (x, y) => {
    coverCtx.save();
    coverCtx.beginPath();
    coverCtx.arc(x, y, 20, 0, Math.PI*2);
    coverCtx.clip();
    coverCtx.clearRect(x-20, y-20, 100, 100);
    coverCtx.restore();
  };

  canvas.addEventListener("mousedown", (e) => {
    isMouseDown = true;
    clearArea(e.offsetX, e.offsetY);
  });

  canvas.addEventListener("mousemove", (e) => {
    if(isMouseDown) {
      clearArea(e.offsetX, e.offsetY);
    }
  });

  canvas.addEventListener("mouseup", () => {
    isMouseDown = false;
  });
</script>
</html>

### UniApp 中使用 Canvas 实现刮刮乐效果 在 UniApp 开发环境中,`<canvas>` 组件提供了绘制图形的能力。为了实现刮奖功能,主要依赖于 `globalCompositeOperation='destination-out'` 属性来擦除特定区域的内容,从而暴露出底层图像或文字。 #### 创建 HTML 结构 首先,在页面模板文件中声明 `<canvas>` 元素: ```html <template> <view class="container"> <!-- 定义画布 --> <canvas type="2d" id="scratchCanvas"></canvas> <!-- 底层内容(被遮盖部分),可以是图片或其他形式 --> <image src="/static/images/prize.png" mode="widthFix" style="position:absolute;z-index:-1;"></image> </view> </template> <style scoped> .container { position: relative; } #scratchCanvas { width: 300px; height: 300px; border-radius: 8px; background-color: gray; } </style> ``` 上述代码设置了基本布局样式并引入了一张作为奖励图案的静态资源图片[^2]。 #### 初始化 Canvas 和监听触摸事件 接着,在 JavaScript 文件里初始化 Canvas 上下文对象,并设置好初始状态: ```javascript export default { data() { return { ctx: null, isScratching: false, // 是否正在刮奖标志位 }; }, onLoad() { const query = uni.createSelectorQuery().in(this); query.select('#scratchCanvas') .fields({node: true, size: true}) .exec((res) => { let canvas = res[0].node; this.ctx = canvas.getContext('2d'); // 设置顶层灰色覆盖层 this.ctx.fillStyle = 'gray'; this.ctx.fillRect(0, 0, res[0].width, res[0].height); // 配置合成模式为擦除操作 this.ctx.globalCompositeOperation = 'destination-out'; // 添加触控事件处理程序 canvas.addEventListener('touchstart', (e) => {this.isScratching=true;},false); canvas.addEventListener('touchmove', (e) => {if(this.isScratching)this.draw(e);},false); canvas.addEventListener('touchend', () => {this.isScratching=false;},false); }); }, methods: { draw(event){ var rect=event.target.getBoundingClientRect(); var x=event.touches[0].clientX-rect.left; var y=event.touches[0].clientY-rect.top; this.ctx.beginPath(); this.ctx.arc(x,y,20,0,Math.PI*2,true); this.ctx.fill(); console.log(`Drawing at (${x},${y})`); } } }; ``` 这段脚本实现了当用户手指滑过屏幕时会触发相应的绘画逻辑,进而模拟出真实的刮奖体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

QmagicianRX

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值