@@ -43,10 +43,12 @@ function debounce( fn, delay )
4343
4444function DiagonalSlideshow ( el , options )
4545{
46- var holder , diags , W , H , wl , hl , index , timer = null , i , j , x , y , bx , by , w , margin , margin2 , offset ;
46+ var self = this , holder , diags = null , W , H , wl , hl , index , timer = null , i , j , k , x , y , bx , by , w , side , margin , margin2 , offset ;
47+
48+ if ( ! ( self instanceof DiagonalSlideshow ) ) return new DiagonalSlideshow ( el , options ) ;
4749
4850var endTransition = function ( ) {
49- if ( null != timer ) clearTimeout ( timer ) ;
51+ clearTimeout ( timer ) ;
5052holder . find ( '.diag-remove' ) . remove ( ) ;
5153index = ( index + 1 ) % options . images . length ;
5254var nextimg = new Image ( ) ;
@@ -58,18 +60,16 @@ function DiagonalSlideshow( el, options )
5860} ;
5961
6062var doTransition = function ( ind ) {
61- var p = cutimage ( options . images [ ind ] , diags , W , H ) , i , ngroups , d , sd , animateoptions , last , max ;
63+ var p = cutimage ( options . images [ ind ] , diags , W , H ) , i , ngroups , d , sd , animateoptions ;
6264
6365p = shuffle ( p ) ;
6466ngroups = p . length ;
6567d = 1000 * options . duration / ( ngroups - ( ngroups - 1 ) * options . overlap ) ;
6668sd = d * ( 1 - options . overlap ) ;
67- //last = null; max = -1;
6869for ( i = 0 ; i < ngroups ; i ++ )
6970{
7071p [ i ] . prev ( ) . addClass ( "diag-remove" ) ;
7172p [ i ] . css ( { opacity :0 } ) ;
72- //if ( i*sd >= max ) { last = i; max = i*sd; }
7373}
7474for ( i = 0 ; i < ngroups ; i ++ )
7575{
@@ -82,11 +82,37 @@ function DiagonalSlideshow( el, options )
8282var autoResize = function ( ) {
8383W = stdMath . round ( el . width ( ) ) ; H = stdMath . round ( W / options . aspectRatio ) ;
8484holder . css ( { width :String ( W ) + 'px' , height :String ( H ) + 'px' } ) ;
85+ if ( ! diags )
86+ {
87+ wl = 2 * stdMath . ceil ( 1 / options . size ) + 2 ;
88+ hl = stdMath . ceil ( 1 / ( options . size * options . aspectRatio ) ) + 1 ;
89+ diags = new Array ( wl * hl ) ;
90+ }
91+ w = stdMath . max ( 2 * W / ( wl - 2 ) , H / ( hl - 1 ) ) ; side = w / sqrt2 ;
92+ margin = w / 2 ; margin2 = margin / 2 ; offset = 0 ;
93+ for ( k = 0 , i = 0 ; i < wl ; i ++ )
94+ {
95+ for ( j = 0 ; j < hl ; j ++ , k ++ )
96+ {
97+ x = margin * i - margin ; y = w * j - margin + offset ;
98+ bx = - x + margin2 ; by = - y + margin2 ;
99+ diags [ k ] = {
100+ i :i , j :j , x :x , y :y , imgx :bx , imgy :by , side :side , margin :margin , rows :wl , columns :hl ,
101+ div :( diags [ k ] ?diags [ k ] . div :$ ( '<div class="diag" data-coords="' + i + ' ' + j + '"></div>' ) . appendTo ( holder ) ) . css ( {
102+ left :String ( x ) + "px" ,
103+ top :String ( y ) + "px" ,
104+ width :String ( side ) + "px" ,
105+ height :String ( side ) + "px"
106+ } )
107+ } ;
108+ }
109+ offset = 0 === offset ?- margin :0 ;
110+ }
85111} ;
86112
87113options = $ . extend ( {
88114aspectRatio :1.0 ,
89- side : 200 ,
115+ size : 0.3 ,
90116easing :'linear' ,
91117duration :3 ,
92118delay :5 ,
@@ -100,29 +126,6 @@ function DiagonalSlideshow( el, options )
100126$ ( window ) . on ( 'resize' , debounce ( autoResize , 300 ) ) ;
101127autoResize ( ) ;
102128
103- w = options . side * sqrt2 ; wl = 2 * stdMath . ceil ( W / w ) + 1 ; hl = 2 * stdMath . ceil ( H / w ) + 1 ;
104- margin = w / 2 ; margin2 = margin / 2 ; offset = 0 ;
105-
106- diags = [ ] ;
107- for ( i = 0 ; i < wl ; i ++ )
108- {
109- for ( j = 0 ; j < hl ; j ++ )
110- {
111- x = margin * i - margin ; y = w * j - margin + offset ;
112- bx = - x + margin2 ; by = - y + margin2 ;
113- diags . push ( {
114- i :i , j :j , x :x , y :y , imgx :bx , imgy :by , side :options . side , margin :margin , rows :wl , columns :hl ,
115- div :$ ( '<div class="diag" data-coords="' + i + ' ' + j + '"></div>' ) . css ( {
116- left :String ( x ) + "px" ,
117- top :String ( y ) + "px" ,
118- width :String ( options . side ) + "px" ,
119- height :String ( options . side ) + "px"
120- } ) . appendTo ( holder )
121- } ) ;
122- }
123- offset = 0 === offset ?- margin :0 ;
124- }
125-
126129// init
127130if ( 0 < options . images . length )
128131{
@@ -141,7 +144,7 @@ $.fn.diagonalSlideshow = function( options ) {
141144$ ( this ) . each ( function ( ) {
142145var $el = $ ( this ) ;
143146if ( ! $el . data ( 'diagonal-slideshow' ) )
144- $el . data ( 'diagonal-slideshow' , new DiagonalSlideshow ( $el , options ) ) ;
147+ $el . data ( 'diagonal-slideshow' , new $ . DiagonalSlideshow ( $el , options ) ) ;
145148} ) ;
146149return this ; // chainable
147150} ;