Skip to content

Commit 183d326

Browse files
committed
Animation improvement, still unstable
1 parent 75f2c6e commit 183d326

File tree

2 files changed

+89
-41
lines changed

2 files changed

+89
-41
lines changed

Gruntfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ module.exports = function(grunt) {
6060
},
6161
src: {
6262
files: 'src/**/*.js',
63-
tasks: ['eslint', 'babel', 'concat', 'qunit']
63+
tasks: ['babel', 'concat', 'qunit']
6464
},
6565
test: {
6666
files: 'test/**/*.js',

src/jquery.slotmachine.js

Lines changed: 88 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ const pluginName = 'slotMachine',
1616
randomize: null, // Randomize function, must return a number with the selected position
1717
complete: null, // Callback function(result)
1818
stopHidden: true, // Stops animations if the element isn´t visible on the screen
19-
direction: 'up' // Animation direction ['up'||'down']
19+
direction: 'up', // Animation direction ['up'||'down']
20+
transition: 'ease-in-out'
2021
},
2122
FX_NO_TRANSITION = 'slotMachineNoTransition',
2223
FX_FAST = 'slotMachineBlurFast',
@@ -384,7 +385,11 @@ class SlotMachine {
384385
set _fxClass (FX_SPEED) {
385386
const classes = [FX_FAST, FX_NORMAL, FX_SLOW].join(' ');
386387

387-
this.$tiles.removeClass(classes).addClass(FX_SPEED);
388+
this.$tiles
389+
.add(this._$fakeFirstTile)
390+
.add(this._$fakeLastTile)
391+
.removeClass(classes)
392+
.addClass(FX_SPEED);
388393
}
389394

390395
/**
@@ -394,7 +399,10 @@ class SlotMachine {
394399
*/
395400
set _animationFX (FX_SPEED) {
396401
const delay = this.settings.delay / 4,
397-
$elements = this.$slot.add(this.$tiles);
402+
$elements = this.$slot
403+
.add(this.$tiles)
404+
.add(this._$fakeFirstTile)
405+
.add(this._$fakeLastTile);
398406

399407
this.raf(function cb () {
400408
this._fxClass = FX_SPEED;
@@ -408,11 +416,32 @@ class SlotMachine {
408416
}
409417

410418
/**
411-
* @desc PRIVATE - Set container margin
419+
* @desc PRIVATE - Set css transition delay
412420
* @param {Number} - Transition delay in ms
413421
*/
414-
set _transition (delay) {
415-
this.$container.css('transition', `${delay / 1000}s ease-in-out`);
422+
set delay (delay) {
423+
delay = delay / 1000;
424+
this._delay = delay;
425+
this._changeTransition();
426+
}
427+
428+
/**
429+
* @desc PRIVATE - Set css transition
430+
* @param {String} - Transition type
431+
*/
432+
set transition (transition) {
433+
transition = transition || 'ease-in-out';
434+
this._transition = transition;
435+
this._changeTransition();
436+
}
437+
438+
/**
439+
* @desc PRIVATE - Set css transition property
440+
*/
441+
_changeTransition () {
442+
const delay = this._delay || this.settings.delay,
443+
transition = this._transition || this.settings.transition;
444+
this.$container.css('transition', `${delay}s ${transition}`);
416445
}
417446

418447
/**
@@ -490,6 +519,7 @@ class SlotMachine {
490519
prev () {
491520
this.futureActive = this.prevIndex;
492521
this.running = true;
522+
this.slide = true;
493523
this.stop(false);
494524

495525
return this.futureActive;
@@ -502,11 +532,49 @@ class SlotMachine {
502532
next () {
503533
this.futureActive = this.nextIndex;
504534
this.running = true;
505-
this.stop(false);
535+
this.slide = true;
536+
this.stop(false, () => {this.slide = false;});
506537

507538
return this.futureActive;
508539
}
509540

541+
/**
542+
* @desc PUBLIC - Starts shuffling the elements
543+
* @param {Number} repeations - Number of shuffles (undefined to make infinite animation
544+
* @return {Number} - Returns result index
545+
*/
546+
getDelayFromSpins (spins) {
547+
let delay = this.settings.delay;
548+
549+
switch (spins) {
550+
case 1:
551+
delay /= 0.5;
552+
this._transition = 'ease-out';
553+
this._animationFX = FX_SLOW;
554+
break;
555+
case 2:
556+
delay /= 0.75;
557+
this._transition = 'ease-out';
558+
this._animationFX = FX_SLOW;
559+
break;
560+
case 3:
561+
delay /= 1;
562+
this._transition = 'linear';
563+
this._animationFX = FX_NORMAL;
564+
break;
565+
case 4:
566+
delay /= 1.25;
567+
this._animationFX = FX_NORMAL;
568+
break;
569+
default:
570+
delay /= 1.5;
571+
this._transition = 'linear';
572+
this._animationFX = FX_FAST;
573+
}
574+
575+
return delay;
576+
}
577+
510578
/**
511579
* @desc PUBLIC - Starts shuffling the elements
512580
* @param {Number} repeations - Number of shuffles (undefined to make infinite animation
@@ -526,36 +594,13 @@ class SlotMachine {
526594
this.running = true;
527595
this._fade = true;
528596

529-
// Decreasing spin
530-
if (typeof spins === 'number') {
531-
// Change delay and speed
532-
switch (spins) {
533-
case 1:
534-
case 2:
535-
this._animationFX = FX_SLOW;
536-
break;
537-
case 3:
538-
case 4:
539-
this._animationFX = FX_NORMAL;
540-
delay /= 1.5;
541-
break;
542-
default:
543-
this._animationFX = FX_FAST;
544-
delay /= 2;
545-
}
546-
// Infinite spin
547-
} else {
548-
// Set animation effects
549-
this._animationFX = FX_FAST;
550-
delay /= 2;
551-
}
552-
553597
// Perform animation
554598
if (!this.visible && this.settings.stopHidden === true) {
555599
this.stop();
556600
} else {
601+
delay = this.getDelayFromSpins(spins);
602+
this.delay = delay;
557603
this._animate(this.direction.to);
558-
this._transition = delay;
559604
this.raf(function cb () {
560605
this.resetPosition(this.direction.first);
561606

@@ -598,14 +643,16 @@ class SlotMachine {
598643
}
599644

600645
// Check direction to prevent jumping
601-
if (this.futureActive > this.active) {
602-
// We are moving to the prev (first to last)
603-
if (this.active === 0 && this.futureActive === this.$tiles.length - 1) {
604-
this.resetPosition(this.direction.firstToLast);
646+
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) {
654+
this.resetPosition(this.direction.lastToFirst);
605655
}
606-
// We are moving to the next (last to first)
607-
} else if (this.active === this.$tiles.length - 1 && this.futureActive === 0) {
608-
this.resetPosition(this.direction.lastToFirst);
609656
}
610657

611658
// Update last choosen element index
@@ -615,11 +662,12 @@ class SlotMachine {
615662
const delay = this.settings.delay * 3;
616663

617664
// Perform animation
618-
this._transition = delay;
665+
this.delay = delay;
619666
this._animate(this.getTileOffset(this.active));
620667
this.raf(function cb () {
621668
this.stopping = false;
622669
this.running = false;
670+
this.slide = false;
623671
this.futureActive = null;
624672

625673
if (typeof this._oncompleteStack[0] === 'function') {

0 commit comments

Comments
 (0)