Recently somebody talked to me about creating a slot machine. I looked on the web and could not find any good slot machine implementation in javascript. So I decided to write a quick one. My implementation uses jquery and a couple of jquery plugins for animation. Spritely is a wonderful jquery plugin for background animation. I also used a plugin for animating background-position since jquery does not support it natively.
Lets start building the slot machine. Demo here
First of all we need a sprite for our slot machine. This sprite is basically a reel which in motion simulates a slot. Its preferable to have blurred copy of this reel too, to show the effect of motion. For the purpose of this demo I got the sprite from internet.
The markup is very straightforward. We just need placeholders for 3 slots and a button as controller.
Lets got through the javascript now. First we need a class for a slot. Our 3 slots will be objects of this class.
function Slot(el, max, step) { this.speed = 0; //speed of the slot at any point of time this.step = step; //speed will increase at this rate this.si = null; //holds setInterval object for the given slot this.el = el; //dom element of the slot this.maxSpeed = max; //max speed this slot can have }
Why do we need so many paramaters for a slot machine? That’s because each slot of an ideal slot machine should have different acceleration and speed. Also its good to have different max speeds.
Our Slot class will have 4 member functions:
- start() – starts a slot
- stop() – stops a slot
- finalPos() – finds the final position of the slot when its stopped
- reset() – Resets a slot for another run
How it works?
Initially we create an object for each slot of the slot machine. We pass different values for the max-speed and speed-stepper to the constructor.
var a = new Slot('#slot1', 30, 1), b = new Slot('#slot2', 45, 2), c = new Slot('#slot3', 70, 3);
When a user presses ‘Start’, start method is called for each slot. This method kicks off the slot and increments the speed at regular intervals. This can be done using setInterval. Code below:
Slot.prototype.start = function() { var _this = this; $(_this.el).addClass('motion'); $(_this.el).spStart(); _this.si = window.setInterval(function() { if(_this.speed < _this.maxSpeed) { _this.speed += _this.step; $(_this.el).spSpeed(_this.speed); } }, 100); };
Once all the slots reach their maximum speeds, the ‘Stop’ button is enabled. When the user presses ‘Stop’, the speed of the slots start decrementing.
Slot.prototype.stop = function() { var _this = this, limit = 30; clearInterval(_this.si); _this.si = window.setInterval(function() { if(_this.speed > limit) { _this.speed -= _this.step; $(_this.el).spSpeed(_this.speed); } if(_this.speed <= limit) { _this.finalPos(_this.el); $(_this.el).spSpeed(0); $(_this.el).spStop(); clearInterval(_this.si); $(_this.el).removeClass('motion'); _this.speed = 0; } }, 100); };
Once the speed comes below a certain threshold, the final position is calculated for each of the slots based on their current position and the slots are rotated to reach the final position.
Slot.prototype.finalPos = function() { var el = this.el, pos, posMin = 2000000000, best, bgPos, i, j, k; el_id = $(el).attr('id'); pos = document.getElementById(el_id).style.backgroundPosition; pos = pos.split(' ')[1]; pos = parseInt(pos, 10); for(i = 0; i < posArr.length; i++) { for(j = 0;;j++) { k = posArr[i] + (imgHeight * j); if(k > pos) { if((k - pos) < posMin) { posMin = k - pos; best = k; } break; } } } best += imgHeight + 4; bgPos = "0 " + best + "px"; $(el).animate({ backgroundPosition:"(" + bgPos + ")" }, { duration: 200 }); };
Demo
You can see the implementation here.
The full source code can be viewed at my GitHub repository

