纯js实现一个简版大转盘抽奖

最近项目加了个大转盘和刮一刮抽奖的需求,趁着最近不是很忙,研究了一下大转盘抽奖的实现。
下面直接上代码:

<!DOCTYPE html>
<html>
<title>大转盘</title>

<head></head>
<style>
  .pen {
    width: 500px;
    height: 500px;
    border-radius: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
  }

  .point {
    width: 20px;
    height: 20px;
    background: #000;
    border-radius: 50%;
    position: absolute;
    z-index: 4;
  }
  .line {
    height: 100px;
    position: absolute;
    width: 10px;
    background: red;
    z-index: 5;
    bottom: 250px;
}

  .box {
    width: 500px;
    height: 500px;
    border-radius: 100%;
    background: #ddd;
    position: absolute;
    z-index: 3;
  }

  #canvasBox {
    position: relative;
  }

  .rotateClass {
    animation: rotate 10s ease-in-out 0s 1 normal forwards;
  }

  #canvasBox canvas {
    position: absolute;

  }
</style>

<body>
  <div class="pen">
    <div class="box" id='canvasBox'></div>
    <div class="point"></div>
    <div class="line"> </div>
  </div>
  <script>
    let imgList=['img1','img2','']
    let n = 10 //转盘分格数
    // n 转盘分为n块
    function init(n) {
      // 画n个圆弧
      for (let i = 0; i < n; i++) {
        let canvas = document.createElement('canvas')
        canvas.setAttribute('id', 'parentCanvas' + i)
        canvas.width = 500
        canvas.height = 500
        canvas.style.borderRadius = '50%'
        canvas.style.transform = `rotateZ(${360*i/n}deg)`
        let canvasBox = document.querySelector('#canvasBox')
        canvasBox.appendChild(canvas)
        let ctx = canvas.getContext('2d')
        ctx.translate(250, 250)
        ctx.beginPath()
        ctx.moveTo(0, 0)
        // 开始
        let startDeg=270-360/n/2
        // 结束
        let endDeg=270+360/n/2
        ctx.arc(0, 0, 250,startDeg/360 * 2 * Math.PI, endDeg / 360 * 2 * Math.PI, false)
        ctx.lineTo(0, 0)
        ctx.strokeStyle = '#999'
        ctx.stroke();
        if(imgList[i%imgList.length]){
          let img=document.createElement('img')
          img.src=imgList[i%imgList.length]
          img.onload = function(){
            ctx.drawImage(img,-30,-185,70,70)
          }
          
          ctx.font = "30px Verdana";
          ctx.textAlign = 'center'
          ctx.strokeText('canvas' + i, 0, -225)
        }else{
          ctx.font = "30px Verdana";
          ctx.textAlign = 'center'
          ctx.strokeText('谢谢参与' , 0, -125)
        }
      }
    }
    // 旋转
    // n 转盘跨数 count:中奖的次序
    function rotate(n,count = 2) {
      // 每个圆弧的弧度
      let deg = 360/n
      let animation = `@keyframes rotate {
        form{
          transform: rotateZ(0deg);
        }
        to{
          transform: rotateZ(${7200-deg*count}deg);
        }
      }`
      var sheet = document.styleSheets[0]
      sheet.insertRule(animation, 0)
      canvasBox.classList.add('rotateClass')
    }
    init(n)
    rotate(n,8)
  </script>
</body>

</html>

其实上面的代码实现还有很大的改进的空间(暂时还没有时间改造),有什么不对的地方望各位大佬,也期待更多大佬分享自己的实现方法和思路。

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个使用JS实现抽奖转盘: HTML代码: ```html <div id="wheel"></div> <button onclick="start()">开始抽奖</button> ``` CSS代码: ```css #wheel { width: 200px; height: 200px; border-radius: 50%; background-color: #ff9933; position: relative; overflow: hidden; margin: 30px auto; } #wheel:before { content: ''; display: block; width: 0; height: 0; border-style: solid; border-width: 0 100px 173.2px 100px; border-color: transparent transparent #ffcc33 transparent; position: absolute; top: 50px; left: 50px; transform-origin: bottom center; transform: rotate(0deg); transition: transform 6s ease-in-out; } #wheel:after { content: ''; display: block; background-color: #fff; width: 80px; height: 80px; border-radius: 50%; position: absolute; top: 60px; left: 60px; z-index: 1; } button { display: block; margin: 0 auto; padding: 10px 20px; font-size: 20px; font-weight: bold; color: #fff; background-color: #ff9933; border: none; border-radius: 5px; cursor: pointer; transition: background-color 0.3s ease-in-out; } button:hover { background-color: #ffcc33; } ``` JS代码: ```javascript var deg = 0; var speed = 0; var prize = ''; function start() { // 生成一个随机速度 speed = Math.floor(Math.random() * 20) + 10; // 生成一个随机奖品 prize = Math.floor(Math.random() * 6) + 1; // 计算需要旋转的角度 deg = 360 / 6 * (prize - 1) + 360 / 6 / 2 - Math.random() * 360 / 6; // 开始旋转转盘 var wheel = document.querySelector('#wheel:before'); wheel.style.transform = 'rotate(' + deg + 'deg)'; wheel.style.transition = 'transform 6s ease-in-out'; // 6秒后停止旋转 setTimeout(function() { stop(); }, 6000); } function stop() { // 停止旋转 var wheel = document.querySelector('#wheel:before'); wheel.style.transform = 'rotate(' + deg + 'deg)'; wheel.style.transition = 'transform 0s'; // 显示中奖结果 alert('恭喜您获得:奖品' + prize); } ``` 这个抽奖转盘使用了CSS3的旋转效果来实现转盘的转动,使用JS生成随机速度和奖品,并计算需要旋转的角度。在点击开始抽奖按钮后,将旋转指针旋转到指定角度,并在6秒后停止旋转,并弹出中奖结果提示框。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值