vue 点击对图片进行标注并记录当前的坐标

6 篇文章 0 订阅

功能如下:

1、在图片区域内,点击鼠标左键对图片进行标注,并添加标注图片 ;

2、鼠标右键点击 已标注图片 删除当前标注。

<div class="myBiaoZhu" id="myBiaoZhuDiv">
      <img
        id="myBiaoZhu"
        :src="parseImgUrl"
        style="width:1000px;height:638px;"
        alt=""
      />
    </div>

 

 data() {
    return {
      parseImgUrl:"",
      pointImg: require("../../../public/01.svg"),
      pointSize: 10, //点的大小
      banMa: [],
    };
  },

 

mounted() {
    document.getElementById("myBiaoZhu").oncontextmenu = e => {
      if (e && e.preventDefault) {
        //阻止默认浏览器动作(W3C)
        e.preventDefault();
      } else {
        //IE中阻止函数器默认动作的方式
        window.event.returnValue = false;
      }
      return false;
    }; //阻止冒泡行为和默认右键菜单事件
    // console.log(196, this.parseImgUr == undefined);

    document.getElementById("myBiaoZhu").onmousedown = e => {
      e = e || window.event;
      // console.log(this.serialNumber);
      // console.log(this.serialNumber.length);
      // debugger;
      if (this.serialNumber.length == 0 && this.parseImgUrl.length > 0) {
        // console.log(this.banMa.length);
        this.$refs.selectEquipment.visible = true;
        this.$refs.selectEquipment.formConfig.isShowSubmitBtn = true;
        return false;
      }
      if (e.button !== 2 ) {
        //判断是否右击
        /*
      event.screenX是屏幕左上角到鼠标当前位置的x轴距离;
      event.clientX是浏览器左上角到鼠标当前位置的x轴距离;
      event.setoffX是鼠标当前点击控件左上角到鼠标当前位置的x轴距离;
      */
        var x = e.offsetX || e.layerX;
        var y = e.offsetY || e.layerY;
        console.log(x, y);
        var myImg = document.querySelector("#myBiaoZhu");
        var currWidth = myImg.clientWidth;
        var currHeight = myImg.clientHeight;
        var ProportionWidthInImg = x / currWidth;
        var ProportionHeightInImg = y / currHeight;
        // console.log("图片比例高度:"+ProportionHeightInImg)
        // console.log("图片比例宽度:"+ProportionWidthInImg)
        this.banMa.push({
          // id: this.banMa.length + 1,
          positionLat: x,
          positionLng: y,
  
        });
        this.createMarker(x, y);
      }
    };
  },

 


methods: {
      //画点
      createMarker(x, y) {
      var div = document.createElement("div");
      div.className = "marker";
      div.id = "marker" + this.banMa.length;
      y =
        y + document.getElementById("myBiaoZhu").offsetTop - this.pointSize / 2;
      x =
        x +
        document.getElementById("myBiaoZhu").offsetLeft -
        this.pointSize / 2;
      div.style.width = this.pointSize * 4.4 + "px";
      div.style.height = this.pointSize * 5.8 + "px";
      // div.style.backgroundColor = this.pointColor;
      div.style.background = "url(" + this.pointImg + ") no-repeat";
      div.style.position = "absolute";
      div.style.left = x + "px";
      div.style.top = y + "px";
      div.oncontextmenu = e => {
        //阻止冒泡行为和默认右键菜单事件,同时删除该点
        var id = e.target.id;
        document.getElementById("myBiaoZhuDiv").removeChild(div);
        // this.banMa = this.banMa.filter(
        //   item => item.id != id.slice(6, id.length)
        // );
        this.banMa = [];
        this.banMa.push({ regionId: this.regionId });
        this.unbunding(); // 解绑接口 用不到删除即可
        if (e && e.preventDefault) {
          //阻止默认浏览器动作(W3C)
          e.preventDefault();
        } else {
          //IE中阻止函数器默认动作的方式
          window.event.returnValue = false;
        }
        return false;
      };
      document.getElementById("myBiaoZhuDiv").appendChild(div);
      this.submitEqArr();  // 提交接口 用不到删除即可
    },
    },

 参考链接:https://blog.csdn.net/m0_46627730/article/details/106583908?utm_term=vue%E5%9B%BE%E7%89%87%E7%82%B9%E5%87%BB%E4%BD%8D%E7%BD%AE&utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2~all~sobaiduweb~default-1-106583908&spm=3001.4430

