import '@kitware/vtk.js/favicon';
import '@kitware/vtk.js/Rendering/Profiles/Geometry'; import '@kitware/vtk.js/Rendering/Profiles/Molecule';
import vtkActor from '@kitware/vtk.js/Rendering/Core/Actor'; import vtkConeSource from '@kitware/vtk.js/Filters/Sources/ConeSource'; import vtkSphereMapper from '@kitware/vtk.js/Rendering/Core/SphereMapper'; import vtkOpenGLRenderWindow from '@kitware/vtk.js/Rendering/OpenGL/RenderWindow'; import vtkPixelSpaceCallbackMapper from '@kitware/vtk.js/Rendering/Core/PixelSpaceCallbackMapper'; import vtkRenderWindow from '@kitware/vtk.js/Rendering/Core/RenderWindow'; import vtkRenderWindowInteractor from '@kitware/vtk.js/Rendering/Core/RenderWindowInteractor'; import vtkRenderer from '@kitware/vtk.js/Rendering/Core/Renderer'; import vtkInteractorStyleTrackballCamera from '@kitware/vtk.js/Interaction/Style/InteractorStyleTrackballCamera';
import style from './style.module.css';
let textCtx = null; let dims = null;
const renderWindow = vtkRenderWindow.newInstance(); const renderer = vtkRenderer.newInstance({ background: [0.2, 0.3, 0.4] }); renderWindow.addRenderer(renderer);
const coneSource = vtkConeSource.newInstance({ height: 1.0 });
const mapper = vtkSphereMapper.newInstance(); mapper.setInputConnection(coneSource.getOutputPort());
const actor = vtkActor.newInstance(); actor.setMapper(mapper);
const psMapper = vtkPixelSpaceCallbackMapper.newInstance(); psMapper.setInputConnection(coneSource.getOutputPort()); psMapper.setCallback((coordsList) => { if (textCtx && dims) { textCtx.clearRect(0, 0, dims.width, dims.height); coordsList.forEach((xy, idx) => { textCtx.font = '12px serif'; textCtx.textAlign = 'center'; textCtx.textBaseline = 'middle'; textCtx.fillText(`p ${idx}`, xy[0], dims.height - xy[1]); }); } });
const textActor = vtkActor.newInstance(); textActor.setMapper(psMapper);
renderer.addActor(actor); renderer.addActor(textActor); renderer.resetCamera();
const openGLRenderWindow = vtkOpenGLRenderWindow.newInstance(); renderWindow.addView(openGLRenderWindow);
const container = document.createElement('div'); container.classList.add(style.container); document.querySelector('body').appendChild(container); openGLRenderWindow.setContainer(container);
const textCanvas = document.createElement('canvas'); textCanvas.classList.add(style.container, 'textCanvas'); container.appendChild(textCanvas);
textCtx = textCanvas.getContext('2d');
const interactor = vtkRenderWindowInteractor.newInstance(); interactor.setView(openGLRenderWindow); interactor.initialize(); interactor.bindEvents(container);
interactor.setInteractorStyle(vtkInteractorStyleTrackballCamera.newInstance());
function resize() { dims = container.getBoundingClientRect(); openGLRenderWindow.setSize(dims.width, dims.height); textCanvas.setAttribute('width', dims.width); textCanvas.setAttribute('height', dims.height); renderWindow.render(); }
window.addEventListener('resize', resize);
resize();
|