1
+ /**
2
+ * Created by ajax on 2015/12/4.
3
+ */
4
+
5
+ ( function ( ) {
6
+ function Rect ( index , opt ) {
7
+ this . index = index ;
8
+ this . min = 20 ; // 20px
9
+ this . step = 8 ;
10
+ this . h = 10 ;
11
+ this . w = this . min + this . step * this . index ;
12
+ this . div = null ;
13
+ this . from = null ;
14
+ this . tower = null ;
15
+ this . speed = opt . speed ;
16
+ this . draw ( ) ;
17
+ }
18
+ Rect . prototype = {
19
+ draw : function ( ) {
20
+ this . div = $ ( document . createElement ( "div" ) ) ;
21
+ //var opacity = (0.5 + (this.index % 10) / 10).toFixed(1);
22
+ var opacity = 0.3 + Math . random ( ) * 0.7 ;
23
+ this . div . css ( {
24
+ position : "absolute" ,
25
+ backgroundColor : "#000" ,
26
+ opacity : opacity ,
27
+ width : this . w + "px" ,
28
+ height : this . h + 'px' ,
29
+ left : "100px" ,
30
+ top : 100 + this . index * 10 + "px" ,
31
+ borderRadius : "2px" ,
32
+ marginLeft : - ( this . min + this . step * this . index ) / 2 + "px" ,
33
+ borderBottom : "1px solid #ccc"
34
+ } ) ;
35
+ $ ( document . body ) . append ( this . div ) ;
36
+ } ,
37
+ moveTo : function ( fn ) {
38
+ var tower = this . tower ;
39
+ return tower . pushRect ( this , fn ) ;
40
+ }
41
+ } ;
42
+
43
+ function Tower ( index ) {
44
+ this . index = index ;
45
+ this . h = 100 ;
46
+ this . w = 50 ;
47
+ this . top = 200 ;
48
+ this . left = 200 ;
49
+ this . div = null ;
50
+ this . origin = {
51
+ x : this . left + this . index * this . h + this . h ,
52
+ y : this . top + this . h
53
+ } ;
54
+ this . rects = [ ] ; // 该塔拥有的矩形
55
+ this . draw ( ) ; // draw self
56
+ }
57
+ Tower . prototype = {
58
+ draw : function ( ) {
59
+ var div = document . createElement ( "div" ) ;
60
+ var div2 = document . createElement ( "div" ) ;
61
+ div = $ ( div ) ;
62
+ div2 = $ ( div2 ) ;
63
+ div . css ( {
64
+ borderLeft : "1px solid #aaa" ,
65
+ height : this . h + "px" ,
66
+ width : 0 ,
67
+ position : "absolute" ,
68
+ //left : this.left + this.index * this.h + this.h + "px",
69
+ left : this . origin . x + "px" ,
70
+ top : this . origin . y - this . h + "px"
71
+ } ) ;
72
+ div2 . css ( {
73
+ position : "absolute" ,
74
+ width : this . w + "px" ,
75
+ height : 0 ,
76
+ borderBottom : "1px solid #aaa" ,
77
+ bottom : 0 ,
78
+ left : ( - this . w / 2 ) + 'px'
79
+ } ) ;
80
+ div . append ( div2 ) ;
81
+ this . div = div ;
82
+ $ ( document . body ) . append ( div ) ;
83
+ } ,
84
+ pushRect : function ( rect , fn ) { // bool
85
+ var canPush = false ;
86
+ if ( this . rects . length === 0 ) {
87
+ canPush = true ;
88
+ } else {
89
+ var top = this . rects [ this . rects . length - 1 ] ;
90
+ if ( top . index > rect . index ) {
91
+ canPush = true ;
92
+ }
93
+ }
94
+ if ( canPush ) {
95
+ if ( rect . from ) {
96
+ rect . from . rects = rect . from . rects . filter ( function ( item ) {
97
+ return item . index !== rect . index ;
98
+ } ) ;
99
+ }
100
+ this . rects . push ( rect ) ;
101
+ rect . from = this ;
102
+ var index = this . rects . length ;
103
+ rect . div . animate ( {
104
+ top : this . origin . y - rect . h * index + "px" ,
105
+ left : this . origin . x + "px"
106
+ } , rect . speed , fn ) ;
107
+ }
108
+ return canPush ;
109
+ }
110
+ } ;
111
+
112
+ function main ( n , opt ) {
113
+ var towerNum = 3 ,
114
+ towers = [ ] ,
115
+ rects = [ ] ,
116
+ i = 0 ;
117
+ for ( i = 0 ; i < towerNum ; i ++ ) {
118
+ towers . push ( new Tower ( i ) ) ;
119
+ }
120
+ for ( i = 0 ; i < n ; i ++ ) {
121
+ rects . push ( new Rect ( i , {
122
+ speed : parseInt ( opt . speed )
123
+ } ) ) ;
124
+ }
125
+ // for (i = rects.length - 1; i >=0; i--) {
126
+ // rects[i].tower = towers[0];
127
+ // rects[i].moveTo();
128
+ // }
129
+ var index = rects . length - 1 ;
130
+ Q ( index , towers [ 0 ] ) ;
131
+ function Q ( i , to ) {
132
+ if ( i < 0 ) {
133
+ start ( ) ;
134
+ return ;
135
+ }
136
+ move ( i , to , function ( ) {
137
+ i = i - 1 ;
138
+ Q ( i , towers [ 0 ] ) ;
139
+ } ) ;
140
+ }
141
+ function move ( i , to , fn ) {
142
+ rects [ i ] . tower = to ;
143
+ rects [ i ] . moveTo ( fn )
144
+ }
145
+ function start ( ) {
146
+ var steps = [ ] ;
147
+ h ( n - 1 , towers [ 0 ] , towers [ 1 ] , towers [ 2 ] , function ( prop ) {
148
+ steps . push ( prop ) ;
149
+ } ) ;
150
+ var i = 0 ,
151
+ len = steps . length ;
152
+ console . log ( steps ) ;
153
+ loop ( i ) ;
154
+
155
+ function loop ( i ) {
156
+ if ( i >= len ) return ;
157
+ move ( steps [ i ] . index , steps [ i ] . to , function ( ) {
158
+ i = i + 1 ;
159
+ loop ( i ) ;
160
+ } ) ;
161
+ }
162
+ }
163
+
164
+ function h ( n , a , b , c , fn ) {
165
+ if ( n === 0 ) {
166
+ fn ( {
167
+ index : n ,
168
+ to : c
169
+ } ) ;
170
+ } else {
171
+ h ( n - 1 , a , c , b , fn ) ;
172
+ fn ( {
173
+ index : n ,
174
+ to : c
175
+ } ) ;
176
+ h ( n - 1 , b , a , c , fn ) ;
177
+ }
178
+ }
179
+
180
+ }
181
+
182
+ var isRunning = false ;
183
+ $ ( "#btn" ) . on ( "click" , function ( ) {
184
+ if ( isRunning ) return ;
185
+ isRunning = true ;
186
+ var speed = $ ( "#speed" ) . val ( ) ;
187
+ var val = $ ( "#input" ) . val ( ) ;
188
+ main ( val , {
189
+ speed : speed
190
+ } ) ;
191
+
192
+ } ) ;
193
+ } ) ( ) ;
0 commit comments