Skip to content

Commit a491e4a

Browse files
committed
fix(TextActor): add support for property change
fixes #3445
1 parent 9d00695 commit a491e4a

2 files changed

Lines changed: 84 additions & 4 deletions

File tree

Sources/Rendering/Core/TextActor/example/index.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ const params = {
3737
text: 'Hello World!',
3838
x: window.innerWidth / 4,
3939
y: window.innerHeight / 4,
40+
color: [0, 0, 0],
41+
fontFamily: 'Arial',
42+
fontSize: actor.getProperty().getResolution(),
43+
};
44+
45+
const fonts = {
46+
Arial: 'Arial',
47+
Verdana: 'Verdana',
48+
Tahoma: 'Tahoma',
49+
Times: 'Times New Roman',
4050
};
4151

4252
gui
@@ -46,6 +56,27 @@ gui
4656
actor.setInput(value);
4757
renderWindow.render();
4858
});
59+
gui
60+
.add(params, 'fontFamily', fonts)
61+
.name('Font')
62+
.onChange((value) => {
63+
actor.getProperty().setFontFamily(value);
64+
renderWindow.render();
65+
});
66+
gui
67+
.add(params, 'fontSize', 50, 400, 1)
68+
.name('Font Size')
69+
.onChange((value) => {
70+
actor.getProperty().setResolution(value);
71+
renderWindow.render();
72+
});
73+
gui
74+
.addColor(params, 'color')
75+
.name('Font Color')
76+
.onChange((value) => {
77+
actor.getProperty().setFontColor(value[0], value[1], value[2]);
78+
renderWindow.render();
79+
});
4980
gui
5081
.add(params, 'x')
5182
.name('X Position')

Sources/Rendering/Core/TextActor/index.js

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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
125173
const DEFAULT_VALUES = {
126174
mapper: null,
127175
property: null,
176+
propertySubscription: null,
128177
};
129178

130179
export function extend(publicAPI, model, initialValues = {}) {

0 commit comments

Comments
 (0)