I restructured my code again. It looks like the following code:
//global variable
var renderer;
var renderWindow;
var container;
var widgetContainer;
var piecewiseFunction;
var volumeActor;
var volumeMapper;
export default class VTKVolume extends Component {
componentDidMount() {
this.original();
}
componentDidUpdate() {
if (this.props.item === 0) {
this.cortical();
}
if (this.props.item === 1) {
this.hippo();
}
if (this.props.item === 2) {
this.cortical();
this.hippo();
}
}
original() {
...//Share a variable as a global variable
let originalUrl = "..."
vtireader.setUrl(originalUrl).then(() =>{...});
...
}
cortical(){
...//Share a variable as a global variable
let corticalUrl = "..."
vtireader.setUrl(corticalUrl).then(() =>{...});
...
}
hippo(){
...//Share a variable as a global variable
let hippoUrl = "..."
vtireader.setUrl(hippoUrl).then(() =>{...});
...
}
}
I want to use only one canvas for every function call(original(), cortical(), hippo()). The code above seems to recreate the canvas when the function is called. This inefficiency causes memory waste and becomes slower.
This problem has been around for a while. How should I improve my code?
Based on what I can see it is hard to tell. To me it is just poorly designed. Unfortunately discourse does not aim to teach users how to code. So I’m not sure I can be of much help.