feat: 添加导出图层功能,支持将图层转换为位图图像并下载

This commit is contained in:
bighuixiang
2025-06-23 15:56:01 +08:00
parent 1fde475806
commit 5d0511e405
10 changed files with 405 additions and 68 deletions

View File

@@ -3,6 +3,7 @@ import {
generateId,
optimizeCanvasRendering,
} from "../utils/helper.js";
import { imageModeHandler } from "../utils/imageHelper.js";
import { LayerType, OperationType } from "../utils/layerHelper.js";
import { Command, CompositeCommand } from "./Command.js";
import { fabric } from "fabric-with-all";
@@ -26,6 +27,7 @@ export class BatchInitializeRedGreenModeCommand extends Command {
this.redGreenImageUrl = options.redGreenImageUrl;
this.onImageGenerated = options.onImageGenerated;
this.normalLayerOpacity = options.normalLayerOpacity || 0.4;
this.clothingImageOpts = options.clothingImageOpts || null; // 衣服底图选项 - 用于设置图片加载时的选项
// 存储原始状态以便撤销
this.originalCanvasBackground = null;
@@ -398,15 +400,30 @@ export class BatchInitializeRedGreenModeCommand extends Command {
* 设置衣服底图
*/
async _setupClothingImage(img, fixedLayer) {
// 计算图片缩放,保持上下留边距
const margin = 50;
const maxWidth = this.canvas.width - margin * 2;
const maxHeight = this.canvas.height - margin * 2;
const scale = Math.min(maxWidth / img.width, maxHeight / img.height);
if (this.clothingImageOpts?.imageMode) {
// 如果有衣服底图选项,应用这些选项
// 底图加载方式 1.平铺 2.拉伸 3.拉伸平铺 4.拉伸平铺并裁剪 5.包含
// this.clothingImageOpts?.imageMode // 默认不处理 可选 contains, stretch,tile stretchTile, stretchTileCrop
// 通用处理图片模式
imageModeHandler({
imageMode: this.clothingImageOpts?.imageMode,
newImage: img,
canvasWidth: this.canvas.width,
canvasHeight: this.canvas.height,
});
} else {
// 计算图片缩放,保持上下留边距
const margin = 50;
const maxWidth = this.canvas.width - margin * 2;
const maxHeight = this.canvas.height - margin * 2;
const scale = Math.min(maxWidth / img.width, maxHeight / img.height);
img.set({
scaleX: scale,
scaleY: scale,
});
}
img.set({
scaleX: scale,
scaleY: scale,
left: this.canvas.width / 2,
top: this.canvas.height / 2,
originX: "center",