<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Customizable Lottery Ball Machine</title>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
margin: 0;
padding: 0;
background: #121214;
color: #e0e0e0;
display: flex;
flex-direction: column;
min-height: 100vh;
overflow-x: hidden;
}
header {
background-color: #1f1f23;
padding: 1rem 2rem;
text-align: center;
font-size: 1.8rem;
font-weight: bold;
letter-spacing: 2px;
user-select: none;
}
main {
flex: 1;
display: flex;
flex-direction: column;
align-items: center;
padding: 1rem 1rem 3rem;
max-width: 1000px;
margin: 0 auto;
}
form {
background: #2c2c35;
border-radius: 12px;
padding: 1.5rem 2rem;
width: 100%;
max-width: 480px;
box-shadow: 0 0 12px rgba(255, 255, 255, 0.07);
margin-bottom: 2rem;
}
form label {
display: block;
margin-bottom: 0.3rem;
font-weight: 600;
}
form input[type="number"],
form input[type="text"],
form input[type="file"],
form select {
width: 100%;
padding: 0.5rem;
border-radius: 6px;
border: none;
font-size: 1rem;
margin-bottom: 1.25rem;
background: #1f1f23;
color: #e0e0e0;
}
form input[type="file"] {
padding: 0.3rem 0.5rem;
}
.flex-row {
display: flex;
gap: 1rem;
flex-wrap: wrap;
justify-content: space-between;
}
.flex-half {
flex: 1 1 45%;
min-width: 180px;
}
button {
display: block;
width: 100%;
padding: 0.75rem;
background-color: #2e86de;
border: none;
border-radius: 10px;
font-size: 1.2rem;
font-weight: 700;
color: white;
cursor: pointer;
transition: background-color 0.3s ease;
user-select: none;
}
button:disabled {
background-color: #555;
cursor: not-allowed;
}
button:hover:not(:disabled) {
background-color: #1b61b7;
}
/* Lottery Machine Container */
#machine-container {
position: relative;
width: 320px;
height: 380px;
background: linear-gradient(145deg, #23232a, #1b1b20);
border-radius: 30px 30px 16px 16px;
box-shadow:
inset 7px 7px 15px #1a1a1f,
inset -7px -7px 15px #2f2f39;
overflow: visible;
margin-bottom: 2rem;
}
/* The spinning drum */
#drum {
position: absolute;
top: 40px;
left: 50%;
transform: translateX(-50%);
width: 260px;
height: 260px;
border-radius: 50%;
background: radial-gradient(circle at center, #414359 20%, #21212a 90%);
box-shadow: 8px 8px 20px #1a1a1f,
-8px -8px 20px #2f2f39,
inset 0 0 10px #484858;
overflow: visible;
user-select: none;
}
/* The balls container inside the drum */
#balls-container {
position: relative;
width: 260px;
height: 260px;
border-radius: 50%;
overflow: visible;
user-select: none;
}
/* Single ball */
.ball {
position: absolute;
width: 40px;
height: 40px;
border-radius: 50%;
background: #e63946;
background-size: cover;
background-position: center;
box-shadow:
0 3px 6px rgba(0,0,0,0.3),
inset 0 3px 6px rgba(255,255,255,0.3);
display: flex;
justify-content: center;
align-items: center;
font-weight: bold;
font-size: 1.2rem;
color: #fff;
user-select: none;
cursor: default;
border: 2px solid #a72631;
}
/* The rolling track */
#track {
position: relative;
width: 320px;
height: 80px;
background: linear-gradient(90deg, #252527 0%, #1d1d1f 100%);
border-radius: 20px;
box-shadow:
inset 0 4px 8px #3b3b3f;
overflow: visible;
user-select: none;
}
/* The rolling ball that outputs */
#output-ball {
position: absolute;
top: 20px;
width: 40px;
height: 40px;
border-radius: 50%;
background: #e63946;
background-size: cover;
background-position: center;
box-shadow:
0 3px 6px rgba(0,0,0,0.3),
inset 0 3px 6px rgba(255,255,255,0.3);
display: flex;
justify-content: center;
align-items: center;
font-weight: bold;
font-size: 1.5rem;
color: #fff;
border: 2px solid #a72631;
user-select: none;
pointer-events: none;
}
/* Prompt and upload instructions styling */
#customize-info {
font-size: 0.9rem;
color: #b5b5b5;
margin-top: -1rem;
margin-bottom: 0.8rem;
font-style: italic;
user-select: none;
}
/* Hide file input default label */
input[type="file"]::file-selector-button {
background-color: #2e86de;
color: white;
border: none;
padding: 6px 12px;
border-radius: 6px;
cursor: pointer;
transition: background-color 0.3s ease;
user-select: none;
}
input[type="file"]::file-selector-button:hover {
background-color: #1b61b7;
}
/* Responsive */
@media (max-width: 600px) {
#machine-container {
width: 260px;
height: 320px;
}
#drum {
width: 210px;
height: 210px;
}
#balls-container {
width: 210px;
height: 210px;
}
.ball, #output-ball {
width: 32px;
height: 32px;
font-size: 1rem;
border-width: 1.5px;
}
#track {
width: 260px;
height: 60px;
}
#output-ball {
top: 15px;
}
form {
max-width: 100%;
}
}
</style>
</head>
<body>
<header>Customizable Lottery Ball Machine</header>
<main>
<form id="control-form" autocomplete="off" novalidate>
<div class="flex-row">
<div class="flex-half">
<label for="num-balls">Number of Balls in Machine (1 - 50):</label>
<input
type="number"
id="num-balls"
name="numBalls"
min="1"
max="50"
value="10"
required
/>
</div>
<div class="flex-half">
<label for="spin-length">Spin Length (ms, 1 - 1,000,000,000):</label>
<input
type="number"
id="spin-length"
name="spinLength"
min="1"
max="1000000000"
value="5000"
required
/>
</div>
</div>
<div class="flex-row">
<div class="flex-half">
<label for="spin-speed">Spin Speed (rotations per second, 1 - 1,000,000,000):</label>
<input
type="number"
id="spin-speed"
name="spinSpeed"
min="1"
max="1000000000"
value="5"
required
/>
</div>
<div class="flex-half">
<label for="ball-prompt">Customize Ball by Prompt (Text):</label>
<input
type="text"
id="ball-prompt"
name="ballPrompt"
placeholder="Example: 'Star, blue background'"
maxlength="50"
/>
</div>
</div>
<label for="ball-image-upload">Or Upload an Image to Customize Ball:</label>
<input
type="file"
id="ball-image-upload"
name="ballImageUpload"
accept="image/*"
/>
<div id="customize-info">If both prompt and image are provided, the uploaded image will be used.</div>
<button type="submit" id="spin-btn">Spin</button>
</form>
<div id="machine-container" aria-label="Lottery Ball Machine">
<div id="drum" aria-live="polite" aria-atomic="true" aria-relevant="additions removals">
<div id="balls-container"></div>
</div>
</div>
<div id="track" aria-label="Ball output track">
<div id="output-ball" aria-live="polite" aria-atomic="true" aria-relevant="text"></div>
</div>
</main>
<script>
(() => {
const form = document.getElementById('control-form');
const numBallsInput = document.getElementById('num-balls');
const spinLengthInput = document.getElementById('spin-length');
const spinSpeedInput = document.getElementById('spin-speed');
const ballPromptInput = document.getElementById('ball-prompt');
const ballImageInput = document.getElementById('ball-image-upload');
const ballsContainer = document.getElementById('balls-container');
const drum = document.getElementById('drum');
const track = document.getElementById('track');
const outputBall = document.getElementById('output-ball');
const spinBtn = document.getElementById('spin-btn');
// Constants for layout
const drumRadius = 130; // half of 260px drum container width/height
const ballDiameter = 40;
const ballRadius = ballDiameter / 2;
// State
let spinning = false;
let balls = [];
let animationFrameId = null;
// Utility: create ball element with number and customization
function createBall(number, customization) {
const ballEl = document.createElement('div');
ballEl.classList.add('ball');
ballEl.textContent = number;
// Apply customization
if (customization.image) {
ballEl.style.backgroundImage = `url(${customization.image})`;
ballEl.textContent = '';
ballEl.style.backgroundColor = 'transparent';
ballEl.style.borderColor = '#fff';
ballEl.style.boxShadow = '0 0 10px #fff';
} else if (customization.prompt) {
// For prompt, simulate some styling based on keywords (demo)
// e.g. check for colors or shapes in prompt
const prompt = customization.prompt.toLowerCase();
if (prompt.includes('blue')) {
ballEl.style.backgroundColor = '#1e3a8a';
ballEl.style.borderColor = '#154290';
ballEl.style.boxShadow = 'inset 0 0 10px #3b82f6';
} else if (prompt.includes('green')) {
ballEl.style.backgroundColor = '#166534';
ballEl.style.borderColor = '#14532d';
ballEl.style.boxShadow = 'inset 0 0 10px #4ade80';
} else if (prompt.includes('star')) {
ballEl.style.backgroundColor = '#f9a825'; // gold-ish
ballEl.style.borderColor = '#c17900';
ballEl.style.boxShadow = '0 0 6px #facc15, inset 0 0 6px #f59e0b';
ballEl.innerHTML = '★';
ballEl.style.fontSize = '1.6rem';
ballEl.style.color = '#fff';
} else if (prompt.includes('red')) {
ballEl.style.backgroundColor = '#b91c1c';
ballEl.style.borderColor = '#7f1d1d';
ballEl.style.boxShadow = 'inset 0 0 10px #ef4444';
} else {
// Default prompt style
ballEl.style.backgroundColor = '#e63946';
ballEl.style.borderColor = '#a72631';
ballEl.style.boxShadow =
'0 3px 6px rgba(0,0,0,0.3), inset 0 3px 6px rgba(255,255,255,0.3)';
}
} else {
// Default ball style (red)
ballEl.style.backgroundColor = '#e63946';
ballEl.style.borderColor = '#a72631';
ballEl.style.boxShadow =
'0 3px 6px rgba(0,0,0,0.3), inset 0 3px 6px rgba(255,255,255,0.3)';
}
return ballEl;
}
// Arrange balls around the drum circle
function arrangeBalls(numBalls, customization) {
ballsContainer.innerHTML = '';
balls = [];
const angleStep = (2 * Math.PI) / numBalls;
for (let i = 0; i < numBalls; i++) {
// Number on ball: 1 to numBalls inclusive
const ballNumber = i + 1;
// Create ball with customization
let customData = { prompt: null, image: null };
if (customization.image) {
customData.image = customization.image;
} else if (customization.prompt) {
customData.prompt = customization.prompt;
}
const ballEl = createBall(ballNumber, customData);
ballsContainer.appendChild(ballEl);
// Position ball on circle
// Calculate center point:
// Drum is 260x260 -> center at (130,130)
// We shift by half ball size for centering ball element center
// x = centerX + radius * cos(angle) - ballRadius
// y = centerY + radius * sin(angle) - ballRadius
const angle = i * angleStep - Math.PI / 2; // start top vertically
const cx = drumRadius + drumRadius * 0.75 * Math.cos(angle) - ballRadius;
const cy = drumRadius + drumRadius * 0.75 * Math.sin(angle) - ballRadius;
ballEl.style.left = `${cx}px`;
ballEl.style.top = `${cy}px`;
balls.push({
element: ballEl,
angle,
number: ballNumber,
});
}
}
// Spin animation using rotation of drum, then output winning ball rolling on track
async function spinMachine({ numBalls, spinLength, spinSpeed, customization }) {
if (spinning) return; // prevent re-spin
spinning = true;
spinBtn.disabled = true;
outputBall.style.transition = 'none';
outputBall.style.left = '-60px'; // hide outside track left side
outputBall.style.backgroundImage = '';
outputBall.textContent = '';
outputBall.style.backgroundColor = '#0000';
// Arrange balls first
arrangeBalls(numBalls, customization);
// Animate spinning
// spinSpeed = rotations per second, spinLength = ms
// Convert spin length to seconds
const spinDurationSec = spinLength / 1000;
// We'll use requestAnimationFrame for smooth rotation
let startTime = null;
// We rotate the #balls-container inside drum
ballsContainer.style.transition = 'none';
function animateSpin(timestamp) {
if (!startTime) startTime = timestamp;
const elapsed = (timestamp - startTime) / 1000; // in seconds
if (elapsed < spinDurationSec) {
// rotation in degrees: rotations per second * elapsed * 360
// Add some easing (slow down at end)
const easeOutFactor = 1 - Math.pow(elapsed / spinDurationSec, 3);
const rotations = spinSpeed * elapsed * easeOutFactor;
const deg = rotations * 360;
ballsContainer.style.transform = `rotate(${deg}deg)`;
animationFrameId = requestAnimationFrame(animateSpin);
} else {
// End spin
// Final rotation angle:
const totalRotations = spinSpeed * spinDurationSec;
const finalRotationDeg = totalRotations * 360 * 0.05; // slow gentle last offset for natural look
ballsContainer.style.transform = `rotate(${finalRotationDeg}deg)`;
cancelAnimationFrame(animationFrameId);
// Choose winning ball randomly (uniformly)
const chosenIndex = Math.floor(Math.random() * numBalls);
const winningBall = balls[chosenIndex];
// Highlight winning ball's actual angle on static drum
// We rotate drum so winning ball is at certain fixed angle (top center)
// Compute current angle of ballsContainer rotation first
// We can calculate offset rotation to bring winning ball top center (angle = -90deg)
const ballsContainerStyle = window.getComputedStyle(ballsContainer);
const matrix = ballsContainerStyle.transform;
let currentRotation = 0;
if (matrix && matrix !== 'none') {
const values = matrix.match(/matrix\((.+)\)/);
if (values && values[1]) {
const parts = values[1].split(', ');
const a = parseFloat(parts[0]);
const b = parseFloat(parts[1]);
currentRotation = Math.atan2(b, a) * (180 / Math.PI);
}
}
// Angle we want the winning ball to rotate to = -90deg (top)
// Current winning ball angle + current container rotation + offset = -90deg
// offset = -90deg - (winningBall.angle * 180/pi + currentRotation)
const winningBallAngleDeg = (winningBall.angle * 180) / Math.PI;
let offsetDeg = -90 - (winningBallAngleDeg + currentRotation);
// Animate drum to offsetDeg to position winning ball at top center
ballsContainer.style.transition = 'transform 2s ease-in-out';
ballsContainer.style.transform = `rotate(${currentRotation + offsetDeg}deg)`;
ballsContainer.addEventListener(
'transitionend',
() => {
// Now the winning ball is at top center (fixed)
// Show output ball rolling on the track
// Setup output ball style using winning ball customization
// Clear previous
outputBall.style.transition = 'none';
outputBall.style.left = '-60px'; // outside left track
if (customization.image) {
outputBall.style.backgroundImage = `url(${customization.image})`;
outputBall.textContent = '';
outputBall.style.backgroundColor = 'transparent';
outputBall.style.borderColor = '#fff';
outputBall.style.boxShadow = '0 0 10px #fff';
} else if (customization.prompt) {
const prompt = customization.prompt.toLowerCase();
if (prompt.includes('blue')) {
outputBall.style.backgroundColor = '#1e3a8a';
outputBall.style.borderColor = '#154290';
outputBall.style.boxShadow = 'inset 0 0 10px #3b82f6';
outputBall.textContent = winningBall.number;
outputBall.style.color = '#fff';
} else if (prompt.includes('green')) {
outputBall.style.backgroundColor = '#166534';
outputBall.style.borderColor = '#14532d';
outputBall.style.boxShadow = 'inset 0 0 10px #4ade80';
outputBall.textContent = winningBall.number;
outputBall.style.color = '#fff';
} else if (prompt.includes('star')) {
outputBall.style.backgroundColor = '#f9a825';
outputBall.style.borderColor = '#c17900';
outputBall.style.boxShadow = '0 0 6px #facc15, inset 0 0 6px #f59e0b';
outputBall.innerHTML = '★';
outputBall.style.fontSize = '1.6rem';
outputBall.style.color = '#fff';
} else if (prompt.includes('red')) {
outputBall.style.backgroundColor = '#b91c1c';
outputBall.style.borderColor = '#7f1d1d';
outputBall.style.boxShadow = 'inset 0 0 10px #ef4444';
outputBall.textContent = winningBall.number;
outputBall.style.color = '#fff';
} else {
outputBall.style.backgroundColor = '#e63946';
outputBall.style.borderColor = '#a72631';
outputBall.style.boxShadow =
'0 3px 6px rgba(0,0,0,0.3), inset 0 3px 6px rgba(255,255,255,0.3)';
outputBall.textContent = winningBall.number;
outputBall.style.color = '#fff';
}
outputBall.style.backgroundImage = '';
} else {
// Default style
outputBall.style.backgroundColor = '#e63946';
outputBall.style.borderColor = '#a72631';
outputBall.style.boxShadow =
'0 3px 6px rgba(0,0,0,0.3), inset 0 3px 6px rgba(255,255,255,0.3)';
outputBall.textContent = winningBall.number;
outputBall.style.color = '#fff';
outputBall.style.backgroundImage = '';
}
// Animate rolling ball from left off screen to right end of track
outputBall.style.transition = 'left 3s cubic-bezier(0.5, 0, 0.5, 1)';
const trackWidth = track.clientWidth;
outputBall.style.left = `${trackWidth - ballDiameter - 10}px`; // 10px padding from right edge
outputBall.addEventListener('transitionend', function onRollEnd() {
outputBall.removeEventListener('transitionend', onRollEnd);
// End of spin process
spinning = false;
spinBtn.disabled = false;
});
},
{ once: true }
);
}
}
animationFrameId = requestAnimationFrame(animateSpin);
}
// Handle file input and convert image to data URL
function loadImageAsDataURL(file) {
return new Promise((resolve, reject) => {
if (!file) resolve(null);
const reader = new FileReader();
reader.onload = () => {
resolve(reader.result);
};
reader.onerror = () => {
reject(new Error('Failed to load image'));
};
reader.readAsDataURL(file);
});
}
form.addEventListener('submit', async (event) => {
event.preventDefault();
// Validate inputs
const numBalls = parseInt(numBallsInput.value, 10);
let spinLength = parseInt(spinLengthInput.value, 10);
let spinSpeed = parseInt(spinSpeedInput.value, 10);
const ballPrompt = ballPromptInput.value.trim();
const ballImageFile = ballImageInput.files[0] || null;
// Clamp values
if (Number.isNaN(numBalls) || numBalls < 1) {
alert('Number of balls must be between 1 and 50.');
numBallsInput.focus();
return;
}
if (numBalls > 50) {
alert('Maximum of 50 balls allowed.');
numBallsInput.focus();
return;
}
if (Number.isNaN(spinLength) || spinLength < 1) {
spinLength = 5000;
}
if (spinLength > 1000000000) spinLength = 1000000000;
if (Number.isNaN(spinSpeed) || spinSpeed < 1) {
spinSpeed = 5;
}
if (spinSpeed > 1000000000) spinSpeed = 1000000000;
// Load image if provided
let ballImageData = null;
if (ballImageFile) {
try {
ballImageData = await loadImageAsDataURL(ballImageFile);
} catch (e) {
alert('Failed to load the uploaded image. Please try another file.');
return;
}
}
const customization = {
prompt: ballImageData ? null : ballPrompt || null,
image: ballImageData,
};
spinMachine({
numBalls,
spinLength,
spinSpeed,
customization,
});
});
// Initialize default display
arrangeBalls(10, { prompt: null, image: null });
outputBall.style.left = '-60px';
})();
</script>
</body>
</html>
Explanation:
- The page is a complete lottery ball machine simulation with controls.
- You can select how many balls to add (1-50).
- You can customize the balls either by entering a textual prompt or uploading an image. If both provided, image takes precedence.
- You can set the spin length in milliseconds (1 to 1,000,000,000) and spin speed in rotations per second (1 to 1,000,000,000).
- Pressing "Spin" will spin the balls inside a drum (circle), slowing down and then selecting a random winning ball.
- After the spinning stops, the winning ball rolls out on a track from left to right.
- Styles and layout are responsive and visually modern.
- The balls are arranged evenly on the circular drum using trigonometric positioning.
- The spinning uses
requestAnimationFrame for smooth animation with easing out.
- The output ball replicates the customization and displays the winning number prominently.
Open this HTML file in a modern browser to use the lottery ball machine.