@johnmorrisdotca/kyogijo-stadiums 0.0.0-stage → 0.4.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/CHANGELOG.md +129 -0
- package/LICENSE +21 -0
- package/NOTICE.md +293 -0
- package/README.md +600 -2
- package/dist/annotate.d.ts +47 -0
- package/dist/annotate.js +349 -0
- package/dist/atlas-entry.d.ts +4 -0
- package/dist/atlas-entry.js +3 -0
- package/dist/atlas.d.ts +131 -0
- package/dist/atlas.js +149 -0
- package/dist/background.d.ts +56 -0
- package/dist/background.js +127 -0
- package/dist/ballpark.d.ts +48 -0
- package/dist/ballpark.js +129 -0
- package/dist/canvas-entry.d.ts +2 -0
- package/dist/canvas-entry.js +2 -0
- package/dist/canvas.d.ts +85 -0
- package/dist/canvas.js +279 -0
- package/dist/charts-entry.d.ts +10 -0
- package/dist/charts-entry.js +8 -0
- package/dist/charts.baseball.d.ts +30 -0
- package/dist/charts.baseball.js +152 -0
- package/dist/charts.curling.d.ts +52 -0
- package/dist/charts.curling.js +196 -0
- package/dist/charts.d.ts +172 -0
- package/dist/charts.js +508 -0
- package/dist/charts.network.d.ts +66 -0
- package/dist/charts.network.js +64 -0
- package/dist/charts.soccer.d.ts +43 -0
- package/dist/charts.soccer.js +191 -0
- package/dist/charts.types.d.ts +396 -0
- package/dist/charts.types.js +1 -0
- package/dist/colour.d.ts +29 -0
- package/dist/colour.js +97 -0
- package/dist/common.d.ts +14 -0
- package/dist/common.js +32 -0
- package/dist/court.d.ts +89 -0
- package/dist/court.js +248 -0
- package/dist/courts.style.d.ts +20 -0
- package/dist/courts.style.js +54 -0
- package/dist/cricket.board.d.ts +18 -0
- package/dist/cricket.board.js +67 -0
- package/dist/curling.board.d.ts +24 -0
- package/dist/curling.board.js +147 -0
- package/dist/d3-entry.d.ts +3 -0
- package/dist/d3-entry.js +2 -0
- package/dist/d3.d.ts +45 -0
- package/dist/d3.js +56 -0
- package/dist/draw.d.ts +52 -0
- package/dist/draw.js +705 -0
- package/dist/element-define.d.ts +2 -0
- package/dist/element-define.js +4 -0
- package/dist/element-entry.d.ts +1 -0
- package/dist/element-entry.js +2 -0
- package/dist/element.d.ts +39 -0
- package/dist/element.js +321 -0
- package/dist/eras-entry.d.ts +2 -0
- package/dist/eras-entry.js +2 -0
- package/dist/eras.d.ts +204 -0
- package/dist/eras.js +396 -0
- package/dist/events-entry.d.ts +3 -0
- package/dist/events-entry.js +3 -0
- package/dist/events.d.ts +54 -0
- package/dist/events.data.d.ts +59 -0
- package/dist/events.data.js +297 -0
- package/dist/events.js +127 -0
- package/dist/features.d.ts +31 -0
- package/dist/features.js +284 -0
- package/dist/field.score.d.ts +5 -0
- package/dist/field.score.js +33 -0
- package/dist/fields/afl.d.ts +11 -0
- package/dist/fields/afl.js +103 -0
- package/dist/fields/cricket.d.ts +9 -0
- package/dist/fields/cricket.js +81 -0
- package/dist/fields/fieldhockey.d.ts +6 -0
- package/dist/fields/fieldhockey.js +81 -0
- package/dist/fields/gaelic.d.ts +10 -0
- package/dist/fields/gaelic.js +86 -0
- package/dist/fields/handball.d.ts +6 -0
- package/dist/fields/handball.js +85 -0
- package/dist/fields/lacrosse.d.ts +17 -0
- package/dist/fields/lacrosse.js +169 -0
- package/dist/fields/rugby.d.ts +10 -0
- package/dist/fields/rugby.js +130 -0
- package/dist/fields.d.ts +12 -0
- package/dist/fields.js +44 -0
- package/dist/fields.kit.d.ts +63 -0
- package/dist/fields.kit.js +108 -0
- package/dist/fields.style.d.ts +4 -0
- package/dist/fields.style.js +19 -0
- package/dist/fields.types.d.ts +155 -0
- package/dist/fields.types.js +1 -0
- package/dist/format.d.ts +10 -0
- package/dist/format.js +28 -0
- package/dist/formations.d.ts +18 -0
- package/dist/formations.js +211 -0
- package/dist/frames-entry.d.ts +8 -0
- package/dist/frames-entry.js +7 -0
- package/dist/frames.d.ts +199 -0
- package/dist/frames.js +271 -0
- package/dist/frames.providers.d.ts +16 -0
- package/dist/frames.providers.js +302 -0
- package/dist/geojson-entry.d.ts +2 -0
- package/dist/geojson-entry.js +2 -0
- package/dist/geojson.d.ts +79 -0
- package/dist/geojson.js +147 -0
- package/dist/geometry-entry.d.ts +7 -0
- package/dist/geometry-entry.js +5 -0
- package/dist/geometry.d.ts +295 -0
- package/dist/geometry.js +361 -0
- package/dist/grounds-entry.d.ts +4 -0
- package/dist/grounds-entry.js +3 -0
- package/dist/grounds.d.ts +134 -0
- package/dist/grounds.data.d.ts +2 -0
- package/dist/grounds.data.js +1849 -0
- package/dist/grounds.js +74 -0
- package/dist/heat.colour.d.ts +37 -0
- package/dist/heat.colour.js +54 -0
- package/dist/heat.d.ts +43 -0
- package/dist/heat.js +614 -0
- package/dist/heat.stats.d.ts +123 -0
- package/dist/heat.stats.js +413 -0
- package/dist/heat.types.d.ts +145 -0
- package/dist/heat.types.js +1 -0
- package/dist/heat.zones.d.ts +54 -0
- package/dist/heat.zones.js +424 -0
- package/dist/homeside.d.ts +16 -0
- package/dist/homeside.js +43 -0
- package/dist/index.d.ts +47 -0
- package/dist/index.js +29 -0
- package/dist/layers-entry.d.ts +19 -0
- package/dist/layers-entry.js +11 -0
- package/dist/layers.d.ts +41 -0
- package/dist/layers.env.d.ts +72 -0
- package/dist/layers.env.js +79 -0
- package/dist/layers.js +533 -0
- package/dist/layers.marks.d.ts +45 -0
- package/dist/layers.marks.js +168 -0
- package/dist/layers.types.d.ts +226 -0
- package/dist/layers.types.js +1 -0
- package/dist/layout.d.ts +97 -0
- package/dist/layout.js +381 -0
- package/dist/layout.style.d.ts +6 -0
- package/dist/layout.style.js +17 -0
- package/dist/layouts-entry.d.ts +5 -0
- package/dist/layouts-entry.js +4 -0
- package/dist/leagues/aussie.d.ts +57 -0
- package/dist/leagues/aussie.js +36 -0
- package/dist/leagues/baseball.d.ts +58 -0
- package/dist/leagues/baseball.js +171 -0
- package/dist/leagues/basketball.d.ts +67 -0
- package/dist/leagues/basketball.js +128 -0
- package/dist/leagues/courts.d.ts +212 -0
- package/dist/leagues/courts.js +114 -0
- package/dist/leagues/cricket.d.ts +45 -0
- package/dist/leagues/cricket.js +34 -0
- package/dist/leagues/curling.d.ts +64 -0
- package/dist/leagues/curling.js +53 -0
- package/dist/leagues/fieldhockey.d.ts +48 -0
- package/dist/leagues/fieldhockey.js +29 -0
- package/dist/leagues/fields.d.ts +10 -0
- package/dist/leagues/fields.js +6 -0
- package/dist/leagues/football.d.ts +8 -0
- package/dist/leagues/football.js +82 -0
- package/dist/leagues/gaelic.d.ts +50 -0
- package/dist/leagues/gaelic.js +29 -0
- package/dist/leagues/handball.d.ts +45 -0
- package/dist/leagues/handball.js +32 -0
- package/dist/leagues/hockey.d.ts +63 -0
- package/dist/leagues/hockey.js +166 -0
- package/dist/leagues/lacrosse.d.ts +103 -0
- package/dist/leagues/lacrosse.js +175 -0
- package/dist/leagues/rugby.d.ts +59 -0
- package/dist/leagues/rugby.js +72 -0
- package/dist/leagues/sources.d.ts +18 -0
- package/dist/leagues/sources.js +95 -0
- package/dist/leagues/types.d.ts +43 -0
- package/dist/leagues/types.js +5 -0
- package/dist/leagues-entry.d.ts +23 -0
- package/dist/leagues-entry.js +15 -0
- package/dist/leagues.d.ts +172 -0
- package/dist/leagues.js +462 -0
- package/dist/look.d.ts +50 -0
- package/dist/look.js +186 -0
- package/dist/mark.d.ts +35 -0
- package/dist/mark.js +59 -0
- package/dist/mount-entry.d.ts +2 -0
- package/dist/mount-entry.js +2 -0
- package/dist/mount.d.ts +63 -0
- package/dist/mount.js +167 -0
- package/dist/mow.d.ts +36 -0
- package/dist/mow.js +111 -0
- package/dist/names.d.ts +77 -0
- package/dist/names.js +97 -0
- package/dist/outline.d.ts +25 -0
- package/dist/outline.js +112 -0
- package/dist/parks-entry.d.ts +4 -0
- package/dist/parks-entry.js +3 -0
- package/dist/parks.d.ts +200 -0
- package/dist/parks.data.d.ts +2 -0
- package/dist/parks.data.js +4489 -0
- package/dist/parks.js +92 -0
- package/dist/plot-entry.d.ts +2 -0
- package/dist/plot-entry.js +2 -0
- package/dist/plot.d.ts +74 -0
- package/dist/plot.js +68 -0
- package/dist/png-entry.d.ts +2 -0
- package/dist/png-entry.js +2 -0
- package/dist/png.d.ts +26 -0
- package/dist/png.js +46 -0
- package/dist/ranges.d.ts +33 -0
- package/dist/ranges.js +178 -0
- package/dist/ranges.names.d.ts +31 -0
- package/dist/ranges.names.js +65 -0
- package/dist/react-entry.d.ts +2 -0
- package/dist/react-entry.js +2 -0
- package/dist/react.d.ts +53 -0
- package/dist/react.js +58 -0
- package/dist/replay-entry.d.ts +5 -0
- package/dist/replay-entry.js +4 -0
- package/dist/replay.control.d.ts +47 -0
- package/dist/replay.control.js +184 -0
- package/dist/replay.d.ts +170 -0
- package/dist/replay.dom.d.ts +21 -0
- package/dist/replay.dom.js +388 -0
- package/dist/replay.js +579 -0
- package/dist/replay.types.d.ts +152 -0
- package/dist/replay.types.js +1 -0
- package/dist/restyle.d.ts +21 -0
- package/dist/restyle.js +70 -0
- package/dist/road.d.ts +26 -0
- package/dist/road.draw.d.ts +6 -0
- package/dist/road.draw.js +269 -0
- package/dist/road.js +59 -0
- package/dist/road.model.d.ts +63 -0
- package/dist/road.model.js +215 -0
- package/dist/road.style.d.ts +7 -0
- package/dist/road.style.js +26 -0
- package/dist/road.types.d.ts +122 -0
- package/dist/road.types.js +1 -0
- package/dist/scene-entry.d.ts +7 -0
- package/dist/scene-entry.js +5 -0
- package/dist/scene.css.d.ts +44 -0
- package/dist/scene.css.js +296 -0
- package/dist/scene.d.ts +27 -0
- package/dist/scene.fit.d.ts +12 -0
- package/dist/scene.fit.js +18 -0
- package/dist/scene.flat.d.ts +38 -0
- package/dist/scene.flat.js +167 -0
- package/dist/scene.js +420 -0
- package/dist/scene.path.d.ts +40 -0
- package/dist/scene.path.js +357 -0
- package/dist/scene.types.d.ts +172 -0
- package/dist/scene.types.js +1 -0
- package/dist/scene.xml.d.ts +31 -0
- package/dist/scene.xml.js +81 -0
- package/dist/score.d.ts +70 -0
- package/dist/score.js +212 -0
- package/dist/sets.board.d.ts +22 -0
- package/dist/sets.board.js +89 -0
- package/dist/sheet.d.ts +18 -0
- package/dist/sheet.js +38 -0
- package/dist/special.d.ts +44 -0
- package/dist/special.js +204 -0
- package/dist/special.style.d.ts +8 -0
- package/dist/special.style.js +36 -0
- package/dist/strings.d.ts +581 -0
- package/dist/strings.js +411 -0
- package/dist/style.d.ts +35 -0
- package/dist/style.js +177 -0
- package/dist/surfaces/baseball.d.ts +33 -0
- package/dist/surfaces/baseball.js +208 -0
- package/dist/surfaces/baseball.walls.d.ts +16 -0
- package/dist/surfaces/baseball.walls.js +150 -0
- package/dist/surfaces/basketball.d.ts +8 -0
- package/dist/surfaces/basketball.js +199 -0
- package/dist/surfaces/curling.d.ts +23 -0
- package/dist/surfaces/curling.js +128 -0
- package/dist/surfaces/field.d.ts +12 -0
- package/dist/surfaces/field.js +163 -0
- package/dist/surfaces/football.d.ts +12 -0
- package/dist/surfaces/football.js +208 -0
- package/dist/surfaces/hockey.d.ts +10 -0
- package/dist/surfaces/hockey.js +166 -0
- package/dist/surfaces/net.d.ts +7 -0
- package/dist/surfaces/net.js +151 -0
- package/dist/surfaces/soccer-providers.d.ts +124 -0
- package/dist/surfaces/soccer-providers.js +334 -0
- package/dist/surfaces/soccer-spec.d.ts +138 -0
- package/dist/surfaces/soccer-spec.js +175 -0
- package/dist/surfaces/soccer.d.ts +25 -0
- package/dist/surfaces/soccer.js +270 -0
- package/dist/surfaces/surface.d.ts +180 -0
- package/dist/surfaces/surface.js +49 -0
- package/dist/team.common.d.ts +75 -0
- package/dist/team.common.js +97 -0
- package/dist/team.formation.d.ts +24 -0
- package/dist/team.formation.js +121 -0
- package/dist/team.geometry.d.ts +66 -0
- package/dist/team.geometry.js +304 -0
- package/dist/team.layers.d.ts +16 -0
- package/dist/team.layers.js +32 -0
- package/dist/team.motion.d.ts +9 -0
- package/dist/team.motion.js +325 -0
- package/dist/team.network.d.ts +43 -0
- package/dist/team.network.js +215 -0
- package/dist/team.shape.d.ts +12 -0
- package/dist/team.shape.js +217 -0
- package/dist/team.types.d.ts +189 -0
- package/dist/team.types.js +1 -0
- package/dist/text.d.ts +31 -0
- package/dist/text.js +88 -0
- package/dist/themes.d.ts +14 -0
- package/dist/themes.js +24 -0
- package/dist/types.d.ts +565 -0
- package/dist/types.js +1 -0
- package/dist/units.d.ts +45 -0
- package/dist/units.js +90 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.js +2 -0
- package/package.json +234 -4
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
// The layers that draw the shape of a team from where its players stand: the Voronoi cells (who controls the space), the Delaunay triangulation (who
|
|
2
|
+
// is near whom) and the convex hull (how much ground the team covers). Written for this package from the plain geometry of team.geometry.ts;
|
|
3
|
+
// the ideas are those of mplsoccer's Voronoi, Delaunay and convex-hull examples (MIT), and no code of it is used.
|
|
4
|
+
import { num } from "./format.js";
|
|
5
|
+
import { opacityOf, newMade, toArray, unitOfLayer } from "./layers.env.js";
|
|
6
|
+
import { attrs, dots, frameNote, squareWord, unitWord, groupByTeam, paintOf, placeAll, polygonPath, round, teamClass, teamKey, teamLabel, titled, labelOf } from "./team.common.js";
|
|
7
|
+
import { convexHull, delaunay, polygonArea, polygonCentroid, triangulationEdges, voronoiCells } from "./team.geometry.js";
|
|
8
|
+
import { fill } from "./strings.js";
|
|
9
|
+
import { FEET_PER } from "./units.js";
|
|
10
|
+
const who = (p) => labelOf(p.d);
|
|
11
|
+
/** The share of the surface (0 to 1) nearer each site than any other, counted on a grid of cells laid over the surface and kept where the surface is. A repeated site has nothing. */
|
|
12
|
+
export function surfaceShares(sites, env, across = 120) {
|
|
13
|
+
const { box } = env.outline;
|
|
14
|
+
const out = sites.map(() => 0);
|
|
15
|
+
if (sites.length === 0)
|
|
16
|
+
return out;
|
|
17
|
+
const side = Math.max(box.w, box.h) / across;
|
|
18
|
+
const nx = Math.max(1, Math.ceil(box.w / side));
|
|
19
|
+
const ny = Math.max(1, Math.ceil(box.h / side));
|
|
20
|
+
const first = new Map();
|
|
21
|
+
const owner = sites.map((s, i) => {
|
|
22
|
+
const key = `${s[0]},${s[1]}`;
|
|
23
|
+
const was = first.get(key);
|
|
24
|
+
if (was === undefined)
|
|
25
|
+
first.set(key, i);
|
|
26
|
+
return was ?? i;
|
|
27
|
+
});
|
|
28
|
+
let total = 0;
|
|
29
|
+
for (let j = 0; j < ny; j += 1) {
|
|
30
|
+
const y = box.y + (j + 0.5) * (box.h / ny);
|
|
31
|
+
for (let i = 0; i < nx; i += 1) {
|
|
32
|
+
const x = box.x + (i + 0.5) * (box.w / nx);
|
|
33
|
+
if (!env.outline.contains(x, y))
|
|
34
|
+
continue;
|
|
35
|
+
let best = -1;
|
|
36
|
+
let nearest = Infinity;
|
|
37
|
+
for (let k = 0; k < sites.length; k += 1) {
|
|
38
|
+
if (owner[k] !== k)
|
|
39
|
+
continue;
|
|
40
|
+
const d = (sites[k][0] - x) ** 2 + (sites[k][1] - y) ** 2;
|
|
41
|
+
if (d < nearest) {
|
|
42
|
+
nearest = d;
|
|
43
|
+
best = k;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
out[best] += 1;
|
|
47
|
+
total += 1;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return total === 0 ? out : out.map((n) => n / total);
|
|
51
|
+
}
|
|
52
|
+
function computeVoronoi(layer, env) {
|
|
53
|
+
const unit = unitOfLayer(layer, env);
|
|
54
|
+
const { items, skipped } = placeAll(layer, env, toArray(layer.data, "a voronoi layer"), unit);
|
|
55
|
+
const sites = items.map((p) => [p.x, p.y]);
|
|
56
|
+
const cells = voronoiCells(sites, env.outline.box);
|
|
57
|
+
const shares = surfaceShares(sites, env);
|
|
58
|
+
const teams = [...groupByTeam(items, "team").entries()].map(([team, list]) => ({ team, share: list.reduce((sum, p) => sum + shares[items.indexOf(p)], 0), players: list.length }));
|
|
59
|
+
return { players: items, cells, shares, skipped, teams, shared: cells.filter((c) => c.length === 0).length };
|
|
60
|
+
}
|
|
61
|
+
export function drawVoronoi(layer, env) {
|
|
62
|
+
const made = newMade();
|
|
63
|
+
const words = env.words.team;
|
|
64
|
+
const c = computeVoronoi(layer, env);
|
|
65
|
+
made.drawn = c.players.length;
|
|
66
|
+
made.skipped = c.skipped;
|
|
67
|
+
made.cls = "ky-tm";
|
|
68
|
+
made.opacity = opacityOf(layer.opacity);
|
|
69
|
+
made.style = `--w:${num(env.base * 0.12)}`;
|
|
70
|
+
const out = [];
|
|
71
|
+
c.players.forEach((p, i) => {
|
|
72
|
+
if (c.cells[i].length === 0)
|
|
73
|
+
return;
|
|
74
|
+
const { cls, css } = paintOf(p.d);
|
|
75
|
+
out.push(titled("path", `${attrs(`ky-sp${cls === "" ? "" : ` ${cls}`}`, css)} d="${polygonPath(c.cells[i])}"`, `${[who(p), teamKey(p.d.team) === "" ? "" : teamLabel(teamKey(p.d.team), env)].filter((t) => t !== "").join(", ")}: ${round(100 * c.shares[i])}%`));
|
|
76
|
+
});
|
|
77
|
+
if (layer.points !== false)
|
|
78
|
+
out.push(dots(c.players, env.base * 0.55, () => undefined));
|
|
79
|
+
made.marks = out.join("");
|
|
80
|
+
made.title = fill(words.voronoiTitle, { n: c.players.length });
|
|
81
|
+
const list = [...c.teams].sort((a, b) => b.share - a.share).map((t) => fill(words.shareItem, { name: teamLabel(t.team, env), pct: round(100 * t.share) })).join(env.words.layers.listSep);
|
|
82
|
+
const parts = [fill(words.voronoiNote, { list })];
|
|
83
|
+
if (c.shared > 0)
|
|
84
|
+
parts.push(fill(words.voronoiShared, { n: c.shared }));
|
|
85
|
+
if (c.skipped > 0)
|
|
86
|
+
parts.push(fill(env.words.layers.skipped, { n: c.skipped }));
|
|
87
|
+
made.text = c.players.length === 0 ? "" : parts.join(env.words.said.gap);
|
|
88
|
+
return made;
|
|
89
|
+
}
|
|
90
|
+
export function voronoiTable(layer, env) {
|
|
91
|
+
const w = env.words.team;
|
|
92
|
+
const l = env.words.layers;
|
|
93
|
+
const c = computeVoronoi(layer, env);
|
|
94
|
+
const title = typeof layer.title === "string" && layer.title !== "" ? layer.title : fill(w.voronoiTitle, { n: c.players.length });
|
|
95
|
+
return [
|
|
96
|
+
{
|
|
97
|
+
head: [l.tableNo, w.tablePlayer, l.tableTeam, l.tableX, l.tableY, w.tableShare],
|
|
98
|
+
rows: c.players.map((p, i) => [i + 1, who(p), teamKey(p.d.team), round(p.d.x, 2), round(p.d.y, 2), round(100 * c.shares[i])]),
|
|
99
|
+
title,
|
|
100
|
+
note: frameNote(layer, env),
|
|
101
|
+
},
|
|
102
|
+
];
|
|
103
|
+
}
|
|
104
|
+
/** The groups a layer draws a shape for: the players of each team, or all of them. */
|
|
105
|
+
function groupsOf(layer, env) {
|
|
106
|
+
const unit = unitOfLayer(layer, env);
|
|
107
|
+
const { items, skipped } = placeAll(layer, env, toArray(layer.data, `a ${layer.kind} layer`), unit);
|
|
108
|
+
return { groups: groupByTeam(items, layer.by), skipped, all: items };
|
|
109
|
+
}
|
|
110
|
+
function computeDelaunay(layer, env) {
|
|
111
|
+
const { groups, skipped, all } = groupsOf(layer, env);
|
|
112
|
+
const out = [];
|
|
113
|
+
for (const [team, players] of groups) {
|
|
114
|
+
const t = delaunay(players.map((p) => [p.x, p.y]));
|
|
115
|
+
const edges = triangulationEdges(t).map(([a, b]) => [t.points[a], t.points[b]]);
|
|
116
|
+
const triangles = [];
|
|
117
|
+
for (let i = 0; i < t.triangles.length; i += 3)
|
|
118
|
+
triangles.push([t.points[t.triangles[i]], t.points[t.triangles[i + 1]], t.points[t.triangles[i + 2]]]);
|
|
119
|
+
out.push({ team, players, edges, triangles });
|
|
120
|
+
}
|
|
121
|
+
return { groups: out, all, skipped };
|
|
122
|
+
}
|
|
123
|
+
export function drawDelaunay(layer, env) {
|
|
124
|
+
const made = newMade();
|
|
125
|
+
const words = env.words.team;
|
|
126
|
+
const c = computeDelaunay(layer, env);
|
|
127
|
+
made.drawn = c.all.length;
|
|
128
|
+
made.skipped = c.skipped;
|
|
129
|
+
made.cls = "ky-tm";
|
|
130
|
+
made.opacity = opacityOf(layer.opacity);
|
|
131
|
+
made.style = `--w:${num(env.base * 0.16)}`;
|
|
132
|
+
const out = [];
|
|
133
|
+
for (const g of c.groups) {
|
|
134
|
+
const cls = teamClass(g.team);
|
|
135
|
+
const name = teamLabel(g.team, env);
|
|
136
|
+
const tip = `${name}: ${fill(words.delaunayNote, { triangles: g.triangles.length, edges: g.edges.length })}`;
|
|
137
|
+
if (layer.triangles === true && g.triangles.length > 0)
|
|
138
|
+
out.push(titled("path", `${attrs(`ky-sp${cls === "" ? "" : ` ${cls}`}`, "")} d="${g.triangles.map((t) => polygonPath(t)).join("")}"`, tip));
|
|
139
|
+
else if (g.edges.length > 0)
|
|
140
|
+
out.push(titled("path", `${attrs(`ky-sp ky-sp-e${cls === "" ? "" : ` ${cls}`}`, "")} d="${g.edges.map(([a, b]) => `M${num(a[0])} ${num(a[1])}L${num(b[0])} ${num(b[1])}`).join("")}"`, tip));
|
|
141
|
+
}
|
|
142
|
+
if (layer.points !== false)
|
|
143
|
+
out.push(dots(c.all, env.base * 0.55, () => undefined));
|
|
144
|
+
made.marks = out.join("");
|
|
145
|
+
made.title = fill(words.delaunayTitle, { n: c.all.length });
|
|
146
|
+
const triangles = c.groups.reduce((s, g) => s + g.triangles.length, 0);
|
|
147
|
+
const edges = c.groups.reduce((s, g) => s + g.edges.length, 0);
|
|
148
|
+
const parts = [fill(words.delaunayNote, { triangles, edges })];
|
|
149
|
+
if (c.skipped > 0)
|
|
150
|
+
parts.push(fill(env.words.layers.skipped, { n: c.skipped }));
|
|
151
|
+
made.text = c.all.length === 0 ? "" : parts.join(env.words.said.gap);
|
|
152
|
+
return made;
|
|
153
|
+
}
|
|
154
|
+
export function delaunayTable(layer, env) {
|
|
155
|
+
const w = env.words.team;
|
|
156
|
+
const l = env.words.layers;
|
|
157
|
+
const c = computeDelaunay(layer, env);
|
|
158
|
+
return [
|
|
159
|
+
{
|
|
160
|
+
head: [l.tableNo, l.tableTeam, w.tablePlayers, w.tableTriangles, w.tableEdges],
|
|
161
|
+
rows: c.groups.map((g, i) => [i + 1, teamLabel(g.team, env), g.players.length, g.triangles.length, g.edges.length]),
|
|
162
|
+
title: typeof layer.title === "string" && layer.title !== "" ? layer.title : fill(w.delaunayTitle, { n: c.all.length }),
|
|
163
|
+
note: "",
|
|
164
|
+
},
|
|
165
|
+
];
|
|
166
|
+
}
|
|
167
|
+
function computeHull(layer, env) {
|
|
168
|
+
const { groups, skipped, all } = groupsOf(layer, env);
|
|
169
|
+
const unit = unitOfLayer(layer, env);
|
|
170
|
+
const k = FEET_PER[unit];
|
|
171
|
+
const hulls = [...groups.entries()].map(([team, players]) => {
|
|
172
|
+
const corners = convexHull(players.map((p) => [p.x, p.y]));
|
|
173
|
+
return { team, players, corners, area: polygonArea(corners) / (k * k), centroid: polygonCentroid(corners) };
|
|
174
|
+
});
|
|
175
|
+
return { hulls, all, skipped, unit };
|
|
176
|
+
}
|
|
177
|
+
export function drawHull(layer, env) {
|
|
178
|
+
const made = newMade();
|
|
179
|
+
const words = env.words.team;
|
|
180
|
+
const c = computeHull(layer, env);
|
|
181
|
+
made.drawn = c.all.length;
|
|
182
|
+
made.skipped = c.skipped;
|
|
183
|
+
made.cls = "ky-tm";
|
|
184
|
+
made.opacity = opacityOf(layer.opacity);
|
|
185
|
+
made.style = `--w:${num(env.base * 0.16)}`;
|
|
186
|
+
const out = [];
|
|
187
|
+
for (const h of c.hulls) {
|
|
188
|
+
const cls = teamClass(h.team);
|
|
189
|
+
const tip = fill(words.hullItem, { name: teamLabel(h.team, env), n: h.players.length, area: Math.round(h.area), unit: squareWord(c.unit, env) });
|
|
190
|
+
if (h.corners.length >= 3)
|
|
191
|
+
out.push(titled("path", `${attrs(`ky-sp${cls === "" ? "" : ` ${cls}`}`, "")} d="${polygonPath(h.corners)}"`, tip));
|
|
192
|
+
else if (h.corners.length === 2)
|
|
193
|
+
out.push(titled("path", `${attrs(`ky-sp ky-sp-e${cls === "" ? "" : ` ${cls}`}`, "")} d="M${num(h.corners[0][0])} ${num(h.corners[0][1])}L${num(h.corners[1][0])} ${num(h.corners[1][1])}"`, tip));
|
|
194
|
+
}
|
|
195
|
+
if (layer.points !== false)
|
|
196
|
+
out.push(dots(c.all, env.base * 0.55, () => undefined));
|
|
197
|
+
made.marks = out.join("");
|
|
198
|
+
made.title = fill(words.hullTitle, { n: c.all.length });
|
|
199
|
+
const parts = [c.hulls.map((h) => fill(words.hullItem, { name: teamLabel(h.team, env), n: h.players.length, area: Math.round(h.area), unit: squareWord(c.unit, env) })).join(env.words.layers.listSep) + env.words.said.stop];
|
|
200
|
+
if (c.skipped > 0)
|
|
201
|
+
parts.push(fill(env.words.layers.skipped, { n: c.skipped }));
|
|
202
|
+
made.text = c.all.length === 0 ? "" : parts.join(env.words.said.gap);
|
|
203
|
+
return made;
|
|
204
|
+
}
|
|
205
|
+
export function hullTable(layer, env) {
|
|
206
|
+
const w = env.words.team;
|
|
207
|
+
const l = env.words.layers;
|
|
208
|
+
const c = computeHull(layer, env);
|
|
209
|
+
return [
|
|
210
|
+
{
|
|
211
|
+
head: [l.tableNo, l.tableTeam, w.tablePlayers, w.tableCorners, w.tableArea],
|
|
212
|
+
rows: c.hulls.map((h, i) => [i + 1, teamLabel(h.team, env), h.players.length, h.corners.length, Math.round(h.area)]),
|
|
213
|
+
title: typeof layer.title === "string" && layer.title !== "" ? layer.title : fill(w.hullTitle, { n: c.all.length }),
|
|
214
|
+
note: fill(w.tableUnit, { unit: unitWord(c.unit, env) }),
|
|
215
|
+
},
|
|
216
|
+
];
|
|
217
|
+
}
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import type { ZoneSetId } from "./heat.zones.ts";
|
|
2
|
+
import type { LayerBase, LayerTeam, MarkDatum } from "./layers.types.ts";
|
|
3
|
+
/** A player at a place. `team` (`home`, `away` or another) is what colours the player's cell, triangles and hull. */
|
|
4
|
+
export interface PlayerDatum extends MarkDatum {
|
|
5
|
+
x: number | null;
|
|
6
|
+
y: number | null;
|
|
7
|
+
/** The player's id, name and shirt number, for a label and for the table. */
|
|
8
|
+
id?: string | number;
|
|
9
|
+
name?: string;
|
|
10
|
+
number?: string | number;
|
|
11
|
+
}
|
|
12
|
+
/** Players, with how their shape is drawn. */
|
|
13
|
+
interface PlayersBase extends LayerBase {
|
|
14
|
+
data: readonly PlayerDatum[];
|
|
15
|
+
/** The opacity of the whole layer, 0 to 1. */
|
|
16
|
+
opacity?: number;
|
|
17
|
+
/** Mark each player with a dot. On by default. */
|
|
18
|
+
points?: boolean;
|
|
19
|
+
}
|
|
20
|
+
/** The part of the surface nearest each player, coloured by team: who controls the space (the Voronoi diagram of the players, clipped to the surface). */
|
|
21
|
+
export interface VoronoiLayer extends PlayersBase {
|
|
22
|
+
kind: "voronoi";
|
|
23
|
+
}
|
|
24
|
+
/** The Delaunay triangulation of the players: triangles whose circle holds no other player, a mesh of near neighbours. */
|
|
25
|
+
export interface DelaunayLayer extends PlayersBase {
|
|
26
|
+
kind: "delaunay";
|
|
27
|
+
/** One triangulation for each team (`team`, the default) or one for all the players. */
|
|
28
|
+
by?: "team" | "all";
|
|
29
|
+
/** Fill the triangles, not only draw their edges. */
|
|
30
|
+
triangles?: boolean;
|
|
31
|
+
}
|
|
32
|
+
/** The convex hull of the players: the smallest convex shape that holds them, for each team. */
|
|
33
|
+
export interface HullLayer extends PlayersBase {
|
|
34
|
+
kind: "hull";
|
|
35
|
+
/** One hull for each team (`team`, the default) or one for all the players. */
|
|
36
|
+
by?: "team" | "all";
|
|
37
|
+
}
|
|
38
|
+
/** A pass: who passed to whom, from where to where. The places are the passer's and, if given, the receiver's. */
|
|
39
|
+
export interface PassDatum extends MarkDatum {
|
|
40
|
+
/** The id of the passer and of the receiver (a number or a name; the same id is the same player). */
|
|
41
|
+
from: string | number;
|
|
42
|
+
to: string | number;
|
|
43
|
+
/** Where the pass was made from, and where it was received. */
|
|
44
|
+
x?: number | null;
|
|
45
|
+
y?: number | null;
|
|
46
|
+
x2?: number | null;
|
|
47
|
+
y2?: number | null;
|
|
48
|
+
/** The names for the labels and the table, by id. The last one given for an id is used. */
|
|
49
|
+
fromName?: string;
|
|
50
|
+
toName?: string;
|
|
51
|
+
fromNumber?: string | number;
|
|
52
|
+
toNumber?: string | number;
|
|
53
|
+
}
|
|
54
|
+
/** A player of a pass network whose place is given and not worked out from the passes. */
|
|
55
|
+
export interface NetworkNode {
|
|
56
|
+
id: string | number;
|
|
57
|
+
x: number;
|
|
58
|
+
y: number;
|
|
59
|
+
team?: LayerTeam;
|
|
60
|
+
name?: string;
|
|
61
|
+
number?: string | number;
|
|
62
|
+
}
|
|
63
|
+
/** A pass network: a node for each player at the average place of the passes he made and received, sized by touches, and a link for each pair sized by passes. */
|
|
64
|
+
export interface NetworkLayer extends LayerBase {
|
|
65
|
+
kind: "network";
|
|
66
|
+
data: readonly PassDatum[];
|
|
67
|
+
/** Places of your own for some players, over the averages. */
|
|
68
|
+
nodes?: readonly NetworkNode[];
|
|
69
|
+
/** Leave out links of fewer passes than this. 1 by default. */
|
|
70
|
+
minPasses?: number;
|
|
71
|
+
/** Draw a link for each direction, bowed apart and with a head, and not one for the pair. */
|
|
72
|
+
directed?: boolean;
|
|
73
|
+
/** The width of a node (a share of the surface's longer side by default), from the fewest touches to the most, in the layer's units. */
|
|
74
|
+
nodeSize?: readonly [number, number];
|
|
75
|
+
/** The width of a link, from the fewest passes to the most, in the layer's units. */
|
|
76
|
+
linkWidth?: readonly [number, number];
|
|
77
|
+
/** The opacity of a link, from the fewest passes to the most. [0.3, 0.9] by default. */
|
|
78
|
+
linkOpacity?: readonly [number, number];
|
|
79
|
+
/** What is written in a node: the shirt `number` (the default when there are any), the `name`, the `id` or `none`. */
|
|
80
|
+
labels?: "number" | "name" | "id" | "none";
|
|
81
|
+
/** The opacity of the whole layer, 0 to 1. */
|
|
82
|
+
opacity?: number;
|
|
83
|
+
}
|
|
84
|
+
/** A movement: from `(x, y)` to `(x2, y2)`, or the vector `(dx, dy)` from `(x, y)`. */
|
|
85
|
+
export interface MoveDatum extends MarkDatum {
|
|
86
|
+
x: number | null;
|
|
87
|
+
y: number | null;
|
|
88
|
+
x2?: number | null;
|
|
89
|
+
y2?: number | null;
|
|
90
|
+
dx?: number | null;
|
|
91
|
+
dy?: number | null;
|
|
92
|
+
/** The mover, for a sonar of each player. */
|
|
93
|
+
id?: string | number;
|
|
94
|
+
name?: string;
|
|
95
|
+
number?: string | number;
|
|
96
|
+
}
|
|
97
|
+
/** The average direction of the movements in each cell of a grid, as an arrow (a quiver plot). */
|
|
98
|
+
export interface FlowLayer extends LayerBase {
|
|
99
|
+
kind: "flow";
|
|
100
|
+
data: readonly MoveDatum[];
|
|
101
|
+
/** The grid: a number of cells across the surface's longer side (square cells, 12 by default), `[across, down]`, or `{ x, y }` as for a heat map. */
|
|
102
|
+
bins?: number | readonly [number, number];
|
|
103
|
+
/** Leave out a cell with fewer movements than this. 2 by default. */
|
|
104
|
+
minCount?: number;
|
|
105
|
+
/** The length of an arrow: `mean` (the default: in proportion to the cell's mean movement, the longest filling most of a cell), or `fixed` (all alike; the opacity then shows how steady the direction is). */
|
|
106
|
+
lengths?: "mean" | "fixed";
|
|
107
|
+
/** The team that colours the arrows (`home`, `away`). The movements' own team when they all share one, else the layer's colour. */
|
|
108
|
+
team?: LayerTeam;
|
|
109
|
+
/** The opacity of the whole layer, 0 to 1. */
|
|
110
|
+
opacity?: number;
|
|
111
|
+
}
|
|
112
|
+
/** A rose for each group of movements: how many go in each direction, or how long they are on average. */
|
|
113
|
+
export interface SonarLayer extends LayerBase {
|
|
114
|
+
kind: "sonar";
|
|
115
|
+
data: readonly MoveDatum[];
|
|
116
|
+
/** What a rose is of: `all` (one rose, at the middle of the movements' starts; the default), `player` (one for each `id`, at the average start of his), `grid` (one for each cell of a grid) or `zone` (one for each zone of `zones`). */
|
|
117
|
+
by?: "all" | "player" | "grid" | "zone";
|
|
118
|
+
/** How many directions a rose is cut into. 12 by default. */
|
|
119
|
+
angles?: number;
|
|
120
|
+
/** A bar's length: the number of movements (`count`, the default) or their mean length (`mean`). */
|
|
121
|
+
statistic?: "count" | "mean";
|
|
122
|
+
/** For `by: "grid"`: the cells, as for a flow layer (6 across by default). */
|
|
123
|
+
bins?: number | readonly [number, number];
|
|
124
|
+
/** For `by: "zone"`: the set of zones, and what a zone set takes as in a zone chart. */
|
|
125
|
+
zones?: ZoneSetId;
|
|
126
|
+
direction?: "left" | "right";
|
|
127
|
+
end?: "left" | "right" | "both";
|
|
128
|
+
los?: number;
|
|
129
|
+
bats?: "L" | "R" | "none";
|
|
130
|
+
/** The radius of the longest bar, in the layer's units. A share of the surface by default. */
|
|
131
|
+
radius?: number;
|
|
132
|
+
/** Leave out a group with fewer movements than this. 1 by default. */
|
|
133
|
+
minCount?: number;
|
|
134
|
+
team?: LayerTeam;
|
|
135
|
+
opacity?: number;
|
|
136
|
+
}
|
|
137
|
+
/** A named place of a formation, in the numbers of the formation's frame (see `Formation`). */
|
|
138
|
+
export interface FormationPosition {
|
|
139
|
+
/** The position's id: `GK`, `LB`, `QB`. Unique within a formation. */
|
|
140
|
+
id: string;
|
|
141
|
+
x: number;
|
|
142
|
+
y: number;
|
|
143
|
+
}
|
|
144
|
+
/** The sports that have formations. */
|
|
145
|
+
export type FormationSport = "soccer" | "nhl" | "nba" | "nfl" | "mlb";
|
|
146
|
+
/** A formation: where each position stands. Points in the numbers of a registered frame, for a team that attacks the right end of the surface as it is drawn wide (a football formation: the offence going right, with the ball at midfield). */
|
|
147
|
+
export interface Formation {
|
|
148
|
+
id: string;
|
|
149
|
+
sport: FormationSport;
|
|
150
|
+
/** The frame the positions are in: `opta` for soccer, `nhl-api`, `nba-stats`, `nfl-tracking`, `retrosheet`. */
|
|
151
|
+
frame: string;
|
|
152
|
+
name: {
|
|
153
|
+
en: string;
|
|
154
|
+
ja: string;
|
|
155
|
+
};
|
|
156
|
+
/** What the formation is, in a sentence: the side of the ball, the strategy. */
|
|
157
|
+
about: {
|
|
158
|
+
en: string;
|
|
159
|
+
ja: string;
|
|
160
|
+
};
|
|
161
|
+
positions: readonly FormationPosition[];
|
|
162
|
+
}
|
|
163
|
+
/** A player to stand on a position. */
|
|
164
|
+
export interface FormationPlayer extends MarkDatum {
|
|
165
|
+
/** The id of the position the player stands on. */
|
|
166
|
+
position: string;
|
|
167
|
+
number?: string | number;
|
|
168
|
+
name?: string;
|
|
169
|
+
}
|
|
170
|
+
/** A formation drawn on the surface: a labelled mark for each position, with the players on them when `data` has some. */
|
|
171
|
+
export interface FormationLayer extends LayerBase {
|
|
172
|
+
kind: "formation";
|
|
173
|
+
/** The id of a formation in `FORMATIONS` (`4-4-2`), or a formation of your own. */
|
|
174
|
+
formation: string | Formation;
|
|
175
|
+
/** The team that colours the marks (`home`, `away`). */
|
|
176
|
+
team?: LayerTeam;
|
|
177
|
+
/** Players for some or all of the positions. */
|
|
178
|
+
data?: readonly FormationPlayer[];
|
|
179
|
+
/** What is written in a mark: the `position`'s id (the default without players), the `number` (the default with them), the `name` or `none`. */
|
|
180
|
+
labels?: "position" | "number" | "name" | "none";
|
|
181
|
+
/** Write a player's name under his mark. */
|
|
182
|
+
names?: boolean;
|
|
183
|
+
/** The width of a mark, in the layer's units. A share of the surface's longer side by default. */
|
|
184
|
+
size?: number;
|
|
185
|
+
/** The line of scrimmage of a football formation, as the ball lies in the frame `nfl-tracking` (yards from the back of the left end zone). Midfield by default. */
|
|
186
|
+
los?: number;
|
|
187
|
+
opacity?: number;
|
|
188
|
+
}
|
|
189
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/text.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/** An estimate of the width of a string, in ems. Characters of Japanese and other full-width scripts are one em. */
|
|
2
|
+
export declare function textWidth(text: string, bold?: boolean): number;
|
|
3
|
+
/** The text in capitals when it has Latin letters; any other text is left as it is. */
|
|
4
|
+
export declare function capitals(text: string): string;
|
|
5
|
+
/**
|
|
6
|
+
* A short form of a name: its initials when it has several words, else its first letters. Japanese names give their first two or three characters.
|
|
7
|
+
*
|
|
8
|
+
* This is a guess at a label, not a code. For a national team it is often not the code the sport uses: "Spain" gives SPA, where FIFA's
|
|
9
|
+
* three-letter code is ESP ("Germany" gives GER, which happens to match; "Netherlands" gives NET, FIFA's is NED). Pass each team's `abbr` yourself
|
|
10
|
+
* (the FIFA, World Rugby or ICC code your data carries); the package bundles no table of country codes.
|
|
11
|
+
*/
|
|
12
|
+
export declare function abbreviate(name: string): string;
|
|
13
|
+
/** The lines a name is set in, the font size that fits them, and the width of the widest. */
|
|
14
|
+
export interface FittedLines {
|
|
15
|
+
lines: string[];
|
|
16
|
+
size: number;
|
|
17
|
+
width: number;
|
|
18
|
+
height: number;
|
|
19
|
+
}
|
|
20
|
+
/** The line height as a multiple of the font size. */
|
|
21
|
+
export declare const LINE_HEIGHT = 1.1;
|
|
22
|
+
/**
|
|
23
|
+
* Set a name in the biggest size that fits a box, on one line or, at a space, on two.
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* import { fitLines } from "@johnmorrisdotca/kyogijo-stadiums";
|
|
27
|
+
* console.log(fitLines("GOLDEN STATE WARRIORS", 40, 12, 8).lines);
|
|
28
|
+
* // [ 'GOLDEN STATE', 'WARRIORS' ]
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export declare function fitLines(name: string, maxWidth: number, maxHeight: number, maxSize: number): FittedLines;
|
package/dist/text.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
// Measuring and fitting text without a browser. Widths are estimates for a bold sans-serif face, a little generous so
|
|
2
|
+
// that a name stays inside its place whichever font the page falls back to.
|
|
3
|
+
const NARROW = new Set("iIjl!|.,:;'`");
|
|
4
|
+
const WIDE = new Set("MWmw@%");
|
|
5
|
+
/** An estimate of the width of a string, in ems. Characters of Japanese and other full-width scripts are one em. */
|
|
6
|
+
export function textWidth(text, bold = true) {
|
|
7
|
+
let width = 0;
|
|
8
|
+
for (const ch of text) {
|
|
9
|
+
const code = ch.codePointAt(0) ?? 0;
|
|
10
|
+
if (code >= 0x2e80)
|
|
11
|
+
width += 1.02;
|
|
12
|
+
else if (ch === " ")
|
|
13
|
+
width += 0.3;
|
|
14
|
+
else if (NARROW.has(ch))
|
|
15
|
+
width += 0.34;
|
|
16
|
+
else if (WIDE.has(ch))
|
|
17
|
+
width += 0.92;
|
|
18
|
+
else if (ch >= "0" && ch <= "9")
|
|
19
|
+
width += 0.62;
|
|
20
|
+
else if (ch >= "A" && ch <= "Z")
|
|
21
|
+
width += 0.7;
|
|
22
|
+
else if (ch === "-" || ch === "–" || ch === "—")
|
|
23
|
+
width += 0.45;
|
|
24
|
+
else
|
|
25
|
+
width += 0.58;
|
|
26
|
+
}
|
|
27
|
+
return bold ? width * 1.04 : width;
|
|
28
|
+
}
|
|
29
|
+
/** The text in capitals when it has Latin letters; any other text is left as it is. */
|
|
30
|
+
export function capitals(text) {
|
|
31
|
+
return /[a-z]/i.test(text) ? text.toLocaleUpperCase("en") : text;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* A short form of a name: its initials when it has several words, else its first letters. Japanese names give their first two or three characters.
|
|
35
|
+
*
|
|
36
|
+
* This is a guess at a label, not a code. For a national team it is often not the code the sport uses: "Spain" gives SPA, where FIFA's
|
|
37
|
+
* three-letter code is ESP ("Germany" gives GER, which happens to match; "Netherlands" gives NET, FIFA's is NED). Pass each team's `abbr` yourself
|
|
38
|
+
* (the FIFA, World Rugby or ICC code your data carries); the package bundles no table of country codes.
|
|
39
|
+
*/
|
|
40
|
+
export function abbreviate(name) {
|
|
41
|
+
const clean = name.trim();
|
|
42
|
+
if (clean.length === 0)
|
|
43
|
+
return "";
|
|
44
|
+
// Japanese: the first two characters of a name with kanji (読売), the first three of one in kana alone (メトロ).
|
|
45
|
+
if (/[\u2e80-\u9fff\uff00-\uffef]/.test(clean))
|
|
46
|
+
return [...clean].slice(0, /[\u3400-\u9fff]/.test(clean) ? 2 : 3).join("");
|
|
47
|
+
const words = clean.split(/\s+/).filter((word) => /[a-z0-9]/i.test(word));
|
|
48
|
+
if (words.length >= 2)
|
|
49
|
+
return words.slice(0, 3).map((word) => word[0]).join("").toLocaleUpperCase("en");
|
|
50
|
+
return clean.slice(0, 3).toLocaleUpperCase("en");
|
|
51
|
+
}
|
|
52
|
+
/** The ways a name splits into two lines at a space, each as [first, second]. */
|
|
53
|
+
function splits(name) {
|
|
54
|
+
const words = name.split(" ");
|
|
55
|
+
const out = [];
|
|
56
|
+
for (let i = 1; i < words.length; i += 1)
|
|
57
|
+
out.push([words.slice(0, i).join(" "), words.slice(i).join(" ")]);
|
|
58
|
+
return out;
|
|
59
|
+
}
|
|
60
|
+
/** The line height as a multiple of the font size. */
|
|
61
|
+
export const LINE_HEIGHT = 1.1;
|
|
62
|
+
/**
|
|
63
|
+
* Set a name in the biggest size that fits a box, on one line or, at a space, on two.
|
|
64
|
+
* @example
|
|
65
|
+
* ```ts
|
|
66
|
+
* import { fitLines } from "@johnmorrisdotca/kyogijo-stadiums";
|
|
67
|
+
* console.log(fitLines("GOLDEN STATE WARRIORS", 40, 12, 8).lines);
|
|
68
|
+
* // [ 'GOLDEN STATE', 'WARRIORS' ]
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
export function fitLines(name, maxWidth, maxHeight, maxSize) {
|
|
72
|
+
const one = () => {
|
|
73
|
+
const w = textWidth(name);
|
|
74
|
+
const size = Math.max(0.1, Math.min(maxSize, maxHeight / LINE_HEIGHT, w === 0 ? maxSize : maxWidth / w));
|
|
75
|
+
return { lines: [name], size, width: w * size, height: size * LINE_HEIGHT };
|
|
76
|
+
};
|
|
77
|
+
const single = one();
|
|
78
|
+
let two;
|
|
79
|
+
for (const [a, b] of splits(name)) {
|
|
80
|
+
const w = Math.max(textWidth(a), textWidth(b));
|
|
81
|
+
const size = Math.max(0.1, Math.min(maxSize, maxHeight / (2 * LINE_HEIGHT), w === 0 ? maxSize : maxWidth / w));
|
|
82
|
+
if (two === undefined || size > two.size)
|
|
83
|
+
two = { lines: [a, b], size, width: w * size, height: 2 * size * LINE_HEIGHT };
|
|
84
|
+
}
|
|
85
|
+
// Two lines are worth it only when they are clearly bigger than one.
|
|
86
|
+
const best = two !== undefined && two.size > single.size * 1.18 ? two : single;
|
|
87
|
+
return best;
|
|
88
|
+
}
|
package/dist/themes.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { CourtStyle } from "./types.ts";
|
|
2
|
+
/**
|
|
3
|
+
* Five looks for a basketball court, made of plain colours and no one's marks, to start from: the paint, the border,
|
|
4
|
+
* the wood and the words on the baselines are the parts a court's design is made of. Pass one as `court`, or spread it
|
|
5
|
+
* and change a part.
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* import { COURT_THEMES, drawKyogijo } from "@johnmorrisdotca/kyogijo-stadiums";
|
|
9
|
+
* const svg = drawKyogijo({ sport: "nba", court: { ...COURT_THEMES.twoTone, baselineText: "HARBOUR CITY" } });
|
|
10
|
+
* console.log(svg.includes("--ky-key:#1e4f8f"));
|
|
11
|
+
* // true
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
export declare const COURT_THEMES: Readonly<Record<"maple" | "twoTone" | "night" | "sunburst" | "tournament", CourtStyle>>;
|
package/dist/themes.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Five looks for a basketball court, made of plain colours and no one's marks, to start from: the paint, the border,
|
|
3
|
+
* the wood and the words on the baselines are the parts a court's design is made of. Pass one as `court`, or spread it
|
|
4
|
+
* and change a part.
|
|
5
|
+
* @example
|
|
6
|
+
* ```ts
|
|
7
|
+
* import { COURT_THEMES, drawKyogijo } from "@johnmorrisdotca/kyogijo-stadiums";
|
|
8
|
+
* const svg = drawKyogijo({ sport: "nba", court: { ...COURT_THEMES.twoTone, baselineText: "HARBOUR CITY" } });
|
|
9
|
+
* console.log(svg.includes("--ky-key:#1e4f8f"));
|
|
10
|
+
* // true
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
export const COURT_THEMES = {
|
|
14
|
+
/** Natural maple, a brick-red key and a darker border: the plain court. */
|
|
15
|
+
maple: { wood: "#d9a766", woodAlt: "#cf9c5b", apron: "#c9c3b4", border: "#a97d46", key: "#c25b3c", line: "#ffffff" },
|
|
16
|
+
/** A pale floor with a deep blue key, centre circle and border: one colour carries the whole court. */
|
|
17
|
+
twoTone: { wood: "#e7c78f", woodAlt: "#dfbd82", apron: "#cfd6df", border: "#1e4f8f", key: "#1e4f8f", centreCircle: "#1e4f8f", line: "#ffffff", baselineInk: "#ffffff" },
|
|
18
|
+
/** A dark stain with a black key and gold words: the court of a night game. */
|
|
19
|
+
night: { wood: "#5f4932", woodAlt: "#57422d", apron: "#0b0f16", border: "#0b1220", key: "#0f1b2e", centreCircle: "#0f1b2e", line: "#f3f0e6", baselineInk: "#e8c36a" },
|
|
20
|
+
/** Maple with the whole three-point area painted gold and a red border: a court that is painted wall to wall. */
|
|
21
|
+
/** A floor painted from edge to edge, not wood: a deep blue ground, a gold key and gold three-point areas, a gold border and centre. */
|
|
22
|
+
tournament: { wood: "#12508a", woodAlt: "#12508a", apron: "#0a2a47", border: "#e0a100", key: "#e0a100", centreCircle: "#e0a100", line: "#ffffff", baselineInk: "#ffffff", paintThreePoint: false },
|
|
23
|
+
sunburst: { wood: "#e0b373", woodAlt: "#d8a966", apron: "#e6dfd0", border: "#b5452c", key: "#f2c230", paintThreePoint: true, line: "#1b1b1b", baselineInk: "#b5452c" },
|
|
24
|
+
};
|