How to Build a Prize Wheel App: A Step-by-Step Tutorial
Prize wheels add fun and engagement to any app, from giveaways to gamified learning. In this tutorial, you’ll build a fully functional prize wheel using plain HTML, CSS, and JavaScript — no libraries required.
1. Set Up the HTML Structure
Create a container with a canvas element for the wheel and a button to trigger the spin. Also add a placeholder to display the winning prize.
- Use
<canvas id="wheelCanvas">to draw the wheel. - Add a
<button id="spinBtn">Spin!</button>. - Include a
<p id="result"></p>to show the prize.
2. Style the Wheel with CSS
Position the canvas and button, and use CSS to add a pointer (triangle) above the wheel. Make the button visually appealing with a gradient and hover effect.
- Center the wheel using
text-align: center. - Style the button with padding, border-radius, and background color.
- Add a fixed pointer using
::beforeon the container.
3. Implement the Spin Logic in JavaScript
Define an array of prizes (e.g., [“🎁 Gift”, “$10 Cash”, “Try Again”]). Draw sectors on the canvas using arc() and fillText(). On button click, calculate a random angle between 0 and 360 degrees, then animate the rotation using requestAnimationFrame or CSS transition.
- Store the current rotation angle and apply a random offset on each spin.
- Use
Math.random() * 360 + 720to ensure multiple full spins. - After rotation stops, determine which sector the pointer points to and display the prize.
4. Reset and Customize
After the spin, reset the button state and optionally allow a cooldown to prevent rapid spins. You can easily change sector colors, prizes, or add sound effects by extending the code.
With these steps, you’ve built a fully interactive prize wheel app. Experiment with different designs and prizes to make it your own. Happy spinning!