@@ -77,10 +77,12 @@ class Canvas:
7777
7878 device : Device
7979 depth_format : TextureFormat
80- depth_texture : Texture
81- multisample_texture : Texture
82- multisample : MultisampleState
83- target_texture : Texture
80+ depth_texture : Texture = None
81+ multisample_texture : Texture = None
82+ multisample : MultisampleState = None
83+ target_texture : Texture = None
84+ select_depth_texture : Texture = None
85+ select_texture : Texture = None
8486
8587 width : int = 0
8688 height : int = 0
@@ -142,6 +144,14 @@ def update_html_canvas(self, html_canvas):
142144 self .context .unconfigure ()
143145
144146 self .canvas = html_canvas
147+ self .destroy_textures ()
148+
149+ if html_canvas is None :
150+ self .context = None
151+ for func in self ._on_update_html_canvas :
152+ func (html_canvas )
153+ return
154+
145155 self .context = html_canvas .getContext ("webgpu" )
146156 self .context .configure (
147157 {
@@ -207,10 +217,24 @@ def save_screenshot(self, filename: str):
207217 canvas .remove ()
208218 path .write_bytes (b64decode (canvas .toDataURL (format ).split ("," )[1 ]))
209219
220+ def destroy_textures (self ):
221+ with self ._update_mutex :
222+ for tex in [
223+ self .target_texture ,
224+ self .multisample_texture ,
225+ self .depth_texture ,
226+ self .select_texture ,
227+ self .select_depth_texture ,
228+ ]:
229+ if tex is not None :
230+ tex .destroy ()
231+
210232 @debounce (5 )
211233 def resize (self ):
212234 with self ._update_mutex :
213235 canvas = self .canvas
236+ if canvas is None :
237+ return
214238 rect = canvas .getBoundingClientRect ()
215239 width = int (rect .width )
216240 height = int (rect .height )
@@ -229,6 +253,9 @@ def resize(self):
229253 canvas .height = height
230254
231255 device = self .device
256+
257+ self .destroy_textures ()
258+
232259 self .target_texture = device .createTexture (
233260 size = [width , height , 1 ],
234261 sampleCount = 1 ,
0 commit comments