当前位置:   article > 正文

vue3图片描点标记_vue3 图片标记

vue3 图片标记
  1. <el-dialog v-model="markVisible" title="图片标注" width="70%" :before-close="cancelBiaoZhu">
  2. <el-row>
  3. <el-col :span="24">
  4. <span>(图片标注可点击鼠标右键进行撤销)</span>
  5. </el-col>
  6. <el-col :span="24">
  7. <div class="myBiaoZhu" id="myBiaoZhuDiv" v-if="isRefresh">
  8. <img id="myBiaoZhu" :src="imageUrl" style="max-width: 800px" />
  9. </div>
  10. </el-col>
  11. </el-row>
  12. <!-- 展示标记点位置-->
  13. <!-- <div class="showBiaoZhu" id="showBiaoZhuDiv">-->
  14. <!--&lt;!&ndash; style="max-width: 600px"&ndash;&gt;-->
  15. <!-- <img id="showBiaoZhu" :src="imageUrl" style="width: 600px;height: 200px"/>-->
  16. <!-- </div>-->
  17. <template #footer>
  18. <span class="dialog-footer">
  19. <el-button @click="cancelBiaoZhu" size="default">取 消</el-button>
  20. <el-button type="primary" size="default" @click="endBiaoZhu">结束标注</el-button>
  21. </span>
  22. </template>
  23. </el-dialog>
  24. #myBiaoZhuDiv {
  25. position: relative;
  26. cursor: pointer;
  27. img {
  28. border: solid 1px #000;
  29. display: inline-block;
  30. margin: 20px 20px;
  31. z-index: 1;
  32. }
  33. .marker {
  34. position: absolute;
  35. border-radius: 50%;
  36. z-index: 999;
  37. }
  38. }
  1. // 开始标注
  2. startBiaoZhu() {
  3. this.markVisible = true
  4. this.canBiaoZhu = true
  5. // console.log('ppp',this.banMa)
  6. this.$nextTick(() => {
  7. // let this.tableData[this.picturePropIndex].buList[this.pictureIndex].banMa
  8. // console.log('ppp',document.getElementById('myBiaoZhu'))
  9. document.getElementById('myBiaoZhu').oncontextmenu = (e) => {
  10. if (e && e.preventDefault) {
  11. //阻止默认浏览器动作(W3C)
  12. e.preventDefault()
  13. } else {
  14. //IE中阻止函数器默认动作的方式
  15. window.event.returnValue = false
  16. }
  17. return false
  18. } //阻止冒泡行为和默认右键菜单事件
  19. let arr = JSON.parse(JSON.stringify(this.banMa))
  20. // 如果存在已经有位置信息的点直接回显
  21. arr.forEach((v) => {
  22. // console.log(')0))0',v.proportionHeightInImg)
  23. if (v.proportionHeightInImg != null) {
  24. setTimeout(()=>{
  25. // this.createMarker(v.x,v.y,v.sort)
  26. let markImg = document.querySelector('#myBiaoZhu')
  27. let markWidth = markImg.clientWidth
  28. let markHeight = markImg.clientHeight
  29. // console.log('进行',markWidth,markHeight)
  30. let div = document.createElement('div')
  31. div.className = 'marker'
  32. div.id = 'marker' + v.sort
  33. div.style.width = this.pointSize + 'px'
  34. div.style.height = this.pointSize + 'px'
  35. div.style.backgroundColor = this.pointColor
  36. div.style.left = markWidth * v.proportionWidthInImg + document.getElementById('myBiaoZhu').offsetTop - this.pointSize / 2 + 'px'
  37. div.style.top = markHeight * v.proportionHeightInImg + document.getElementById('myBiaoZhu').offsetLeft - this.pointSize / 2 + 'px'
  38. div.style.borderRadius = '50%'
  39. div.style.position = 'absolute'
  40. div.style.zIndex = 999
  41. div.innerHTML = `${v.sort}`
  42. div.style.color = '#ffffff'
  43. div.style.textAlign = 'center'
  44. div.style.lineHeight = this.pointSize + 'px'
  45. div.oncontextmenu = (e) => {
  46. // this.isRefresh = false
  47. var sort = e.target.id
  48. // console.log('sss',sort)
  49. document.getElementById('myBiaoZhuDiv').removeChild(div)
  50. this.banMa.forEach((item, index) => {
  51. if (item.sort == sort.slice(6, sort.length)) {
  52. // console.log('进',item.id)
  53. // delete this.banMa[index];
  54. this.banMa[index].x = null
  55. this.banMa[index].y = null
  56. this.banMa[index].proportionHeightInImg = null
  57. this.banMa[index].proportionWidthInImg = null
  58. }
  59. // console.log('&&&&',item.id,id.slice(6,id.length),item.id == id.slice(6,id.length))
  60. })
  61. this.canBiaoZhu = true
  62. if (e && e.preventDefault) {
  63. //阻止默认浏览器动作(W3C)
  64. e.preventDefault()
  65. } else {
  66. //IE中阻止函数器默认动作的方式
  67. window.event.returnValue = false
  68. }
  69. return false
  70. } //阻止冒泡行为和默认右键菜单事件,删除该点、
  71. document.getElementById('myBiaoZhuDiv').appendChild(div)
  72. },500)
  73. }
  74. })
  75. document.getElementById('myBiaoZhu').onmousedown = (e) => {
  76. // console.log('进')
  77. e = e || window.event
  78. if (e.button !== 2) {
  79. //判断是否右击
  80. if (this.canBiaoZhu) {
  81. //判断是否可以进行标注
  82. var x = e.offsetX || e.layerX
  83. var y = e.offsetY || e.layerY
  84. // console.log(x,y)
  85. var myImg = document.querySelector('#myBiaoZhu')
  86. var currWidth = myImg.clientWidth
  87. var currHeight = myImg.clientHeight
  88. var proportionWidthInImg = x / currWidth
  89. var proportionHeightInImg = y / currHeight
  90. // console.log("图片比例高度:"+proportionHeightInImg)
  91. // console.log("图片比例宽度:"+proportionWidthInImg)
  92. let obj = {
  93. // id:this.banMa.length+1,
  94. sort: null,
  95. x,
  96. y,
  97. proportionHeightInImg,
  98. proportionWidthInImg,
  99. }
  100. let arr = JSON.parse(JSON.stringify(this.banMa))
  101. let flag = false
  102. for (let i = 0; i < arr.length; i++) {
  103. if (this.banMa[i].proportionHeightInImg == null) {
  104. // console.log('jinjinjin')
  105. flag = true
  106. obj.sort = Number(this.banMa[i].sort)
  107. this.banMa[i] = obj
  108. break
  109. }
  110. }
  111. if (obj.sort === this.banMa[this.banMa.length - 1].sort && this.banMa[this.banMa.length - 1].x != null) {
  112. // console.log('进')
  113. this.canBiaoZhu = false
  114. }
  115. if (obj.sort != null) {
  116. this.createMarker(x, y, obj.sort)
  117. } else {
  118. ElMessage({
  119. message: '标注点数已达上限!',
  120. type: 'warning',
  121. })
  122. }
  123. // if(this.banMa[this.banMa.length-1].id===obj.id){
  124. // this.canBiaoZhu = false
  125. // }
  126. // console.log('ppp')
  127. // var markImg = document.querySelector("#showBiaoZhu")
  128. // var markWidth = markImg.clientWidth
  129. // var markHeight = markImg.clientHeight
  130. // var div = document.createElement('div')
  131. // div.className = 'show'
  132. // div.style.width = this.pointSize + 'px'
  133. // div.style.height = this.pointSize + 'px'
  134. // div.style.backgroundColor = this.pointColor
  135. // div.style.left = markWidth*ProportionWidthInImg + document.getElementById('showBiaoZhu').offsetTop- this.pointSize/2 + 'px'
  136. // div.style.top = markHeight*ProportionHeightInImg + document.getElementById('showBiaoZhu').offsetLeft - this.pointSize/2 + 'px'
  137. // div.style.borderRadius = '50%'
  138. // div.style.position='absolute'
  139. // div.style.zIndex= 999
  140. // div.innerHTML=`${this.banMa.length}`
  141. // div.style.color='#ffffff'
  142. // div.style.textAlign='center'
  143. // div.style.lineHeight = this.pointSize + 'px'
  144. // document.getElementById('showBiaoZhuDiv').appendChild(div)
  145. } else {
  146. ElMessage({
  147. message: '标注点数已达上限!',
  148. type: 'warning',
  149. })
  150. }
  151. } else {
  152. console.log(e)
  153. }
  154. }
  155. })
  156. },
  157. //画点
  158. createMarker(x, y, sort) {
  159. // console.log('ppp',id)
  160. var div = document.createElement('div')
  161. div.className = 'marker'
  162. div.id = 'marker' + sort
  163. y = y + document.getElementById('myBiaoZhu').offsetTop - this.pointSize / 2
  164. x = x + document.getElementById('myBiaoZhu').offsetLeft - this.pointSize / 2
  165. // console.log('@@@',document.getElementById('myBiaoZhu').offsetTop,document.getElementById('myBiaoZhu').offsetLeft)
  166. div.style.width = this.pointSize + 'px'
  167. div.style.height = this.pointSize + 'px'
  168. div.style.backgroundColor = this.pointColor
  169. div.style.left = x + 'px'
  170. div.style.top = y + 'px'
  171. div.style.borderRadius = '50%'
  172. div.style.position = 'absolute'
  173. div.style.zIndex = 999
  174. div.innerHTML = `${sort}`
  175. div.style.color = '#ffffff'
  176. div.style.textAlign = 'center'
  177. div.style.lineHeight = this.pointSize + 'px'
  178. div.oncontextmenu = (e) => {
  179. // this.isRefresh = false
  180. // console.log('pppp',e)
  181. var sort = e.target.id
  182. document.getElementById('myBiaoZhuDiv').removeChild(div)
  183. this.banMa.forEach((item, index) => {
  184. if (item.sort == sort.slice(6, sort.length)) {
  185. // console.log('进',item.id)
  186. // delete this.banMa[index];
  187. this.banMa[index].x = null
  188. this.banMa[index].y = null
  189. this.banMa[index].proportionHeightInImg = null
  190. this.banMa[index].proportionWidthInImg = null
  191. }
  192. // console.log('&&&&',item.id,id.slice(6,id.length),item.id == id.slice(6,id.length))
  193. })
  194. this.canBiaoZhu = true
  195. if (e && e.preventDefault) {
  196. //阻止默认浏览器动作(W3C)
  197. e.preventDefault()
  198. } else {
  199. //IE中阻止函数器默认动作的方式
  200. window.event.returnValue = false
  201. }
  202. return false
  203. } //阻止冒泡行为和默认右键菜单事件,删除该点
  204. document.getElementById('myBiaoZhuDiv').appendChild(div)
  205. },
  206. //结束标注
  207. endBiaoZhu() {
  208. this.tableData[this.picturePropIndex].buList[this.pictureIndex].banMa = JSON.parse(JSON.stringify(this.banMa))
  209. this.cancelBiaoZhu()
  210. this.markVisible = false
  211. this.canBiaoZhu = false
  212. // this.comfirmAdd()
  213. },
  214. // 取消标注
  215. cancelBiaoZhu() {
  216. this.markVisible = false
  217. this.canBiaoZhu = false
  218. this.banMa.forEach((v) => {
  219. let div = document.getElementById('marker' + v.sort)
  220. if (div) {
  221. document.getElementById('myBiaoZhuDiv').removeChild(div)
  222. }
  223. })
  224. this.comfirmAdd()
  225. this.banMa = []
  226. },

