@@ -14,6 +14,10 @@ function vtkTextActor(publicAPI, model) {
1414 // Set our className
1515 model . classHierarchy . push ( 'vtkTextActor' ) ;
1616
17+ const superDelete = publicAPI . delete ;
18+ const superGetProperty = publicAPI . getProperty ;
19+ const superSetProperty = publicAPI . setProperty ;
20+
1721 publicAPI . makeProperty = vtkTextProperty . newInstance ;
1822
1923 const texture = vtkTexture . newInstance ( {
@@ -38,7 +42,7 @@ function vtkTextActor(publicAPI, model) {
3842 const backgroundColor = publicAPI . getProperty ( ) . getBackgroundColor ( ) ;
3943
4044 const dpr = Math . max ( window . devicePixelRatio || 1 , 1 ) ;
41- const ctx = canvas . getContext ( '2d' ) ;
45+ const ctx = canvas . getContext ( '2d' , { willReadFrequently : true } ) ;
4246
4347 // Set the text properties to measure
4448 const textSize = fontSizeScale ( resolution ) * dpr ;
@@ -110,21 +114,66 @@ function vtkTextActor(publicAPI, model) {
110114 return ImageHelper . canvasToImageData ( canvas ) ;
111115 }
112116
117+ function updateTexture ( ) {
118+ const image = createImageData ( model . input ) ;
119+ texture . setInputData ( image , 0 ) ;
120+ }
121+
122+ function bindProperty ( property ) {
123+ if ( model . propertySubscription ) {
124+ model . propertySubscription . unsubscribe ( ) ;
125+ model . propertySubscription = null ;
126+ }
127+
128+ if ( property ) {
129+ model . propertySubscription = property . onModified ( ( ) => {
130+ if ( model . input !== undefined ) {
131+ updateTexture ( ) ;
132+ }
133+ } ) ;
134+ }
135+ }
136+
113137 mapper . setInputConnection ( plane . getOutputPort ( ) ) ;
114138
115139 publicAPI . setMapper ( mapper ) ;
116140 publicAPI . addTexture ( texture ) ;
117141
118- model . _onInputChanged = ( _publicAPI , _model , value ) => {
119- const image = createImageData ( value ) ;
120- texture . setInputData ( image , 0 ) ;
142+ publicAPI . getProperty = ( ) => {
143+ const property = superGetProperty ( ) ;
144+ if ( model . property !== property || ! model . propertySubscription ) {
145+ bindProperty ( property ) ;
146+ }
147+ return property ;
148+ } ;
149+
150+ publicAPI . setProperty = ( property ) => {
151+ const changed = superSetProperty ( property ) ;
152+ bindProperty ( property ) ;
153+ if ( model . input !== undefined ) {
154+ updateTexture ( ) ;
155+ }
156+ return changed ;
157+ } ;
158+
159+ publicAPI . delete = ( ) => {
160+ if ( model . propertySubscription ) {
161+ model . propertySubscription . unsubscribe ( ) ;
162+ model . propertySubscription = null ;
163+ }
164+ superDelete ( ) ;
165+ } ;
166+
167+ model . _onInputChanged = ( ) => {
168+ updateTexture ( ) ;
121169 } ;
122170}
123171
124172// Default property values
125173const DEFAULT_VALUES = {
126174 mapper : null ,
127175 property : null ,
176+ propertySubscription : null ,
128177} ;
129178
130179export function extend ( publicAPI , model , initialValues = { } ) {
0 commit comments