index.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <template>
  2. <div id="app">
  3. <!-- 图片区域 -->
  4. <div class="box">
  5. <div
  6. ref="imageArea"
  7. class="image-area"
  8. @click="handleClick"
  9. >
  10. <img src="../../assets/dot.jpeg" alt="图片" class="image">
  11. <template v-for="shape in shapes">
  12. <div
  13. v-for="(point, index) in shape.points"
  14. :key="`shape-${shape.type}-point-${index}`"
  15. :style="getPointStyle(point)"
  16. class="point-marker"
  17. ></div>
  18. <div v-if="shape.type === '1' || shape.type === '2'"
  19. :style="getRectangleStyle(shape.points)"
  20. class="rectangle"
  21. ></div>
  22. <polyline v-if="shape.type === '3'"
  23. :points="getLinePoints(shape.points)"
  24. class="3"
  25. fill="none" stroke="red" stroke-width="2"/>
  26. </template>
  27. </div>
  28. <!-- -->
  29. <div>
  30. <h3 style="padding-left: 20px;">鼠标点击获取图形坐标</h3>
  31. <el-form :model="formObj" ref="formObj" label-width="120px" class="demo-dynamic">
  32. <el-form-item label="区域类型:">
  33. <el-radio-group v-model="currentShapeType" @change="changeType">
  34. <el-radio label="1">卧床</el-radio>
  35. <el-radio label="2">喷淋</el-radio>
  36. <el-radio label="3">水槽</el-radio>
  37. </el-radio-group>
  38. </el-form-item>
  39. <!-- 卧床 -->
  40. <el-form-item
  41. label="卧床(1):"
  42. required
  43. v-if=" currentShapeType == '1'"
  44. >
  45. <div v-if="formObj.bed.length>0" key="1">
  46. <div><span style="padding-right: 20px;">左上:{{ `X:${formObj.bed[0]?.x || ''}Y:${formObj.bed[0]?.y || ''}`}}</span> <span>右上:{{ `X:${formObj.bed[1]?.x || ''}Y:${formObj.bed[1]?.y || ''}`}}</span></div>
  47. <div><span style="padding-right: 20px;">左下:{{ `X:${formObj.bed[3]?.x || ''}Y:${formObj.bed[3]?.y || ''}`}}</span> <span>右下:{{ `X:${formObj.bed[2]?.x || ''}Y:${formObj.bed[2]?.y || ''}`}}</span></div>
  48. </div>
  49. </el-form-item>
  50. <el-form-item
  51. label="卧床(2):"
  52. required
  53. v-if=" currentShapeType == '1'"
  54. >
  55. <div v-if="formObj.bed1.length>0 " key="2">
  56. <div><span style="padding-right: 20px;">左上:{{ `X:${formObj.bed1[0]?.x || ''}Y:${formObj.bed1[0]?.y || ''}`}}</span> <span>右上:{{ `X:${formObj.bed1[1]?.x || ''}Y:${formObj.bed1[1]?.y || ''}`}}</span></div>
  57. <div><span style="padding-right: 20px;">左下:{{ `X:${formObj.bed1[3]?.x || ''}Y:${formObj.bed1[3]?.y || ''}`}}</span> <span>右下:{{ `X:${formObj.bed1[2]?.x || ''}Y:${formObj.bed1[2]?.y || ''}`}}</span></div>
  58. </div>
  59. </el-form-item>
  60. <!--喷淋 -->
  61. <el-form-item
  62. label="喷淋:"
  63. required
  64. v-if=" currentShapeType == '2'"
  65. >
  66. <div v-if="formObj.spray.length>0 && currentShapeType == '2' " key="1">
  67. <div><span style="padding-right: 20px;">左上:{{ `X:${formObj.spray[0]?.x || ''}Y:${formObj.spray[0]?.y || ''}`}}</span> <span>右上:{{ `X:${formObj.spray[1]?.x || ''}Y:${formObj.spray[1]?.y || ''}`}}</span></div>
  68. <div><span style="padding-right: 20px;">左下:{{ `X:${formObj.spray[3]?.x || ''}Y:${formObj.spray[3]?.y || ''}`}}</span> <span>右下:{{ `X:${formObj.spray[2]?.x || ''}Y:${formObj.spray[2]?.y || ''}`}}</span></div>
  69. </div>
  70. </el-form-item>
  71. <!--水槽 -->
  72. <el-form-item
  73. label="水槽:"
  74. required
  75. v-if=" currentShapeType == '3'"
  76. >
  77. <div v-if="formObj.sprayList.length>0 " key="1">
  78. <div v-for="(item,index) in formObj.sprayList" :key="index"><span style="padding-right: 20px;" >第{{index+1}}点:{{ `X:${item?.x || ''}Y:${item?.y || ''}`}}</span> </div>
  79. </div>
  80. </el-form-item>
  81. <el-form-item>
  82. <el-button type="primary" @click="submitForm('formObj')">确认提交</el-button>
  83. <el-button @click="handleConfirm">重置</el-button>
  84. </el-form-item>
  85. </el-form>
  86. </div>
  87. </div>
  88. <!-- 显示当前形状的坐标点 -->
  89. <pre>{{ shapes }}</pre>
  90. </div>
  91. </template>
  92. <script>
  93. export default {
  94. data() {
  95. return {
  96. currentShapeType: '1',
  97. shapes: [],
  98. currentPoints: [],
  99. formObj: {
  100. type:'1',
  101. bed:[],
  102. bed1:[],
  103. spray:[],
  104. sprayList:[]
  105. }
  106. };
  107. },
  108. methods: {
  109. changeType(val){
  110. this.currentShapeType = val;
  111. this.currentPoints = [];
  112. },
  113. resetShape() {
  114. this.currentPoints = [];
  115. },
  116. handleClick(event) {
  117. const rect = this.$refs.imageArea.getBoundingClientRect();
  118. const x = event.clientX - rect.left;
  119. const y = event.clientY - rect.top;
  120. this.currentPoints.push({ x, y });
  121. if(this.currentShapeType == '1'){
  122. if(this.formObj.bed.length < 4){
  123. this.formObj.bed.push({ x, y });
  124. }else{
  125. this.formObj.bed1.push({ x, y });
  126. }
  127. console.log( this.formObj.bed,'hss')
  128. } else if (this.currentShapeType == '2') {
  129. this.formObj.spray=[...this.currentPoints]
  130. } else if(this.currentShapeType == '3'){
  131. this.formObj.sprayList = [...this.currentPoints]
  132. }
  133. const requiredPoints = this.getRequiredPointsForShape(this.currentShapeType);
  134. if (this.currentPoints.length === requiredPoints) {
  135. this.shapes.push({
  136. type: this.currentShapeType,
  137. points: [...this.currentPoints],
  138. });
  139. this.currentPoints = [];
  140. }
  141. },
  142. getRequiredPointsForShape(shapeType) {
  143. switch (shapeType) {
  144. case '1':
  145. return 4;
  146. case '2':
  147. return 4;
  148. case '3':
  149. return 8; // Example: 10 points for the 3
  150. default:
  151. return 4;
  152. }
  153. },
  154. getPointStyle(point) {
  155. return {
  156. top: `${point.y}px`,
  157. left: `${point.x}px`,
  158. position: 'absolute',
  159. width: '10px',
  160. height: '10px',
  161. backgroundColor: 'blue',
  162. borderRadius: '50%',
  163. };
  164. },
  165. getRectangleStyle(points) {
  166. if (points.length < 4) return {};
  167. const [leftup, leftdown, rightup, rightdown] = points;
  168. const width = Math.abs(rightup.x - leftup.x);
  169. const height = Math.abs(leftdown.y - rightdown.y);
  170. return {
  171. top: `${leftup.y}px`,
  172. left: `${leftup.x}px`,
  173. width: `${width}px`,
  174. height: `${height}px`,
  175. border: '2px solid red',
  176. position: 'absolute',
  177. };
  178. },
  179. getLinePoints(points) {
  180. return points.map(point => `${point.x},${point.y}`).join(' ');
  181. },
  182. // 重置
  183. handleConfirm(){
  184. this.currentPoints = []
  185. this.shapes= []
  186. this.currentShapeType = '1';
  187. this.formObj ={
  188. bed:[],
  189. bed1:[],
  190. spray:[],
  191. sprayList:[]
  192. }
  193. },
  194. //
  195. submitForm(formName) {
  196. console.log(this.formObj,'提交')
  197. this.$refs[formName].validate((valid) => {
  198. if (valid) {
  199. alert('submit!');
  200. } else {
  201. console.log('error submit!!');
  202. return false;
  203. }
  204. });
  205. },
  206. resetForm(formName) {
  207. this.$refs[formName].resetFields();
  208. },
  209. removeDomain(item) {
  210. var index = this.formObj.bedList.indexOf(item)
  211. if (index !== -1) {
  212. this.formObj.bedList.splice(index, 1)
  213. }
  214. },
  215. addDomain() {
  216. this.formObj.bedList.push({
  217. value: '',
  218. key: Date.now()
  219. });
  220. }
  221. },
  222. };
  223. </script>
  224. <style scoped>
  225. .el-button {
  226. padding:10px;
  227. }
  228. .box{
  229. display: flex;
  230. }
  231. .image-area {
  232. position: relative;
  233. width: 100%;
  234. height: auto;
  235. display: inline-block;
  236. width:700px;
  237. height:600px;
  238. }
  239. .image {
  240. width: 100%;
  241. height: auto;
  242. }
  243. .rectangle {
  244. border: 2px solid red;
  245. position: absolute;
  246. }
  247. .point-marker {
  248. position: absolute;
  249. }
  250. .3 {
  251. position: absolute;
  252. fill: none;
  253. stroke: red;
  254. stroke-width: 2px;
  255. }
  256. </style>