@@ -36,7 +36,7 @@ class EyeMode {
3636 topAndLatSizes ( area_width , area_height , options ) {
3737 let top_size , lat_size ;
3838
39- if ( options . is_vertical ) {
39+ if ( options . is_vertical ) {
4040 top_size = area_width ;
4141 lat_size = area_height ;
4242 } else {
@@ -46,41 +46,45 @@ class EyeMode {
4646
4747 return [ top_size , lat_size ] ;
4848 }
49+
50+ /** Add a ransparent background to avoid blemishes from previous drawings. */
51+ clearArea ( cr , area_width , area_height ) {
52+ cr . save ( ) ;
53+ cr . setSourceRGBA ( 0 , 0 , 0 , 0 ) ;
54+ cr . rectangle ( 0 , 0 , area_width , area_height ) ;
55+ cr . fill ( ) ;
56+ cr . restore ( ) ;
57+ }
4958}
5059
5160class EyelidMode extends EyeMode {
5261 drawEye ( area , options ) {
53- let [ area_width , area_height ] = area . get_surface_size ( ) ;
54- let [ mouse_x , mouse_y ] = [ options . mouse_x , options . mouse_y ] ;
55- let [ area_x , area_y ] = [ options . area_x , options . area_y ] ;
56-
57- area_x += area_width / 2 ;
58- area_y += area_height / 2 ;
59-
60- mouse_x -= area_x ;
61- mouse_y -= area_y ;
62+ const [ area_width , area_height ] = area . allocation . get_size ( ) ;
63+ const mouse_x = options . mouse_x - options . area_x - area_width / 2 ;
64+ const mouse_y = options . mouse_y - options . area_y - area_height / 2 ;
6265
6366 const mouse_ang = Math . atan2 ( mouse_y , mouse_x ) ;
6467 let mouse_rad = Math . sqrt ( mouse_x * mouse_x + mouse_y * mouse_y ) ;
6568
66- let [ top_size , lat_size ] = this . topAndLatSizes ( area_width , area_height , options ) ;
67- let eye_rad = ( top_size - options . padding ) / 2 ;
68- if ( 2 * eye_rad > lat_size ) eye_rad = lat_size / 2 ;
69+ const [ top_size , lat_size ] = this . topAndLatSizes ( area_width , area_height , options ) ;
70+ let eye_rad = Math . min ( top_size - options . padding , lat_size ) / 2 ;
6971
7072 const iris_rad = eye_rad * 0.5 ;
7173 const pupil_rad = iris_rad * 0.4 ;
7274
7375 const max_rad = eye_rad * ( Math . pow ( Math . cos ( mouse_ang ) , 4 ) * 0.5 + 0.25 ) ;
7476
75- if ( mouse_rad > max_rad )
76- mouse_rad = max_rad ;
77+ mouse_rad = Math . min ( mouse_rad , max_rad ) ;
7778
7879 const iris_arc = Math . asin ( iris_rad / eye_rad ) ;
7980 const iris_r = eye_rad * Math . cos ( iris_arc ) ;
8081
8182 const eye_ang = Math . atan ( mouse_rad / iris_r ) ;
8283
8384 let cr = area . get_context ( ) ;
85+ this . clearArea ( cr , area_width , area_height ) ;
86+
87+ cr . save ( ) ;
8488
8589 // -- Drawing the base of the eye
8690
@@ -91,12 +95,12 @@ class EyelidMode extends EyeMode {
9195
9296 const x_def = iris_rad * Math . cos ( mouse_ang ) * ( Math . sin ( eye_ang ) ) ;
9397 const y_def = iris_rad * Math . sin ( mouse_ang ) * ( Math . sin ( eye_ang ) ) ;
94- let amp ;
9598
9699 const top_lid = 0.8 ;
97100 const bottom_lid = 0.6 ;
98101
99- amp = eye_rad * top_lid ;
102+ let amp = eye_rad * top_lid ;
103+
100104 cr . moveTo ( - eye_rad , 0 ) ;
101105 cr . curveTo ( x_def - iris_rad , y_def + amp ,
102106 x_def + iris_rad , y_def + amp , eye_rad , 0 ) ;
@@ -142,45 +146,39 @@ class EyelidMode extends EyeMode {
142146 cr . arc ( 0 , 0 , 1.0 , 0 , 2 * Math . PI ) ;
143147 cr . fill ( ) ;
144148
145- cr . save ( ) ;
146149 cr . restore ( ) ;
147- cr . $dispose ( ) ;
148150 }
149151}
150152
151153
152154class BulbMode extends EyeMode {
153155 drawEye ( area , options ) {
154- let [ area_width , area_height ] = area . get_surface_size ( ) ;
155- let [ mouse_x , mouse_y ] = [ options . mouse_x , options . mouse_y ] ;
156- let [ area_x , area_y ] = [ options . area_x , options . area_y ] ;
157-
158- area_x += area_width / 2 ;
159- area_y += area_height / 2 ;
160-
161- mouse_x -= area_x ;
162- mouse_y -= area_y ;
156+ const [ area_width , area_height ] = area . allocation . get_size ( ) ;
157+ const mouse_x = options . mouse_x - options . area_x - area_width / 2 ;
158+ const mouse_y = options . mouse_y - options . area_y - area_height / 2 ;
163159
164160 let mouse_rad = Math . sqrt ( mouse_x * mouse_x + mouse_y * mouse_y ) ;
165161 const mouse_ang = Math . atan2 ( mouse_y , mouse_x ) ;
166162
167- let [ top_size , lat_size ] = this . topAndLatSizes ( area_width , area_height , options ) ;
168- let eye_rad = ( top_size - options . padding ) / 2 ;
169- if ( 2 * eye_rad > lat_size ) eye_rad = lat_size / 2.1 ;
163+ const [ top_size , lat_size ] = this . topAndLatSizes ( area_width , area_height , options ) ;
164+ let eye_rad = Math . min ( top_size - options . padding , lat_size ) / 2 ;
170165
171166 const iris_rad = eye_rad * 0.6 ;
172167 const pupil_rad = iris_rad * 0.4 ;
173168
174169 const max_rad = eye_rad * Math . cos ( Math . asin ( ( iris_rad ) / eye_rad ) ) - options . line_width ;
175170
176- if ( mouse_rad > max_rad ) mouse_rad = max_rad ;
171+ mouse_rad = Math . min ( mouse_rad , max_rad ) ;
177172
178173 const iris_arc = Math . asin ( iris_rad / eye_rad ) ;
179174 const iris_r = eye_rad * Math . cos ( iris_arc ) ;
180175
181176 const eye_ang = Math . atan ( mouse_rad / iris_r ) ;
182177
183- const cr = area . get_context ( ) ;
178+ let cr = area . get_context ( ) ;
179+ this . clearArea ( cr , area_width , area_height ) ;
180+
181+ cr . save ( ) ;
184182
185183 // -- Drawing the base of the eye
186184
@@ -217,9 +215,7 @@ class BulbMode extends EyeMode {
217215 cr . arc ( 0 , 0 , 1.0 , 0 , 2 * Math . PI ) ;
218216 cr . fill ( ) ;
219217
220- cr . save ( ) ;
221218 cr . restore ( ) ;
222- cr . $dispose ( ) ;
223219 }
224220}
225221
0 commit comments