HelloWorld——1.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <template>
  2. <div id="vtkContainer" />
  3. </template>
  4. <script>
  5. import '@kitware/vtk.js/favicon';
  6. // Load the rendering pieces we want to use (for both WebGL and WebGPU)
  7. import '@kitware/vtk.js/Rendering/Profiles/Geometry';
  8. import { onMounted } from 'vue';
  9. import vtkRenderWindowWithControlBar from '@kitware/vtk.js/Rendering/Misc/RenderWindowWithControlBar';
  10. import vtkSlider from '@kitware/vtk.js/Interaction/UI/Slider';
  11. import vtkCornerAnnotation from '@kitware/vtk.js/Interaction/UI/CornerAnnotation';
  12. import vtkActor from '@kitware/vtk.js/Rendering/Core/Actor';
  13. import vtkConeSource from '@kitware/vtk.js/Filters/Sources/ConeSource';
  14. import vtkMapper from '@kitware/vtk.js/Rendering/Core/Mapper';
  15. export default {
  16. name: 'HelloWorld',
  17. //体制
  18. setup() {
  19. onMounted(() => {
  20. // Define container size/position
  21. const body = document.querySelector('body');
  22. // const rootContainer = document.createElement('div');
  23. // rootContainer.style.position = 'relative';
  24. // rootContainer.style.width = '500px';
  25. // rootContainer.style.height = '500px';
  26. // body.appendChild(rootContainer);
  27. body.style.margin = '0';
  28. // Create render window inside container
  29. const rootContainer =document.getElementById("vtkContainer");
  30. rootContainer.style.position = 'relative';
  31. rootContainer.style.width = '500px';
  32. rootContainer.style.height = '500px';
  33. console.log("rootContainer:",rootContainer);
  34. const renderWindow = vtkRenderWindowWithControlBar.newInstance({
  35. controlSize: 25,
  36. });
  37. renderWindow.setContainer(rootContainer);
  38. // Add some content to the renderer
  39. const coneSource = vtkConeSource.newInstance();
  40. const mapper = vtkMapper.newInstance();
  41. const actor = vtkActor.newInstance();
  42. mapper.setInputConnection(coneSource.getOutputPort());
  43. actor.setMapper(mapper);
  44. renderWindow.getRenderer().addActor(actor);
  45. renderWindow.getRenderer().resetCamera();
  46. renderWindow.getRenderWindow().render();
  47. // Set control bar to be red so we can see it + layout setup...
  48. renderWindow.getControlContainer().style.background = '#eee';
  49. renderWindow.getControlContainer().style.display = 'flex';
  50. renderWindow.getControlContainer().style.alignItems = 'stretch';
  51. renderWindow.getControlContainer().style.justifyContent = 'stretch';
  52. renderWindow.getControlContainer().innerHTML = `
  53. <button alt="Left" title="Left" value="left">L</button>
  54. <button alt="Top" title="Top" value="top">T</button>
  55. <button alt="Right" title="Right" value="right">R</button>
  56. <button alt="Bottom" title="Bottom" value="bottom">B</button>
  57. <div class="js-slider"></div>
  58. `;
  59. // Add corner annotation
  60. const cornerAnnotation = vtkCornerAnnotation.newInstance();
  61. cornerAnnotation.setContainer(renderWindow.getRenderWindowContainer());
  62. cornerAnnotation.getAnnotationContainer().style.color = 'white';
  63. cornerAnnotation.updateMetadata(coneSource.get('resolution', 'mtime'));
  64. cornerAnnotation.updateTemplates({
  65. nw(meta) {
  66. return `<b>Resolution: </b> ${meta.resolution}`;
  67. },
  68. se(meta) {
  69. return `<span style="font-size: 50%"><i style="color: red;">mtime:</i>${meta.mtime}</span><br/>Annotation Corner`;
  70. },
  71. });
  72. // Add slider to the control bar
  73. const sliderContainer = renderWindow
  74. .getControlContainer()
  75. .querySelector('.js-slider');
  76. sliderContainer.style.flex = '1';
  77. sliderContainer.style.position = 'relative';
  78. sliderContainer.style.minWidth = '25px';
  79. sliderContainer.style.minHeight = '25px';
  80. const slider = vtkSlider.newInstance();
  81. slider.generateValues(6, 60, 55);
  82. slider.setValue(6);
  83. slider.setContainer(sliderContainer);
  84. slider.onValueChange((resolution) => {
  85. coneSource.set({ resolution });
  86. renderWindow.getRenderWindow().render();
  87. cornerAnnotation.updateMetadata(coneSource.get('resolution', 'mtime'));
  88. });
  89. function updateSizeAndOrientation() {
  90. renderWindow.resize();
  91. slider.resize();
  92. renderWindow.getControlContainer().style.flexFlow = slider.getOrientation()
  93. ? 'row'
  94. : 'column';
  95. setTimeout(slider.resize, 0);
  96. }
  97. updateSizeAndOrientation();
  98. // Handle UI to change bar location
  99. function onClick(e) {
  100. renderWindow.setControlPosition(e.target.value);
  101. updateSizeAndOrientation();
  102. }
  103. const list = document.querySelectorAll('button');
  104. let count = list.length;
  105. while (count--) {
  106. list[count].style.width = '25px';
  107. list[count].style.height = '25px';
  108. list[count].style.flex = 'none';
  109. list[count].addEventListener('click', onClick);
  110. }
  111. });
  112. return ;
  113. }
  114. }
  115. </script>
  116. <style scoped>
  117. .controls {
  118. position: absolute;
  119. top: 25px;
  120. left: 25px;
  121. background: white;
  122. padding: 12px;
  123. }
  124. </style>