CylinderSource

Live example

Source

import 'vtk.js/Sources/favicon';

import vtkFullScreenRenderWindow from 'vtk.js/Sources/Rendering/Misc/FullScreenRenderWindow';
import vtkActor from 'vtk.js/Sources/Rendering/Core/Actor';
import vtkCylinderSource from 'vtk.js/Sources/Filters/Sources/CylinderSource';
import vtkMapper from 'vtk.js/Sources/Rendering/Core/Mapper';

import controlPanel from './controlPanel.html';

// ----------------------------------------------------------------------------
// Standard rendering code setup
// ----------------------------------------------------------------------------

const fullScreenRenderer = vtkFullScreenRenderWindow.newInstance({
background: [0, 0, 0],
});
const renderer = fullScreenRenderer.getRenderer();
const renderWindow = fullScreenRenderer.getRenderWindow();

// ----------------------------------------------------------------------------
// Example code
// ----------------------------------------------------------------------------

function createCylinderPipeline() {
const cylinderSource = vtkCylinderSource.newInstance();
const actor = vtkActor.newInstance();
const mapper = vtkMapper.newInstance();

actor.setMapper(mapper);
mapper.setInputConnection(cylinderSource.getOutputPort());

renderer.addActor(actor);
return { cylinderSource, mapper, actor };
}

const pipelines = [createCylinderPipeline(), createCylinderPipeline()];

// Create red wireframe baseline
pipelines[0].actor.getProperty().setRepresentation(1);
pipelines[0].actor.getProperty().setColor(1, 0, 0);

renderer.resetCamera();
renderWindow.render();

// -----------------------------------------------------------
// UI control handling
// -----------------------------------------------------------

fullScreenRenderer.addController(controlPanel);

['height', 'radius', 'resolution'].forEach((propertyName) => {
document.querySelector(`.${propertyName}`).addEventListener('input', (e) => {
const value = Number(e.target.value);
pipelines[0].cylinderSource.set({ [propertyName]: value });
pipelines[1].cylinderSource.set({ [propertyName]: value });
renderWindow.render();
});
});

document.querySelector('.capping').addEventListener('change', (e) => {
const capping = !!e.target.checked;
pipelines[0].cylinderSource.set({ capping });
pipelines[1].cylinderSource.set({ capping });
renderWindow.render();
});

const centerElems = document.querySelectorAll('.center');

function updateTransformedCylinder() {
const center = [0, 0, 0];
for (let i = 0; i < 3; i++) {
center[Number(centerElems[i].dataset.index)] = Number(centerElems[i].value);
}
console.log('updateTransformedCylinder', center);
pipelines[1].cylinderSource.set({ center });
renderWindow.render();
}

for (let i = 0; i < 3; i++) {
centerElems[i].addEventListener('input', updateTransformedCylinder);
}

// -----------------------------------------------------------
// Make some variables global so that you can inspect and
// modify objects in your browser's developer console:
// -----------------------------------------------------------

global.pipelines = pipelines;
global.renderer = renderer;
global.renderWindow = renderWindow;