@@ -452,6 +452,22 @@ class SlotMachine {
452452 this . $container . css ( 'transform' , `matrix(1, 0, 0, 1, 0, ${ margin } )` ) ;
453453 }
454454
455+ /**
456+ * @desc PRIVATE - Is moving from the first element to the last
457+ * @return {Boolean }
458+ */
459+ _isGoingBackward ( ) {
460+ return this . futureActive > this . active && this . active === 0 && this . futureActive === this . $tiles . length - 1 ;
461+ }
462+
463+ /**
464+ * @desc PRIVATE - Is moving from the last element to the first
465+ * @param {Boolean }
466+ */
467+ _isGoingForward ( ) {
468+ return this . futureActive <= this . active && this . active === this . $tiles . length - 1 && this . futureActive === 0 ;
469+ }
470+
455471 /**
456472 * @desc PUBLIC - Custom setTimeout using requestAnimationFrame
457473 * @param function cb - Callback
@@ -520,7 +536,7 @@ class SlotMachine {
520536 this . futureActive = this . prevIndex ;
521537 this . running = true ;
522538 this . slide = true ;
523- this . stop ( false ) ;
539+ this . stop ( ) ;
524540
525541 return this . futureActive ;
526542 }
@@ -533,7 +549,7 @@ class SlotMachine {
533549 this . futureActive = this . nextIndex ;
534550 this . running = true ;
535551 this . slide = true ;
536- this . stop ( false , ( ) => { this . slide = false ; } ) ;
552+ this . stop ( ) ;
537553
538554 return this . futureActive ;
539555 }
@@ -581,34 +597,29 @@ class SlotMachine {
581597 * @return {Number } - Returns result index
582598 */
583599 shuffle ( spins , onComplete ) {
584- let delay = this . settings . delay ;
585-
586600 // Make spins optional
587601 if ( typeof spins === 'function' ) {
588602 onComplete = spins ;
589603 }
590-
591- if ( onComplete ) {
592- this . _oncompleteStack [ 1 ] = onComplete ;
593- }
604+ this . _oncompleteStack . push ( onComplete ) ;
594605 this . running = true ;
595- this . _fade = true ;
596-
597606 // Perform animation
598607 if ( ! this . visible && this . settings . stopHidden === true ) {
599608 this . stop ( ) ;
600609 } else {
601- delay = this . getDelayFromSpins ( spins ) ;
610+ const delay = this . getDelayFromSpins ( spins ) ;
602611 this . delay = delay ;
603612 this . _animate ( this . direction . to ) ;
604613 this . raf ( function cb ( ) {
605- this . resetPosition ( this . direction . first ) ;
606-
607- if ( spins - 1 <= 0 ) {
608- this . stop ( ) ;
609- } else {
610- // Repeat animation
611- this . shuffle ( spins - 1 ) ;
614+ if ( ! this . stopping ) {
615+ this . resetPosition ( this . direction . first ) ;
616+
617+ if ( spins - 1 <= 0 ) {
618+ this . stop ( ) ;
619+ } else {
620+ // Repeat animation
621+ this . shuffle ( spins - 1 ) ;
622+ }
612623 }
613624 } . bind ( this ) , delay ) ;
614625 }
@@ -620,18 +631,13 @@ class SlotMachine {
620631 * @desc PUBLIC - Stop shuffling the elements
621632 * @return {Number } - Returns result index
622633 */
623- stop ( showGradient ) {
634+ stop ( ) {
624635 if ( ! this . running ) {
625636 return ;
626637 } else if ( this . stopping ) {
627638 return this . futureActive ;
628639 }
629640
630- // Stop animation NOW!!!!!!!
631- this . $container . clearQueue ( ) . stop ( true , false ) ;
632-
633- this . _fade = showGradient === undefined ? true : showGradient ;
634- this . _animationFX = FX_SLOW ;
635641 this . running = true ;
636642 this . stopping = true ;
637643 // Set current active element
@@ -644,46 +650,32 @@ class SlotMachine {
644650
645651 // Check direction to prevent jumping
646652 if ( this . slide ) {
647- if ( this . futureActive > this . active ) {
648- // We are moving to the prev (first to last)
649- if ( this . active === 0 && this . futureActive === this . $tiles . length - 1 ) {
650- this . resetPosition ( this . direction . firstToLast ) ;
651- }
652- // We are moving to the next (last to first)
653- } else if ( this . active === this . $tiles . length - 1 && this . futureActive === 0 ) {
653+ if ( this . _isGoingBackward ( ) ) {
654+ this . resetPosition ( this . direction . firstToLast ) ;
655+ } else if ( this . _isGoingForward ( ) ) {
654656 this . resetPosition ( this . direction . lastToFirst ) ;
655657 }
656658 }
657659
658660 // Update last choosen element index
659661 this . active = this . futureActive ;
660662
661- // Get delay
662- const delay = this . settings . delay * 3 ;
663-
664663 // Perform animation
664+ const delay = this . getDelayFromSpins ( 1 ) * 2 ;
665665 this . delay = delay ;
666+ this . _animationFX = FX_STOP ;
666667 this . _animate ( this . getTileOffset ( this . active ) ) ;
667668 this . raf ( function cb ( ) {
668669 this . stopping = false ;
669670 this . running = false ;
670671 this . slide = false ;
671672 this . futureActive = null ;
672673
673- if ( typeof this . _oncompleteStack [ 0 ] === 'function' ) {
674- this . _oncompleteStack [ 0 ] . apply ( this , [ this . active ] ) ;
675- }
676- if ( typeof this . _oncompleteStack [ 1 ] === 'function' ) {
677- this . _oncompleteStack [ 1 ] . apply ( this , [ this . active ] ) ;
678- }
674+ this . _oncompleteStack . filter ( ( fn ) => typeof fn === 'function' ) . forEach ( ( fn ) => {
675+ fn . apply ( this , [ this . active ] ) ;
676+ } ) ;
679677 } . bind ( this ) , delay ) ;
680678
681- // Disable blur
682- this . raf ( function cb ( ) {
683- this . _fade = false ;
684- this . _animationFX = FX_STOP ;
685- } . bind ( this ) , delay / 1.75 ) ;
686-
687679 return this . active ;
688680 }
689681
0 commit comments