@@ -14,6 +14,7 @@ class ViewController extends Component {
1414
1515 this . renderer = vtkRenderer . newInstance ( ) ;
1616 this . viewRef = React . createRef ( ) ;
17+ this . resizeObserver = new ResizeObserver ( ( ) => this . onResize ( ) ) ;
1718
1819 if ( props . root ) {
1920 this . renderWindow = props . root . renderWindow ;
@@ -41,15 +42,21 @@ class ViewController extends Component {
4142 this . renderWindow . addRenderer ( this . renderer ) ;
4243
4344 const view = this . viewRef . current ;
44- const container = view . containerRef . current ;
45- container . addEventListener ( 'pointerenter' , this . onEnter ) ;
46-
47- if ( ! this . props . root ) {
48- this . openglRenderWindow . setContainer ( container ) ;
49- if ( this . props . interactive ) {
50- this . interactor . bindEvents ( container ) ;
45+ const container = view ?. containerRef . current ;
46+ if ( view && container ) {
47+ container . addEventListener ( 'pointerenter' , this . onEnter ) ;
48+ if ( this . props . root ) {
49+ this . props . root . observeRendererResize ( container , this . renderer ) ;
50+ } else {
51+ this . resizeObserver . observe ( container ) ;
52+ this . openglRenderWindow . setContainer ( container ) ;
53+ if ( this . props . interactive ) {
54+ this . interactor . bindEvents ( container ) ;
55+ }
56+ this . interactor . setInteractorStyle ( view . style ) ;
57+ // initial resize
58+ this . onResize ( ) ;
5159 }
52- this . interactor . setInteractorStyle ( view . style ) ;
5360 }
5461 }
5562
@@ -58,6 +65,11 @@ class ViewController extends Component {
5865 const container = view . containerRef . current ;
5966 container . removeEventListener ( 'pointerenter' , this . onEnter ) ;
6067
68+ this . resizeObserver . disconnect ( ) ;
69+ if ( this . props . root ) {
70+ this . props . root . unobserveRendererResize ( container ) ;
71+ }
72+
6173 // MultiViewRoot parent may delete the render window first in WillUnmount.
6274 if ( ! this . renderWindow . isDeleted ( ) ) {
6375 this . renderWindow . removeRenderer ( this . renderer ) ;
@@ -99,7 +111,6 @@ class ViewController extends Component {
99111 renderer = { this . renderer }
100112 interactor = { this . interactor }
101113 ref = { this . viewRef }
102- onResize = { this . onResize }
103114 { ...filteredProps }
104115 />
105116 ) ;
@@ -129,31 +140,13 @@ class ViewController extends Component {
129140
130141 onResize ( ) {
131142 const container = this . viewRef . current ?. containerRef . current ;
132- if ( container ) {
133- if ( this . props . root ) {
134- const containerBox = container . getBoundingClientRect ( ) ;
135- const canvasBox = this . openglRenderWindow
136- . getCanvas ( )
137- . getBoundingClientRect ( ) ;
138-
139- // relative to canvas
140- const top = containerBox . top - canvasBox . top ;
141- const left = containerBox . left - canvasBox . left ;
142-
143- const xmin = left / canvasBox . width ;
144- const xmax = ( left + containerBox . width ) / canvasBox . width ;
145- const ymin = 1 - ( top + containerBox . height ) / canvasBox . height ;
146- const ymax = 1 - top / canvasBox . height ;
147-
148- this . renderer . setViewport ( xmin , ymin , xmax , ymax ) ;
149- } else {
150- const devicePixelRatio = window . devicePixelRatio || 1 ;
151- const { width, height } = container . getBoundingClientRect ( ) ;
152- const w = Math . floor ( width * devicePixelRatio ) ;
153- const h = Math . floor ( height * devicePixelRatio ) ;
154- this . openglRenderWindow . setSize ( Math . max ( w , 10 ) , Math . max ( h , 10 ) ) ;
155- this . renderWindow . render ( ) ;
156- }
143+ if ( container && ! this . props . root ) {
144+ const devicePixelRatio = window . devicePixelRatio || 1 ;
145+ const { width, height } = container . getBoundingClientRect ( ) ;
146+ const w = Math . floor ( width * devicePixelRatio ) ;
147+ const h = Math . floor ( height * devicePixelRatio ) ;
148+ this . openglRenderWindow . setSize ( Math . max ( w , 10 ) , Math . max ( h , 10 ) ) ;
149+ this . renderWindow . render ( ) ;
157150 }
158151 }
159152}
0 commit comments