当前位置:首页 > VUE

vue实现刮奖

2026-01-17 02:04:41VUE

Vue 实现刮奖功能

刮奖功能通常通过 Canvas 或遮罩层实现,以下是两种常见方法的实现步骤:

使用 Canvas 实现

创建一个 Vue 组件,利用 HTML5 Canvas 的绘图功能模拟刮奖效果。

<template>
  <div class="scratch-card">
    <canvas 
      ref="canvas" 
      @mousedown="startScratch" 
      @mousemove="scratch" 
      @mouseup="endScratch"
      @touchstart="startScratch"
      @touchmove="scratch"
      @touchend="endScratch"
    ></canvas>
    <div class="prize">{{ prize }}</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isDrawing: false,
      prize: '一等奖',
      canvas: null,
      ctx: null
    }
  },
  mounted() {
    this.initCanvas()
  },
  methods: {
    initCanvas() {
      this.canvas = this.$refs.canvas
      this.ctx = this.canvas.getContext('2d')

      // 设置canvas尺寸
      this.canvas.width = 300
      this.canvas.height = 150

      // 绘制刮奖层
      this.ctx.fillStyle = '#999'
      this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height)
      this.ctx.font = '20px Arial'
      this.ctx.fillStyle = '#000'
      this.ctx.fillText('刮开查看', 100, 75)
    },
    startScratch(e) {
      this.isDrawing = true
      this.scratch(e)
    },
    scratch(e) {
      if (!this.isDrawing) return

      const rect = this.canvas.getBoundingClientRect()
      const x = (e.clientX || e.touches[0].clientX) - rect.left
      const y = (e.clientY || e.touches[0].clientY) - rect.top

      // 使用圆形擦除
      this.ctx.globalCompositeOperation = 'destination-out'
      this.ctx.beginPath()
      this.ctx.arc(x, y, 15, 0, Math.PI * 2)
      this.ctx.fill()
    },
    endScratch() {
      this.isDrawing = false
    }
  }
}
</script>

<style>
.scratch-card {
  position: relative;
  width: 300px;
  height: 150px;
}
.prize {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: -1;
}
canvas {
  border: 1px solid #ccc;
}
</style>

使用 CSS 遮罩实现

对于简单需求,可以使用 CSS 的 mask 属性实现刮奖效果。

<template>
  <div 
    class="scratch-mask" 
    @mousedown="startScratch"
    @mousemove="scratch"
    @mouseup="endScratch"
    @touchstart="startScratch"
    @touchmove="scratch"
    @touchend="endScratch"
    :style="maskStyle"
  >
    <div class="prize">{{ prize }}</div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isDrawing: false,
      prize: '二等奖',
      points: []
    }
  },
  computed: {
    maskStyle() {
      if (this.points.length === 0) return {}

      let radialGradients = this.points.map(point => 
        `radial-gradient(circle 15px at ${point.x}px ${point.y}px, transparent 0, transparent 15px, black 15px)`
      ).join(', ')

      return {
        '-webkit-mask-image': radialGradients,
        'mask-image': radialGradients
      }
    }
  },
  methods: {
    startScratch(e) {
      this.isDrawing = true
      this.addPoint(e)
    },
    scratch(e) {
      if (!this.isDrawing) return
      this.addPoint(e)
    },
    endScratch() {
      this.isDrawing = false
    },
    addPoint(e) {
      const rect = e.target.getBoundingClientRect()
      const x = (e.clientX || e.touches[0].clientX) - rect.left
      const y = (e.clientY || e.touches[0].clientY) - rect.top

      this.points.push({ x, y })
    }
  }
}
</script>

<style>
.scratch-mask {
  width: 300px;
  height: 150px;
  background-color: #999;
  position: relative;
  -webkit-mask-composite: source-out;
  mask-composite: subtract;
}
.prize {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>

优化建议

  1. 对于移动端,确保添加 touch 事件支持
  2. 可以添加刮开比例检测,当刮开面积超过一定比例时自动显示全部内容
  3. 考虑添加动画效果提升用户体验
  4. 对于复杂需求,可以考虑使用第三方库如 scratchcard-js

两种方法各有优劣,Canvas 实现更灵活但代码量稍多,CSS 实现更简洁但兼容性稍差。根据项目需求选择合适方案。

vue实现刮奖

标签: vue
分享给朋友:

相关文章

拖拽式编程vue实现

拖拽式编程vue实现

拖拽式编程在 Vue 中的实现方法 使用 HTML5 原生拖放 API Vue 可以结合 HTML5 的拖放 API 实现基础拖拽功能。通过 draggable 属性标记可拖拽元素,监听 dragst…

vue按钮实现截屏

vue按钮实现截屏

Vue 按钮实现截屏的方法 在 Vue 项目中实现截屏功能可以通过多种方式完成,以下是几种常见的方法: 使用 html2canvas 库 安装 html2canvas 库: npm insta…

vue实现关键词轮播

vue实现关键词轮播

Vue 实现关键词轮播 基础实现方案 使用 Vue 的 v-for 和 setInterval 实现基础轮播效果: <template> <div class="keyword…

vue实现字母添加排序

vue实现字母添加排序

实现字母添加排序的方法 在Vue中实现字母添加排序功能,可以通过以下步骤完成: 创建Vue实例并定义数据 new Vue({ el: '#app', data: { letters…

vue实现微博发布动态

vue实现微博发布动态

使用Vue实现微博发布动态功能 创建Vue组件结构 新建一个WeiboPost.vue组件,包含文本框、图片上传和发布按钮: <template> <div class="w…

vue 指令实现

vue 指令实现

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