canvas绘制大屏xy轴 辅助线 让组件实现对齐功能 还包括 取消隐藏 下方缩放组件的变化 这边的单位也跟着变化

采用 div布局让 整个rule组件固定定位在一个地方
固定一处的是尺子
辅助线是跟着你点击的位置而生成的 left 点击位置距离0点距离生成

以下是一个简单的多边形绘制 Vue 组件,具有拖拽和缩放功能: ``` <template> <div class="polygon" :style="polygonStyles"> <div class="handle top-left" @mousedown="startDrag($event, 'top-left')"></div> <div class="handle top-right" @mousedown="startDrag($event, 'top-right')"></div> <div class="handle bottom-left" @mousedown="startDrag($event, 'bottom-left')"></div> <div class="handle bottom-right" @mousedown="startDrag($event, 'bottom-right')"></div> <canvas ref="canvas" @mousedown="startDraw" @mousemove="draw" @mouseup="endDraw"></canvas> </div> </template> <script> export default { data() { return { drawing: false, points: [], dragHandle: null, mouseStart: null, polygonStyles: { position: 'absolute', top: '0', left: '0', width: '100%', height: '100%', cursor: 'crosshair' } }; }, mounted() { this.resizeCanvas(); window.addEventListener('resize', this.resizeCanvas); }, beforeDestroy() { window.removeEventListener('resize', this.resizeCanvas); }, methods: { resizeCanvas() { const canvas = this.$refs.canvas; canvas.width = canvas.offsetWidth; canvas.height = canvas.offsetHeight; }, startDraw(event) { this.drawing = true; this.mouseStart = this.getMousePosition(event); this.points = [this.mouseStart]; }, draw(event) { if (this.drawing) { const point = this.getMousePosition(event); const canvas = this.$refs.canvas; const context = canvas.getContext('2d'); context.clearRect(0, 0, canvas.width, canvas.height); context.beginPath(); context.moveTo(this.points[0].x, this.points[0].y); for (let i = 1; i < this.points.length; i++) { context.lineTo(this.points[i].x, this.points[i].y); } context.lineTo(point.x, point.y); context.stroke(); } }, endDraw(event) { this.drawing = false; this.mouseStart = null; }, startDrag(event, handle) { this.dragHandle = handle; this.mouseStart = this.getMousePosition(event); }, drag(event) { if (this.dragHandle) { const mouseCurrent = this.getMousePosition(event); const deltaX = mouseCurrent.x - this.mouseStart.x; const deltaY = mouseCurrent.y - this.mouseStart.y; const polygon = this.$el; const polygonRect = polygon.getBoundingClientRect(); const polygonStyles = window.getComputedStyle(polygon); let newWidth = parseInt(polygonStyles.width) + deltaX; let newHeight = parseInt(polygonStyles.height) + deltaY; if (this.dragHandle.includes('left')) { polygon.style.left = (polygonRect.left + deltaX) + 'px'; newWidth = parseInt(polygonStyles.width) - deltaX; } if (this.dragHandle.includes('top')) { polygon.style.top = (polygonRect.top + deltaY) + 'px'; newHeight = parseInt(polygonStyles.height) - deltaY; } polygon.style.width = newWidth + 'px'; polygon.style.height = newHeight + 'px'; this.mouseStart = mouseCurrent; } }, endDrag() { this.dragHandle = null; this.mouseStart = null; }, getMousePosition(event) { const canvas = this.$refs.canvas; const rect = canvas.getBoundingClientRect(); return { x: event.clientX - rect.left, y: event.clientY - rect.top }; } } }; </script> <style> .polygon { position: relative; display: inline-block; } .handle { position: absolute; width: 10px; height: 10px; background-color: #fff; border: 1px solid #000; cursor: move; } .top-left { top: -5px; left: -5px; } .top-right { top: -5px; right: -5px; } .bottom-left { bottom: -5px; left: -5px; } .bottom-right { bottom: -5px; right: -5px; } </style> ``` 这个组件包含一个 `canvas` 元素,可以用鼠标绘制多边形。同时,组件还包含四个拖拽手柄,可以用来缩放多边形。当用户按下鼠标时,可以通过 `mousedown` 事件开始绘制多边形或拖拽多边形。当用户移动鼠标时,可以通过 `mousemove` 事件更新多边形或手柄的位置。当用户松开鼠标时,可以通过 `mouseup` 事件结束绘制或拖拽操作。 注意,这个组件只是一个简单的示例,可能不适用于所有情况。如果需要更高级的多边形绘制缩放功能,可以考虑使用第三方库,如 Konva.js 或 Fabric.js。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值