当前位置:首页 > JavaScript

js实现抽奖转盘效果

2026-04-04 11:06:58JavaScript

js实现抽奖转盘效果

js实现抽奖转盘效果

实现抽奖转盘效果

抽奖转盘通常由旋转动画和结果判定两部分组成。以下是一个基于Canvas和JavaScript的实现方案:

初始化Canvas和转盘绘制

const canvas = document.getElementById('wheel');
const ctx = canvas.getContext('2d');
const segments = ['一等奖', '二等奖', '三等奖', '谢谢参与', '再来一次'];
const colors = ['#FF6384', '#36A2EB', '#FFCE56', '#4BC0C0', '#9966FF'];
const center = { x: canvas.width/2, y: canvas.height/2 };
const radius = Math.min(canvas.width, canvas.height)/2 - 10;

function drawWheel() {
    const arcAngle = (2 * Math.PI) / segments.length;

    segments.forEach((segment, index) => {
        ctx.beginPath();
        ctx.fillStyle = colors[index];
        ctx.moveTo(center.x, center.y);
        ctx.arc(center.x, center.y, radius, 
                index * arcAngle, (index + 1) * arcAngle);
        ctx.lineTo(center.x, center.y);
        ctx.fill();

        // 绘制文本
        ctx.save();
        ctx.translate(center.x, center.y);
        ctx.rotate(index * arcAngle + arcAngle/2);
        ctx.textAlign = 'right';
        ctx.fillStyle = '#fff';
        ctx.font = '16px Arial';
        ctx.fillText(segment, radius - 10, 5);
        ctx.restore();
    });
}

实现旋转动画

let currentRotation = 0;
let isSpinning = false;

function spin() {
    if(isSpinning) return;

    isSpinning = true;
    const spinTime = 3000; // 旋转总时间
    const startTime = Date.now();
    const targetRotation = currentRotation + 10 + Math.random() * 20;

    function animate() {
        const elapsed = Date.now() - startTime;
        const progress = Math.min(elapsed / spinTime, 1);
        const easeOut = 1 - Math.pow(1 - progress, 3);
        currentRotation = easeOut * targetRotation;

        ctx.clearRect(0, 0, canvas.width, canvas.height);
        ctx.save();
        ctx.translate(center.x, center.y);
        ctx.rotate(currentRotation);
        ctx.translate(-center.x, -center.y);
        drawWheel();
        ctx.restore();

        if(progress < 1) {
            requestAnimationFrame(animate);
        } else {
            isSpinning = false;
            determineResult();
        }
    }

    requestAnimationFrame(animate);
}

function determineResult() {
    const normalizedRotation = currentRotation % (2 * Math.PI);
    const segmentAngle = (2 * Math.PI) / segments.length;
    const winningIndex = Math.floor(
        (2 * Math.PI - normalizedRotation) / segmentAngle
    ) % segments.length;

    alert(`恭喜获得: ${segments[winningIndex]}`);
}

HTML结构

<canvas id="wheel" width="400" height="400"></canvas>
<button onclick="spin()">开始抽奖</button>

CSS样式

#wheel {
    display: block;
    margin: 0 auto;
    background: #f5f5f5;
    border-radius: 50%;
    box-shadow: 0 0 10px rgba(0,0,0,0.2);
}

button {
    display: block;
    margin: 20px auto;
    padding: 10px 20px;
    font-size: 16px;
    background: #4CAF50;
    color: white;
    border: none;
    border-radius: 5px;
    cursor: pointer;
}

完整实现步骤

  1. 初始化Canvas并设置尺寸
  2. 定义奖项和对应颜色
  3. 绘制转盘扇形区域和文字
  4. 实现旋转动画使用requestAnimationFrame
  5. 添加缓动函数使旋转更自然
  6. 计算最终停止位置确定获奖结果
  7. 添加开始按钮触发旋转

优化建议

  1. 添加指针指示器
  2. 实现多次旋转惯性效果
  3. 增加音效增强体验
  4. 使用图片替代纯色扇形
  5. 添加开始/停止动画的过渡效果

这个实现使用了Canvas绘制和JavaScript动画基础,可以根据需要调整转盘大小、奖项数量和旋转时间等参数。

标签: 转盘效果
分享给朋友:

相关文章

vue实现转盘抽奖

vue实现转盘抽奖

Vue 实现转盘抽奖 基本思路 转盘抽奖的核心是通过 CSS 动画实现旋转效果,Vue 负责控制旋转逻辑和数据绑定。关键点包括转盘扇形区域的绘制、旋转动画的触发与停止、中奖结果的判定。 实现步骤 H…

vue实现对话框效果

vue实现对话框效果

Vue 对话框实现方法 使用组件化方式封装对话框 创建一个独立的对话框组件(如Dialog.vue),通过v-model控制显示隐藏: <template> <div clas…

vue实现翻页效果

vue实现翻页效果

使用Vue实现翻页效果 在Vue中实现翻页效果通常需要结合分页组件和数据处理逻辑。以下是几种常见方法: 基础分页组件实现 <template> <div>…

vue实现效果展示

vue实现效果展示

Vue 实现效果展示的方法 Vue.js 提供了多种方式来实现动态效果展示,以下是一些常见的方法: 使用 Vue 的过渡和动画系统 Vue 内置了 <transition> 和 <…

vue实现突出效果

vue实现突出效果

Vue 实现突出效果的方法 在 Vue 中实现突出效果通常涉及动态样式绑定、动画过渡或第三方库的使用。以下是几种常见实现方式: 动态类名绑定 通过 v-bind:class 或简写 :class 实…

vue实现滑动效果

vue实现滑动效果

Vue 实现滑动效果的方法 在 Vue 中实现滑动效果可以通过多种方式,包括使用 CSS 过渡、第三方动画库或直接操作 DOM。以下是几种常见的方法: 使用 CSS 过渡和 Vue 的 <tr…