Skip to content

Commit bce3b2c

Browse files
committed
🐞 fix: 修复canvas-size参数失效问题
fix #2
1 parent f132005 commit bce3b2c

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed

Diff for: src/handler.ts

+9-11
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@ import fs from "fs/promises";
22
import { createCanvas } from "node-canvas-webgl";
33
import { FfmpegFrameExporter } from "./exporter.js";
44
import { AssetPath, SpineRenderer, TexturePath, loadTexture } from "./renderer.js";
5-
import { formatOutputPath, traverseDir } from "./utils.js";
5+
import { formatOutputPath, traverseDir, Viewsize } from "./utils.js";
66
import { TExporterType } from "./exporter.js";
77
import { AssetManager, ManagedWebGLRenderingContext } from "@node-spine-runtimes/webgl-3.8.99";
88
import sharp from "sharp";
99
import path from "path";
1010
import { formatString } from "./utils.js";
1111

12-
export function parseCanvasSize(size: string) {
13-
const canvasWxH = size.split("x");
14-
if (canvasWxH.length !== 2) {
12+
export function parseCanvasSize(size: string): Viewsize {
13+
const canvasSize = size.split("x");
14+
if (canvasSize.length !== 2) {
1515
throw new Error("Canvas size format error! \n" + "Correct format: [width]x[height], for example 500x500.");
1616
}
17-
return { width: parseInt(canvasWxH[0]), height: parseInt(canvasWxH[1]) };
17+
return { width: parseInt(canvasSize[0]), height: parseInt(canvasSize[1]) };
1818
}
1919

2020
export interface SpineAnimationExportOptions {
@@ -48,10 +48,7 @@ export async function exportSpineAnimation(inputDir: string, options: SpineAnima
4848
};
4949
await checkParams();
5050

51-
const isOldCanvasMode = typeof canvasSize === "string";
52-
const size = isOldCanvasMode ? parseCanvasSize(canvasSize) : { width: 1000, height: 1000 };
53-
54-
const renderer = new SpineRenderer(createCanvas(size.width, size.height));
51+
const renderer = new SpineRenderer(createCanvas(1000, 1000));
5552
const exporter = new FfmpegFrameExporter(exporterMaxConcurrent)
5653

5754
const paths = await traverseDir(inputDir, AssetPath);
@@ -67,12 +64,13 @@ export async function exportSpineAnimation(inputDir: string, options: SpineAnima
6764
if (selectedAnimation.length && !selectedAnimation.includes(animationName)) {
6865
continue;
6966
}
70-
67+
const viewsize = typeof canvasSize === "string" ? parseCanvasSize(canvasSize) : undefined;
7168
console.log(`${assetProcess}Start rendering the animation '${animationName}' of asset '${assetName}'...`);
7269
const animationFrames = renderer.render({
7370
skeleton,
7471
state,
7572
animationName,
73+
viewsize,
7674
fps,
7775
endPosition,
7876
});
@@ -85,7 +83,7 @@ export async function exportSpineAnimation(inputDir: string, options: SpineAnima
8583
{
8684
outputPath: formatOutputPath(outputPath, formatObject),
8785
fps,
88-
autoCrop: isOldCanvasMode,
86+
autoCrop: viewsize !== undefined,
8987
}
9088
);
9189
}

Diff for: src/renderer.ts

+12-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import {
2323
} from "@node-spine-runtimes/webgl-3.8.99";
2424
import fs from "fs";
2525
import path from "path";
26-
import { sleep, replacePathSpecific, removePathExtension } from "./utils.js";
26+
import { sleep, replacePathSpecific, removePathExtension, Viewsize } from "./utils.js";
2727

2828
export interface LoadedResult {
2929
skeleton: Skeleton;
@@ -33,6 +33,7 @@ export interface LoadedResult {
3333
export interface RenderOptions extends LoadedResult {
3434
animationName?: string;
3535
fps: number;
36+
viewsize?: Viewsize;
3637
endPosition?: number;
3738
}
3839

@@ -171,13 +172,20 @@ export class SpineRenderer {
171172
this.renderer.drawSkeleton(skeleton, true);
172173
this.renderer.end();
173174
};
174-
let { skeleton, state, animationName = skeleton.data.animations[0].name, fps, endPosition = Infinity } = options;
175+
let { skeleton, state, animationName = skeleton.data.animations[0].name, fps, viewsize, endPosition = Infinity } = options;
175176

176177
state.setAnimation(0, animationName, false);
177178

178179
const viewport = calculateAnimationViewport(skeleton.data.findAnimation(animationName)!, skeleton, fps);
179-
this.canvas.width = Math.round(viewport.width);
180-
this.canvas.height = Math.round(viewport.height);
180+
if (viewsize === undefined) {
181+
this.canvas.width = Math.round(viewport.width);
182+
this.canvas.height = Math.round(viewport.height);
183+
}
184+
else {
185+
this.canvas.width = viewsize.width;
186+
this.canvas.height = viewsize.height;
187+
}
188+
181189
if (this.canvas.width % 2 !== 0) this.canvas.width += 1;
182190
if (this.canvas.height % 2 !== 0) this.canvas.height += 1;
183191

Diff for: src/utils.ts

+5
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,8 @@ export function defer<T = void>(): Deferred<T> {
8989
})
9090
return { resolve, reject, promise };
9191
}
92+
93+
export interface Viewsize {
94+
width: number;
95+
height: number;
96+
}

0 commit comments

Comments
 (0)