🚀 refactor gif creator ( a bit )
This commit is contained in:
@@ -8,23 +8,33 @@ import { step } from "@snk/compute/step";
|
|||||||
import * as tmp from "tmp";
|
import * as tmp from "tmp";
|
||||||
import * as execa from "execa";
|
import * as execa from "execa";
|
||||||
|
|
||||||
|
const withTmpDir = async <T>(
|
||||||
|
handler: (dir: string) => Promise<T>
|
||||||
|
): Promise<T> => {
|
||||||
|
const { name: dir, removeCallback: cleanUp } = tmp.dirSync({
|
||||||
|
unsafeCleanup: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
return await handler(dir);
|
||||||
|
} finally {
|
||||||
|
cleanUp();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
export const createGif = async (
|
export const createGif = async (
|
||||||
grid0: Grid,
|
grid0: Grid,
|
||||||
chain: Snake[],
|
chain: Snake[],
|
||||||
drawOptions: Options,
|
drawOptions: Options,
|
||||||
gifOptions: { frameDuration: number; step: number }
|
gifOptions: { frameDuration: number; step: number }
|
||||||
) => {
|
) =>
|
||||||
|
withTmpDir(async (dir) => {
|
||||||
const width = drawOptions.sizeCell * (grid0.width + 2);
|
const width = drawOptions.sizeCell * (grid0.width + 2);
|
||||||
const height = drawOptions.sizeCell * (grid0.height + 4) + 100;
|
const height = drawOptions.sizeCell * (grid0.height + 4) + 100;
|
||||||
|
|
||||||
const { name: dir, removeCallback: cleanUp } = tmp.dirSync({
|
|
||||||
unsafeCleanup: true,
|
|
||||||
});
|
|
||||||
|
|
||||||
const canvas = createCanvas(width, height);
|
const canvas = createCanvas(width, height);
|
||||||
const ctx = canvas.getContext("2d")!;
|
const ctx = canvas.getContext("2d")!;
|
||||||
|
|
||||||
try {
|
|
||||||
const grid = copyGrid(grid0);
|
const grid = copyGrid(grid0);
|
||||||
const stack: Color[] = [];
|
const stack: Color[] = [];
|
||||||
|
|
||||||
@@ -91,7 +101,4 @@ export const createGif = async (
|
|||||||
);
|
);
|
||||||
|
|
||||||
return fs.readFileSync(optimizedFileName);
|
return fs.readFileSync(optimizedFileName);
|
||||||
} finally {
|
});
|
||||||
cleanUp();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|||||||
Reference in New Issue
Block a user