Spinner ?
Spinner ?
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Enhanced Spinner Wheel with Fireworks</title>
<style>
body {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
background-color: #f0f0f0;
font-family: Arial, sans-serif;
transition: background-color 0.5s ease;
position: relative;
overflow: hidden;
}
#wheel {
width: 300px;
height: 300px;
border-radius: 50%;
position: relative;
overflow: hidden;
border: 16px solid #f3f3f3; /* Light grey */
transition: transform 4s ease-out;
}
.segment {
position: absolute;
width: 50%;
height: 50%;
background-color: #3498db; /* Default color */
transform-origin: 100% 100%;
clip-path: polygon(0 0, 100% 0, 100% 100%);
display: flex;
align-items: center;
justify-content: flex-end;
color: white;
font-weight: bold;
padding-right: 10px; /* Add some padding */
transition: transform 0.5s ease; /* Animation for segments */
}
#spinButton, #resetButton {
margin-top: 20px;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
background-color: #3498db;
color: white;
border: none;
border-radius: 5px;
transition: background-color 0.3s;
}
#spinButton:hover, #resetButton:hover {
background-color: #2980b9;
}
#result {
margin-top: 20px;
font-size: 20px;
font-weight: bold;
}
#segmentForm {
margin-top: 20px;
display: flex;
flex-direction: column;
align-items: center;
}
#segmentForm input {
margin: 5px;
padding: 5px;
width: 200px;
}
.glow {
box-shadow: 0 0 20px gold;
}
canvas {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: none; /* Allow clicks to pass through */
display: none; /* Hide canvas initially */
}
</style>
</head>
<body>
<div id="segmentForm">
<input type="text" id="segmentLabel" placeholder="Segment Label" />
<input type="color" id="segmentColor" value="#3498db" />
<button onclick="addSegment()">Add Segment</button>
</div>
<canvas id="fireworksCanvas"></canvas>
<script>
const segments = [];
const wheel = document.getElementById('wheel');
const resultDisplay = document.getElementById('result');
const spinSound = document.getElementById('spinSound');
const winSound = document.getElementById('winSound');
const canvas = document.getElementById('fireworksCanvas');
const ctx = canvas.getContext('2d');
function addSegment() {
const label = document.getElementById('segmentLabel').value;
const color = document.getElementById('segmentColor').value;
function drawSegments() {
wheel.innerHTML = ''; // Clear existing segments
segments.forEach((segment, index) => {
const segmentDiv = document.createElement('div');
segmentDiv.className = 'segment';
segmentDiv.style.backgroundColor = segment.color;
segmentDiv.style.transform = `rotate(${index * (360 /
segments.length)}deg)`;
segmentDiv.innerText = segment.label;
segmentDiv.classList.add('animate'); // Add animation class
wheel.appendChild(segmentDiv);
});
}
function spin() {
if (isSpinning) return; // Prevent multiple spins
isSpinning = true;
setTimeout(() => {
isSpinning = false; // Re-enable the button after spinning
const actualDegree = randomDegree % 360; // Get the final degree
const winningSegmentIndex = Math.floor((actualDegree / 360) *
segments.length);
const winningSegment = segments[winningSegmentIndex];
resultDisplay.innerText = `You won: ${winningSegment.label}`;
winSound.play(); // Play win sound
highlightWinningSegment(winningSegmentIndex);
showFireworks(); // Trigger fireworks effect
}, 4000); // Match timeout with the transition duration
}
function highlightWinningSegment(index) {
const segmentDivs = document.querySelectorAll('.segment');
segmentDivs.forEach((div, i) => {
div.classList.remove('glow'); // Remove glow from all segments
if (i === index) {
div.classList.add('glow'); // Add glow effect to winning segment
}
});
}
function resetWheel() {
segments.length = 0; // Clear segments
drawSegments(); // Redraw the wheel
resultDisplay.innerText = ''; // Clear result display
document.body.style.backgroundColor = '#f0f0f0'; // Reset background color
hideFireworks(); // Hide fireworks canvas
}
function getRandomColor() {
const letters = '0123456789ABCDEF';
let color = '#';
for (let i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
function showFireworks() {
canvas.style.display = 'block'; // Show canvas
ctx.clearRect(0, 0, canvas.width, canvas.height); // Clear previous
drawings
for (let i = 0; i < 100; i++) {
createFirework();
}
setTimeout(hideFireworks, 3000); // Hide after 3 seconds
}
function hideFireworks() {
canvas.style.display = 'none'; // Hide canvas
}
function createFirework() {
const x = Math.random() * canvas.width;
const y = Math.random() * canvas.height;
const color = getRandomColor();
const radius = Math.random() * 3 + 2;
ctx.beginPath();
ctx.arc(x, y, radius, 0, Math.PI * 2);
ctx.fillStyle = color;
ctx.fill();
ctx.closePath();
}
</script>
</body>
</html>