UNPKG

spin-wheel

Version:

An easy to use, themeable component for randomising choices and prizes.

82 lines (63 loc) 2.14 kB
import {Wheel} from '../../../dist/spin-wheel-esm.js'; import {loadFonts, loadImages} from '../../../scripts/util.js'; import {props} from './props.js'; window.onload = async () => { await loadFonts(props.map(i => i.itemLabelFont)); const wheel = new Wheel(document.querySelector('.wheel-wrapper')); const dropdown = document.querySelector('select'); const images = []; for (const p of props) { // Initalise dropdown with the names of each example: const opt = document.createElement('option'); opt.textContent = p.name; dropdown.append(opt); // Convert image urls into actual images: images.push(initImage(p, 'image')); images.push(initImage(p, 'overlayImage')); for (const item of p.items) { images.push(initImage(item, 'image')); } } await loadImages(images); // Show the wheel once everything has loaded document.querySelector('.wheel-wrapper').style.visibility = 'visible'; // Handle dropdown change: dropdown.onchange = () => { wheel.init({ ...props[dropdown.selectedIndex], rotation: wheel.rotation, // Preserve value. }); }; // Select default: dropdown.options[0].selected = 'selected'; dropdown.onchange(); // Save object globally for easy debugging. window.wheel = wheel; const btnSpin = document.querySelector('button'); let modifier = 0; window.addEventListener('click', (e) => { // Listen for click event on spin button: if (e.target === btnSpin) { const {duration, winningItemRotaion} = calcSpinToValues(); wheel.spinTo(winningItemRotaion, duration); } }); function calcSpinToValues() { const duration = 3000; const winningItemRotaion = getRandomInt(360, 360 * 1.75) + modifier; modifier += 360 * 1.75; return {duration, winningItemRotaion}; } function getRandomInt(min, max) { min = Math.ceil(min); max = Math.floor(max); return Math.floor(Math.random() * (max - min)) + min; } function initImage(obj, pName) { if (!obj[pName]) return null; const i = new Image(); i.src = obj[pName]; obj[pName] = i; return i; } };