@@ -105,65 +105,72 @@ export class NgtCanvas {
105105
106106 // NOTE: this signal is updated outside of Zone
107107 protected resizeResult = signal < ResizeResult > ( { } as ResizeResult , { equal : Object . is } ) ;
108+ private configurator = signal < NgtCanvasConfigurator | null > ( null ) ;
108109
109- private configurator ?: NgtCanvasConfigurator ;
110110 private glEnvironmentInjector ?: EnvironmentInjector ;
111111 private glRef ?: ComponentRef < unknown > ;
112112
113113 constructor ( ) {
114+ // NOTE: this means that everything in NgtCanvas will be in afterNextRender.
115+ // this allows the content of NgtCanvas to use effect instead of afterNextRender
114116 afterNextRender ( ( ) => {
117+ const canvasElement = this . glCanvas ( ) . nativeElement ;
115118 this . zone . runOutsideAngular ( ( ) => {
116- this . configurator = this . initRoot ( this . glCanvas ( ) . nativeElement ) ;
117- effect (
118- ( ) => {
119- this . noZoneResizeEffect ( ) ;
120- } ,
121- { injector : this . injector } ,
122- ) ;
119+ this . configurator . set ( this . initRoot ( canvasElement ) ) ;
123120 } ) ;
121+
122+ effect (
123+ ( ) => {
124+ const resizeResult = this . resizeResult ( ) ;
125+ if ( ! resizeResult . width || resizeResult . width <= 0 || ! resizeResult . height || resizeResult . height <= 0 )
126+ return ;
127+
128+ const configurator = this . configurator ( ) ;
129+ if ( ! configurator ) return ;
130+
131+ const canvasOptions = {
132+ gl : this . gl ( ) ,
133+ shadows : this . shadows ( ) ,
134+ legacy : this . legacy ( ) ,
135+ linear : this . linear ( ) ,
136+ flat : this . flat ( ) ,
137+ orthographic : this . orthographic ( ) ,
138+ frameloop : this . frameloop ( ) ,
139+ performance : this . performance ( ) ,
140+ dpr : this . dpr ( ) ,
141+ raycaster : this . raycaster ( ) ,
142+ scene : this . scene ( ) ,
143+ camera : this . camera ( ) ,
144+ events : this . events ( ) ,
145+ eventSource : this . eventSource ( ) ,
146+ eventPrefix : this . eventPrefix ( ) ,
147+ lookAt : this . lookAt ( ) ,
148+ size : resizeResult ,
149+ } ;
150+
151+ this . zone . runOutsideAngular ( ( ) => {
152+ configurator . configure ( canvasOptions ) ;
153+
154+ untracked ( ( ) => {
155+ if ( this . glRef ) {
156+ this . glRef . changeDetectorRef . detectChanges ( ) ;
157+ } else {
158+ this . noZoneRender ( ) ;
159+ }
160+ } ) ;
161+ } ) ;
162+ } ,
163+ { injector : this . injector } ,
164+ ) ;
124165 } ) ;
125166
126167 inject ( DestroyRef ) . onDestroy ( ( ) => {
127168 this . glRef ?. destroy ( ) ;
128169 this . glEnvironmentInjector ?. destroy ( ) ;
129- this . configurator ?. destroy ( ) ;
170+ this . configurator ( ) ?. destroy ( ) ;
130171 } ) ;
131172 }
132173
133- private noZoneResizeEffect ( ) {
134- const resizeResult = this . resizeResult ( ) ;
135- if ( resizeResult . width > 0 && resizeResult . height > 0 ) {
136- if ( ! this . configurator ) this . configurator = this . initRoot ( this . glCanvas ( ) . nativeElement ) ;
137- this . configurator . configure ( {
138- gl : this . gl ( ) ,
139- shadows : this . shadows ( ) ,
140- legacy : this . legacy ( ) ,
141- linear : this . linear ( ) ,
142- flat : this . flat ( ) ,
143- orthographic : this . orthographic ( ) ,
144- frameloop : this . frameloop ( ) ,
145- performance : this . performance ( ) ,
146- dpr : this . dpr ( ) ,
147- raycaster : this . raycaster ( ) ,
148- scene : this . scene ( ) ,
149- camera : this . camera ( ) ,
150- events : this . events ( ) ,
151- eventSource : this . eventSource ( ) ,
152- eventPrefix : this . eventPrefix ( ) ,
153- lookAt : this . lookAt ( ) ,
154- size : resizeResult ,
155- } ) ;
156-
157- untracked ( ( ) => {
158- if ( this . glRef ) {
159- this . glRef . changeDetectorRef . detectChanges ( ) ;
160- } else {
161- this . noZoneRender ( ) ;
162- }
163- } ) ;
164- }
165- }
166-
167174 private noZoneRender ( ) {
168175 // NOTE: destroy previous instances if existed
169176 this . glEnvironmentInjector ?. destroy ( ) ;
0 commit comments