@altnovatrail/picture-kit 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/NOTICE +7 -0
- package/README.md +27 -2
- package/lib/emoji.d.ts +16 -0
- package/lib/emoji.js +64 -0
- package/lib/index.d.ts +9 -0
- package/lib/index.js +10 -0
- package/lib/kind.d.ts +22 -0
- package/lib/kind.js +2 -0
- package/lib/kinds/balance.d.ts +34 -0
- package/lib/kinds/balance.js +217 -0
- package/lib/kinds/branches.d.ts +33 -0
- package/lib/kinds/branches.js +473 -0
- package/lib/kinds/calendar.d.ts +55 -0
- package/lib/kinds/calendar.js +532 -0
- package/lib/kinds/chart.d.ts +48 -0
- package/lib/kinds/chart.js +645 -0
- package/lib/kinds/choices.d.ts +14 -0
- package/lib/kinds/choices.js +53 -0
- package/lib/kinds/clock.d.ts +36 -0
- package/lib/kinds/clock.js +219 -0
- package/lib/kinds/coins.d.ts +37 -0
- package/lib/kinds/coins.js +550 -0
- package/lib/kinds/column.d.ts +37 -0
- package/lib/kinds/column.js +410 -0
- package/lib/kinds/commands-parts.d.ts +12 -0
- package/lib/kinds/commands-parts.js +24 -0
- package/lib/kinds/commands.d.ts +66 -0
- package/lib/kinds/commands.js +789 -0
- package/lib/kinds/containers.d.ts +52 -0
- package/lib/kinds/containers.js +758 -0
- package/lib/kinds/countfig.d.ts +41 -0
- package/lib/kinds/countfig.js +372 -0
- package/lib/kinds/cubes.d.ts +70 -0
- package/lib/kinds/cubes.js +371 -0
- package/lib/kinds/die.d.ts +32 -0
- package/lib/kinds/die.js +301 -0
- package/lib/kinds/domino.d.ts +37 -0
- package/lib/kinds/domino.js +450 -0
- package/lib/kinds/dotgrid.d.ts +38 -0
- package/lib/kinds/dotgrid.js +411 -0
- package/lib/kinds/figure.d.ts +62 -0
- package/lib/kinds/figure.js +687 -0
- package/lib/kinds/fold-parts.d.ts +73 -0
- package/lib/kinds/fold-parts.js +315 -0
- package/lib/kinds/fold.d.ts +52 -0
- package/lib/kinds/fold.js +650 -0
- package/lib/kinds/fraction.d.ts +50 -0
- package/lib/kinds/fraction.js +568 -0
- package/lib/kinds/gauges.d.ts +53 -0
- package/lib/kinds/gauges.js +880 -0
- package/lib/kinds/gears.d.ts +40 -0
- package/lib/kinds/gears.js +787 -0
- package/lib/kinds/grid.d.ts +45 -0
- package/lib/kinds/grid.js +591 -0
- package/lib/kinds/groups.d.ts +23 -0
- package/lib/kinds/groups.js +335 -0
- package/lib/kinds/index.d.ts +3 -0
- package/lib/kinds/index.js +87 -0
- package/lib/kinds/lanes.d.ts +36 -0
- package/lib/kinds/lanes.js +220 -0
- package/lib/kinds/layers.d.ts +31 -0
- package/lib/kinds/layers.js +419 -0
- package/lib/kinds/line.d.ts +38 -0
- package/lib/kinds/line.js +739 -0
- package/lib/kinds/magic-parts.d.ts +72 -0
- package/lib/kinds/magic-parts.js +186 -0
- package/lib/kinds/magic.d.ts +66 -0
- package/lib/kinds/magic.js +664 -0
- package/lib/kinds/map.d.ts +32 -0
- package/lib/kinds/map.js +370 -0
- package/lib/kinds/matchsticks.d.ts +26 -0
- package/lib/kinds/matchsticks.js +434 -0
- package/lib/kinds/necklace.d.ts +27 -0
- package/lib/kinds/necklace.js +342 -0
- package/lib/kinds/net.d.ts +47 -0
- package/lib/kinds/net.js +241 -0
- package/lib/kinds/overlay.d.ts +50 -0
- package/lib/kinds/overlay.js +519 -0
- package/lib/kinds/pieces.d.ts +41 -0
- package/lib/kinds/pieces.js +420 -0
- package/lib/kinds/pipes.d.ts +46 -0
- package/lib/kinds/pipes.js +657 -0
- package/lib/kinds/plane-parts.d.ts +105 -0
- package/lib/kinds/plane-parts.js +372 -0
- package/lib/kinds/plane.d.ts +35 -0
- package/lib/kinds/plane.js +755 -0
- package/lib/kinds/pyramid.d.ts +26 -0
- package/lib/kinds/pyramid.js +228 -0
- package/lib/kinds/ropes.d.ts +30 -0
- package/lib/kinds/ropes.js +571 -0
- package/lib/kinds/shapes.d.ts +59 -0
- package/lib/kinds/shapes.js +490 -0
- package/lib/kinds/solids.d.ts +78 -0
- package/lib/kinds/solids.js +1058 -0
- package/lib/kinds/tangram.d.ts +52 -0
- package/lib/kinds/tangram.js +713 -0
- package/lib/kinds/target.d.ts +39 -0
- package/lib/kinds/target.js +761 -0
- package/lib/kinds/tiles.d.ts +55 -0
- package/lib/kinds/tiles.js +606 -0
- package/lib/kinds/venn.d.ts +38 -0
- package/lib/kinds/venn.js +710 -0
- package/lib/noto.d.ts +2 -0
- package/lib/noto.js +212 -0
- package/lib/objects.d.ts +7 -0
- package/lib/objects.js +226 -0
- package/lib/pen.d.ts +32 -0
- package/lib/pen.js +60 -0
- package/lib/read.d.ts +9 -0
- package/lib/read.js +49 -0
- package/lib/render.d.ts +24 -0
- package/lib/render.js +68 -0
- package/lib/solve/balance.d.ts +46 -0
- package/lib/solve/balance.js +118 -0
- package/lib/style.d.ts +70 -0
- package/lib/style.js +39 -0
- package/lib/svg.d.ts +22 -0
- package/lib/svg.js +48 -0
- package/lib/things.d.ts +40 -0
- package/lib/things.js +269 -0
- package/package.json +28 -4
package/lib/style.d.ts
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
export declare const ink = "#1d2433";
|
|
2
|
+
export declare const ground = "#ffffff";
|
|
3
|
+
export declare const soft = "#eef1f6";
|
|
4
|
+
export declare const faint = "#c9d0dc";
|
|
5
|
+
/** The width of an outline at the picture's own scale. */
|
|
6
|
+
export declare const outline = 2.5;
|
|
7
|
+
/** The font of labels; the picture carries no font file of its own. */
|
|
8
|
+
export declare const font = "'Golos Text', 'Nunito', 'Segoe UI', system-ui, -apple-system, Roboto, sans-serif";
|
|
9
|
+
export declare const colors: {
|
|
10
|
+
readonly red: {
|
|
11
|
+
readonly fill: "#e8453c";
|
|
12
|
+
readonly dark: "#b8302a";
|
|
13
|
+
readonly word: "red";
|
|
14
|
+
};
|
|
15
|
+
readonly orange: {
|
|
16
|
+
readonly fill: "#f28c38";
|
|
17
|
+
readonly dark: "#c46a22";
|
|
18
|
+
readonly word: "orange";
|
|
19
|
+
};
|
|
20
|
+
readonly yellow: {
|
|
21
|
+
readonly fill: "#f7c531";
|
|
22
|
+
readonly dark: "#c99a12";
|
|
23
|
+
readonly word: "yellow";
|
|
24
|
+
};
|
|
25
|
+
readonly green: {
|
|
26
|
+
readonly fill: "#3cae5c";
|
|
27
|
+
readonly dark: "#2a8344";
|
|
28
|
+
readonly word: "green";
|
|
29
|
+
};
|
|
30
|
+
readonly blue: {
|
|
31
|
+
readonly fill: "#3d7fe0";
|
|
32
|
+
readonly dark: "#2a5fb0";
|
|
33
|
+
readonly word: "blue";
|
|
34
|
+
};
|
|
35
|
+
readonly purple: {
|
|
36
|
+
readonly fill: "#9b5de5";
|
|
37
|
+
readonly dark: "#7442b5";
|
|
38
|
+
readonly word: "purple";
|
|
39
|
+
};
|
|
40
|
+
readonly pink: {
|
|
41
|
+
readonly fill: "#f37fb0";
|
|
42
|
+
readonly dark: "#c95a8a";
|
|
43
|
+
readonly word: "pink";
|
|
44
|
+
};
|
|
45
|
+
readonly brown: {
|
|
46
|
+
readonly fill: "#9a6a44";
|
|
47
|
+
readonly dark: "#71492c";
|
|
48
|
+
readonly word: "brown";
|
|
49
|
+
};
|
|
50
|
+
readonly gray: {
|
|
51
|
+
readonly fill: "#a3abb8";
|
|
52
|
+
readonly dark: "#77808f";
|
|
53
|
+
readonly word: "gray";
|
|
54
|
+
};
|
|
55
|
+
readonly white: {
|
|
56
|
+
readonly fill: "#ffffff";
|
|
57
|
+
readonly dark: "#c9d0dc";
|
|
58
|
+
readonly word: "white";
|
|
59
|
+
};
|
|
60
|
+
readonly black: {
|
|
61
|
+
readonly fill: "#2b3242";
|
|
62
|
+
readonly dark: "#141824";
|
|
63
|
+
readonly word: "black";
|
|
64
|
+
};
|
|
65
|
+
};
|
|
66
|
+
export type ColorName = keyof typeof colors;
|
|
67
|
+
export declare const colorNames: ColorName[];
|
|
68
|
+
export declare function isColor(name: unknown): name is ColorName;
|
|
69
|
+
/** A label's text colour on a fill: dark on light fills, white on dark ones. */
|
|
70
|
+
export declare function textOn(name: ColorName): string;
|
package/lib/style.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// The one style of every picture: flat colours, a dark outline of one width,
|
|
2
|
+
// round corners, a white ground. A colour has a word, so that a statement can
|
|
3
|
+
// say "the red ball" and a learner who does not tell colours apart, or a
|
|
4
|
+
// screen reader, gets the same word.
|
|
5
|
+
export const ink = "#1d2433";
|
|
6
|
+
export const ground = "#ffffff";
|
|
7
|
+
export const soft = "#eef1f6";
|
|
8
|
+
export const faint = "#c9d0dc";
|
|
9
|
+
/** The width of an outline at the picture's own scale. */
|
|
10
|
+
export const outline = 2.5;
|
|
11
|
+
/** The font of labels; the picture carries no font file of its own. */
|
|
12
|
+
export const font = "'Golos Text', 'Nunito', 'Segoe UI', system-ui, -apple-system, Roboto, sans-serif";
|
|
13
|
+
export const colors = {
|
|
14
|
+
red: { fill: "#e8453c", dark: "#b8302a", word: "red" },
|
|
15
|
+
orange: { fill: "#f28c38", dark: "#c46a22", word: "orange" },
|
|
16
|
+
yellow: { fill: "#f7c531", dark: "#c99a12", word: "yellow" },
|
|
17
|
+
green: { fill: "#3cae5c", dark: "#2a8344", word: "green" },
|
|
18
|
+
blue: { fill: "#3d7fe0", dark: "#2a5fb0", word: "blue" },
|
|
19
|
+
purple: { fill: "#9b5de5", dark: "#7442b5", word: "purple" },
|
|
20
|
+
pink: { fill: "#f37fb0", dark: "#c95a8a", word: "pink" },
|
|
21
|
+
brown: { fill: "#9a6a44", dark: "#71492c", word: "brown" },
|
|
22
|
+
gray: { fill: "#a3abb8", dark: "#77808f", word: "gray" },
|
|
23
|
+
white: { fill: "#ffffff", dark: "#c9d0dc", word: "white" },
|
|
24
|
+
black: { fill: "#2b3242", dark: "#141824", word: "black" },
|
|
25
|
+
};
|
|
26
|
+
export const colorNames = Object.keys(colors);
|
|
27
|
+
export function isColor(name) {
|
|
28
|
+
return typeof name === "string" && Object.hasOwn(colors, name);
|
|
29
|
+
}
|
|
30
|
+
/** A label's text colour on a fill: dark on light fills, white on dark ones. */
|
|
31
|
+
export function textOn(name) {
|
|
32
|
+
return name === "white" ||
|
|
33
|
+
name === "yellow" ||
|
|
34
|
+
name === "pink" ||
|
|
35
|
+
name === "gray" ||
|
|
36
|
+
name === "orange"
|
|
37
|
+
? ink
|
|
38
|
+
: "#ffffff";
|
|
39
|
+
}
|
package/lib/svg.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/** n writes a coordinate: a tenth of a pixel, no "-0", no trailing zeros. */
|
|
2
|
+
/**
|
|
3
|
+
* byCodeUnit orders strings by their UTF-16 code units, as sort() does with no compare function,
|
|
4
|
+
* but said outright: the order must not depend on a locale, or the same scene would give other
|
|
5
|
+
* bytes on another machine.
|
|
6
|
+
*/
|
|
7
|
+
export declare function byCodeUnit(a: string, b: string): number;
|
|
8
|
+
export declare function n(value: number): string;
|
|
9
|
+
/** esc makes a text safe inside an element or an attribute. */
|
|
10
|
+
export declare function esc(text: string): string;
|
|
11
|
+
export type Attrs = Record<string, string | number | undefined>;
|
|
12
|
+
/** attrs writes attributes in the order given; numbers go through n. */
|
|
13
|
+
export declare function attrs(values: Attrs): string;
|
|
14
|
+
/** el writes one element, empty when it has no children. */
|
|
15
|
+
export declare function el(tag: string, values: Attrs, children?: string): string;
|
|
16
|
+
/** A point. */
|
|
17
|
+
export type P = {
|
|
18
|
+
x: number;
|
|
19
|
+
y: number;
|
|
20
|
+
};
|
|
21
|
+
/** path writes the d of a polygon through the points, closed or not. */
|
|
22
|
+
export declare function poly(points: readonly P[], closed?: boolean): string;
|
package/lib/svg.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// Small helpers that write SVG markup as text. Numbers are rounded to a tenth
|
|
2
|
+
// of a pixel, so that the same scene always gives the same bytes.
|
|
3
|
+
/** n writes a coordinate: a tenth of a pixel, no "-0", no trailing zeros. */
|
|
4
|
+
/**
|
|
5
|
+
* byCodeUnit orders strings by their UTF-16 code units, as sort() does with no compare function,
|
|
6
|
+
* but said outright: the order must not depend on a locale, or the same scene would give other
|
|
7
|
+
* bytes on another machine.
|
|
8
|
+
*/
|
|
9
|
+
export function byCodeUnit(a, b) {
|
|
10
|
+
if (a === b)
|
|
11
|
+
return 0;
|
|
12
|
+
return a < b ? -1 : 1;
|
|
13
|
+
}
|
|
14
|
+
export function n(value) {
|
|
15
|
+
const rounded = Math.round(value * 10) / 10;
|
|
16
|
+
return Object.is(rounded, -0) ? "0" : String(rounded);
|
|
17
|
+
}
|
|
18
|
+
const escapes = {
|
|
19
|
+
"&": "&",
|
|
20
|
+
"<": "<",
|
|
21
|
+
">": ">",
|
|
22
|
+
'"': """,
|
|
23
|
+
"'": "'",
|
|
24
|
+
};
|
|
25
|
+
/** esc makes a text safe inside an element or an attribute. */
|
|
26
|
+
export function esc(text) {
|
|
27
|
+
return text.replace(/[&<>"']/g, (c) => escapes[c] ?? c);
|
|
28
|
+
}
|
|
29
|
+
/** attrs writes attributes in the order given; numbers go through n. */
|
|
30
|
+
export function attrs(values) {
|
|
31
|
+
let out = "";
|
|
32
|
+
for (const [name, value] of Object.entries(values)) {
|
|
33
|
+
if (value === undefined)
|
|
34
|
+
continue;
|
|
35
|
+
out += ` ${name}="${typeof value === "number" ? n(value) : esc(value)}"`;
|
|
36
|
+
}
|
|
37
|
+
return out;
|
|
38
|
+
}
|
|
39
|
+
/** el writes one element, empty when it has no children. */
|
|
40
|
+
export function el(tag, values, children) {
|
|
41
|
+
return children === undefined || children === ""
|
|
42
|
+
? `<${tag}${attrs(values)}/>`
|
|
43
|
+
: `<${tag}${attrs(values)}>${children}</${tag}>`;
|
|
44
|
+
}
|
|
45
|
+
/** path writes the d of a polygon through the points, closed or not. */
|
|
46
|
+
export function poly(points, closed = true) {
|
|
47
|
+
return (points.map((p, i) => `${i === 0 ? "M" : "L"}${n(p.x)} ${n(p.y)}`).join("") + (closed ? "Z" : ""));
|
|
48
|
+
}
|
package/lib/things.d.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { Pen } from "./pen.ts";
|
|
2
|
+
import { type Problems } from "./read.ts";
|
|
3
|
+
import { type ColorName } from "./style.ts";
|
|
4
|
+
/**
|
|
5
|
+
* A thing is what stands in a place of a scene: an object of the list, drawn
|
|
6
|
+
* as its emoji; or something the picture draws itself in the palette — a
|
|
7
|
+
* ball, a shape, a card with a number or a letter —; or a "?" where the
|
|
8
|
+
* question asks; or nothing, an empty place.
|
|
9
|
+
*/
|
|
10
|
+
export type Thing = string | {
|
|
11
|
+
ball: ColorName;
|
|
12
|
+
label?: string;
|
|
13
|
+
} | {
|
|
14
|
+
shape: ShapeName;
|
|
15
|
+
color: ColorName;
|
|
16
|
+
label?: string;
|
|
17
|
+
} | {
|
|
18
|
+
card: string;
|
|
19
|
+
color?: ColorName;
|
|
20
|
+
} | {
|
|
21
|
+
weight: string;
|
|
22
|
+
} | {
|
|
23
|
+
ask: true;
|
|
24
|
+
} | {
|
|
25
|
+
empty: true;
|
|
26
|
+
};
|
|
27
|
+
export declare const shapeNames: readonly ["circle", "square", "triangle", "star", "heart", "diamond", "hexagon", "pentagon", "oval", "cross", "moon"];
|
|
28
|
+
export type ShapeName = (typeof shapeNames)[number];
|
|
29
|
+
/** checkThing records what is wrong with a thing at a path. */
|
|
30
|
+
export declare function checkThing(value: unknown, path: string, problems: Problems): void;
|
|
31
|
+
/** wordOf names a thing, one or many, for the text that reads a picture. */
|
|
32
|
+
export declare function wordOf(thing: Thing, count?: number): string;
|
|
33
|
+
/** count says how many: "3 pears", "1 pear". */
|
|
34
|
+
export declare function count(thing: Thing, how: number): string;
|
|
35
|
+
/** shapePath is the outline of a shape centred at cx, cy, about size across. */
|
|
36
|
+
export declare function shapePath(shape: ShapeName, cx: number, cy: number, size: number): string;
|
|
37
|
+
/** drawThing draws a thing centred at cx, cy, size pixels across. */
|
|
38
|
+
export declare function drawThing(pen: Pen, thing: Thing, cx: number, cy: number, size: number): void;
|
|
39
|
+
/** sameThing tells whether two things are the same, as a scene names them. */
|
|
40
|
+
export declare function sameThing(a: Thing, b: Thing): boolean;
|
package/lib/things.js
ADDED
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
import { isObject, objects } from "./objects.js";
|
|
2
|
+
import { isObj, text } from "./read.js";
|
|
3
|
+
import { colorNames, colors, ink, isColor, outline, textOn } from "./style.js";
|
|
4
|
+
import { n, poly } from "./svg.js";
|
|
5
|
+
export const shapeNames = [
|
|
6
|
+
"circle",
|
|
7
|
+
"square",
|
|
8
|
+
"triangle",
|
|
9
|
+
"star",
|
|
10
|
+
"heart",
|
|
11
|
+
"diamond",
|
|
12
|
+
"hexagon",
|
|
13
|
+
"pentagon",
|
|
14
|
+
"oval",
|
|
15
|
+
"cross",
|
|
16
|
+
"moon",
|
|
17
|
+
];
|
|
18
|
+
/** checkThing records what is wrong with a thing at a path. */
|
|
19
|
+
export function checkThing(value, path, problems) {
|
|
20
|
+
if (typeof value === "string") {
|
|
21
|
+
if (!isObject(value))
|
|
22
|
+
problems.push(`${path}: "${value}" is not an object of the list`);
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
if (!isObj(value)) {
|
|
26
|
+
problems.push(`${path}: must be an object id or a thing`);
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
if ("ball" in value) {
|
|
30
|
+
if (!isColor(value.ball))
|
|
31
|
+
problems.push(`${path}.ball: must be a colour: ${colorNames.join(", ")}`);
|
|
32
|
+
if (value.label !== undefined)
|
|
33
|
+
text(value.label, `${path}.label`, problems, 3);
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
if ("shape" in value) {
|
|
37
|
+
if (!shapeNames.includes(value.shape))
|
|
38
|
+
problems.push(`${path}.shape: must be one of ${shapeNames.join(", ")}`);
|
|
39
|
+
if (!isColor(value.color))
|
|
40
|
+
problems.push(`${path}.color: must be a colour`);
|
|
41
|
+
if (value.label !== undefined)
|
|
42
|
+
text(value.label, `${path}.label`, problems, 3);
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
if ("card" in value) {
|
|
46
|
+
text(value.card, `${path}.card`, problems, 3);
|
|
47
|
+
if (value.color !== undefined && !isColor(value.color))
|
|
48
|
+
problems.push(`${path}.color: must be a colour`);
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
if ("weight" in value) {
|
|
52
|
+
text(value.weight, `${path}.weight`, problems, 6);
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
if (value.ask === true || value.empty === true)
|
|
56
|
+
return;
|
|
57
|
+
problems.push(`${path}: must be an object id, a ball, a shape, a card, a weight, ask or empty`);
|
|
58
|
+
}
|
|
59
|
+
/** wordOf names a thing, one or many, for the text that reads a picture. */
|
|
60
|
+
export function wordOf(thing, count = 1) {
|
|
61
|
+
const many = count !== 1;
|
|
62
|
+
if (typeof thing === "string") {
|
|
63
|
+
const entry = objects[thing];
|
|
64
|
+
return entry ? (many ? entry.words : entry.word) : thing;
|
|
65
|
+
}
|
|
66
|
+
if ("ball" in thing)
|
|
67
|
+
return `${colors[thing.ball].word} ball${many ? "s" : ""}${thing.label ? ` ${thing.label}` : ""}`;
|
|
68
|
+
if ("shape" in thing)
|
|
69
|
+
return `${colors[thing.color].word} ${thing.shape}${many ? "s" : ""}${thing.label ? ` ${thing.label}` : ""}`;
|
|
70
|
+
if ("card" in thing)
|
|
71
|
+
return `card${many ? "s" : ""} "${thing.card}"`;
|
|
72
|
+
if ("weight" in thing)
|
|
73
|
+
return `weight${many ? "s" : ""} of ${thing.weight}`;
|
|
74
|
+
if ("ask" in thing)
|
|
75
|
+
return "question mark";
|
|
76
|
+
return "empty place";
|
|
77
|
+
}
|
|
78
|
+
/** count says how many: "3 pears", "1 pear". */
|
|
79
|
+
export function count(thing, how) {
|
|
80
|
+
return `${how} ${wordOf(thing, how)}`;
|
|
81
|
+
}
|
|
82
|
+
function star(cx, cy, r, points = 5, inner = 0.45) {
|
|
83
|
+
const out = [];
|
|
84
|
+
for (let i = 0; i < points * 2; i++) {
|
|
85
|
+
const radius = i % 2 === 0 ? r : r * inner;
|
|
86
|
+
const angle = -Math.PI / 2 + (i * Math.PI) / points;
|
|
87
|
+
out.push({ x: cx + radius * Math.cos(angle), y: cy + radius * Math.sin(angle) });
|
|
88
|
+
}
|
|
89
|
+
return out;
|
|
90
|
+
}
|
|
91
|
+
function regular(cx, cy, r, sides, turn = -Math.PI / 2) {
|
|
92
|
+
return Array.from({ length: sides }, (_, i) => ({
|
|
93
|
+
x: cx + r * Math.cos(turn + (2 * Math.PI * i) / sides),
|
|
94
|
+
y: cy + r * Math.sin(turn + (2 * Math.PI * i) / sides),
|
|
95
|
+
}));
|
|
96
|
+
}
|
|
97
|
+
/** shapePath is the outline of a shape centred at cx, cy, about size across. */
|
|
98
|
+
export function shapePath(shape, cx, cy, size) {
|
|
99
|
+
const r = size / 2;
|
|
100
|
+
switch (shape) {
|
|
101
|
+
case "circle":
|
|
102
|
+
return `M${n(cx - r)} ${n(cy)}a${n(r)} ${n(r)} 0 1 0 ${n(2 * r)} 0a${n(r)} ${n(r)} 0 1 0 ${n(-2 * r)} 0Z`;
|
|
103
|
+
case "oval":
|
|
104
|
+
return `M${n(cx - r)} ${n(cy)}a${n(r)} ${n(r * 0.66)} 0 1 0 ${n(2 * r)} 0a${n(r)} ${n(r * 0.66)} 0 1 0 ${n(-2 * r)} 0Z`;
|
|
105
|
+
case "square":
|
|
106
|
+
return poly([
|
|
107
|
+
{ x: cx - r * 0.86, y: cy - r * 0.86 },
|
|
108
|
+
{ x: cx + r * 0.86, y: cy - r * 0.86 },
|
|
109
|
+
{ x: cx + r * 0.86, y: cy + r * 0.86 },
|
|
110
|
+
{ x: cx - r * 0.86, y: cy + r * 0.86 },
|
|
111
|
+
]);
|
|
112
|
+
case "triangle":
|
|
113
|
+
return poly(regular(cx, cy + r * 0.18, r * 1.05, 3));
|
|
114
|
+
case "diamond":
|
|
115
|
+
return poly(regular(cx, cy, r, 4));
|
|
116
|
+
case "pentagon":
|
|
117
|
+
return poly(regular(cx, cy + r * 0.06, r, 5));
|
|
118
|
+
case "hexagon":
|
|
119
|
+
return poly(regular(cx, cy, r, 6, 0));
|
|
120
|
+
case "star":
|
|
121
|
+
return poly(star(cx, cy + r * 0.06, r * 1.04));
|
|
122
|
+
case "cross": {
|
|
123
|
+
const a = r * 0.34;
|
|
124
|
+
return poly([
|
|
125
|
+
{ x: cx - a, y: cy - r },
|
|
126
|
+
{ x: cx + a, y: cy - r },
|
|
127
|
+
{ x: cx + a, y: cy - a },
|
|
128
|
+
{ x: cx + r, y: cy - a },
|
|
129
|
+
{ x: cx + r, y: cy + a },
|
|
130
|
+
{ x: cx + a, y: cy + a },
|
|
131
|
+
{ x: cx + a, y: cy + r },
|
|
132
|
+
{ x: cx - a, y: cy + r },
|
|
133
|
+
{ x: cx - a, y: cy + a },
|
|
134
|
+
{ x: cx - r, y: cy + a },
|
|
135
|
+
{ x: cx - r, y: cy - a },
|
|
136
|
+
{ x: cx - a, y: cy - a },
|
|
137
|
+
]);
|
|
138
|
+
}
|
|
139
|
+
case "heart": {
|
|
140
|
+
const t = cy - r * 0.45;
|
|
141
|
+
return `M${n(cx)} ${n(cy + r * 0.9)}C${n(cx - r * 1.25)} ${n(cy + r * 0.1)} ${n(cx - r * 0.95)} ${n(t - r * 0.75)} ${n(cx)} ${n(t)}C${n(cx + r * 0.95)} ${n(t - r * 0.75)} ${n(cx + r * 1.25)} ${n(cy + r * 0.1)} ${n(cx)} ${n(cy + r * 0.9)}Z`;
|
|
142
|
+
}
|
|
143
|
+
case "moon":
|
|
144
|
+
// A crescent: the outer circle from tip to tip round the left, and back
|
|
145
|
+
// along a wider, shallower arc whose centre lies to the right.
|
|
146
|
+
return `M${n(cx + r * 0.5)} ${n(cy - r * 0.866)}A${n(r)} ${n(r)} 0 1 0 ${n(cx + r * 0.5)} ${n(cy + r * 0.866)}A${n(r * 0.9)} ${n(r * 0.9)} 0 0 1 ${n(cx + r * 0.5)} ${n(cy - r * 0.866)}Z`;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
/** drawThing draws a thing centred at cx, cy, size pixels across. */
|
|
150
|
+
export function drawThing(pen, thing, cx, cy, size) {
|
|
151
|
+
if (typeof thing === "string") {
|
|
152
|
+
const entry = objects[thing];
|
|
153
|
+
if (entry)
|
|
154
|
+
pen.emoji(entry.code, cx, cy, size);
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
if ("ball" in thing) {
|
|
158
|
+
const r = size * 0.44;
|
|
159
|
+
const c = colors[thing.ball];
|
|
160
|
+
pen.el("circle", { cx, cy, r, fill: c.fill, stroke: ink, "stroke-width": outline });
|
|
161
|
+
pen.el("ellipse", {
|
|
162
|
+
cx: cx - r * 0.38,
|
|
163
|
+
cy: cy - r * 0.42,
|
|
164
|
+
rx: r * 0.26,
|
|
165
|
+
ry: r * 0.16,
|
|
166
|
+
fill: "#ffffff",
|
|
167
|
+
"fill-opacity": 0.55,
|
|
168
|
+
transform: `rotate(-35 ${n(cx - r * 0.38)} ${n(cy - r * 0.42)})`,
|
|
169
|
+
});
|
|
170
|
+
if (thing.label)
|
|
171
|
+
pen.text(cx, cy + r * 0.06, thing.label, { size: r * 0.95, fill: textOn(thing.ball) });
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
if ("shape" in thing) {
|
|
175
|
+
pen.el("path", {
|
|
176
|
+
d: shapePath(thing.shape, cx, cy, size * 0.9),
|
|
177
|
+
fill: colors[thing.color].fill,
|
|
178
|
+
stroke: ink,
|
|
179
|
+
"stroke-width": outline,
|
|
180
|
+
"stroke-linejoin": "round",
|
|
181
|
+
});
|
|
182
|
+
if (thing.label) {
|
|
183
|
+
// A moon is hollow at its centre: its label goes in the thick of the crescent.
|
|
184
|
+
const moon = thing.shape === "moon";
|
|
185
|
+
pen.text(moon ? cx - size * 0.24 : cx, cy + size * 0.04, thing.label, {
|
|
186
|
+
size: size * (moon ? 0.26 : 0.36),
|
|
187
|
+
fill: textOn(thing.color),
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
if ("card" in thing) {
|
|
193
|
+
const color = thing.color ?? "white";
|
|
194
|
+
const w = size * 0.74;
|
|
195
|
+
const h = size * 0.92;
|
|
196
|
+
pen.el("rect", {
|
|
197
|
+
x: cx - w / 2,
|
|
198
|
+
y: cy - h / 2,
|
|
199
|
+
width: w,
|
|
200
|
+
height: h,
|
|
201
|
+
rx: size * 0.1,
|
|
202
|
+
fill: colors[color].fill,
|
|
203
|
+
stroke: ink,
|
|
204
|
+
"stroke-width": outline,
|
|
205
|
+
});
|
|
206
|
+
pen.text(cx, cy, thing.card, {
|
|
207
|
+
size: size * (thing.card.length > 2 ? 0.32 : 0.46),
|
|
208
|
+
fill: textOn(color),
|
|
209
|
+
});
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
if ("weight" in thing) {
|
|
213
|
+
const w = size * 0.86;
|
|
214
|
+
const top = cy - size * 0.18;
|
|
215
|
+
const bottom = cy + size * 0.42;
|
|
216
|
+
pen.el("circle", {
|
|
217
|
+
cx,
|
|
218
|
+
cy: top - size * 0.1,
|
|
219
|
+
r: size * 0.14,
|
|
220
|
+
fill: "none",
|
|
221
|
+
stroke: ink,
|
|
222
|
+
"stroke-width": outline * 1.4,
|
|
223
|
+
});
|
|
224
|
+
pen.el("path", {
|
|
225
|
+
d: poly([
|
|
226
|
+
{ x: cx - w * 0.36, y: top },
|
|
227
|
+
{ x: cx + w * 0.36, y: top },
|
|
228
|
+
{ x: cx + w / 2, y: bottom },
|
|
229
|
+
{ x: cx - w / 2, y: bottom },
|
|
230
|
+
]),
|
|
231
|
+
fill: "#5d6676",
|
|
232
|
+
stroke: ink,
|
|
233
|
+
"stroke-width": outline,
|
|
234
|
+
"stroke-linejoin": "round",
|
|
235
|
+
});
|
|
236
|
+
pen.text(cx, (top + bottom) / 2 + 1, thing.weight, {
|
|
237
|
+
size: Math.min(size * 0.38, (w * 1.7) / Math.max(2, thing.weight.length)),
|
|
238
|
+
fill: "#ffffff",
|
|
239
|
+
});
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
if ("ask" in thing) {
|
|
243
|
+
const r = size * 0.42;
|
|
244
|
+
pen.el("circle", {
|
|
245
|
+
cx,
|
|
246
|
+
cy,
|
|
247
|
+
r,
|
|
248
|
+
fill: "#ffffff",
|
|
249
|
+
stroke: ink,
|
|
250
|
+
"stroke-width": outline,
|
|
251
|
+
"stroke-dasharray": "5 4",
|
|
252
|
+
});
|
|
253
|
+
pen.text(cx, cy, "?", { size: r * 1.3 });
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
pen.el("circle", {
|
|
257
|
+
cx,
|
|
258
|
+
cy,
|
|
259
|
+
r: size * 0.36,
|
|
260
|
+
fill: "none",
|
|
261
|
+
stroke: "#c9d0dc",
|
|
262
|
+
"stroke-width": outline,
|
|
263
|
+
"stroke-dasharray": "4 4",
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
/** sameThing tells whether two things are the same, as a scene names them. */
|
|
267
|
+
export function sameThing(a, b) {
|
|
268
|
+
return JSON.stringify(a) === JSON.stringify(b);
|
|
269
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,30 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@altnovatrail/picture-kit",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Draws the pictures of tasks from scenes: a closed list of kinds, objects from Noto Emoji, the same scene always the same SVG.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/altnovatrail/picture-kit.git"
|
|
9
|
+
},
|
|
10
|
+
"type": "module",
|
|
11
|
+
"engines": {
|
|
12
|
+
"node": ">=24"
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"lib",
|
|
16
|
+
"NOTICE"
|
|
17
|
+
],
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./lib/index.d.ts",
|
|
21
|
+
"default": "./lib/index.js"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"sideEffects": false,
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@biomejs/biome": "2.6.9",
|
|
27
|
+
"@types/node": "24.19.4",
|
|
28
|
+
"typescript": "7.0.2"
|
|
29
|
+
}
|
|
30
|
+
}
|