|
|
@@ -19,6 +19,8 @@ const DEFAULT_CLIP_SETTINGS = {
|
|
|
svgUrl: '',
|
|
|
svgWidth: 100,
|
|
|
svgHeight: 100,
|
|
|
+ fillColor: '#FFFFFF', // 背景色
|
|
|
+ fillOpacity: 0.5, // 背景透明度
|
|
|
};
|
|
|
|
|
|
export default {
|
|
|
@@ -152,8 +154,13 @@ export default {
|
|
|
|
|
|
// 优先使用默认值
|
|
|
let imageSettings = { ...DEFAULT_CLIP_SETTINGS };
|
|
|
- // 若已有 clipPath,合并其中的属性
|
|
|
- if (this.editLayer.clipPath) {
|
|
|
+
|
|
|
+ // 如果图片对象有保存的裁剪设置,优先使用
|
|
|
+ if (this.editLayer.clipSettings) {
|
|
|
+ imageSettings = { ...imageSettings, ...this.editLayer.clipSettings };
|
|
|
+ }
|
|
|
+ // 否则从 clipPath 恢复基本几何属性
|
|
|
+ else if (this.editLayer.clipPath) {
|
|
|
imageSettings = {
|
|
|
...imageSettings,
|
|
|
width: this.editLayer.clipPath.width || imageSettings.width,
|
|
|
@@ -172,6 +179,11 @@ export default {
|
|
|
// 更新 clipSettings,用于 UI 显示
|
|
|
this.clipSettings = imageSettings;
|
|
|
|
|
|
+ // 如果有裁剪设置,恢复背景对象
|
|
|
+ if (imageSettings.shape && this.editLayer.clipSettings) {
|
|
|
+ this.createClipBackground();
|
|
|
+ }
|
|
|
+
|
|
|
// 同步描边图形(如果存在)
|
|
|
if (this.editLayer.strokeObj) {
|
|
|
this.fcanvas.add(markRaw(this.editLayer.strokeObj));
|
|
|
@@ -461,6 +473,12 @@ export default {
|
|
|
// 应用剪裁区域
|
|
|
this.editLayer.set({ clipPath: clipObj });
|
|
|
|
|
|
+ // 保存裁剪设置到图片对象
|
|
|
+ this.editLayer.clipSettings = { ...this.clipSettings };
|
|
|
+
|
|
|
+ // 创建背景对象(方案二:独立背景对象)
|
|
|
+ this.createClipBackground();
|
|
|
+
|
|
|
// 更新画布
|
|
|
this.updateClipStroke()
|
|
|
this.updateCanvasState();
|
|
|
@@ -471,6 +489,64 @@ export default {
|
|
|
this.$message.error('剪裁设置错误: ' + error.message);
|
|
|
}
|
|
|
},
|
|
|
+
|
|
|
+ // 创建裁剪背景对象(方案二:独立背景对象)
|
|
|
+ createClipBackground() {
|
|
|
+ if (!this.judge() || !this.clipSettings.shape) return;
|
|
|
+
|
|
|
+ // 移除现有的背景对象
|
|
|
+ if (this.editLayer.clipBackgroundObj) {
|
|
|
+ this.fcanvas.remove(this.editLayer.clipBackgroundObj);
|
|
|
+ this.editLayer.clipBackgroundObj = null;
|
|
|
+ }
|
|
|
+
|
|
|
+ const { shape, width, height, radius, offsetX, offsetY, rectRadius, fillColor, fillOpacity, strokeColor, strokeWidth } = this.clipSettings;
|
|
|
+
|
|
|
+ let bgObj;
|
|
|
+
|
|
|
+ if (shape === 'rect') {
|
|
|
+ bgObj = new fabric.Rect({
|
|
|
+ width: width,
|
|
|
+ height: height,
|
|
|
+ left: offsetX,
|
|
|
+ top: offsetY,
|
|
|
+ rx: rectRadius,
|
|
|
+ ry: rectRadius,
|
|
|
+ fill: fillColor,
|
|
|
+ stroke: strokeColor,
|
|
|
+ strokeWidth: strokeWidth,
|
|
|
+ opacity: fillOpacity,
|
|
|
+ selectable: false,
|
|
|
+ evented: false,
|
|
|
+ absolutePositioned: true
|
|
|
+ });
|
|
|
+ } else if (shape === 'circle') {
|
|
|
+ bgObj = new fabric.Circle({
|
|
|
+ radius: radius,
|
|
|
+ left: offsetX,
|
|
|
+ top: offsetY,
|
|
|
+ fill: fillColor,
|
|
|
+ stroke: strokeColor,
|
|
|
+ strokeWidth: strokeWidth,
|
|
|
+ opacity: fillOpacity,
|
|
|
+ selectable: false,
|
|
|
+ evented: false,
|
|
|
+ absolutePositioned: true
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ if (bgObj) {
|
|
|
+ // 绑定到当前对象
|
|
|
+ this.editLayer.clipBackgroundObj = bgObj;
|
|
|
+
|
|
|
+ // 添加到画布
|
|
|
+ this.fcanvas.add(markRaw(bgObj));
|
|
|
+
|
|
|
+ // 确保层级正确:背景在最下面,图片在上面
|
|
|
+ this.fcanvas.sendToBack(bgObj);
|
|
|
+ this.fcanvas.bringToFront(this.editLayer);
|
|
|
+ }
|
|
|
+ },
|
|
|
// 切换剪裁形状,自动填充默认值(位置跟随图层,尺寸为画布一半)
|
|
|
onClipShapeChange(shape){
|
|
|
if(!this.editLayer || this.editLayer.type !== 'image'){
|
|
|
@@ -488,7 +564,7 @@ export default {
|
|
|
const canvasW = this.fcanvas?.width || obj.canvas?.width || 800
|
|
|
const canvasH = this.fcanvas?.height || obj.canvas?.height || 800
|
|
|
const initWidth = Math.max(10, Math.round(canvasW / 2))
|
|
|
- const initHeight = Math.max(10, Math.round(canvasH / 2))
|
|
|
+ const initHeight = initWidth // 高度和宽度保持一致
|
|
|
const initRadius = Math.max(5, Math.round(Math.min(canvasW, canvasH) / 4))
|
|
|
const initSettings = {
|
|
|
shape,
|
|
|
@@ -601,6 +677,18 @@ export default {
|
|
|
this.editLayer.strokeObj = null;
|
|
|
}
|
|
|
|
|
|
+ // 移除背景对象(如果存在)
|
|
|
+ if (this.editLayer.clipBackgroundObj) {
|
|
|
+ this.fcanvas.remove(this.editLayer.clipBackgroundObj);
|
|
|
+ this.editLayer.clipBackgroundObj = null;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 清除保存的裁剪设置
|
|
|
+ delete this.editLayer.clipSettings;
|
|
|
+
|
|
|
+ // 重置UI设置
|
|
|
+ this.clipSettings = { ...DEFAULT_CLIP_SETTINGS };
|
|
|
+
|
|
|
// 更新画布状态
|
|
|
this.updateCanvasState();
|
|
|
this.fcanvas.requestRenderAll();
|