Skip to content

Commit 1799d05

Browse files
committed
🚀 generate frames
1 parent a149bab commit 1799d05

File tree

7 files changed

+429
-32
lines changed

7 files changed

+429
-32
lines changed

jest.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module.exports = {
22
preset: "ts-jest",
33
testEnvironment: "node",
4+
testMatch: ["**/__tests__/**/?(*.)+(spec|test).ts"],
45
};

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"scripts": {
1717
"type": "tsc --noEmit",
1818
"lint": "yarn prettier -c '**/*.{ts,js,json,md,yml,yaml}' '!dist/**'",
19-
"test": "jest --verbose --passWithNoTests",
19+
"test": "jest --verbose --passWithNoTests --no-cache",
2020
"build": "( cd packages/demo ; yarn build )",
2121
"dev": "( cd packages/demo ; yarn dev )"
2222
}

packages/demo/index.ts

+17-18
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { Point } from "@snk/compute/point";
1010
const copySnake = (x: Point[]) => x.map((p) => ({ ...p }));
1111

1212
export const run = async () => {
13-
const options = {
13+
const drawOptions = {
1414
sizeBorderRadius: 2,
1515
sizeCell: 16,
1616
sizeDot: 12,
@@ -22,35 +22,34 @@ export const run = async () => {
2222

2323
const gameOptions = { maxSnakeLength: 5 };
2424

25-
const grid = generateGrid(14, 7, { colors: [1, 2, 3, 4], emptyP: 3 });
25+
const grid0 = generateGrid(14, 7, { colors: [1, 2, 3, 4], emptyP: 3 });
2626

27-
const canvas = document.createElement("canvas");
28-
canvas.width = options.sizeCell * (grid.width + 4);
29-
canvas.height = options.sizeCell * (grid.height + 4) + 100;
30-
const ctx = canvas.getContext("2d")!;
31-
32-
document.body.appendChild(canvas);
33-
34-
const snake = [
27+
const snake0 = [
3528
{ x: 4, y: -1 },
3629
{ x: 3, y: -1 },
3730
{ x: 2, y: -1 },
3831
{ x: 1, y: -1 },
3932
{ x: 0, y: -1 },
4033
];
41-
const stack: Color[] = [];
34+
const stack0: Color[] = [];
4235

43-
const chain = computeBestRun(grid, snake, gameOptions);
36+
const chain = computeBestRun(grid0, snake0, gameOptions);
37+
38+
const canvas = document.createElement("canvas");
39+
canvas.width = drawOptions.sizeCell * (grid0.width + 4);
40+
canvas.height = drawOptions.sizeCell * (grid0.height + 4) + 100;
41+
document.body.appendChild(canvas);
42+
const ctx = canvas.getContext("2d")!;
4443

45-
const update = (x: number) => {
46-
const s = copySnake(snake);
47-
const q = stack.slice();
48-
const g = copyGrid(grid);
44+
const update = (n: number) => {
45+
const snake = copySnake(snake0);
46+
const stack = stack0.slice();
47+
const grid = copyGrid(grid0);
4948

50-
for (let i = 0; i < x; i++) step(g, s, q, chain[i], gameOptions);
49+
for (let i = 0; i < n; i++) step(grid, snake, stack, chain[i], gameOptions);
5150

5251
ctx.clearRect(0, 0, 9999, 9999);
53-
drawWorld(ctx, g, s, q, options);
52+
drawWorld(ctx, grid, snake, stack, drawOptions);
5453
};
5554

5655
const input: any = document.createElement("input");

packages/git-creator/__tests__/dev.ts

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { createGif } from "..";
2+
import { generateGrid } from "@snk/compute/generateGrid";
3+
import { computeBestRun } from "@snk/compute";
4+
5+
const drawOptions = {
6+
sizeBorderRadius: 2,
7+
sizeCell: 16,
8+
sizeDot: 12,
9+
colorBorder: "#1b1f230a",
10+
colorDots: { 1: "#9be9a8", 2: "#40c463", 3: "#30a14e", 4: "#216e39" },
11+
colorEmpty: "#ebedf0",
12+
colorSnake: "purple",
13+
};
14+
15+
const gameOptions = { maxSnakeLength: 5 };
16+
17+
const grid = generateGrid(14, 7, { colors: [1, 2, 3, 4], emptyP: 3 });
18+
19+
const snake = [
20+
{ x: 4, y: -1 },
21+
{ x: 3, y: -1 },
22+
{ x: 2, y: -1 },
23+
{ x: 1, y: -1 },
24+
{ x: 0, y: -1 },
25+
];
26+
27+
const commands = computeBestRun(grid, snake, gameOptions);
28+
29+
createGif(grid, snake, commands, drawOptions, gameOptions);

packages/git-creator/index.ts

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import * as fs from "fs";
2+
import * as path from "path";
3+
import { createCanvas } from "canvas";
4+
import { Grid, copyGrid, Color } from "@snk/compute/grid";
5+
import { Point } from "@snk/compute/point";
6+
import { copySnake } from "@snk/compute/snake";
7+
import { drawWorld } from "@snk/draw/drawWorld";
8+
import { step } from "@snk/compute/step";
9+
10+
// @ts-ignore
11+
import * as mkdirp from "mkdirp";
12+
13+
export const createGif = (
14+
grid0: Grid,
15+
snake0: Point[],
16+
commands: Point[],
17+
drawOptions: Parameters<typeof drawWorld>[4],
18+
gameOptions: Parameters<typeof step>[4]
19+
) => {
20+
const grid = copyGrid(grid0);
21+
const snake = copySnake(snake0);
22+
const stack: Color[] = [];
23+
24+
const width = drawOptions.sizeCell * (grid.width + 4);
25+
const height = drawOptions.sizeCell * (grid.height + 4) + 100;
26+
27+
const dir = path.join(__dirname, "tmp", Math.random().toString(36).slice(2));
28+
mkdirp.sync(dir);
29+
30+
const canvas = createCanvas(width, height);
31+
const ctx = canvas.getContext("2d")!;
32+
33+
const writeImage = (i: number) => {
34+
ctx.clearRect(0, 0, 99999, 99999);
35+
drawWorld(ctx, grid, snake, stack, drawOptions);
36+
37+
const buffer = canvas.toBuffer("image/png", {
38+
compressionLevel: 0,
39+
filters: canvas.PNG_FILTER_NONE,
40+
});
41+
42+
const filename = path.join(dir, `${i.toString().padStart(4, "0")}.png`);
43+
44+
console.log(filename);
45+
46+
fs.writeFileSync(filename, buffer);
47+
};
48+
49+
writeImage(0);
50+
51+
for (let i = 0; i < commands.length; i++) {
52+
step(grid, snake, stack, commands[i], gameOptions);
53+
writeImage(i + 1);
54+
}
55+
};

packages/git-creator/package.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "@snk/gif-creator",
3+
"version": "1.0.0",
4+
"dependencies": {
5+
"@snk/compute": "1.0.0",
6+
"@snk/draw": "1.0.0",
7+
"canvas": "2.6.1",
8+
"mkdirp": "1.0.4"
9+
},
10+
"devDependencies": {
11+
"@types/mkdirp": "1.0.1",
12+
"@zeit/ncc": "0.22.3"
13+
},
14+
"scripts": {
15+
"dev": "ncc run __tests__/dev.ts"
16+
}
17+
}

0 commit comments

Comments
 (0)