@@ -39,8 +39,8 @@ def _set(self, rect):
3939
4040 def _set_from_fract (self , rect ):
4141 """set rect from fractional representation"""
42- _ , _ , cw , ch = self . _canvas_rect
43- mult = np . array ([ cw , ch , cw , ch ])
42+ rect = np . asarray ( rect , dtype = float ). copy ()
43+ x_offset , y_offset , cw , ch = self . _canvas_rect
4444
4545 # check that widths, heights are valid:
4646 if rect [0 ] + rect [2 ] > 1 :
@@ -54,25 +54,35 @@ def _set_from_fract(self, rect):
5454
5555 # assign values to the arrays, don't just change the reference
5656 self ._rect_frac [:] = rect
57- self ._rect_screen_space [:] = self ._rect_frac * mult
57+ x_px = x_offset + rect [0 ] * cw
58+ y_px = y_offset + rect [1 ] * ch
59+ w_px = rect [2 ] * cw
60+ h_px = rect [3 ] * ch
61+ self ._rect_screen_space [:] = np .array ([x_px , y_px , w_px , h_px ])
5862
5963 def _set_from_screen_space (self , rect ):
6064 """set rect from screen space representation"""
61- _ , _ , cw , ch = self ._canvas_rect
65+ x_offset , y_offset , cw , ch = self ._canvas_rect
6266 mult = np .array ([cw , ch , cw , ch ])
6367 # for screen coords allow (x, y) = 1 or 0, but w, h must be > 1
6468 # check that widths, heights are valid
65- if rect [0 ] + rect [2 ] > cw :
69+
70+ # account for potential x and y offset
71+ rect_offset = rect .copy ()
72+ rect_offset [0 ] -= x_offset
73+ rect_offset [1 ] -= y_offset
74+
75+ if rect_offset [0 ] + rect_offset [2 ] > cw :
6676 raise ValueError (
67- f"invalid rect: { rect } \n x + width > canvas width: { rect [0 ]} + { rect [2 ]} > { cw } "
77+ f"invalid rect: { rect } \n x + width > canvas width: { rect_offset [0 ]} + { rect_offset [2 ]} > { cw } "
6878 )
69- if rect [1 ] + rect [3 ] > ch :
79+ if rect_offset [1 ] + rect_offset [3 ] > ch :
7080 raise ValueError (
71- f"invalid rect: { rect } \n y + height > canvas height: { rect [1 ]} + { rect [3 ]} >{ ch } "
81+ f"invalid rect: { rect } \n y + height > canvas height: { rect_offset [1 ]} + { rect_offset [3 ]} >{ ch } "
7282 )
7383
74- self ._rect_frac [:] = rect / mult
75- self ._rect_screen_space [:] = rect
84+ self ._rect_frac [:] = rect_offset / mult
85+ self ._rect_screen_space [:] = rect_offset
7686
7787 @property
7888 def x (self ) -> np .float64 :
0 commit comments