UNPKG

slot-machine-gen

Version:

Create an extremely biased, web-based slot machine game.

1 lines 3.08 kB
"use strict";function SlotMachine(t,e,n,i){var p=this,h=24;if(p.options=Object.assign({reelHeight:1200,reelWidth:200,reelOffset:20,slotYAxis:0,animSpeed:1e3,click2Spin:!0,sounds:{reelsBegin:null,reelsEnd:null},rngFunc:function(){return Math.random()}},i),!(0<e.length))throw new Error("Failed to initialize (missing reels)");t.setAttribute("aria-label","Slot machine");var o=document.createElement("div");o.classList.add("display");for(var s=0;s<e.length;s++){var r=document.createElement("div");r.classList.add("reel"),r.setAttribute("role","none"),r.style.transform="rotateY("+p.options.slotYAxis+"deg)",o.appendChild(r)}p.options.click2Spin&&(i="Click to spin",o.addEventListener("click",a),o.setAttribute("aria-label",i),o.setAttribute("role","button"),o.setAttribute("title",i),o.style.cursor="pointer"),t.appendChild(o);var l=document.createElement("div");function a(){var o=[];n&&(o.push=function(){Array.prototype.push.apply(this,arguments),o.length===e.length&&window.setTimeout(function(){p.isAnimating=!1,n(o)},p.options.animSpeed)}),c(p.options.sounds.reelsBegin),e.forEach(function(t){var e=function(t){for(var e=0,n=t.length,i=0;i<n;i++)e+=t[i].weight;for(var o=d()*e,s=0;s<n;s++){var r=t[s],l=r.weight;if(o<l)return r;o-=l}}(t.symbols),n=e.position,i=t.element;i.classList.remove("stop"),i.classList.toggle("spin"),i.childNodes.forEach(function(t,e){t.style.backgroundPositionY=f(e,n)+"px"}),window.setTimeout(function(){i.classList.replace("spin","stop"),c(p.options.sounds.reelsEnd),o.push(e)},p.options.animSpeed*function(t,e){void 0===t&&(t=1);void 0===e&&(e=10);t=Math.ceil(t),e=Math.floor(e);return Math.floor(d()*(Math.floor(e)-t))+t}(1,4))})}function d(){return p.options.rngFunc()}function f(t,e){return-Math.abs(m()*t+(e-p.options.reelOffset))}function m(){return p.options.reelHeight/h}function c(t){var e;t&&((e=new Audio).src=t,e.onerror=function(){return console.warn("Failed to load audio: "+t)},e.play())}return l.classList.add("slots"),l.setAttribute("aria-label","Reels"),e.forEach(function(t,e){void 0===(n=(t=t).symbols[0].position)&&(n=0),(n=document.createElement("div")).style.transform="rotateY("+p.options.slotYAxis+"deg)",n.classList.add("reel"),i=function(t,e){void 0===e&&(e=0);var n=m(),i=p.options.reelWidth,o=360/h,s=Math.trunc(Math.tan(90/Math.PI-o)*(.5*n)*4),r=s+n/2,l=document.createElement("ul");l.style.height=n+"px",l.style.marginTop=r+"px",l.style.width=i+"px",l.classList.add("strip");for(var a=0;a<h;a++){var d=document.createElement("li"),c=(d.append(a.toString()),f(a,e)),u=h*o-a*o;d.style.background="url("+t.imageSrc+") 0 "+c+"px",d.style.height=n+"px",d.style.width=i+"px",d.style.transform="rotateX("+u+"deg) translateZ("+s+"px)",l.appendChild(d)}return l}(t,t.symbols[0].position),t.element=i,n.appendChild(i);var n,i,t=n;t.setAttribute("aria-label","Reel "+(e+1)),l.appendChild(t)}),t.appendChild(l),p.play=function(){var t;t=a,p.isAnimating||(p.isAnimating=!0,t.call(p))},p}window.slotMachine=function(t,e,n,i){return new SlotMachine(t,e,n,i)},"undefined"!=typeof module&&module.exports&&(module.exports=SlotMachine);