Commit 814989b2 authored by liujiangnan's avatar liujiangnan

feat: 矩形可以移动

parent ce52db0f
......@@ -398,25 +398,31 @@ html, body {
/* 矩形框样式 */
.text-rect {
position: absolute;
border: 2px dashed #409EFF;
border: 2px solid rgba(64, 158, 255, 0.7);
background-color: rgba(64, 158, 255, 0.1);
cursor: move;
transform-origin: top left;
user-select: none;
}
.text-rect.selected {
border-color: #67C23A;
background-color: rgba(103, 194, 58, 0.1);
border-color: #409EFF;
background-color: rgba(64, 158, 255, 0.2);
z-index: 2;
}
.text-rect.highlighted {
border-color: #E6A23C;
background-color: rgba(230, 162, 60, 0.2);
border-color: #67C23A;
background-color: rgba(103, 194, 58, 0.2);
z-index: 3;
}
/* 预览模式下禁用拖拽 */
.preview-mode .text-rect {
pointer-events: none;
cursor: default;
opacity: 0.7;
}
.text-content {
position: absolute;
bottom: -20px;
......
......@@ -93,7 +93,7 @@ new Vue({
let data = await new Promise((resolve) => {
window.courseware.getData((data) => {
resolve(data);
});
}, t_name);
});
if (!data) {
const response = await fetch('./defaultData.json');
......@@ -834,24 +834,17 @@ new Vue({
getRectStyle(rect) {
if (!rect || !this.imageLoaded || !this.imageRect) return { display: 'none' };
// 将字符串转换为数值
const x = parseFloat(rect.x);
const y = parseFloat(rect.y);
const width = parseFloat(rect.width);
const height = parseFloat(rect.height);
// 计算实际的像素位置
const actualX = x * this.imageWidth;
const actualY = y * this.imageHeight;
const actualWidth = width * this.imageWidth;
const actualHeight = height * this.imageHeight;
return {
position: 'absolute',
left: actualX + 'px',
top: actualY + 'px',
width: actualWidth + 'px',
height: actualHeight + 'px'
left: (x * 100) + '%',
top: (y * 100) + '%',
width: (width * 100) + '%',
height: (height * 100) + '%'
};
},
......@@ -871,18 +864,34 @@ new Vue({
// 开始拖拽
startDrag(event, groupIndex, itemIndex) {
if (event.target.classList.contains('resize-handle') || !this.imageLoaded || this.isPreviewMode) return;
if (event.target.classList.contains('resize-handle')) return;
if (this.isPreviewMode) return;
// 获取图片容器的位置信息
const imageWrapper = this.$refs.imageContainer.querySelector('.image-wrapper');
const imageRect = imageWrapper.getBoundingClientRect();
this.isDragging = true;
this.dragStartX = event.clientX;
this.dragStartY = event.clientY;
this.dragStartX = event.clientX - imageRect.left;
this.dragStartY = event.clientY - imageRect.top;
this.selectedGroup = groupIndex;
this.currentGroup = this.currentPage.groups[groupIndex];
this.selectedItemIndex = itemIndex;
this.originalRect = this.currentGroup.group[itemIndex].rect ? { ...this.currentGroup.group[itemIndex].rect } : null;
const rect = this.currentGroup.group[itemIndex].rect;
this.originalRect = {
x: rect ? rect.x : 0.2,
y: rect ? rect.y : 0.2,
width: rect ? rect.width : 0.2,
height: rect ? rect.height : 0.2
};
// 记录初始点击位置相对于矩形框的偏移
this.dragOffsetX = this.dragStartX - (this.originalRect.x * this.imageWidth);
this.dragOffsetY = this.dragStartY - (this.originalRect.y * this.imageHeight);
event.preventDefault();
event.stopPropagation(); // 阻止事件冒泡
event.stopPropagation();
},
// 开始调整大小
......@@ -902,42 +911,58 @@ new Vue({
// 处理鼠标移动
handleMouseMove(event) {
if ((!this.isDragging && !this.isResizing) || !this.currentGroup || !this.originalRect || !this.imageLoaded) return;
if (!this.isDragging && !this.isResizing) return;
if (!this.currentGroup || !this.originalRect) return;
// 获取图片容器的位置信息
const imageWrapper = this.$refs.imageContainer.querySelector('.image-wrapper');
const imageRect = imageWrapper.getBoundingClientRect();
const currentRect = this.currentGroup.group[this.selectedItemIndex].rect;
if (!currentRect || !this.imageRect) return;
if (!currentRect) return;
// 计算鼠标移动的相对距离
const deltaX = (event.clientX - this.dragStartX) / this.imageWidth;
const deltaY = (event.clientY - this.dragStartY) / this.imageHeight;
if (this.isDragging) {
// 计算新位置,确保不超出图片边界
currentRect.x = Math.max(0, Math.min(1 - currentRect.width, this.originalRect.x + deltaX));
currentRect.y = Math.max(0, Math.min(1 - currentRect.height, this.originalRect.y + deltaY));
// 计算鼠标相对于图片容器的位置
const mouseX = event.clientX - imageRect.left;
const mouseY = event.clientY - imageRect.top;
// 考虑拖拽起始点的偏移
let newX = (mouseX - this.dragOffsetX) / this.imageWidth;
let newY = (mouseY - this.dragOffsetY) / this.imageHeight;
// 限制在图片范围内
newX = Math.max(0, Math.min(1 - currentRect.width, newX));
newY = Math.max(0, Math.min(1 - currentRect.height, newY));
// 更新位置
this.$set(currentRect, 'x', newX);
this.$set(currentRect, 'y', newY);
} else if (this.isResizing) {
// 计算最小宽度和高度(5像素对应的相对值)
const minWidth = 5 / this.imageWidth;
const minHeight = 5 / this.imageHeight;
// 计算新尺寸,确保不超出图片边界且不小于5像素
currentRect.width = Math.max(minWidth, Math.min(1 - currentRect.x, this.originalRect.width + deltaX));
currentRect.height = Math.max(minHeight, Math.min(1 - currentRect.y, this.originalRect.height + deltaY));
// 计算鼠标相对于图片的位置
const mouseX = event.clientX - imageRect.left;
const mouseY = event.clientY - imageRect.top;
// 计算新的宽度和高度
const newWidth = Math.max(5, mouseX - (this.originalRect.x * this.imageWidth)) / this.imageWidth;
const newHeight = Math.max(5, mouseY - (this.originalRect.y * this.imageHeight)) / this.imageHeight;
// 限制大小
currentRect.width = Math.min(1 - currentRect.x, newWidth);
currentRect.height = Math.min(1 - currentRect.y, newHeight);
}
},
// 处理鼠标松开
handleMouseUp(event) {
// 如果是拖拽或调整大小操作,阻止后续的 click 事件
if (this.isDragging || this.isResizing) {
event.preventDefault();
event.stopPropagation();
this.isDragging = false;
this.isResizing = false;
this.dragStartX = null;
this.dragStartY = null;
this.dragOffsetX = null;
this.dragOffsetY = null;
this.originalRect = null;
}
this.isDragging = false;
this.isResizing = false;
},
// 更新音频区域
......@@ -1982,7 +2007,14 @@ new Vue({
save() {
if (!this.isDataModified) return;
console.log('----save----');
let data = {
audio_points: this.audioPoints,
pages: this.pages
};
window.courseware.setData(data, () => {
console.log('----save----');
this.isDataModified = false;
},t_name);
}
},
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment