双层拖拽事件,用鼠标画矩形,拖动右下角可以再次改变矩形大小,方案一 有BUG

本文主要是介绍双层拖拽事件,用鼠标画矩形,拖动右下角可以再次改变矩形大小,方案一 有BUG,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在这里插入图片描述

<template><div class="mycanvas-container"><vue-drag-resize :isActive = 'true'/> <div class="left"><p>展示视口</p><div class="myshow"><img :src="mysrc" alt width="100%" /><!-- <div id="canvas" style="width:100%;height:100%" class="mycanvas"></div> --></div></div><div class="center"><p>操作视口</p><div class="myedit" ref="myedit"  @mousedown.prevent="" @mousemove.prevent="" @mouseup.prevent=""@contextmenu.prevent=""><!-- <img src="@/assets/jia.svg" alt=""> --><img :src="mysrc"  @mousedown.prevent="onMousedown" @mousemove="onMousemove" /><divclass="myedit-span"v-for="(item, index) in mydata":key="index":style="getSpanStyle(item)"@contextmenu.prevent="onContextmenu(item, $event)"><div class="br" @mousedown.prevent="onMousedownbr(item,$event)"@mousemove="onMousemovebr(item,$event)"@mouseup="onMouseupbr(item,$event)"></div></div><!-- <div id="canvas" style="width:100%;height:100%" class="mycanvas"></div> --></div></div><div class="right"><img src alt class="mybutton" /><input v-show="0" ref="file" type="file" class="mybutton" @change="onChange" /><button class="mybutton" @click="selectFile">导入图片</button><button class="mybutton">新增标注</button><button class="mybutton">修改标注</button><button class="mybutton">删除</button><button class="mybutton">保存</button></div><div class="myMenu" v-show="mymenu.current" :style="mymenu.style"><!-- <button @click="onRemoveItem">删除</button> --><ui-button type="primary" @click="onRemoveItem">删除</ui-button><ui-select></ui-select><select name="" id=""><option value="1">ceshi</option></select></div></div>
</template><script>import jiaIcon from "./jia.svg";
export default {data() {return {mysrc: "",mydata: [],mymenu: { current: null, style: { left: 0, top: 0 } }};},mounted() {document.addEventListener('mouseup', this.onMouseup)this.getData()},beforeDestroy() {document.removeEventListener('mouseup', this.onMouseup)},methods: {getData(){let url='/index'this.axios(url,{params:{status:1}}).then(data=>{console.log(data)}).catch(err => this.$Message.error(err.message))},getXY(e) {let rect = this.$refs.myedit.getBoundingClientRect()return {x: e.clientX - rect.left,y: e.clientY - rect.top}},// 上传图片1onChange(e) {this.mysrc = window.URL.createObjectURL(e.target.files[0]);e.target.value = ''},// 上传图片2,selectFile() {this.$refs.file.click();},// 矩形右下角拖动事件1onMousedownbr(item,e){e.target.removeEventListener('mousemove',this.onMousemove)e.target.removeEventListener('mouseup',this.onMouseup)this.canmove=trueconsole.log(1)console.log(this.getXY(e))this.startPosbr=this.getXY(e)console.log(2)console.log(this.startPosbr)e.target.addEventListener('mousemove',this.onMousemovebr)e.target.addEventListener('mouseup',this.onMouseupbr)},onMousemovebr(item,e){if(this.canmove){let { x, y } = this.getXY(e)console.log(3)console.log(this.getXY(e))item.w=item.w+(x-this.startPosbr.x)item.h=item.h+(y-this.startPosbr.y)console.log(4)console.log(item.w)// Math.sqrt(9)// 9**.5}},onMouseupbr(item,e){this.canmove=falsethis.startPos =this.startPosbr= null;e.target.removeEventListener('mousemove',this.onMousemovebr)e.target.removeEventListener('mouseup',this.onMouseupbr)},// 矩形右下角拖动事件2onMousedown(e) {e.target.addEventListener('mousemove',this.onMousemove)e.target.addEventListener('mouseup',this.onMouseup)this.mymenu.current = nulllet { x, y } = this.getXY(e)this.currentItem = { x, y, w: 0, h: 0, now: Date.now() }this.startPos = { x, y }this.mydata.push(this.currentItem)},onMousemove(e) {if (!this.currentItem) return;let { x, y } = this.getXY(e)this.currentItem.w = Math.abs(x - this.startPos.x)this.currentItem.h = Math.abs(y - this.startPos.y)},onMouseup(e) {this.currentItem = this.startPos =this.startPosbr= null;// this.mydata = this.mydata.filter(_ => _.w > 10 && _.h > 10)e.target.removeEventListener('mousemove',this.onMousemove)e.target.removeEventListener('mouseup',this.onMouseup)},onContextmenu(item, e) {this.mymenu = {current: item,style: {top: e.clientY + 'px',left: e.clientX + 'px'}}},onRemoveItem() {this.mydata.splice(this.mydata.indexOf(this.mymenu.current), 1)this.mymenu = { ...this.mymenu, current: null }},getSpanStyle(item) {return {width: `${item.w}px`,height: `${item.h}px`,top: `${item.y}px`,left: `${item.x}px`};}}
};
</script>
<style lang="less" scoped>
// 设置绘图样式1
body {user-select: none;
}.myMenu {position: fixed;top: 400px;left: 400px;width: 100px;padding: 8px 0;background-color: #fff;> * {width: 100%;}
}#canvas > div {/* border: 2px solid green; */position: absolute;background-color: transparent;
}#canvas > div > span {position: absolute;top: 50%;left: 50%;transform: translate(-50%, -50%);font-family: simsun;font-size: 9pt;
}// 设置绘图样式2.mycanvas-container {display: flex;justify-content: center;align-items: center;.left,.center,.right {width: 300px;// height: 520px;margin: 20px;p {text-align: center;}.myshow,.myedit {width: 300px;// height: 500px;border: 1px solid #000;position: relative;.myedit-span {position: absolute;border: 1px dashed #fff;// background: url("./jia.svg") no-repeat center center;background-size: contain;}.br,.divcenter{width: 10px;height: 10px;position: absolute;border: 1px solid #f00;background: #fff;border-radius: 50%;bottom:-5px;right:-5px;cursor:nwse-resize;}.divcenter{top:50%;left:50%;transform:translate(-5px ,-5px);cursor:move;}.mycanvas {border: 1px solid pink;position: absolute;top: 0;left: 0;}img {width: 100%;}}}.right {width: 150px;display: flex;justify-content: center;align-items: left;flex-direction: column;.mybutton {margin-top: 20px;display: block;}}
}
</style>

