优化识别

This commit is contained in:
tianmo committed 2026-09-09 00:27:25 +08:00
1 parent 7263a841cb
commit 5cb2e76471
9 files changed
+302 -66

No files matched your search

+19 -1
View File
@@ -7,6 +7,7 @@ const fs = require('fs');
const vm = require('vm');
const path = require('path');
const utils = require('./spine-utils.js');
const spineBadge = require('./spine-badge.js');
let canvasLib = null;
try {
@@ -15,6 +16,22 @@ try {
canvasLib = null;
}
let spineBadgePromise = null;
/** 在缩略图右下角绘制官方 Spine badge,仅等比缩放,不改变官方图形。 */
async function drawSpineBadge(ctx, width, height) {
if (!canvasLib || !ctx) return;
try {
if (!spineBadgePromise) {
spineBadgePromise = canvasLib.loadImage(spineBadge.path);
}
const badge = await spineBadgePromise;
const badgeSize = Math.max(44, Math.round(Math.min(width, height) * 0.15));
const margin = Math.max(10, Math.round(badgeSize * 0.2));
ctx.drawImage(badge, width - badgeSize - margin, height - badgeSize - margin, badgeSize, badgeSize);
} catch (e) { /* 缺少 badge 时不影响缩略图主体 */ }
}
/**
* 在隔离的 vm 上下文中加载本地 Spine 运行时,返回 spine 命名空间对象。
* 使用独立上下文可避免不同版本运行时的全局变量/类相互污染(例如 4.x 与 3.x
@@ -188,8 +205,9 @@ async function renderThumbnail({ skeletonPath, atlasPath, texturePaths, version,
ctx.restore();
}
await drawSpineBadge(ctx, cw, ch);
const buf = canvas.toBuffer('image/png');
return buf;
}
module.exports = { renderThumbnail, loadRuntime, buildTextureAtlas, readSkeletonData, getBounds, canvasLib };
module.exports = { renderThumbnail, loadRuntime, buildTextureAtlas, readSkeletonData, getBounds, drawSpineBadge, canvasLib };