当前位置:首页 > VUE

移动端vue实现刮刮卡

2026-02-23 08:34:59VUE

实现思路

在移动端Vue中实现刮刮卡效果,核心是利用Canvas绘制蒙层,通过触摸事件擦除蒙层显示底层内容。关键点包括Canvas初始化、触摸事件监听以及擦除算法实现。

基本实现步骤

安装依赖(如需要) 若项目未安装Vue-Touch,可通过npm安装:

npm install vue-touch@next

模板结构

<template>
  <div class="scratch-container">
    <canvas 
      ref="canvas" 
      @touchstart="handleTouchStart" 
      @touchmove="handleTouchMove" 
      @touchend="handleTouchEnd">
    </canvas>
    <div class="prize-content">
      <!-- 底层奖品信息 -->
      <p>{{ prizeText }}</p>
    </div>
  </div>
</template>

样式设置

.scratch-container {
  position: relative;
  width: 300px;
  height: 150px;
}

canvas {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 2;
  background-color: #ccc;
  touch-action: none;
}

.prize-content {
  position: absolute;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

JavaScript逻辑

export default {
  data() {
    return {
      isDrawing: false,
      ctx: null,
      prizeText: "一等奖"
    }
  },
  mounted() {
    this.initCanvas();
  },
  methods: {
    initCanvas() {
      const canvas = this.$refs.canvas;
      canvas.width = canvas.offsetWidth;
      canvas.height = canvas.offsetHeight;

      this.ctx = canvas.getContext('2d');
      this.ctx.fillStyle = '#999';
      this.ctx.fillRect(0, 0, canvas.width, canvas.height);
      this.ctx.globalCompositeOperation = 'destination-out';
    },

    handleTouchStart(e) {
      this.isDrawing = true;
      const touch = e.touches[0];
      this.drawCircle(touch.clientX, touch.clientY);
    },

    handleTouchMove(e) {
      if (!this.isDrawing) return;
      const touch = e.touches[0];
      const rect = this.$refs.canvas.getBoundingClientRect();
      const x = touch.clientX - rect.left;
      const y = touch.clientY - rect.top;
      this.drawCircle(x, y);
    },

    handleTouchEnd() {
      this.isDrawing = false;
      this.checkScratchCompletion();
    },

    drawCircle(x, y) {
      this.ctx.beginPath();
      this.ctx.arc(x, y, 15, 0, Math.PI * 2);
      this.ctx.fill();
    },

    checkScratchCompletion() {
      const imageData = this.ctx.getImageData(
        0, 0, 
        this.$refs.canvas.width, 
        this.$refs.canvas.height
      );
      const pixels = imageData.data;
      let transparentCount = 0;

      for (let i = 0; i < pixels.length; i += 4) {
        if (pixels[i+3] === 0) {
          transparentCount++;
        }
      }

      const transparency = transparentCount / (pixels.length/4);
      if (transparency > 0.7) {
        console.log('刮刮卡已完成');
      }
    }
  }
}

优化方案

性能优化 使用requestAnimationFrame优化绘制性能:

handleTouchMove(e) {
  if (!this.isDrawing) return;
  requestAnimationFrame(() => {
    const touch = e.touches[0];
    const rect = this.$refs.canvas.getBoundingClientRect();
    this.drawCircle(touch.clientX - rect.left, touch.clientY - rect.top);
  });
}

适配方案 添加窗口大小变化监听:

mounted() {
  this.initCanvas();
  window.addEventListener('resize', this.handleResize);
},
beforeDestroy() {
  window.removeEventListener('resize', this.handleResize);
},
methods: {
  handleResize() {
    this.initCanvas();
  }
}

注意事项

  1. 移动端需确保添加了viewport meta标签:

    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
  2. 对于高清屏设备,需要考虑Canvas的像素比:

    
    initCanvas() {
    const canvas = this.$refs.canvas;
    const ratio = window.devicePixelRatio || 1;

canvas.width = canvas.offsetWidth ratio; canvas.height = canvas.offsetHeight ratio;

this.ctx = canvas.getContext('2d'); this.ctx.scale(ratio, ratio); // ...其余初始化代码 }

移动端vue实现刮刮卡


3. 擦除算法可根据需求调整,如改为矩形擦除:
```javascript
drawRect(x, y) {
  this.ctx.fillRect(x-10, y-10, 20, 20);
}

标签: 刮刮卡vue
分享给朋友:

相关文章

vue 指令实现

vue 指令实现

Vue 指令实现 Vue 指令是 Vue.js 提供的特殊属性,用于在 DOM 元素上添加特殊行为。指令以 v- 前缀开头,例如 v-model、v-if、v-for 等。以下是实现自定义指令和常用内…

vue 实现拖拽

vue 实现拖拽

Vue 实现拖拽功能 使用 HTML5 原生拖拽 API HTML5 提供了原生的拖拽 API,可以通过 draggable 属性和相关事件实现拖拽功能。 <template> &l…

vue怎么实现

vue怎么实现

Vue 实现方法 Vue 是一个流行的前端框架,用于构建用户界面。以下是几种常见的实现方法: 数据绑定 使用 v-model 指令实现双向数据绑定,适用于表单输入元素。 <input v-m…

vue实现handsontable

vue实现handsontable

Vue 中实现 Handsontable 要在 Vue 项目中集成 Handsontable,可以使用官方提供的 @handsontable/vue 封装库。以下是一个完整的实现步骤。 安装依赖 确…

vue实现曲线

vue实现曲线

Vue 实现曲线的方法 在 Vue 中实现曲线可以通过多种方式,包括使用 SVG、Canvas 或第三方库如 D3.js、ECharts 等。以下是几种常见的方法: 使用 SVG 绘制曲线 SVG…

vue mvvm 实现

vue mvvm 实现

Vue MVVM 实现原理 Vue.js 的 MVVM(Model-View-ViewModel)架构通过数据绑定和响应式系统实现视图与数据的自动同步。以下是其核心实现机制: 数据劫持与响应式系统…