lucky-canvas抽奖插件-大转盘-九宫格-老虎机

  地址 https://100px.net/usage/uni-app.html

  在vue中使用

npm 安装:npm install @lucky-canvas/vue@latest

 在mian.js中引入:

// 完整加载
import VueLuckyCanvas from '@lucky-canvas/vue'
Vue.use(VueLuckyCanvas)

下面是一个基本的 demo 更多抽奖类型或复杂的效果可以跳转示例页面查看

<template>
  <div>
    <LuckyWheel
    ref="myLucky"
    width="300px"
    height="300px"
    :prizes="prizes"
    :blocks="blocks"
    :buttons="buttons"
    @start="startCallback"
    @end="endCallback"
  />
  </div>
  
</template>
<script>
export default {
  name: "index",
  data() {
    return {
      //背景区域
      blocks: [{ padding: "13px", background: "#617df2" }],
      //prizes奖品列表   top文字的位置  count奖品数量
      prizes: [
        { fonts: [{ text: "手机", top: "10%" }],background: "#e9e8fe"},
        { fonts: [{ text: "1", top: "10%" }], background: "#b8c5f2" },
        { fonts: [{ text: "2", top: "10%" }], background: "#e9e8fe" },
        { fonts: [{ text: "3", top: "10%" }], background: "#b8c5f2" },
        { fonts: [{ text: "4", top: "10%" }], background: "#e9e8fe" },
        { fonts: [{ text: "5", top: "10%" }], background: "#b8c5f2" },
        { fonts: [{ text: "6", top: "10%" }], background: "#e9e8fe" },
        { fonts: [{ text: "7", top: "10%" }], background: "#b8c5f2" }
      ],
      //抽奖按钮
      buttons: [
        { radius: "50px", background: "#617df2" },
        { radius: "45px", background: "#afc8ff" },
        {
          radius: "40px",
          background: "#869cfa",
          pointer: true, //是否显示指针
          fonts: [{ text: "开始\n抽奖", top: "-20px" }] //奖品文字
        }
      ]
    };
  },
  methods: {
    // 点击抽奖按钮会触发star回调,star回调开始抽奖前
    startCallback() {
      // 调用抽奖组件的play方法开始游戏
      this.$refs.myLucky.play();
      // 模拟调用接口异步抽奖
      setTimeout(() => {
        // 返回的是奖品列表,index从0开始
        const index = Math.floor(Math.random() * 9); //Math.floor(Math.random() * 9)返回一个0-8之间的随机整数
        console.log(index, "随机数");
        // 调用stop停止旋转并传递中奖索引
        this.$refs.myLucky.stop(index);
      }, 3000);
    },
    // 抽奖结束会触发end回调,end回调结束抽奖后
    endCallback(prize) {
      console.log(prize);
    }
  }
};
</script>

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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); } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值