11class Scene {
2- static ruler = 20
2+ static range = 10
33 constructor ( ctx , width , height ) {
44 this . ctx = ctx
55 this . width = width
@@ -9,20 +9,44 @@ class Scene {
99 }
1010 init ( ) {
1111 this . tiles = [ ]
12+ this . cd = 50
13+ this . tiles . push ( new Tile ( this , 3 ) )
14+ this . tiles . push ( new Tile ( this , 1 ) )
15+ this . tiles . push ( new Tile ( this , 2 ) )
16+ this . tiles . push ( new Tile ( this , 5 ) )
17+ this . tiles . push ( new Tile ( this , 4 ) )
18+ this . mark = new Date ( )
1219 this . update ( )
1320 }
21+ canChange ( ) {
22+ return Date . now ( ) - this . mark >= this . cd
23+ }
1424 sort ( ) {
1525 this . tiles . sort ( ( a , b ) => b . value - a . value )
1626 }
1727 add ( tile ) {
1828 this . tiles . push ( tile )
1929 }
30+ updateRuler ( ) {
31+ let maxObj = this . tiles [ 0 ]
32+ if ( ! maxObj ) return
33+ let max = maxObj . value
34+ let dv = 10 ** ( ( ~ ~ max ) . toString ( ) . length ) * 0.2
35+ let newRng = Math . ceil ( max / dv ) * dv
36+ if ( Scene . range !== newRng ) {
37+ this . filling = true
38+ }
39+ Scene . range = newRng
40+ }
2041 update ( ) {
2142 this . ctx . clearRect ( 0 , 0 , this . width , this . height )
2243 this . sort ( )
44+ this . updateRuler ( )
2345 this . tiles . slice ( 0 , this . count ) . forEach ( ( e , i ) => {
46+ this . filling && ( e . filling = true )
2447 e . update ( i )
2548 } )
49+ this . filling = false
2650 requestAnimationFrame ( this . update . bind ( this ) )
2751 }
2852}
@@ -42,19 +66,25 @@ class Tile {
4266 this . width = 0
4367 this . currVal = 0
4468 this . filled = false
45- this . W = this . value * width / Scene . ruler
69+ this . W = this . value * width / Scene . range
4670 this . posX = 0
4771 this . posY = this . order * Tile . TOTAL_HEIGHT
4872 this . segment = 50
4973 this . dw = this . W / this . segment
5074 this . dv = this . value / this . segment
5175 this . dy = 0
76+ this . index = 0
77+ this . filling = true
78+ }
79+ getWidth ( ) {
80+ let { width } = this . scene
81+ return this . value * width / Scene . range
5282 }
5383 render ( ) {
5484 let { ctx } = this . scene
5585 ctx . fillRect ( this . posX , this . posY , this . width , this . height )
5686 ctx . font = '16px 黑体'
57- ctx . fillText ( this . currVal , this . width + 16 , this . posY + ( 14 + Tile . BAR_HEIGHT ) / 2 )
87+ ctx . fillText ( this . value , this . width + 16 , this . posY + ( 14 + Tile . BAR_HEIGHT ) / 2 )
5888 }
5989 exchange ( i ) {
6090 let Y = i * Tile . TOTAL_HEIGHT
@@ -66,19 +96,22 @@ class Tile {
6696 if ( this . dy > 0 ) this . posY = Math . min ( this . posY , Y )
6797 if ( this . dy < 0 ) this . posY = Math . max ( this . posY , Y )
6898 }
69- filling ( ) {
70- if ( ! this . filled ) {
71- this . width + = this . dw
72- this . currVal += this . dv
73- this . currVal = + this . currVal . toFixed ( 2 )
99+ stretch ( ) {
100+ if ( this . index === 0 ) {
101+ let W = this . getWidth ( )
102+ this . dw = ( W - this . width ) / this . segment
103+ this . index = 0
74104 }
75- if ( this . width >= this . W ) {
76- this . filled = true
105+ if ( this . index ++ === this . segment ) {
106+ this . filling = false
107+ this . index = 0
108+ return
77109 }
110+ this . width += this . dw
78111 }
79112 update ( i ) {
80- this . filling ( )
81113 this . exchange ( i )
114+ this . filling && this . stretch ( )
82115 this . render ( )
83116 }
84117}
0 commit comments