要实现图片标注、放大、缩小并存储标注数据,可以使用以下步骤: 1. 加载图片:使用Vue的`<img>`标签加载图片。 2. 标注图片:使用Vue的`<canvas>`标签绘制图片,通过鼠标事件监听用户的标注操作,将标注数据存储在Vue的数据中。 3. 放大、缩小:通过CSS的`transform`属性实现图片的放大、缩小效果,同时也需要更新绘制标注的画布大小和坐标。 4. 存储标注数据:将标注数据存储在本地存储或后端数据库中,可以使用Vue的`localStorage`或`axios`库实现。 下面是一个简单的示例代码: ``` <template> <div> <img ref="image" :src="imageUrl" @load="onImageLoad"> <div ref="canvasWrapper" class="canvas-wrapper"> <canvas ref="canvas" @mousedown="onMouseDown" @mousemove="onMouseMove" @mouseup="onMouseUp"></canvas> </div> </div> </template> <script> export default { data() { return { imageUrl: 'https://image.url', imageWidth: 0, imageHeight: 0, canvasWidth: 0, canvasHeight: 0, canvasLeft: 0, canvasTop: 0, isMouseDown: false, startX: 0, startY: 0, annotations: [] } }, methods: { onImageLoad() { this.imageWidth = this.$refs.image.width this.imageHeight = this.$refs.image.height this.canvasWidth = this.imageWidth this.canvasHeight = this.imageHeight this.$refs.canvas.width = this.canvasWidth this.$refs.canvas.height = this.canvasHeight this.canvasLeft = this.$refs.canvasWrapper.offsetLeft this.canvasTop = this.$refs.canvasWrapper.offsetTop this.drawAnnotations() }, onMouseDown(event) { this.isMouseDown = true this.startX = event.pageX - this.canvasLeft this.startY = event.pageY - this.canvasTop }, onMouseMove(event) { if (this.isMouseDown) { const x = event.pageX - this.canvasLeft const y = event.pageY - this.canvasTop const width = x - this.startX const height = y - this.startY const annotation = { x: this.startX, y: this.startY, width, height } this.annotations.push(annotation) this.drawAnnotations() } }, onMouseUp() { this.isMouseDown = false }, drawAnnotations() { const ctx = this.$refs.canvas.getContext('2d') ctx.clearRect(0, 0, this.canvasWidth, this.canvasHeight) ctx.drawImage(this.$refs.image, 0, 0, this.imageWidth, this.imageHeight) for (const annotation of this.annotations) { ctx.strokeRect(annotation.x, annotation.y, annotation.width, annotation.height) } }, zoomIn() { this.canvasWidth *= 1.2 this.canvasHeight *= 1.2 this.$refs.canvas.width = this.canvasWidth this.$refs.canvas.height = this.canvasHeight this.drawAnnotations() }, zoomOut() { this.canvasWidth /= 1.2 this.canvasHeight /= 1.2 this.$refs.canvas.width = this.canvasWidth this.$refs.canvas.height = this.canvasHeight this.drawAnnotations() }, saveAnnotations() { localStorage.setItem('annotations', JSON.stringify(this.annotations)) }, loadAnnotations() { const annotations = JSON.parse(localStorage.getItem('annotations')) if (annotations) { this.annotations = annotations this.drawAnnotations() } } } } </script> <style scoped> .canvas-wrapper { position: relative; overflow: hidden; } canvas { position: absolute; top: 0; left: 0; } </style> ```
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值