🚀 generate frames

This commit is contained in:
platane
2020-07-18 17:46:22 +02:00
parent a149bab26a
commit 1799d05bbf
7 changed files with 429 additions and 32 deletions

View File

@@ -0,0 +1,29 @@
import { createGif } from "..";
import { generateGrid } from "@snk/compute/generateGrid";
import { computeBestRun } from "@snk/compute";
const drawOptions = {
sizeBorderRadius: 2,
sizeCell: 16,
sizeDot: 12,
colorBorder: "#1b1f230a",
colorDots: { 1: "#9be9a8", 2: "#40c463", 3: "#30a14e", 4: "#216e39" },
colorEmpty: "#ebedf0",
colorSnake: "purple",
};
const gameOptions = { maxSnakeLength: 5 };
const grid = generateGrid(14, 7, { colors: [1, 2, 3, 4], emptyP: 3 });
const snake = [
{ x: 4, y: -1 },
{ x: 3, y: -1 },
{ x: 2, y: -1 },
{ x: 1, y: -1 },
{ x: 0, y: -1 },
];
const commands = computeBestRun(grid, snake, gameOptions);
createGif(grid, snake, commands, drawOptions, gameOptions);

View File

@@ -0,0 +1,55 @@
import * as fs from "fs";
import * as path from "path";
import { createCanvas } from "canvas";
import { Grid, copyGrid, Color } from "@snk/compute/grid";
import { Point } from "@snk/compute/point";
import { copySnake } from "@snk/compute/snake";
import { drawWorld } from "@snk/draw/drawWorld";
import { step } from "@snk/compute/step";
// @ts-ignore
import * as mkdirp from "mkdirp";
export const createGif = (
grid0: Grid,
snake0: Point[],
commands: Point[],
drawOptions: Parameters<typeof drawWorld>[4],
gameOptions: Parameters<typeof step>[4]
) => {
const grid = copyGrid(grid0);
const snake = copySnake(snake0);
const stack: Color[] = [];
const width = drawOptions.sizeCell * (grid.width + 4);
const height = drawOptions.sizeCell * (grid.height + 4) + 100;
const dir = path.join(__dirname, "tmp", Math.random().toString(36).slice(2));
mkdirp.sync(dir);
const canvas = createCanvas(width, height);
const ctx = canvas.getContext("2d")!;
const writeImage = (i: number) => {
ctx.clearRect(0, 0, 99999, 99999);
drawWorld(ctx, grid, snake, stack, drawOptions);
const buffer = canvas.toBuffer("image/png", {
compressionLevel: 0,
filters: canvas.PNG_FILTER_NONE,
});
const filename = path.join(dir, `${i.toString().padStart(4, "0")}.png`);
console.log(filename);
fs.writeFileSync(filename, buffer);
};
writeImage(0);
for (let i = 0; i < commands.length; i++) {
step(grid, snake, stack, commands[i], gameOptions);
writeImage(i + 1);
}
};

View File

@@ -0,0 +1,17 @@
{
"name": "@snk/gif-creator",
"version": "1.0.0",
"dependencies": {
"@snk/compute": "1.0.0",
"@snk/draw": "1.0.0",
"canvas": "2.6.1",
"mkdirp": "1.0.4"
},
"devDependencies": {
"@types/mkdirp": "1.0.1",
"@zeit/ncc": "0.22.3"
},
"scripts": {
"dev": "ncc run __tests__/dev.ts"
}
}