Skip to content

Commit 3d3fc48

Browse files
committed
Updated package version
1 parent 093f276 commit 3d3fc48

File tree

6 files changed

+64
-44
lines changed

6 files changed

+64
-44
lines changed

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "jQuery-SlotMachine",
33
"description": "A simple jQuery plugin to make slot machine animation effect",
4-
"version": "2.1.0",
4+
"version": "2.2.0",
55
"keywords": [
66
"slots",
77
"gambling",

dist/jquery.slotmachine.js

Lines changed: 58 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
/*! SlotMachine - v2.1.0 - 2015-07-21
1+
/*! SlotMachine - v2.2.0 - 2015-11-05
22
* https://github.com/josex2r/jQuery-SlotMachine
33
* Copyright (c) 2015 Jose Luis Represa; Licensed MIT */
44
(function($, window, document, undefined) {
55

66
var pluginName = "slotMachine",
77
defaults = {
8-
active: 0, //Active element [int]
9-
delay: 200, //Animation time [int]
10-
auto: false, //Repeat delay [false||int]
11-
spins: 5, //Number of spins when auto [int]
12-
randomize: null, //Randomize function, must return an integer with the selected position
8+
active: 0, //Active element [Number]
9+
delay: 200, //Animation time [Number]
10+
auto: false, //Repeat delay [false||Number]
11+
spins: 5, //Number of spins when auto [Number]
12+
randomize: null, //Randomize function, must return a number with the selected position
1313
complete: null, //Callback function(result)
1414
stopHidden: true, //Stops animations if the element isn´t visible on the screen
1515
direction: 'up' //Animation direction ['up'||'down']
@@ -88,11 +88,11 @@
8888

8989
function Timer(fn, delay) {
9090
var startTime,
91-
self = this,
92-
timer,
93-
_fn = fn,
94-
_args = arguments,
95-
_delay = delay;
91+
self = this,
92+
timer,
93+
_fn = fn,
94+
_args = arguments,
95+
_delay = delay;
9696

9797
this.running = false;
9898

@@ -244,10 +244,32 @@
244244
}
245245
}
246246

247+
/**
248+
* @desc PUBLIC - Custom setTimeout using requestAnimationFrame
249+
* @param function cb - Callback
250+
* @param Number timeout - Timeout delay
251+
*/
252+
SlotMachine.prototype.raf = function(cb, timeout) {
253+
var _raf = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame,
254+
startTime = new Date().getTime(),
255+
_rafHandler = function(/*timestamp*/){
256+
var drawStart = new Date().getTime(),
257+
diff = drawStart - startTime;
258+
259+
if(diff < timeout){
260+
_raf(_rafHandler);
261+
}else if(typeof cb === 'function'){
262+
cb();
263+
}
264+
};
265+
266+
_raf(_rafHandler);
267+
};
268+
247269
/**
248270
* @desc PUBLIC - Get element offset top
249-
* @param int index - Element position
250-
* @return int - Negative offset in px
271+
* @param Number index - Element position
272+
* @return Number - Negative offset in px
251273
*/
252274
SlotMachine.prototype.getTileOffset = function(index) {
253275
var offset = 0;
@@ -259,7 +281,7 @@
259281

260282
/**
261283
* @desc PUBLIC - Get current showing element index
262-
* @return int - Element index
284+
* @return Number - Element index
263285
*/
264286
SlotMachine.prototype.getVisibleTile = function() {
265287
var firstTileHeight = this.$tiles.first().height(),
@@ -270,7 +292,7 @@
270292

271293
/**
272294
* @desc PUBLIC - Changes randomize function
273-
* @param function|int - Set new randomize function
295+
* @param function|Number - Set new randomize function
274296
*/
275297
SlotMachine.prototype.setRandomize = function(rnd) {
276298
if (typeof rnd === 'number') {
@@ -285,7 +307,7 @@
285307
/**
286308
* @desc PUBLIC - Get random element different than last shown
287309
* @param boolean cantBeTheCurrent - true||undefined if cant be choosen the current element, prevents repeat
288-
* @return int - Element index
310+
* @return Number - Element index
289311
*/
290312
SlotMachine.prototype.getRandom = function(cantBeTheCurrent) {
291313
var rnd,
@@ -299,7 +321,7 @@
299321

300322
/**
301323
* @desc PUBLIC - Get random element based on the custom randomize function
302-
* @return int - Element index
324+
* @return Number - Element index
303325
*/
304326
SlotMachine.prototype.getCustom = function() {
305327
var choosen;
@@ -317,7 +339,7 @@
317339

318340
/**
319341
* @desc PRIVATE - Get the previous element (no direction related)
320-
* @return int - Element index
342+
* @return Number - Element index
321343
*/
322344
SlotMachine.prototype._getPrev = function() {
323345
var prevIndex = (this.active - 1 < 0) ? (this.$tiles.length - 1) : (this.active - 1);
@@ -326,7 +348,7 @@
326348

327349
/**
328350
* @desc PRIVATE - Get the next element (no direction related)
329-
* @return int - Element index
351+
* @return Number - Element index
330352
*/
331353
SlotMachine.prototype._getNext = function() {
332354
var nextIndex = (this.active + 1 < this.$tiles.length) ? (this.active + 1) : 0;
@@ -335,15 +357,15 @@
335357

336358
/**
337359
* @desc PUBLIC - Get the previous element dor selected direction
338-
* @return int - Element index
360+
* @return Number - Element index
339361
*/
340362
SlotMachine.prototype.getPrev = function() {
341363
return this._direction.selected === 'up' ? this._getPrev() : this._getNext();
342364
};
343365

344366
/**
345367
* @desc PUBLIC - Get the next element
346-
* @return int - Element index
368+
* @return Number - Element index
347369
*/
348370
SlotMachine.prototype.getNext = function() {
349371
return this._direction.selected === 'up' ? this._getNext() : this._getPrev();
@@ -367,17 +389,15 @@
367389
* @param string||boolean fade - Set fade gradient effect
368390
*/
369391
SlotMachine.prototype._setAnimationFX = function(FX_SPEED, fade) {
370-
var self = this;
371-
372-
setTimeout(function() {
373-
self.$tiles.removeClass([FX_FAST, FX_NORMAL, FX_SLOW].join(' ')).addClass(FX_SPEED);
392+
this.raf(function() {
393+
this.$tiles.removeClass([FX_FAST, FX_NORMAL, FX_SLOW].join(' ')).addClass(FX_SPEED);
374394

375395
if (fade !== true || FX_SPEED === FX_STOP) {
376-
self.$slot.add(self.$tiles).removeClass(FX_GRADIENT);
396+
this.$slot.add(this.$tiles).removeClass(FX_GRADIENT);
377397
} else {
378-
self.$slot.add(self.$tiles).addClass(FX_GRADIENT);
398+
this.$slot.add(this.$tiles).addClass(FX_GRADIENT);
379399
}
380-
}, this.settings.delay / 4);
400+
}.bind(this), this.settings.delay / 4);
381401
};
382402

383403
/**
@@ -389,7 +409,7 @@
389409

390410
/**
391411
* @desc PRIVATE - Checks if the machine is on the screen
392-
* @return int - Returns true if machine is on the screen
412+
* @return Number - Returns true if machine is on the screen
393413
*/
394414
SlotMachine.prototype.isVisible = function() {
395415
//Stop animation if element is [above||below] screen, best for performance
@@ -401,7 +421,7 @@
401421

402422
/**
403423
* @desc PUBLIC - SELECT previous element relative to the current active element
404-
* @return int - Returns result index
424+
* @return Number - Returns result index
405425
*/
406426
SlotMachine.prototype.prev = function() {
407427
this.futureActive = this.getPrev();
@@ -412,7 +432,7 @@
412432

413433
/**
414434
* @desc PUBLIC - SELECT next element relative to the current active element
415-
* @return int - Returns result index
435+
* @return Number - Returns result index
416436
*/
417437
SlotMachine.prototype.next = function() {
418438
this.futureActive = this.getNext();
@@ -423,8 +443,8 @@
423443

424444
/**
425445
* @desc PUBLIC - Starts shuffling the elements
426-
* @param int repeations - Number of shuffles (undefined to make infinite animation
427-
* @return int - Returns result index
446+
* @param Number repeations - Number of shuffles (undefined to make infinite animation
447+
* @return Number - Returns result index
428448
*/
429449
SlotMachine.prototype.shuffle = function(spins, onComplete) {
430450
var self = this;
@@ -491,7 +511,7 @@
491511

492512
/**
493513
* @desc PUBLIC - Stop shuffling the elements
494-
* @return int - Returns result index
514+
* @return Number - Returns result index
495515
*/
496516
SlotMachine.prototype.stop = function(showGradient) {
497517
if (!this.isRunning) {
@@ -549,9 +569,9 @@
549569
});
550570

551571
//Disable blur
552-
setTimeout(function() {
553-
self._setAnimationFX(FX_STOP, false);
554-
}, delay / 1.75);
572+
this.raf(function() {
573+
this._setAnimationFX(FX_STOP, false);
574+
}.bind(this), delay / 1.75);
555575

556576
return this.active;
557577
};
@@ -568,7 +588,7 @@
568588
}
569589
self.isRunning = true;
570590
if (!self.isVisible() && self.settings.stopHidden === true) {
571-
setTimeout(function() {
591+
self.raf(function() {
572592
self._timer.reset();
573593
}, 500);
574594
} else {

0 commit comments

Comments
 (0)