Skip to content

Commit 4fd7a45

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

3 files changed

Lines changed: 81 additions & 10 deletions

File tree

Sources/Rendering/Core/Actor2D/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,17 @@ function vtkActor2D(publicAPI, model) {
4545

4646
publicAPI.makeProperty = vtkProperty2D.newInstance;
4747

48-
publicAPI.getProperty = () => {
48+
publicAPI.ensureProperty = () => {
4949
if (model.property === null) {
50-
model.property = publicAPI.makeProperty();
50+
publicAPI.setProperty(publicAPI.makeProperty());
5151
}
5252
return model.property;
5353
};
5454

55+
publicAPI.getProperty = () => {
56+
return publicAPI.ensureProperty();
57+
};
58+
5559
//----------------------------------------------------------------------------
5660
// Set the Prop2D's position in display coordinates.
5761
publicAPI.setDisplayPosition = (XPos, YPos) => {

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: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ 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+
let propertySubscription = null;
21+
1722
publicAPI.makeProperty = vtkTextProperty.newInstance;
1823

1924
const texture = vtkTexture.newInstance({
@@ -38,7 +43,7 @@ function vtkTextActor(publicAPI, model) {
3843
const backgroundColor = publicAPI.getProperty().getBackgroundColor();
3944

4045
const dpr = Math.max(window.devicePixelRatio || 1, 1);
41-
const ctx = canvas.getContext('2d');
46+
const ctx = canvas.getContext('2d', { willReadFrequently: true });
4247

4348
// Set the text properties to measure
4449
const textSize = fontSizeScale(resolution) * dpr;
@@ -110,22 +115,53 @@ function vtkTextActor(publicAPI, model) {
110115
return ImageHelper.canvasToImageData(canvas);
111116
}
112117

118+
function updateTexture() {
119+
const image = createImageData(model.input);
120+
texture.setInputData(image, 0);
121+
}
122+
123+
function bindProperty(property) {
124+
if (propertySubscription) {
125+
propertySubscription.unsubscribe();
126+
propertySubscription = null;
127+
}
128+
129+
if (property) {
130+
propertySubscription = property.onModified(() => {
131+
if (model.input !== undefined) {
132+
updateTexture();
133+
}
134+
});
135+
}
136+
}
137+
113138
mapper.setInputConnection(plane.getOutputPort());
114139

115140
publicAPI.setMapper(mapper);
116141
publicAPI.addTexture(texture);
117142

118-
model._onInputChanged = (_publicAPI, _model, value) => {
119-
const image = createImageData(value);
120-
texture.setInputData(image, 0);
143+
publicAPI.setProperty = (property) => {
144+
const changed = superSetProperty(property);
145+
bindProperty(property);
146+
if (model.input !== undefined) {
147+
updateTexture();
148+
}
149+
return changed;
121150
};
151+
152+
publicAPI.delete = () => {
153+
if (propertySubscription) {
154+
propertySubscription.unsubscribe();
155+
propertySubscription = null;
156+
}
157+
superDelete();
158+
};
159+
160+
model._onInputChanged = updateTexture;
122161
}
123162

124163
// Default property values
125-
const DEFAULT_VALUES = {
126-
mapper: null,
127-
property: null,
128-
};
164+
const DEFAULT_VALUES = {};
129165

130166
export function extend(publicAPI, model, initialValues = {}) {
131167
Object.assign(model, DEFAULT_VALUES, initialValues);

0 commit comments

Comments
 (0)