diff --git a/README.md b/README.md index 4d31f94..527cdba 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,12 @@ Check if the machine is running: machine.isRunning(); //Returns boolean ``` +Change spin result, if the returned value is out of bounds, the element will be randomly choosen: + +```javascript +machine.setRandomize(foo); //foo must be a function (should return int) or an int +``` + ## Params Params must be an object, optionally containing the next parammeters: diff --git a/dist/jquery.slotmachine.js b/dist/jquery.slotmachine.js index dff01b3..ad790e8 100644 --- a/dist/jquery.slotmachine.js +++ b/dist/jquery.slotmachine.js @@ -1,4 +1,4 @@ -/*! SlotMachine - v2.0.1 - 2014-07-09 +/*! SlotMachine - v2.0.1 - 2014-07-31 * https://github.com/josex2r/jQuery-SlotMachine * Copyright (c) 2014 Jose Luis Represa; Licensed MIT */ ;(function($, window, document, undefined){ @@ -156,6 +156,21 @@ }; } + /** + * @desc PUBLIC - Changes randomize function + * @param function|int - Set new randomize function + */ + function _setRandomize(rnd){ + if( typeof rnd==='number' ){ + var _fn = function(){ + return rnd; + }; + self.settings.randomize = _fn; + }else{ + self.settings.randomize = rnd; + } + } + /** * @desc PRIVATE - Get random element based on the custom randomize function * @return object - Element index and HTML node @@ -539,7 +554,13 @@ */ isRunning : function(){ return _isRunning; - } + }, + + /** + * @desc PUBLIC - Changes randomize function + * @param function|int - Set new randomize function + */ + setRandomize : _setRandomize }; } diff --git a/dist/jquery.slotmachine.min.js b/dist/jquery.slotmachine.min.js index fac98c1..a3d3c55 100644 --- a/dist/jquery.slotmachine.min.js +++ b/dist/jquery.slotmachine.min.js @@ -1,4 +1,4 @@ -/*! SlotMachine - v2.0.1 - 2014-07-09 +/*! SlotMachine - v2.0.1 - 2014-07-31 * https://github.com/josex2r/jQuery-SlotMachine * Copyright (c) 2014 Jose Luis Represa; Licensed MIT */ -!function(a,b,c,d){function e(c,e){function f(b){for(var c=0,d=0;b>d;d++)c+=a(A.get(d)).outerHeight();return-c+u}function i(){return Math.abs(Math.round(parseInt(t.css("margin-top").replace(/px/,""),10)/A.first().height()))-1}function j(a){var b,c=a||!1;do b=Math.floor(Math.random()*A.length);while(c&&b===F.index&&b>=0);return{index:b,el:A.get(b)}}function k(){var a;if(null!==y.settings.randomize&&"function"==typeof y.settings.randomize){var b=y.settings.randomize(F.index);(0>b||b>=A.length)&&(b=0),a={index:b,el:A.get(b)}}else a=j();return a}function l(){var a=F.index-1<0?A.length-1:F.index-1;return{index:a,el:A.get(a)}}function m(){var a=F.index+10){switch(a){case 1:case 2:n("slow",!0);break;case 3:case 4:n("medium",!0),b/=1.5;break;default:n("fast",!0),b/=2}t.animate({marginTop:v},b,"linear",function(){C===!1&&(t.css("margin-top",0),p(a-1))})}else q()}function q(a){if(t.clearQueue().stop(!0,!1),n("slow",!0),E=!0,!r())return"function"==typeof D&&D(!1,!1),n("stop",!1),o(),void(E=!1);var b=j();"function"==typeof a?b=a():null!==y.settings.randomize&&"function"==typeof y.settings.randomize?b=k():y.settings.auto&&(b=m()),F.index=i(),F.el=A.get(F.index);var c=f(b.index),d=3*y.settings.delay;b.index>F.index?0===F.index&&b.index===A.length-1&&t.css("margin-top",f(A.length)):F.index===A.length-1&&0===b.index&&t.css("margin-top",0),F=b,t.animate({marginTop:c},d,"easeOutBounce",function(){n("stop"),E=!1,"function"==typeof D&&(D(z,F),D=null)}),setTimeout(function(){n("false",!1)},d/2)}function r(){if(y.settings.stopHidden===!1)return!1;var c=z.offset().top>a(b).scrollTop()+a(b).height(),d=a(b).scrollTop()>z.height()+z.offset().top;return!c&&!d}function s(){C===!1&&(B=setTimeout(function(){D=s,C=!1,p(5)},y.settings.auto))}this.element=c,this.settings=a.extend({},h,e),this._defaults=h,this._name=g;var t,u,v,w,x,y=this,z=a(c),A=z.children(),B=null,C=!1,D=null,E=!1,F={index:this.settings.active,el:A.get(this.settings.active)};return z.css("overflow","hidden"),A.wrapAll("
"),t=z.find(".slotMachineContainer"),v=-t.height(),w=A.last().clone(),x=A.first().clone(),t.prepend(w),t.append(x),u=-w.outerHeight(),t.css("margin-top",f(y.settings.active)),y.settings.auto!==!1&&(y.settings.auto===!0?p():s()),{auto:s,shuffle:function(a,b){C=!1,D=b,p("number"==typeof a?a:a!==d?5:d)},stop:function(a){y.settings.auto!==!1&&null!==B&&clearTimeout(B),"number"==typeof a&&a>0?(t.clearQueue().stop(!0,!1),C=!1,p(a)):(C=!0,q())},prev:function(){q(l)},next:function(){q(m)},active:function(){return F},isRunning:function(){return E}}}function f(b,c){var d;return a.data(b,"plugin_"+g)?d=a.data(b,"plugin_"+g):(d=new e(b,c),a.data(b,"plugin_"+g,d)),d}var g="slotMachine",h={active:0,delay:200,auto:!1,randomize:null,stopHidden:!0};a(c).ready(function(){a("filter#slotMachineBlurSVG").length<=0&&a("body").append(''),a("filter#slotMachineBlurSVG").length<=0&&a("body").append(''),a("filter#slotMachineBlurSVG").length<=0&&a("body").append(''),a("mask#slotMachineFadeSVG").length<=0&&a("body").append(''),a("body").append("")}),"function"!=typeof a.easing.easeOutBounce&&a.extend(a.easing,{easeOutBounce:function(a,b,c,d,e){return(b/=e)<1/2.75?7.5625*d*b*b+c:2/2.75>b?d*(7.5625*(b-=1.5/2.75)*b+.75)+c:2.5/2.75>b?d*(7.5625*(b-=2.25/2.75)*b+.9375)+c:d*(7.5625*(b-=2.625/2.75)*b+.984375)+c}}),a.fn[g]=function(b){return 1===this.length?f(this,b):this.each(function(){a.data(this,"plugin_"+g)||f(this,b)})}}(jQuery,window,document); \ No newline at end of file +!function(a,b,c,d){function e(c,e){function f(b){for(var c=0,d=0;b>d;d++)c+=a(B.get(d)).outerHeight();return-c+v}function i(){return Math.abs(Math.round(parseInt(u.css("margin-top").replace(/px/,""),10)/B.first().height()))-1}function j(a){var b,c=a||!1;do b=Math.floor(Math.random()*B.length);while(c&&b===G.index&&b>=0);return{index:b,el:B.get(b)}}function k(a){if("number"==typeof a){var b=function(){return a};z.settings.randomize=b}else z.settings.randomize=a}function l(){var a;if(null!==z.settings.randomize&&"function"==typeof z.settings.randomize){var b=z.settings.randomize(G.index);(0>b||b>=B.length)&&(b=0),a={index:b,el:B.get(b)}}else a=j();return a}function m(){var a=G.index-1<0?B.length-1:G.index-1;return{index:a,el:B.get(a)}}function n(){var a=G.index+10){switch(a){case 1:case 2:o("slow",!0);break;case 3:case 4:o("medium",!0),b/=1.5;break;default:o("fast",!0),b/=2}u.animate({marginTop:w},b,"linear",function(){D===!1&&(u.css("margin-top",0),q(a-1))})}else r()}function r(a){if(u.clearQueue().stop(!0,!1),o("slow",!0),F=!0,!s())return"function"==typeof E&&E(!1,!1),o("stop",!1),p(),void(F=!1);var b=j();"function"==typeof a?b=a():null!==z.settings.randomize&&"function"==typeof z.settings.randomize?b=l():z.settings.auto&&(b=n()),G.index=i(),G.el=B.get(G.index);var c=f(b.index),d=3*z.settings.delay;b.index>G.index?0===G.index&&b.index===B.length-1&&u.css("margin-top",f(B.length)):G.index===B.length-1&&0===b.index&&u.css("margin-top",0),G=b,u.animate({marginTop:c},d,"easeOutBounce",function(){o("stop"),F=!1,"function"==typeof E&&(E(A,G),E=null)}),setTimeout(function(){o("false",!1)},d/2)}function s(){if(z.settings.stopHidden===!1)return!1;var c=A.offset().top>a(b).scrollTop()+a(b).height(),d=a(b).scrollTop()>A.height()+A.offset().top;return!c&&!d}function t(){D===!1&&(C=setTimeout(function(){E=t,D=!1,q(5)},z.settings.auto))}this.element=c,this.settings=a.extend({},h,e),this._defaults=h,this._name=g;var u,v,w,x,y,z=this,A=a(c),B=A.children(),C=null,D=!1,E=null,F=!1,G={index:this.settings.active,el:B.get(this.settings.active)};return A.css("overflow","hidden"),B.wrapAll("
"),u=A.find(".slotMachineContainer"),w=-u.height(),x=B.last().clone(),y=B.first().clone(),u.prepend(x),u.append(y),v=-x.outerHeight(),u.css("margin-top",f(z.settings.active)),z.settings.auto!==!1&&(z.settings.auto===!0?q():t()),{auto:t,shuffle:function(a,b){D=!1,E=b,q("number"==typeof a?a:a!==d?5:d)},stop:function(a){z.settings.auto!==!1&&null!==C&&clearTimeout(C),"number"==typeof a&&a>0?(u.clearQueue().stop(!0,!1),D=!1,q(a)):(D=!0,r())},prev:function(){r(m)},next:function(){r(n)},active:function(){return G},isRunning:function(){return F},setRandomize:k}}function f(b,c){var d;return a.data(b,"plugin_"+g)?d=a.data(b,"plugin_"+g):(d=new e(b,c),a.data(b,"plugin_"+g,d)),d}var g="slotMachine",h={active:0,delay:200,auto:!1,randomize:null,stopHidden:!0};a(c).ready(function(){a("filter#slotMachineBlurSVG").length<=0&&a("body").append(''),a("filter#slotMachineBlurSVG").length<=0&&a("body").append(''),a("filter#slotMachineBlurSVG").length<=0&&a("body").append(''),a("mask#slotMachineFadeSVG").length<=0&&a("body").append(''),a("body").append("")}),"function"!=typeof a.easing.easeOutBounce&&a.extend(a.easing,{easeOutBounce:function(a,b,c,d,e){return(b/=e)<1/2.75?7.5625*d*b*b+c:2/2.75>b?d*(7.5625*(b-=1.5/2.75)*b+.75)+c:2.5/2.75>b?d*(7.5625*(b-=2.25/2.75)*b+.9375)+c:d*(7.5625*(b-=2.625/2.75)*b+.984375)+c}}),a.fn[g]=function(b){return 1===this.length?f(this,b):this.each(function(){a.data(this,"plugin_"+g)||f(this,b)})}}(jQuery,window,document); \ No newline at end of file diff --git a/index.html b/index.html index c16e131..8a7876e 100644 --- a/index.html +++ b/index.html @@ -9,7 +9,7 @@ - + @@ -181,10 +181,9 @@ $(document).ready(function(){ //Fake machine 1 - var machine1 = $("#machineFake1").slotMachine({ + window.machine1 = $("#machineFake1").slotMachine({ active: 1, randomize : function(activeElementIndex){ - //This slot machine always shows the element in position '1' return 1; } }); diff --git a/package.json b/package.json index 78d87c0..2f7b55f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "jquery-slotmachine", - "version": "2.0.1-ignored", + "version": "2.0.2-ignored", "engines": { "node": ">= 0.8.0" }, diff --git a/slotmachine.jquery.json b/slotmachine.jquery.json index 95a3ffe..ba0451b 100644 --- a/slotmachine.jquery.json +++ b/slotmachine.jquery.json @@ -11,7 +11,7 @@ "winning", "machine" ], - "version": "2.0.1", + "version": "2.0.2", "download": "https://github.com/josex2r/jQuery-SlotMachine", "homepage": "https://github.com/josex2r/jQuery-SlotMachine", "demo": "http://josex2r.github.io/jQuery-SlotMachine/", diff --git a/src/jquery.slotmachine.js b/src/jquery.slotmachine.js index 393b3a3..5520daa 100644 --- a/src/jquery.slotmachine.js +++ b/src/jquery.slotmachine.js @@ -160,6 +160,21 @@ }; } + /** + * @desc PUBLIC - Changes randomize function + * @param function|int - Set new randomize function + */ + function _setRandomize(rnd){ + if( typeof rnd==='number' ){ + var _fn = function(){ + return rnd; + }; + self.settings.randomize = _fn; + }else{ + self.settings.randomize = rnd; + } + } + /** * @desc PRIVATE - Get random element based on the custom randomize function * @return object - Element index and HTML node @@ -543,7 +558,13 @@ */ isRunning : function(){ return _isRunning; - } + }, + + /** + * @desc PUBLIC - Changes randomize function + * @param function|int - Set new randomize function + */ + setRandomize : _setRandomize }; }