@@ -14,6 +14,8 @@ function vtkTextActor(publicAPI, model) {
1414 // Set our className
1515 model . classHierarchy . push ( 'vtkTextActor' ) ;
1616
17+ const superClass = { ...publicAPI } ;
18+
1719 publicAPI . makeProperty = vtkTextProperty . newInstance ;
1820
1921 const texture = vtkTexture . newInstance ( {
@@ -38,7 +40,7 @@ function vtkTextActor(publicAPI, model) {
3840 const backgroundColor = publicAPI . getProperty ( ) . getBackgroundColor ( ) ;
3941
4042 const dpr = Math . max ( window . devicePixelRatio || 1 , 1 ) ;
41- const ctx = canvas . getContext ( '2d' ) ;
43+ const ctx = canvas . getContext ( '2d' , { willReadFrequently : true } ) ;
4244
4345 // Set the text properties to measure
4446 const textSize = fontSizeScale ( resolution ) * dpr ;
@@ -110,21 +112,55 @@ function vtkTextActor(publicAPI, model) {
110112 return ImageHelper . canvasToImageData ( canvas ) ;
111113 }
112114
115+ function updateTexture ( ) {
116+ const image = createImageData ( model . input ) ;
117+ texture . setInputData ( image , 0 ) ;
118+ model . textureBuildTime . modified ( ) ;
119+ }
120+
121+ function updateTextureIfNeeded ( ) {
122+ if ( model . input === undefined ) {
123+ return ;
124+ }
125+
126+ const propertyMTime = publicAPI . getProperty ( ) . getMTime ( ) ;
127+ if (
128+ model . textureBuildTime . getMTime ( ) < propertyMTime ||
129+ model . textureBuildTime . getMTime ( ) < model . textureUpdateTime . getMTime ( )
130+ ) {
131+ updateTexture ( ) ;
132+ }
133+ }
134+
113135 mapper . setInputConnection ( plane . getOutputPort ( ) ) ;
114136
115137 publicAPI . setMapper ( mapper ) ;
116138 publicAPI . addTexture ( texture ) ;
117139
118- model . _onInputChanged = ( _publicAPI , _model , value ) => {
119- const image = createImageData ( value ) ;
120- texture . setInputData ( image , 0 ) ;
140+ publicAPI . setProperty = ( property ) => {
141+ const changed = superClass . setProperty ( property ) ;
142+ if ( changed ) {
143+ model . textureUpdateTime . modified ( ) ;
144+ }
145+ return changed ;
146+ } ;
147+
148+ publicAPI . getTextures = ( ) => {
149+ updateTextureIfNeeded ( ) ;
150+ return superClass . getTextures ( ) ;
151+ } ;
152+
153+ publicAPI . delete = ( ) => superClass . delete ( ) ;
154+
155+ model . _onInputChanged = ( ) => {
156+ model . textureUpdateTime . modified ( ) ;
121157 } ;
122158}
123159
124160// Default property values
125161const DEFAULT_VALUES = {
126- mapper : null ,
127- property : null ,
162+ textureBuildTime : null ,
163+ textureUpdateTime : null ,
128164} ;
129165
130166export function extend ( publicAPI , model , initialValues = { } ) {
@@ -135,6 +171,10 @@ export function extend(publicAPI, model, initialValues = {}) {
135171
136172 // Build VTK API
137173 macro . setGet ( publicAPI , model , [ 'input' ] ) ;
174+ model . textureBuildTime = { } ;
175+ macro . obj ( model . textureBuildTime , { mtime : 0 } ) ;
176+ model . textureUpdateTime = { } ;
177+ macro . obj ( model . textureUpdateTime , { mtime : 0 } ) ;
138178
139179 // Object methods
140180 vtkTextActor ( publicAPI , model ) ;
0 commit comments