From 59541a9d3dedbe7c8c6bfecd43264941675de9d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=BF=97=E9=B9=8F?= <2916022834@qq.com> Date: Tue, 6 Jan 2026 15:09:02 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=9D=E5=AD=98=E7=94=BB=E5=B8=83=E6=8E=92?= =?UTF-8?q?=E9=99=A4=E7=89=B9=E6=AE=8A=E5=9B=BE=E5=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CanvasEditor/managers/CanvasManager.js | 66 +++++++++++-------- .../Canvas/CanvasEditor/utils/layerUtils.js | 8 ++- 2 files changed, 45 insertions(+), 29 deletions(-) diff --git a/src/component/Canvas/CanvasEditor/managers/CanvasManager.js b/src/component/Canvas/CanvasEditor/managers/CanvasManager.js index c8220b5d..23bc7bad 100644 --- a/src/component/Canvas/CanvasEditor/managers/CanvasManager.js +++ b/src/component/Canvas/CanvasEditor/managers/CanvasManager.js @@ -1198,43 +1198,55 @@ export class CanvasManager { // this.canvas.discardActiveObject(); this.canvas.renderAll(); + + // 排除颜色图层和特殊组图层 + const excludedLayers = [SpecialLayerId.COLOR, SpecialLayerId.SPECIAL_GROUP]; + this.layers.value.forEach((layer) => { + if(excludedLayers.includes(layer.id)){ + excludedLayers.push(...layer.children?.map((child) => child.id)); + } + }) + + const canvas = this.canvas.toJSON([ + "id", + "type", + "layerId", + "layerName", + "isBackground", + "isLocked", + "isVisible", + "isFixed", + "parentId", + "eraser", + "eraserable", + "erasable", + "customType", + "fill_", + "scaleX", + "scaleY", + "top", + "left", + "width", + "height", + ]); + canvas.objects = canvas.objects.filter((v) => !excludedLayers.includes(v.layerId)); + const simplifyLayersData = simplifyLayers( - JSON.parse(JSON.stringify(this.layers.value)) + JSON.parse(JSON.stringify(this.layers.value)), + excludedLayers ); - const data = JSON.stringify({ - canvas: this.canvas.toJSON([ - "id", - "type", - "layerId", - "layerName", - "isBackground", - "isLocked", - "isVisible", - "isFixed", - "parentId", - "eraser", - "eraserable", - "erasable", - "customType", - "fill_", - "scaleX", - "scaleY", - "top", - "left", - "width", - "height", - ]), + const data = { + canvas, layers: simplifyLayersData, // 简化图层数据 - // layers: JSON.stringify(JSON.parse(JSON.stringify(this.layers.value))), // 全数据 version: "1.0", // 添加版本信息 timestamp: new Date().toISOString(), // 添加时间戳 canvasWidth: this.canvasWidth.value, canvasHeight: this.canvasHeight.value, canvasColor: this.canvasColor.value, activeLayerId: this.layerManager?.activeLayerId?.value, - }); + }; console.log("获取画布JSON数据...", data); - return data; + return JSON.stringify(data); } catch (error) { console.error("获取画布JSON失败:", error); throw new Error("获取画布JSON失败"); diff --git a/src/component/Canvas/CanvasEditor/utils/layerUtils.js b/src/component/Canvas/CanvasEditor/utils/layerUtils.js index 57600af7..5effb1db 100644 --- a/src/component/Canvas/CanvasEditor/utils/layerUtils.js +++ b/src/component/Canvas/CanvasEditor/utils/layerUtils.js @@ -155,15 +155,19 @@ export function validateLayerAssociations(layers, canvasObjects) { /** * 简化layers对象属性,只保留必要的属性 * @param {Array} layers 图层数组 + * @param {Array} excludedLayers 排除的图层ID数组 * @returns {Array} 简化后的图层数组 */ -export function simplifyLayers(layers) { +export function simplifyLayers(layers, excludedLayers = []) { if (!layers || !isArray(layers)) { console.warn("simplifyLayers 请传入有效的图层数组:", layers); return []; } return layers.map((layer) => { + // 检查是否在排除列表中 + if (excludedLayers && excludedLayers.includes(layer.id)) return null; + const simplifiedLayer = { id: layer.id, name: layer.name, @@ -215,7 +219,7 @@ export function simplifyLayers(layers) { }; return simplifiedLayer; - }); + }).filter((layer) => !!layer); } /**