WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit ab5e6c7

Browse files
committed
rename animation options + add backgroundColor option for gif
1 parent a865fcc commit ab5e6c7

File tree

18 files changed

+100
-135
lines changed

18 files changed

+100
-135
lines changed

bun.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"lockfileVersion": 1,
3+
"configVersion": 0,
34
"workspaces": {
45
"": {
56
"name": "snk",
@@ -51,7 +52,7 @@
5152
"name": "@snk/draw",
5253
"version": "1.0.0",
5354
"dependencies": {
54-
"@snk/solver": "1.0.0",
55+
"@snk/types": "1.0.0",
5556
},
5657
},
5758
"packages/gif-creator": {
@@ -60,10 +61,10 @@
6061
"dependencies": {
6162
"@snk/draw": "1.0.0",
6263
"@snk/solver": "1.0.0",
64+
"@snk/types": "1.0.0",
6365
"canvas": "3.1.0",
6466
"gif-encoder-2": "1.0.5",
6567
"gifsicle": "5.3.0",
66-
"tmp": "0.2.3",
6768
},
6869
"devDependencies": {
6970
"@types/gifsicle": "5.2.2",
@@ -97,6 +98,7 @@
9798
"version": "1.0.0",
9899
"dependencies": {
99100
"@snk/solver": "1.0.0",
101+
"@snk/types": "1.0.0",
100102
},
101103
},
102104
"packages/types": {
@@ -1156,8 +1158,6 @@
11561158

11571159
"timed-out": ["[email protected]", "", {}, "sha512-G7r3AhovYtr5YKOWQkta8RKAPb+J9IsO4uVmzjl8AZwfhs8UcUwTiD6gcJYSgOtzyjvQKrKYn41syHbUWMkafA=="],
11581160

1159-
"tmp": ["[email protected]", "", {}, "sha512-nZD7m9iCPC5g0pYmcaxogYKggSfLsdxl8of3Q/oIbqCqLLIO9IAF0GWjX1z9NZRHPiXv8Wex4yDCaZsgEw0Y8w=="],
1160-
11611161
"to-buffer": ["[email protected]", "", {}, "sha512-lx9B5iv7msuFYE3dytT+KE5tap+rNYw+K4jVkb9R/asAb+pbBSM17jtunHplhBe6RRJdZx3Pn2Jph24O32mOVg=="],
11621162

11631163
"to-regex-range": ["[email protected]", "", { "dependencies": { "is-number": "^7.0.0" } }, "sha512-65P7iz6X5yEr1cwcgvQxbbIw7Uk3gOy5dIdtZ4rDveLqhrdJP+Li/Hx6tyK0NEb+2GCyneCMJiGqrADCSNk8sQ=="],

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"packages/*"
1414
],
1515
"scripts": {
16-
"type": "tsc --noEmit",
16+
"type": "tsc",
1717
"lint": "prettier -c '**/*.{ts,js,json,md,yml,yaml}' '!packages/*/dist/**' '!svg-only/dist/**'",
1818
"dev:demo": "( cd packages/demo ; npm run dev )",
1919
"build:demo": "( cd packages/demo ; npm run build )",

packages/action/__tests__/__snapshots__/outputsOptions.spec.ts.snap

Lines changed: 13 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,13 @@
1-
// Jest Snapshot v1, https://goo.gl/fbAQLP
2-
3-
exports[`should parse /out.svg?{"color_snake":"yellow","color_dots":["#000","#111","#222","#333","#444"]} 1`] = `
4-
{
5-
"animationOptions": {
6-
"frameDuration": 100,
7-
"step": 1,
8-
},
9-
"drawOptions": {
10-
"colorDotBorder": "#1b1f230a",
11-
"colorDots": [
12-
"#000",
13-
"#111",
14-
"#222",
15-
"#333",
16-
"#444",
17-
],
18-
"colorEmpty": "#000",
19-
"colorSnake": "yellow",
20-
"dark": undefined,
21-
"sizeCell": 16,
22-
"sizeDot": 12,
23-
"sizeDotBorderRadius": 2,
24-
},
25-
"filename": "/out.svg",
26-
"format": "svg",
27-
}
28-
`;
1+
// Bun Snapshot v1, https://bun.sh/docs/test/snapshots
292

303
exports[`should parse /out.svg?color_snake=orange&color_dots=#000,#111,#222,#333,#444 1`] = `
314
{
325
"animationOptions": {
33-
"frameDuration": 100,
34-
"step": 1,
6+
"frameByStep": 1,
7+
"stepDurationMs": 100,
358
},
369
"drawOptions": {
10+
"colorBackground": "#ffffff",
3711
"colorDotBorder": "#1b1f230a",
3812
"colorDots": [
3913
"#000",
@@ -54,13 +28,14 @@ exports[`should parse /out.svg?color_snake=orange&color_dots=#000,#111,#222,#333
5428
}
5529
`;
5630

57-
exports[`should parse /out.svg?color_snake=orange&color_dots=#000,#111,#222,#333,#444&dark_color_dots=#a00,#a11,#a22,#a33,#a44 1`] = `
31+
exports[`should parse /out.svg?{"color_snake":"yellow","color_dots":["#000","#111","#222","#333","#444"]} 1`] = `
5832
{
5933
"animationOptions": {
60-
"frameDuration": 100,
61-
"step": 1,
34+
"frameByStep": 1,
35+
"stepDurationMs": 100,
6236
},
6337
"drawOptions": {
38+
"colorBackground": "#ffffff",
6439
"colorDotBorder": "#1b1f230a",
6540
"colorDots": [
6641
"#000",
@@ -70,19 +45,8 @@ exports[`should parse /out.svg?color_snake=orange&color_dots=#000,#111,#222,#333
7045
"#444",
7146
],
7247
"colorEmpty": "#000",
73-
"colorSnake": "orange",
74-
"dark": {
75-
"colorDotBorder": "#1b1f230a",
76-
"colorDots": [
77-
"#a00",
78-
"#a11",
79-
"#a22",
80-
"#a33",
81-
"#a44",
82-
],
83-
"colorEmpty": "#a00",
84-
"colorSnake": "orange",
85-
},
48+
"colorSnake": "yellow",
49+
"dark": undefined,
8650
"sizeCell": 16,
8751
"sizeDot": 12,
8852
"sizeDotBorderRadius": 2,
@@ -95,10 +59,11 @@ exports[`should parse /out.svg?color_snake=orange&color_dots=#000,#111,#222,#333
9559
exports[`should parse path/to/out.gif 1`] = `
9660
{
9761
"animationOptions": {
98-
"frameDuration": 100,
99-
"step": 1,
62+
"frameByStep": 1,
63+
"stepDurationMs": 100,
10064
},
10165
"drawOptions": {
66+
"colorBackground": "#ffffff",
10267
"colorDotBorder": "#1b1f230a",
10368
"colorDots": [
10469
"#ebedf0",
@@ -109,7 +74,6 @@ exports[`should parse path/to/out.gif 1`] = `
10974
],
11075
"colorEmpty": "#ebedf0",
11176
"colorSnake": "purple",
112-
"dark": undefined,
11377
"sizeCell": 16,
11478
"sizeDot": 12,
11579
"sizeDotBorderRadius": 2,

packages/action/__tests__/outputsOptions.spec.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,6 @@ it("should parse filename", () => {
5151

5252
// overwrite colors (json)
5353
`/out.svg?{"color_snake":"yellow","color_dots":["#000","#111","#222","#333","#444"]}`,
54-
55-
// overwrite dark colors
56-
"/out.svg?color_snake=orange&color_dots=#000,#111,#222,#333,#444&dark_color_dots=#a00,#a11,#a22,#a33,#a44",
5754
].forEach((entry) =>
5855
it(`should parse ${entry}`, () => {
5956
expect(parseEntry(entry)).toMatchSnapshot();

packages/action/outputsOptions.ts

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import type { AnimationOptions } from "@snk/gif-creator";
2-
import type { DrawOptions as DrawOptions } from "@snk/svg-creator";
2+
import type { DrawOptions as DrawOptionsSvg } from "@snk/svg-creator";
3+
import type { DrawOptions as DrawOptionsGif } from "@snk/gif-creator";
34
import { palettes } from "./palettes";
45

6+
type DrawOptions = DrawOptionsSvg & DrawOptionsGif;
7+
58
export const parseOutputsOption = (lines: string[]) => lines.map(parseEntry);
69

710
export const parseEntry = (entry: string) => {
@@ -32,51 +35,31 @@ export const parseEntry = (entry: string) => {
3235
sizeCell: 16,
3336
sizeDot: 12,
3437
...palettes["default"],
35-
dark: palettes["default"].dark && { ...palettes["default"].dark },
3638
};
37-
const animationOptions: AnimationOptions = { step: 1, frameDuration: 100 };
39+
const animationOptions: AnimationOptions = {
40+
frameByStep: 1,
41+
stepDurationMs: 100,
42+
};
3843

3944
{
40-
const palette = palettes[sp.get("palette")!];
45+
const palette = palettes[sp.get("palette") as keyof typeof palettes];
4146
if (palette) {
4247
Object.assign(drawOptions, palette);
43-
drawOptions.dark = palette.dark && { ...palette.dark };
44-
}
45-
}
46-
47-
{
48-
const dark_palette = palettes[sp.get("dark_palette")!];
49-
if (dark_palette) {
50-
const clone = { ...dark_palette, dark: undefined };
51-
drawOptions.dark = clone;
5248
}
5349
}
5450

55-
if (sp.has("color_snake")) drawOptions.colorSnake = sp.get("color_snake")!;
5651
if (sp.has("color_dots")) {
5752
const colors = sp.get("color_dots")!.split(/[,;]/);
5853
drawOptions.colorDots = colors;
5954
drawOptions.colorEmpty = colors[0];
6055
drawOptions.dark = undefined;
6156
}
57+
if (sp.has("color_snake")) drawOptions.colorSnake = sp.get("color_snake")!;
58+
if (sp.has("color_background"))
59+
drawOptions.colorBackground = sp.get("color_background")!;
6260
if (sp.has("color_dot_border"))
6361
drawOptions.colorDotBorder = sp.get("color_dot_border")!;
6462

65-
if (sp.has("dark_color_dots")) {
66-
const colors = sp.get("dark_color_dots")!.split(/[,;]/);
67-
drawOptions.dark = {
68-
colorDotBorder: drawOptions.colorDotBorder,
69-
colorSnake: drawOptions.colorSnake,
70-
...drawOptions.dark,
71-
colorDots: colors,
72-
colorEmpty: colors[0],
73-
};
74-
}
75-
if (sp.has("dark_color_dot_border") && drawOptions.dark)
76-
drawOptions.dark.colorDotBorder = sp.get("color_dot_border")!;
77-
if (sp.has("dark_color_snake") && drawOptions.dark)
78-
drawOptions.dark.colorSnake = sp.get("color_snake")!;
79-
8063
return {
8164
filename,
8265
format: format as "svg" | "gif",

packages/action/palettes.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
1-
import { DrawOptions as DrawOptions } from "@snk/svg-creator";
2-
3-
export const basePalettes: Record<
4-
string,
5-
Pick<
6-
DrawOptions,
7-
"colorDotBorder" | "colorEmpty" | "colorSnake" | "colorDots" | "dark"
8-
>
9-
> = {
1+
export const basePalettes = {
102
"github-light": {
3+
colorBackground: "#ffffff",
114
colorDotBorder: "#1b1f230a",
125
colorDots: ["#ebedf0", "#9be9a8", "#40c463", "#30a14e", "#216e39"],
136
colorEmpty: "#ebedf0",
147
colorSnake: "purple",
158
},
169
"github-dark": {
10+
colorBackground: "#0c1116",
1711
colorDotBorder: "#1b1f230a",
1812
colorEmpty: "#161b22",
1913
colorDots: ["#161b22", "#01311f", "#034525", "#0f6d31", "#00c647"],
@@ -22,6 +16,10 @@ export const basePalettes: Record<
2216
};
2317

2418
// aliases
25-
export const palettes = { ...basePalettes };
26-
palettes["github"] = palettes["github-light"];
27-
palettes["default"] = palettes["github"];
19+
export const palettes = {
20+
...basePalettes,
21+
22+
// aliases
23+
github: basePalettes["github-light"],
24+
default: basePalettes["github-light"],
25+
};

packages/demo/demo.interactive.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,13 @@ const createViewer = ({
202202
schemaSelect.style.alignSelf = "flex-start";
203203
schemaSelect.value = "github-light";
204204
schemaSelect.addEventListener("change", () => {
205-
Object.assign(drawOptions, basePalettes[schemaSelect.value]);
205+
Object.assign(
206+
drawOptions,
207+
basePalettes[schemaSelect.value as keyof typeof basePalettes],
208+
);
206209

207210
svgString = createSvg(grid0, cells, chain, drawOptions, {
208-
frameDuration: 100,
211+
stepDurationMs: 100,
209212
} as AnimationOptions);
210213
const svgImageUri = `data:image/*;charset=utf-8;base64,${btoa(svgString)}`;
211214
svgLink.href = svgImageUri;
@@ -239,7 +242,7 @@ const createViewer = ({
239242
// svg
240243
const svgLink = document.createElement("a");
241244
let svgString = createSvg(grid0, cells, chain, drawOptions, {
242-
frameDuration: 100,
245+
stepDurationMs: 100,
243246
} as AnimationOptions);
244247
const svgImageUri = `data:image/*;charset=utf-8;base64,${btoa(svgString)}`;
245248
svgLink.href = svgImageUri;

packages/demo/demo.svg.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ chain.push(...getPathToPose(chain.slice(-1)[0], snake)!);
1111

1212
(async () => {
1313
const svg = await createSvg(grid, null, chain, drawOptions, {
14-
frameDuration: 200,
14+
stepDurationMs: 200,
1515
} as AnimationOptions);
1616

1717
const container = document.createElement("div");

packages/draw/drawWorld.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export type Options = {
99
colorEmpty: string;
1010
colorDotBorder: string;
1111
colorSnake: string;
12+
colorBackground?: string;
1213
sizeCell: number;
1314
sizeDot: number;
1415
sizeDotBorderRadius: number;
@@ -43,6 +44,11 @@ export const drawWorld = (
4344
) => {
4445
ctx.save();
4546

47+
if (o.colorBackground) {
48+
ctx.fillStyle = o.colorBackground;
49+
ctx.fillRect(0, 0, 99999, 99999);
50+
}
51+
4652
ctx.translate(1 * o.sizeCell, 2 * o.sizeCell);
4753
drawGrid(ctx, grid, cells, o);
4854
drawSnake(ctx, snake, o);
@@ -77,6 +83,11 @@ export const drawLerpWorld = (
7783
) => {
7884
ctx.save();
7985

86+
if (o.colorBackground) {
87+
ctx.fillStyle = o.colorBackground;
88+
ctx.fillRect(0, 0, 99999, 99999);
89+
}
90+
8091
ctx.translate(1 * o.sizeCell, 2 * o.sizeCell);
8192
drawGrid(ctx, grid, cells, o);
8293
drawSnakeLerp(ctx, snake0, snake1, k, o);

packages/draw/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
"name": "@snk/draw",
33
"version": "1.0.0",
44
"dependencies": {
5-
"@snk/solver": "1.0.0"
5+
"@snk/types": "1.0.0"
66
}
77
}

0 commit comments

Comments
 (0)