双层拖拽事件,用鼠标画矩形,拖动右下角可以再次改变矩形大小,方案一 有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基于注解实现数据库字段回填的完整方案

《SpringBoot基于注解实现数据库字段回填的完整方案》这篇文章主要为大家详细介绍了SpringBoot如何基于注解实现数据库字段回填的相关方法,文中的示例代码讲解详细,感兴趣的小伙伴可以了解... 目录数据库表pom.XMLRelationFieldRelationFieldMapping基础的一些代

前端缓存策略的自解方案全解析

《前端缓存策略的自解方案全解析》缓存从来都是前端的一个痛点,很多前端搞不清楚缓存到底是何物,:本文主要介绍前端缓存的自解方案,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录一、为什么“清缓存”成了技术圈的梗二、先给缓存“把个脉”:浏览器到底缓存了谁?三、设计思路:把“发版”做成“自愈”四、代码

解决docker目录内存不足扩容处理方案

《解决docker目录内存不足扩容处理方案》文章介绍了Docker存储目录迁移方法:因系统盘空间不足,需将Docker数据迁移到更大磁盘(如/home/docker),通过修改daemon.json配... 目录1、查看服务器所有磁盘的使用情况2、查看docker镜像和容器存储目录的空间大小3、停止dock

Spring Gateway动态路由实现方案

《SpringGateway动态路由实现方案》本文主要介绍了SpringGateway动态路由实现方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随... 目录前沿何为路由RouteDefinitionRouteLocator工作流程动态路由实现尾巴前沿S

分析 Java Stream 的 peek使用实践与副作用处理方案

《分析JavaStream的peek使用实践与副作用处理方案》StreamAPI的peek操作是中间操作,用于观察元素但不终止流,其副作用风险包括线程安全、顺序混乱及性能问题,合理使用场景有限... 目录一、peek 操作的本质:有状态的中间操作二、副作用的定义与风险场景1. 并行流下的线程安全问题2. 顺

C#实现高性能拍照与水印添加功能完整方案

《C#实现高性能拍照与水印添加功能完整方案》在工业检测、质量追溯等应用场景中,经常需要对产品进行拍照并添加相关信息水印,本文将详细介绍如何使用C#实现一个高性能的拍照和水印添加功能,包含完整的代码实现... 目录1. 概述2. 功能架构设计3. 核心代码实现python3.1 主拍照方法3.2 安全HBIT

MyBatis Plus实现时间字段自动填充的完整方案

《MyBatisPlus实现时间字段自动填充的完整方案》在日常开发中,我们经常需要记录数据的创建时间和更新时间,传统的做法是在每次插入或更新操作时手动设置这些时间字段,这种方式不仅繁琐,还容易遗漏,... 目录前言解决目标技术栈实现步骤1. 实体类注解配置2. 创建元数据处理器3. 服务层代码优化填充机制详

防止Linux rm命令误操作的多场景防护方案与实践

《防止Linuxrm命令误操作的多场景防护方案与实践》在Linux系统中,rm命令是删除文件和目录的高效工具,但一旦误操作,如执行rm-rf/或rm-rf/*,极易导致系统数据灾难,本文针对不同场景... 目录引言理解 rm 命令及误操作风险rm 命令基础常见误操作案例防护方案使用 rm编程 别名及安全删除

Python实现批量CSV转Excel的高性能处理方案

《Python实现批量CSV转Excel的高性能处理方案》在日常办公中,我们经常需要将CSV格式的数据转换为Excel文件,本文将介绍一个基于Python的高性能解决方案,感兴趣的小伙伴可以跟随小编一... 目录一、场景需求二、技术方案三、核心代码四、批量处理方案五、性能优化六、使用示例完整代码七、小结一、

C#使用Spire.Doc for .NET实现HTML转Word的高效方案

《C#使用Spire.Docfor.NET实现HTML转Word的高效方案》在Web开发中,HTML内容的生成与处理是高频需求,然而,当用户需要将HTML页面或动态生成的HTML字符串转换为Wor... 目录引言一、html转Word的典型场景与挑战二、用 Spire.Doc 实现 HTML 转 Word1