|
9 | 9 |
|
10 | 10 | var pluginName = "slotMachine", |
11 | 11 | defaults = { |
12 | | - active: 0, //Active element [int] |
13 | | - delay: 200, //Animation time [int] |
14 | | - auto: false, //Repeat delay [false||int] |
15 | | - spins: 5, //Number of spins when auto [int] |
16 | | - randomize: null, //Randomize function, must return an integer with the selected position |
| 12 | + active: 0, //Active element [Number] |
| 13 | + delay: 200, //Animation time [Number] |
| 14 | + auto: false, //Repeat delay [false||Number] |
| 15 | + spins: 5, //Number of spins when auto [Number] |
| 16 | + randomize: null, //Randomize function, must return a number with the selected position |
17 | 17 | complete: null, //Callback function(result) |
18 | 18 | stopHidden: true, //Stops animations if the element isn´t visible on the screen |
19 | 19 | direction: 'up' //Animation direction ['up'||'down'] |
|
92 | 92 |
|
93 | 93 | function Timer(fn, delay) { |
94 | 94 | var startTime, |
95 | | - self = this, |
96 | | - timer, |
97 | | - _fn = fn, |
98 | | - _args = arguments, |
99 | | - _delay = delay; |
| 95 | + self = this, |
| 96 | + timer, |
| 97 | + _fn = fn, |
| 98 | + _args = arguments, |
| 99 | + _delay = delay; |
100 | 100 |
|
101 | 101 | this.running = false; |
102 | 102 |
|
|
248 | 248 | } |
249 | 249 | } |
250 | 250 |
|
| 251 | + /** |
| 252 | + * @desc PUBLIC - Custom setTimeout using requestAnimationFrame |
| 253 | + * @param function cb - Callback |
| 254 | + * @param Number timeout - Timeout delay |
| 255 | + */ |
| 256 | + SlotMachine.prototype.raf = function(cb, timeout) { |
| 257 | + var _raf = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame, |
| 258 | + startTime = new Date().getTime(), |
| 259 | + _rafHandler = function(timestamp){ |
| 260 | + var drawStart = new Date().getTime(), |
| 261 | + diff = drawStart - startTime; |
| 262 | + |
| 263 | + if(diff < timeout){ |
| 264 | + _raf(_rafHandler); |
| 265 | + }else if(typeof cb === 'function'){ |
| 266 | + cb(); |
| 267 | + } |
| 268 | + }; |
| 269 | + |
| 270 | + _raf(_rafHandler); |
| 271 | + }; |
| 272 | + |
251 | 273 | /** |
252 | 274 | * @desc PUBLIC - Get element offset top |
253 | | - * @param int index - Element position |
254 | | - * @return int - Negative offset in px |
| 275 | + * @param Number index - Element position |
| 276 | + * @return Number - Negative offset in px |
255 | 277 | */ |
256 | 278 | SlotMachine.prototype.getTileOffset = function(index) { |
257 | 279 | var offset = 0; |
|
263 | 285 |
|
264 | 286 | /** |
265 | 287 | * @desc PUBLIC - Get current showing element index |
266 | | - * @return int - Element index |
| 288 | + * @return Number - Element index |
267 | 289 | */ |
268 | 290 | SlotMachine.prototype.getVisibleTile = function() { |
269 | 291 | var firstTileHeight = this.$tiles.first().height(), |
|
274 | 296 |
|
275 | 297 | /** |
276 | 298 | * @desc PUBLIC - Changes randomize function |
277 | | - * @param function|int - Set new randomize function |
| 299 | + * @param function|Number - Set new randomize function |
278 | 300 | */ |
279 | 301 | SlotMachine.prototype.setRandomize = function(rnd) { |
280 | 302 | if (typeof rnd === 'number') { |
|
289 | 311 | /** |
290 | 312 | * @desc PUBLIC - Get random element different than last shown |
291 | 313 | * @param boolean cantBeTheCurrent - true||undefined if cant be choosen the current element, prevents repeat |
292 | | - * @return int - Element index |
| 314 | + * @return Number - Element index |
293 | 315 | */ |
294 | 316 | SlotMachine.prototype.getRandom = function(cantBeTheCurrent) { |
295 | 317 | var rnd, |
|
303 | 325 |
|
304 | 326 | /** |
305 | 327 | * @desc PUBLIC - Get random element based on the custom randomize function |
306 | | - * @return int - Element index |
| 328 | + * @return Number - Element index |
307 | 329 | */ |
308 | 330 | SlotMachine.prototype.getCustom = function() { |
309 | 331 | var choosen; |
|
321 | 343 |
|
322 | 344 | /** |
323 | 345 | * @desc PRIVATE - Get the previous element (no direction related) |
324 | | - * @return int - Element index |
| 346 | + * @return Number - Element index |
325 | 347 | */ |
326 | 348 | SlotMachine.prototype._getPrev = function() { |
327 | 349 | var prevIndex = (this.active - 1 < 0) ? (this.$tiles.length - 1) : (this.active - 1); |
|
330 | 352 |
|
331 | 353 | /** |
332 | 354 | * @desc PRIVATE - Get the next element (no direction related) |
333 | | - * @return int - Element index |
| 355 | + * @return Number - Element index |
334 | 356 | */ |
335 | 357 | SlotMachine.prototype._getNext = function() { |
336 | 358 | var nextIndex = (this.active + 1 < this.$tiles.length) ? (this.active + 1) : 0; |
|
339 | 361 |
|
340 | 362 | /** |
341 | 363 | * @desc PUBLIC - Get the previous element dor selected direction |
342 | | - * @return int - Element index |
| 364 | + * @return Number - Element index |
343 | 365 | */ |
344 | 366 | SlotMachine.prototype.getPrev = function() { |
345 | 367 | return this._direction.selected === 'up' ? this._getPrev() : this._getNext(); |
346 | 368 | }; |
347 | 369 |
|
348 | 370 | /** |
349 | 371 | * @desc PUBLIC - Get the next element |
350 | | - * @return int - Element index |
| 372 | + * @return Number - Element index |
351 | 373 | */ |
352 | 374 | SlotMachine.prototype.getNext = function() { |
353 | 375 | return this._direction.selected === 'up' ? this._getNext() : this._getPrev(); |
|
371 | 393 | * @param string||boolean fade - Set fade gradient effect |
372 | 394 | */ |
373 | 395 | SlotMachine.prototype._setAnimationFX = function(FX_SPEED, fade) { |
374 | | - var self = this; |
375 | | - |
376 | | - setTimeout(function() { |
377 | | - self.$tiles.removeClass([FX_FAST, FX_NORMAL, FX_SLOW].join(' ')).addClass(FX_SPEED); |
| 396 | + this.raf(function() { |
| 397 | + this.$tiles.removeClass([FX_FAST, FX_NORMAL, FX_SLOW].join(' ')).addClass(FX_SPEED); |
378 | 398 |
|
379 | 399 | if (fade !== true || FX_SPEED === FX_STOP) { |
380 | | - self.$slot.add(self.$tiles).removeClass(FX_GRADIENT); |
| 400 | + this.$slot.add(this.$tiles).removeClass(FX_GRADIENT); |
381 | 401 | } else { |
382 | | - self.$slot.add(self.$tiles).addClass(FX_GRADIENT); |
| 402 | + this.$slot.add(this.$tiles).addClass(FX_GRADIENT); |
383 | 403 | } |
384 | | - }, this.settings.delay / 4); |
| 404 | + }.bind(this), this.settings.delay / 4); |
385 | 405 | }; |
386 | 406 |
|
387 | 407 | /** |
|
393 | 413 |
|
394 | 414 | /** |
395 | 415 | * @desc PRIVATE - Checks if the machine is on the screen |
396 | | - * @return int - Returns true if machine is on the screen |
| 416 | + * @return Number - Returns true if machine is on the screen |
397 | 417 | */ |
398 | 418 | SlotMachine.prototype.isVisible = function() { |
399 | 419 | //Stop animation if element is [above||below] screen, best for performance |
|
405 | 425 |
|
406 | 426 | /** |
407 | 427 | * @desc PUBLIC - SELECT previous element relative to the current active element |
408 | | - * @return int - Returns result index |
| 428 | + * @return Number - Returns result index |
409 | 429 | */ |
410 | 430 | SlotMachine.prototype.prev = function() { |
411 | 431 | this.futureActive = this.getPrev(); |
|
416 | 436 |
|
417 | 437 | /** |
418 | 438 | * @desc PUBLIC - SELECT next element relative to the current active element |
419 | | - * @return int - Returns result index |
| 439 | + * @return Number - Returns result index |
420 | 440 | */ |
421 | 441 | SlotMachine.prototype.next = function() { |
422 | 442 | this.futureActive = this.getNext(); |
|
427 | 447 |
|
428 | 448 | /** |
429 | 449 | * @desc PUBLIC - Starts shuffling the elements |
430 | | - * @param int repeations - Number of shuffles (undefined to make infinite animation |
431 | | - * @return int - Returns result index |
| 450 | + * @param Number repeations - Number of shuffles (undefined to make infinite animation |
| 451 | + * @return Number - Returns result index |
432 | 452 | */ |
433 | 453 | SlotMachine.prototype.shuffle = function(spins, onComplete) { |
434 | 454 | var self = this; |
|
495 | 515 |
|
496 | 516 | /** |
497 | 517 | * @desc PUBLIC - Stop shuffling the elements |
498 | | - * @return int - Returns result index |
| 518 | + * @return Number - Returns result index |
499 | 519 | */ |
500 | 520 | SlotMachine.prototype.stop = function(showGradient) { |
501 | 521 | if (!this.isRunning) { |
|
553 | 573 | }); |
554 | 574 |
|
555 | 575 | //Disable blur |
556 | | - setTimeout(function() { |
557 | | - self._setAnimationFX(FX_STOP, false); |
558 | | - }, delay / 1.75); |
| 576 | + this.raf(function() { |
| 577 | + this._setAnimationFX(FX_STOP, false); |
| 578 | + }.bind(this), delay / 1.75); |
559 | 579 |
|
560 | 580 | return this.active; |
561 | 581 | }; |
|
572 | 592 | } |
573 | 593 | self.isRunning = true; |
574 | 594 | if (!self.isVisible() && self.settings.stopHidden === true) { |
575 | | - setTimeout(function() { |
| 595 | + self.raf(function() { |
576 | 596 | self._timer.reset(); |
577 | 597 | }, 500); |
578 | 598 | } else { |
|
0 commit comments