index.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <template>
  2. <Teleport to="#right-pano">
  3. <ElButton @click="board.clear()"> 清除 </ElButton>
  4. <ElButton :disabled="!board.history.state.hasUndo" @click="board.history.undo()">
  5. 撤销
  6. </ElButton>
  7. <ElButton :disabled="!board.history.state.hasRedo" @click="board.history.redo()">
  8. 重做
  9. </ElButton>
  10. <ElButton @click="getData">获取数据</ElButton>
  11. <ElButton v-if="!drawing" @click="drawHandler"> 绘画 </ElButton>
  12. <ElButton v-if="drawing" @click="drawing.cancel()"> 停止 </ElButton>
  13. <ElButton v-if="drawing" @click="drawing.submit()"> 提交 </ElButton>
  14. <ElButton @click="download"> 下载 </ElButton>
  15. <template v-if="activeEntity">
  16. <ElButton @click="activeEntity.copy({ count: 5 })"> 向右复制5个 </ElButton>
  17. <ElButton @click="activeEntity.del()"> 删除 </ElButton>
  18. </template>
  19. <ElSelect v-model="poiType">
  20. <ElOption v-for="key in Object.keys(svgs)" :value="key" :label="key" />
  21. </ElSelect>
  22. <ElButton v-if="!addPoiState" @click="addPoiHandler(poiType)">添加poi</ElButton>
  23. <template v-if="addPoiState">
  24. <ElButton @click="addPoiState.cancel()"> 停止添加poi </ElButton>
  25. </template>
  26. <ElButton @click="board.showPois()"> 显示图例 </ElButton>
  27. <ElButton @click="board.hidenPois()"> 隐藏图例 </ElButton>
  28. <ElButton @click="board.showPoisId()"> 显示图例Id </ElButton>
  29. <ElButton @click="board.hidePoisId()"> 隐藏图例Id </ElButton>
  30. <ElButton @click="board.blurPoi()" v-if="activeEntity"> 去除聚焦 </ElButton>
  31. </Teleport>
  32. <div
  33. class="board-layout"
  34. :style="{ width: width + 'px', height: height + 'px' }"
  35. ref="containerRef"
  36. ></div>
  37. </template>
  38. <script setup lang="ts">
  39. import { shallowRef, watch, ref } from "vue";
  40. import { EditWholeLine, createBoard, EditPoi, changeEnv, floorDataTransform } from "../";
  41. import storeData from "./storeData.json";
  42. import floor from "./floorplan_cad.json";
  43. import { ElButton, ElSelect, ElOption } from "element-plus";
  44. import "element-plus/dist/index.css";
  45. import { svgs } from "../icon";
  46. const poiType = ref(Object.keys(svgs)[0]);
  47. const rooms = floorDataTransform(floor);
  48. changeEnv(false);
  49. withDefaults(defineProps<{ width?: number; height?: number; pixelRation?: number }>(), {
  50. width: 320,
  51. height: 150,
  52. pixelRation: 1,
  53. });
  54. type Board = ReturnType<typeof createBoard>;
  55. const board = createBoard();
  56. board.bound.setRetainScale(true);
  57. setTimeout(() => {
  58. // board.setData({ rooms: rooms });
  59. console.log(storeData);
  60. board.setData(storeData);
  61. board.bound.autoBound(20);
  62. }, 500);
  63. const activeEntity = shallowRef<EditPoi>();
  64. board.bus.on("activePoi", (entity) => {
  65. activeEntity.value = entity;
  66. });
  67. const drawing = shallowRef<ReturnType<EditWholeLine["createPolygon"]>>();
  68. const drawHandler = () => {
  69. drawing.value = board.room.createPolygon();
  70. drawing.value.bus.on("penStart", () => {
  71. console.log("开始绘画");
  72. });
  73. drawing.value.promise.then((result) => {
  74. console.log(result);
  75. drawing.value = null;
  76. });
  77. };
  78. window.board = board;
  79. const addPoiState = shallowRef<ReturnType<Board["addPoi"]>>();
  80. const addPoiHandler = (type: string) => {
  81. addPoiState.value = board.addPoi(type);
  82. addPoiState.value.promise.finally(() => {
  83. addPoiState.value = null;
  84. });
  85. };
  86. const containerRef = shallowRef<HTMLDivElement>();
  87. watch(containerRef, (container, _) => {
  88. container && board.mount(container);
  89. });
  90. const getData = () => {
  91. console.log(JSON.stringify(board.getData(), null, 4));
  92. };
  93. const download = () => {
  94. board.bound.setSize(1920, 1080);
  95. board.tree.stage.toBlob({
  96. width: 1920,
  97. height: 1080,
  98. pixelRatio: 3,
  99. callback(blob) {
  100. window.open(URL.createObjectURL(blob));
  101. },
  102. });
  103. console.log(board);
  104. };
  105. </script>
  106. <style lang="scss" scoped>
  107. .board-layout {
  108. position: absolute;
  109. background: rgba(0, 0, 0, 0.3);
  110. canvas {
  111. display: block;
  112. width: 100%;
  113. height: 100%;
  114. }
  115. }
  116. </style>