这篇关于双层拖拽事件,用鼠标画矩形,拖动右下角可以再次改变矩形大小,方案一 有BUG的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/805179

相关文章

SpringBoot服务获取Pod当前IP的两种方案

《SpringBoot服务获取Pod当前IP的两种方案》在Kubernetes集群中,SpringBoot服务获取Pod当前IP的方案主要有两种,通过环境变量注入或通过Java代码动态获取网络接口IP... 目录方案一:通过 Kubernetes Downward API 注入环境变量原理步骤方案二:通过

Springboot3+将ID转为JSON字符串的详细配置方案

《Springboot3+将ID转为JSON字符串的详细配置方案》:本文主要介绍纯后端实现Long/BigIntegerID转为JSON字符串的详细配置方案,s基于SpringBoot3+和Spr... 目录1. 添加依赖2. 全局 Jackson 配置3. 精准控制(可选)4. OpenAPI (Spri

关于跨域无效的问题及解决(java后端方案)

《关于跨域无效的问题及解决(java后端方案)》:本文主要介绍关于跨域无效的问题及解决(java后端方案),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录通用后端跨域方法1、@CrossOrigin 注解2、springboot2.0 实现WebMvcConfig

在Java中将XLS转换为XLSX的实现方案

《在Java中将XLS转换为XLSX的实现方案》在本文中,我们将探讨传统ExcelXLS格式与现代XLSX格式的结构差异,并为Java开发者提供转换方案,通过了解底层原理、性能优势及实用工具,您将掌握... 目录为什么升级XLS到XLSX值得投入?实际转换过程解析推荐技术方案对比Apache POI实现编程

Java实现本地缓存的常用方案介绍

《Java实现本地缓存的常用方案介绍》本地缓存的代表技术主要有HashMap,GuavaCache,Caffeine和Encahche,这篇文章主要来和大家聊聊java利用这些技术分别实现本地缓存的方... 目录本地缓存实现方式HashMapConcurrentHashMapGuava CacheCaffe

无法启动此程序因为计算机丢失api-ms-win-core-path-l1-1-0.dll修复方案

《无法启动此程序因为计算机丢失api-ms-win-core-path-l1-1-0.dll修复方案》:本文主要介绍了无法启动此程序,详细内容请阅读本文,希望能对你有所帮助... 在计算机使用过程中,我们经常会遇到一些错误提示,其中之一就是"api-ms-win-core-path-l1-1-0.dll丢失

利用Python实现可回滚方案的示例代码

《利用Python实现可回滚方案的示例代码》很多项目翻车不是因为不会做,而是走错了方向却没法回头,技术选型失败的风险我们都清楚,但真正能提前规划“回滚方案”的人不多,本文从实际项目出发,教你如何用Py... 目录描述题解答案(核心思路)题解代码分析第一步:抽象缓存接口第二步:实现两个版本第三步:根据 Fea

SpringBoot实现接口数据加解密的三种实战方案

《SpringBoot实现接口数据加解密的三种实战方案》在金融支付、用户隐私信息传输等场景中,接口数据若以明文传输,极易被中间人攻击窃取,SpringBoot提供了多种优雅的加解密实现方案,本文将从原... 目录一、为什么需要接口数据加解密?二、核心加解密算法选择1. 对称加密(AES)2. 非对称加密(R

MySQL精准控制Binlog日志数量的三种方案

《MySQL精准控制Binlog日志数量的三种方案》作为数据库管理员,你是否经常为服务器磁盘爆满而抓狂?Binlog就像数据库的“黑匣子”,默默记录着每一次数据变动,但若放任不管,几天内这些日志文件就... 目录 一招修改配置文件:永久生效的控制术1.定位my.cnf文件2.添加核心参数不重启热更新:高手应

MySQL中like模糊查询的优化方案

《MySQL中like模糊查询的优化方案》在MySQL中,like模糊查询是一种常用的查询方式,但在某些情况下可能会导致性能问题,本文将介绍八种优化MySQL中like模糊查询的方法,需要的朋友可以参... 目录1. 避免以通配符开头的查询2. 使用全文索引(Full-text Index)3. 使用前缀索