1
+ import numpy as np
1
2
from pygfx import Scene , OrthographicCamera , PerspectiveCamera , PanZoomController , OrbitController , \
2
3
Viewport , WgpuRenderer
3
4
from wgpu .gui .auto import WgpuCanvas
@@ -37,6 +38,13 @@ def __init__(
37
38
self .camera
38
39
)
39
40
41
+ # camera.far and camera.near clipping planes get
42
+ # wonky with setting controller.distance = 0
43
+ if isinstance (self .camera , OrthographicCamera ):
44
+ self .controller .distance = 0
45
+ # also set a initial zoom
46
+ self .controller .zoom (0.8 / self .controller .zoom_value )
47
+
40
48
self .renderer .add_event_handler (self .set_viewport_rect , "resize" )
41
49
42
50
self ._graphics : List [Graphic ] = list ()
@@ -135,6 +143,15 @@ def center_graphic(self, graphic, zoom: float = 1.3):
135
143
self .controller .zoom (zoom )
136
144
137
145
def center_scene (self , zoom : float = 1.3 ):
146
+ """
147
+ Auto-center the scene, does not scale.
148
+
149
+ Parameters
150
+ ----------
151
+ zoom: float, default 1.3
152
+ apply a zoom after centering the scene
153
+
154
+ """
138
155
if not len (self .scene .children ) > 0 :
139
156
return
140
157
@@ -148,6 +165,32 @@ def center_scene(self, zoom: float = 1.3):
148
165
149
166
self .controller .zoom (zoom )
150
167
168
+ def auto_scale (self , maintain_aspect : bool = False , zoom : float = 0.8 ):
169
+ """
170
+ Auto-scale the camera w.r.t to the scene
171
+
172
+ Parameters
173
+ ----------
174
+ maintain_aspect: bool, default ``False``
175
+ maintain the camera aspect ratio for all dimensions, if ``False`` the camera
176
+ is scaled according to the bounds in each dimension.
177
+
178
+ zoom: float, default 0.8
179
+ zoom value for the camera after auto-scaling, if zoom = 1.0 then the graphics
180
+ in the scene will fill the entire canvas.
181
+ """
182
+ self .center_scene ()
183
+ self .camera .maintain_aspect = maintain_aspect
184
+
185
+ width , height , depth = np .ptp (self .scene .get_world_bounding_box (), axis = 0 )
186
+
187
+ self .camera .width = width
188
+ self .camera .height = height
189
+
190
+ # self.controller.distance = 0
191
+
192
+ self .controller .zoom (zoom / self .controller .zoom_value )
193
+
151
194
def get_graphics (self ):
152
195
return self ._graphics
153
196
0 commit comments