lucky-canvas 活动大转盘,老虎机,支持pc以及h5,还支持微信小程序

活动大转盘,老虎机,支持pc以及h5,还支持微信小程序。我就不写教程和使用方式了,api里面写的很详细。

API的链接(点击这里)

lucky-canvas 链接在上面↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
基于 JS + Canvas 实现的【大转盘 & 九宫格 & 老虎机】抽奖,致力于为 WEB 前端提供一个功能强大且专业可靠的营销组件,只需要通过简单配置即可实现自由化定制。

可以说是开箱即用了,下面展示API中的部分代码。vue的事例(有很多API写的很详细,上面有API的链接)

<template>
  <LuckyWheel
    ref="myLucky"
    width="300px"
    height="300px"
    :prizes="prizes"
    :blocks="blocks"
    :buttons="buttons"
    @start="startCallback"
    @end="endCallback"
  />
</template>

<script>
export default {
  data () {
    return {
      blocks: [
        { padding: '10px', background: '#869cfa' }
      ],
      prizes: [
        { background: '#e9e8fe', fonts: [{ text: '0' }] },
        { background: '#b8c5f2', fonts: [{ text: '1' }] },
        { background: '#e9e8fe', fonts: [{ text: '2' }] },
        { background: '#b8c5f2', fonts: [{ text: '3' }] },
        { background: '#e9e8fe', fonts: [{ text: '4' }] },
        { background: '#b8c5f2', fonts: [{ text: '5' }] },
      ],
      buttons: [
        { radius: '40%', background: '#617df2' },
        { radius: '35%', background: '#afc8ff' },
        {
          radius: '30%', background: '#869cfa',
          pointer: true,
          fonts: [{ text: '开始', top: '-10px' }]
        }
      ]
    }
  },
  methods: {
    // 点击抽奖按钮会触发star回调
    startCallback () {
      // 调用抽奖组件的play方法开始游戏
      this.$refs.myLucky.play()
      // 模拟调用接口异步抽奖
      setTimeout(() => {
        // 假设后端返回的中奖索引是0
        const index = 0
        // 调用stop停止旋转并传递中奖索引
        this.$refs.myLucky.stop(index)
      }, 3000)
    },
    // 抽奖结束会触发end回调
    endCallback (prize) {
      console.log(prize)
    },
  }
}
</script>

如果帮助到你,希望留下一个点赞。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
1. 首先,需要在HTML中创建一个包含大转盘的容器元素,并在其中添加一个canvas元素。 ```html <div id="lucky-wheel"> <canvas id="wheel-canvas"></canvas> </div> ``` 2. 在CSS中设置容器元素的样式,如宽度、高度、背景颜色等。 ```css #lucky-wheel { width: 500px; height: 500px; background-color: #fff; border-radius: 50%; position: relative; margin: 0 auto; } ``` 3. 在JavaScript中编写抽奖逻辑代码。首先需要定义奖品数组,每个奖品包含名称、图片、颜色和概率等信息。 ```javascript const prizes = [ { name: '奖品1', image: 'image1.png', color: '#FFA07A', probability: 0.1 }, { name: '奖品2', image: 'image2.png', color: '#87CEFA', probability: 0.2 }, { name: '奖品3', image: 'image3.png', color: '#FFD700', probability: 0.3 }, { name: '奖品4', image: 'image4.png', color: '#00FA9A', probability: 0.4 } ]; ``` 4. 接着,需要定义转盘的样式和绘制函数。转盘的样式可以使用CSS中的渐变和阴影等效果来实现。 ```javascript const wheelStyle = { lineWidth: 2, strokeStyle: '#000', fillStyle: '#fff', shadowColor: '#666', shadowBlur: 4, gradient: { startColor: '#fff', endColor: '#ccc', startX: 0, startY: 0, endX: 0, endY: wheelRadius } }; function drawWheel() { ctx.save(); ctx.translate(wheelCenterX, wheelCenterY); ctx.rotate((Math.PI / 180) * wheelAngle); ctx.beginPath(); ctx.arc(0, 0, wheelRadius, 0, Math.PI * 2); ctx.lineWidth = wheelStyle.lineWidth; ctx.strokeStyle = wheelStyle.strokeStyle; ctx.fillStyle = wheelStyle.fillStyle; ctx.shadowColor = wheelStyle.shadowColor; ctx.shadowBlur = wheelStyle.shadowBlur; ctx.shadowOffsetX = 0; ctx.shadowOffsetY = 0; ctx.stroke(); ctx.fill(); ctx.restore(); drawWheelText(); } ``` 5. 然后,需要定义每个奖品的位置和角度,以及根据概率计算每个奖品在转盘上的占比。 ```javascript const prizeCount = prizes.length; const prizeAngle = 360 / prizeCount; let prizePositions = []; let prizeProbabilities = []; for (let i = 0; i < prizeCount; i++) { const angle = i * prizeAngle; const position = { x: wheelCenterX + Math.sin(degToRad(angle)) * (wheelRadius - wheelTextMargin), y: wheelCenterY - Math.cos(degToRad(angle)) * (wheelRadius - wheelTextMargin) }; prizePositions.push(position); prizeProbabilities.push(prizes[i].probability); } const totalProbability = prizeProbabilities.reduce((a, b) => a + b, 0); const prizeRatios = prizeProbabilities.map(p => p / totalProbability); ``` 6. 接下来,需要编写抽奖函数。该函数首先根据每个奖品的概率计算出随机的中奖结果,然后根据中奖结果的位置和角度旋转转盘,并触发抽奖结束的回调函数。 ```javascript function startLottery() { const random = Math.random(); let sum = 0; for (let i = 0; i < prizeCount; i++) { sum += prizeRatios[i]; if (random <= sum) { const prizeIndex = i; const prizeAngle = 360 - (prizeIndex * prizeAngle) - (prizeAngle / 2); const targetAngle = prizeAngle + 360 * 5; const animate = () => { if (wheelAngle < targetAngle) { wheelAngle += 5; drawWheel(); requestAnimationFrame(animate); } else { onLotteryEnd(prizes[prizeIndex]); } }; requestAnimationFrame(animate); break; } } } ``` 7. 最后,在初始化函数中调用绘制转盘和绑定抽奖按钮的事件处理函数,即可完成大转盘抽奖的实现。 ```javascript function init() { const canvas = document.getElementById('wheel-canvas'); canvas.width = wheelWidth; canvas.height = wheelHeight; ctx = canvas.getContext('2d'); drawWheel(); const button = document.getElementById('lottery-button'); button.addEventListener('click', startLottery); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Wesley-xl

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

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

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

打赏作者

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

抵扣说明:

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

余额充值