This commit is contained in:
X1627315083
2026-01-28 16:11:55 +08:00
parent c8cb2de9ab
commit d94ade6641
2 changed files with 95 additions and 57 deletions

View File

@@ -12,7 +12,6 @@
is-edit is-edit
:clothingImageUrl="selectDetail.path" :clothingImageUrl="selectDetail.path"
:clothingImageUrl2="selectDetail.maskUrl || selectDetail.layersObject[0].maskUrl" :clothingImageUrl2="selectDetail.maskUrl || selectDetail.layersObject[0].maskUrl"
:clothingMinIOPath="selectDetail.minIOPath"
showFixedLayer showFixedLayer
:canvasJSON="canvasJSON" :canvasJSON="canvasJSON"
@canvasLoadJsonSuccess="canvasLoadJsonSuccess" @canvasLoadJsonSuccess="canvasLoadJsonSuccess"

View File

@@ -222,25 +222,35 @@ export default defineComponent({
const currentTransform = element.style?.transform || ''; const currentTransform = element.style?.transform || '';
const currentRotates = (currentTransform.match(/rotate[XYZ]?\([^)]+\)/g) || []); const currentRotates = (currentTransform.match(/rotate[XYZ]?\([^)]+\)/g) || []);
let otherTransforms = currentTransform let otherTransforms = currentTransform
.replace(/scaleX\(-1\)|scaleY\(-1\)/g, '') .replace(/scaleX\([^)]*\)/g, '') // 移除所有 scaleX(...)
.replace(/scaleY\([^)]*\)/g, '') // 移除所有 scaleY(...)
.replace(/rotate[XYZ]?\([^)]+\)/g, '') .replace(/rotate[XYZ]?\([^)]+\)/g, '')
.replace(/\s+/g, ' ') .replace(/\s+/g, ' ')
.trim(); .trim();
const transforms = []; const transforms = [];
if (element.mirror.x) transforms.push('scaleX(-1)'); if (element.mirror && element.mirror.x) transforms.push('scaleX(-1)');
if (element.mirror.y) transforms.push('scaleY(-1)'); if (element.mirror && element.mirror.y) transforms.push('scaleY(-1)');
if (otherTransforms) transforms.push(otherTransforms); if (otherTransforms) transforms.push(otherTransforms);
if (rotateDeg !== null) { if (rotateDeg !== null) {
transforms.push(`rotate(${rotateDeg}deg)`); transforms.push(`rotate(${rotateDeg}deg)`);
} else if (currentRotates.length > 0) { } else if (currentRotates.length > 0) {
transforms.push(...currentRotates); transforms.push(...currentRotates);
} }
element.style.transform = transforms.join(' ').trim(); element.style.transform = transforms.join(' ').trim();
}; };
moveableInstance.value.on('scale', ({ target, delta, direction }) => { moveableInstance.value.on('scale', ({ target, delta, direction }) => {
const frontStyle = detailData.frontBack.front[selectItem.imgDomIndex]; const frontStyle = detailData.frontBack.front[selectItem.imgDomIndex];
if (!frontStyle.mirror) frontStyle.mirror = { x: false, y: false };
// 确保 mirror 对象存在并正确初始化
if (!frontStyle.mirror) {
frontStyle.mirror = { x: false, y: false };
}
// 清除可能存在的重复镜像变换
updateElementTransform(frontStyle);
let width = parseFloat(frontStyle.style.width); let width = parseFloat(frontStyle.style.width);
let height = parseFloat(frontStyle.style.height); let height = parseFloat(frontStyle.style.height);
@@ -249,6 +259,8 @@ export default defineComponent({
let rotation = 0; let rotation = 0;
const originalRatio = width / height; const originalRatio = width / height;
// 提取当前旋转角度
if (frontStyle.style.transform) { if (frontStyle.style.transform) {
const match = frontStyle.style.transform.match(/rotate\(([-\d.]+)deg\)/); const match = frontStyle.style.transform.match(/rotate\(([-\d.]+)deg\)/);
if (match) rotation = parseFloat(match[1]); if (match) rotation = parseFloat(match[1]);
@@ -259,8 +271,13 @@ export default defineComponent({
const x = corner.x * Math.cos(rad) - corner.y * Math.sin(rad); const x = corner.x * Math.cos(rad) - corner.y * Math.sin(rad);
const y = corner.x * Math.sin(rad) + corner.y * Math.cos(rad); const y = corner.x * Math.sin(rad) + corner.y * Math.cos(rad);
const threshold = 0.5; const threshold = 0.5;
return { x: Math.abs(x) > threshold ? (x > 0 ? 1 : -1) : 0, y: Math.abs(y) > threshold ? (y > 0 ? 1 : -1) : 0 }; return {
x: Math.abs(x) > threshold ? (x > 0 ? 1 : -1) : 0,
y: Math.abs(y) > threshold ? (y > 0 ? 1 : -1) : 0
};
}; };
// 根据旋转角度调整方向
if (rotation !== 0) { if (rotation !== 0) {
direction = getAdjustedCorner({ x: direction[0], y: direction[1] }, rotation); direction = getAdjustedCorner({ x: direction[0], y: direction[1] }, rotation);
direction = [direction.x, direction.y]; direction = [direction.x, direction.y];
@@ -274,25 +291,29 @@ export default defineComponent({
let newVal = val * deltaVal; let newVal = val * deltaVal;
// 翻转处理 // 翻转处理 - 只在值真正变为负值时触发镜像
if (newVal <= 0) { if (newVal < 0) {
frontStyle.mirror[mirrorKey] = !frontStyle.mirror[mirrorKey]; frontStyle.mirror[mirrorKey] = !frontStyle.mirror[mirrorKey];
newVal = Math.abs(newVal); newVal = Math.abs(newVal);
updateElementTransform(frontStyle); } else if (newVal === 0) {
// 防止值变为0
newVal = 1;
} }
// 位置调整 // 位置调整
const shouldMove = (!frontStyle.mirror[mirrorKey] && dir === -1) || (frontStyle.mirror[mirrorKey] && dir === 1); const shouldMove = (!frontStyle.mirror[mirrorKey] && dir === -1) || (frontStyle.mirror[mirrorKey] && dir === 1);
// 保持宽高比
if (keepRatio && otherAxisResult) { if (keepRatio && otherAxisResult) {
newVal = isWidth ? otherAxisResult.newVal * originalRatio : otherAxisResult.newVal / originalRatio; newVal = isWidth ? otherAxisResult.newVal * originalRatio : otherAxisResult.newVal / originalRatio;
} }
const adjustPos = shouldMove ? pos - (newVal - val) : pos; const adjustPos = shouldMove ? pos - (newVal - val) : pos;
return { newVal, adjustPos, shouldFlip: newVal !== val && newVal <= 0 }; return { newVal, adjustPos };
}; };
// 处理缩放
if (isDiagonal) { if (isDiagonal) {
const mainAxis = Math.abs(delta[0] - 1) > Math.abs(delta[1] - 1) ? 'width' : 'height'; const mainAxis = Math.abs(delta[0] - 1) > Math.abs(delta[1] - 1) ? 'width' : 'height';
const crossAxis = mainAxis === 'width' ? 'height' : 'width'; const crossAxis = mainAxis === 'width' ? 'height' : 'width';
@@ -318,37 +339,49 @@ export default defineComponent({
frontStyle.style.width = widthResult.newVal + 'px'; frontStyle.style.width = widthResult.newVal + 'px';
frontStyle.style.height = heightResult.newVal + 'px'; frontStyle.style.height = heightResult.newVal + 'px';
} }
// 更新变换,确保正确的顺序
updateElementTransform(frontStyle);
}); });
// 旋转
moveableInstance.value.on('rotate', ({ target, beforeRotate }) => { moveableInstance.value.on('rotate', ({ target, beforeRotate }) => {
let frontStyle = detailData.frontBack.front[selectItem.imgDomIndex]; let frontStyle = detailData.frontBack.front[selectItem.imgDomIndex];
// 确保镜像状态存在 if (!frontStyle.mirror) {
if (!frontStyle.mirror) frontStyle.mirror = { x: false, y: false }; frontStyle.mirror = { x: false, y: false };
}
const { x: isMirroredX, y: isMirroredY } = frontStyle.mirror; const { x: isMirroredX, y: isMirroredY } = frontStyle.mirror;
const currentTransform = frontStyle.style?.transform || '';
// 计算实际旋转角度 const rotateMatch = currentTransform.match(/rotate\(([-\d.]+)deg\)/);
let actualRotate = beforeRotate; let currentAngle = 0;
if (rotateMatch && rotateMatch[1]) {
// 关键逻辑当镜像状态不同时一个true一个false旋转方向反转 const parsed = parseFloat(rotateMatch[1]);
if (isMirroredX !== isMirroredY) { if (!isNaN(parsed)) {
actualRotate = -beforeRotate; currentAngle = parsed;
}
} }
const newBeforeRotate = parseFloat(beforeRotate) || 0;
// 确保角度在 0-360 度范围内 if (!window._lastRotateData) {
actualRotate = actualRotate % 360; window._lastRotateData = {
beforeRotate: newBeforeRotate,
// 如果角度为负数,转换为正数 angle: currentAngle
if (actualRotate < 0) { };
actualRotate += 360;
} }
const delta = newBeforeRotate - window._lastRotateData.beforeRotate;
const shouldReverse = (isMirroredX && !isMirroredY) || (!isMirroredX && isMirroredY);
let newAngle;
if (shouldReverse) {
newAngle = window._lastRotateData.angle - delta;
} else {
newAngle = window._lastRotateData.angle + delta;
}
newAngle = ((newAngle % 360) + 360) % 360;
window._lastRotateData.beforeRotate = newBeforeRotate;
window._lastRotateData.angle = newAngle;
updateElementTransform(frontStyle, newAngle);
});
// 确保角度在 [0, 360) 范围内 // 可选在rotateStart时重置
actualRotate = ((actualRotate % 360) + 360) % 360; moveableInstance.value.on('rotateStart', () => {
window._lastRotateData = null;
updateElementTransform(frontStyle, actualRotate.toFixed(2));
}); });
// 调整大小 // 调整大小
moveableInstance.value.on('resize', ({ target, width, height }) => { moveableInstance.value.on('resize', ({ target, width, height }) => {
@@ -370,12 +403,17 @@ export default defineComponent({
upDataDetail() upDataDetail()
}); });
}; };
watch(()=>selectItem.selectDetail,(newValue,oldValue)=>{ // watch(()=>selectItem.selectDetail,(newValue,oldValue)=>{
if(!newValue && newValue?.id == oldValue?.id)return // if(!newValue && newValue?.id == oldValue?.id)return
selectItem.imgDomIndex = detailData.frontBack.front.findIndex((item:any)=>item.id == newValue.id) // },{immediate: true,})
initMoveableForSelected() watch(()=>selectItem.selectDetail?.id,(newValue,oldValue)=>{
if(newValue){
selectItem.imgDomIndex = detailData.frontBack.front.findIndex((item:any)=>item.id == newValue)
setTimeout(()=>{
initMoveableForSelected()
},100)
}
},{immediate: true,}) },{immediate: true,})
const setRevocation = async ()=>{ const setRevocation = async ()=>{
let frontBack = JSON.parse(JSON.stringify(detailData.frontBack)) let frontBack = JSON.parse(JSON.stringify(detailData.frontBack))
let revocation:any = JSON.parse((await KeyValueDB.get("revocation") as any) || '[]') let revocation:any = JSON.parse((await KeyValueDB.get("revocation") as any) || '[]')
@@ -536,6 +574,7 @@ export default defineComponent({
getMousePosition, getMousePosition,
updataPosition, updataPosition,
updateRect, updateRect,
initMoveableForSelected,
} }
}, },