获取已经有标注信息的数据并回显,通过拧紧信息带括号判断从数字几开始标注,到几结束,限制标注个数

  1. // 步序图片上传
  2. handleChildUpImg(row, index, propIndex) {
  3. // this.cancelBiaoZhu()
  4. this.picturePropIndex = propIndex
  5. this.pictureIndex = index
  6. this.pictureDialogVisible = true
  7. let str = row.stepName
  8. if (str.indexOf('(') != -1 || str.indexOf('(') != -1) {
  9. // console.log('row',row.stepName)
  10. this.banMa = []
  11. this.pointSortList = []
  12. var num1, num2, num3
  13. if (str.indexOf('(') != -1) {
  14. num1 = str.indexOf('(')
  15. } else if (str.indexOf('(') != -1) {
  16. num1 = str.indexOf('(')
  17. } else {
  18. num1 = ''
  19. }
  20. // console.log('次数',str.match(/-/g).length,str.indexOf('-',2))
  21. num2 = str.indexOf('-')
  22. if(str.match(/-/g).length===2){
  23. num2 = str.indexOf('-',num2+1)
  24. }
  25. if (str.indexOf(')') != -1) {
  26. num3 = str.indexOf(')')
  27. } else if (str.indexOf(')') != -1) {
  28. num3 = str.indexOf(')')
  29. } else {
  30. num3 = ''
  31. }
  32. var start = str.slice(num1 + 1, num2)
  33. var end = str.slice(num2 + 1, num3)
  34. for (var i = start; i <= end; i++) {
  35. this.pointSortList.push({ sort: Number(i), x: null, y: null, proportionHeightInImg: null, proportionWidthInImgInImg: null })
  36. }
  37. // console.log('进行',start,end)
  38. this.isNingJin = true
  39. if (row.banMa) {
  40. // this.banMa = []
  41. row.banMa.forEach((v, i) => {
  42. if (v.sort) {
  43. this.banMa.push({ sort: v.sort, x: null, y: null, proportionHeightInImg: v.proportionHeightInImg, proportionWidthInImg: v.proportionWidthInImg })
  44. } else {
  45. let sort = Number(i) + Number(start)
  46. this.banMa.push({ sort, x: null, y: null, proportionHeightInImg: v.proportionHeightInImg, proportionWidthInImg: v.proportionWidthInImg })
  47. }
  48. })
  49. } else {
  50. // console.log('jin',start,end)
  51. // var tips = str.slice(0, num1)
  52. // for (var i = start; i <= end; i++) {
  53. // this.banMa.push({sort:Number(i),
  54. // x:null,
  55. // y:null,
  56. // proportionHeightInImg:null,
  57. // proportionWidthInImgInImg:null})
  58. // }
  59. // console.log('步序',this.banMa)
  60. this.banMa = [...this.pointSortList]
  61. }
  62. // console.log('oooo',this.banMa,row.banMa)
  63. } else {
  64. this.isNingJin = false
  65. this.banMa = []
  66. }
  67. if (row.pictureUrl) {
  68. this.imageUrl = this.mateUrl + row.pictureUrl
  69. this.picUrl = row.pictureUrl
  70. } else {
  71. this.imageUrl = ''
  72. this.picUrl = ''
  73. }
  74. // if(row.banMa){
  75. // this.banMa = row.banMa
  76. // }else{
  77. // this.banMa = []
  78. // }
  79. },

效果

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/109920?site
推荐阅读
相关标签
  

闽ICP备14008679号