Recette 1l : courbe redessinée sur sa taille rendue, anneau du moment décisif seul près de la fin

La courbe se redessine deux images après chaque changement de taille, sans
garder de taille en mémoire : en paysage, un tracé dessiné sur une largeur
périmée ramenait les libellés à 7 px. Deux marques à moins de 16 px à
l'écran n'en font qu'une, l'anneau or, et la légende le dit. Le test
mesure la courbe rendue avant et après chaque capture.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Pierre MARTIN
2026-11-23 06:37:10 +01:00
co-authored by Claude Opus 5.5
parent 991a52787d
commit a7852dfbe9
2 changed files with 106 additions and 60 deletions
+64 -49
View File
@@ -77,6 +77,9 @@ const SVG_NS = "http://www.w3.org/2000/svg";
const CURVE_HEIGHT = 196;
const PAD = { left: 40, right: 12, top: 26, bottom: 26 };
const BUBBLE = { width: 210, height: 44 };
/** Deux marques plus proches que ceci à l'écran (en x et en y) n'en font
* qu'une : l'anneau du moment décisif (charte, « Bilan du match »). */
const MERGE_DISTANCE = 16;
function element<K extends keyof HTMLElementTagNameMap>(
parent: HTMLElement,
@@ -127,9 +130,12 @@ export function createMatchSummaryView(options: MatchSummaryViewOptions): MatchS
const { section } = options;
let drawn = "";
let redrawCurve: (() => void) | null = null;
// Après un changement de taille de la fenêtre, la mise en page (une
// ou deux colonnes) n'est finie qu'aux images suivantes : la courbe se
// redessine après deux images, sur sa taille rendue.
window.addEventListener("resize", () => {
if (!section.hidden) {
redrawCurve?.();
afterLayout(() => redrawCurve?.());
}
});
@@ -278,23 +284,33 @@ export function createMatchSummaryView(options: MatchSummaryViewOptions): MatchS
svg.setAttribute("tabindex", "0");
card.append(svg);
const legend = element(card, "ul", { className: "match-legend" });
// Le moment décisif sur la fin du match : l'anneau or seul.
const marks: [string, string][] = decisiveAtEnd(summary)
? [
["decisive", T.legendDecisiveEnd],
["review", T.legendReview],
]
: [
["decisive", T.legendDecisive],
["review", T.legendReview],
[summary.match.winner === "player" ? "end won" : "end", T.legendEnd],
];
for (const [mark, text] of marks) {
const item = element(legend, "li");
const dot = element(item, "span", { className: `match-mark ${mark}` });
dot.setAttribute("aria-hidden", "true");
item.append(text);
}
// La légende suit le tracé : quand l'anneau du moment décisif et le
// point de fin se touchent à l'écran, l'anneau or seul (charte).
let legendKey = "";
const renderLegend = (merged: boolean) => {
const marks: [string, string][] = merged
? [
["decisive", T.legendDecisiveEnd],
["review", T.legendReview],
]
: [
["decisive", T.legendDecisive],
["review", T.legendReview],
[summary.match.winner === "player" ? "end won" : "end", T.legendEnd],
];
const key = String(merged);
if (key === legendKey) {
return;
}
legendKey = key;
legend.replaceChildren();
for (const [mark, text] of marks) {
const item = element(legend, "li");
const dot = element(item, "span", { className: `match-mark ${mark}` });
dot.setAttribute("aria-hidden", "true");
item.append(text);
}
};
const points = summary.curve;
const known = points.filter((p) => p.chance !== null);
if (known.length < points.length) {
@@ -315,23 +331,16 @@ export function createMatchSummaryView(options: MatchSummaryViewOptions): MatchS
);
svg.dataset.points = JSON.stringify(points.map((p) => [p.game, p.turn, p.chance]));
let selected: number | null = null;
const draw = () => drawCurve(svg, summary, selected);
const draw = () => renderLegend(drawCurve(svg, summary, selected));
redrawCurve = draw;
draw();
// Le tracé suit la taille réelle de la courbe (une unité du viewBox
// pour un pixel, les libellés à leur taille) : il se redessine dès
// qu'elle change, quand la page devient visible ou tourne.
// Le tracé suit la taille rendue de la courbe (une unité du viewBox
// pour un pixel, les libellés à leur taille) : il se redessine à
// chaque changement de taille, après la mise en page.
if (typeof ResizeObserver !== "undefined") {
let size = "";
new ResizeObserver(() => {
const box = svg.getBoundingClientRect();
const now = `${Math.round(box.width)}x${Math.round(box.height)}`;
if (box.width > 0 && now !== size) {
size = now;
draw();
}
}).observe(svg);
new ResizeObserver(() => afterLayout(draw)).observe(svg);
}
afterLayout(draw);
// Lecture au doigt : glisser choisit le point sous le doigt (une
// vibration légère à chaque changement de partie) ; lâcher garde la
@@ -624,7 +633,7 @@ export function createMatchSummaryView(options: MatchSummaryViewOptions): MatchS
* pointillés ; trois marques (moment décisif, décision à revoir, fin) ; la
* bulle du point choisi.
*/
function drawCurve(svg: SVGSVGElement, summary: MatchSummary, selected: number | null): void {
function drawCurve(svg: SVGSVGElement, summary: MatchSummary, selected: number | null): boolean {
const box = svg.getBoundingClientRect();
const width = Math.round(box.width || 360);
const height = Math.round(box.height || CURVE_HEIGHT);
@@ -707,24 +716,38 @@ function drawCurve(svg: SVGSVGElement, summary: MatchSummary, selected: number |
}
const indexOf = (game: number, turn: number) =>
points.findIndex((p) => p.game === game && p.turn === turn);
const mark = (i: number, cls: string, r: number) => {
const at = (i: number): { x: number; y: number } | null => {
const c = points[i]?.chance;
if (i >= 0 && c !== null && c !== undefined) {
svgElement(svg, "circle", { cx: x(i), cy: y(c), r, class: cls }).setAttribute(
return i >= 0 && c !== null && c !== undefined ? { x: x(i), y: y(c) } : null;
};
const mark = (i: number, cls: string, r: number) => {
const p = at(i);
if (p !== null) {
svgElement(svg, "circle", { cx: p.x, cy: p.y, r, class: cls }).setAttribute(
"aria-hidden",
"true",
);
}
};
const decisive = summary.decisive;
const decisiveIndex = decisive === null ? -1 : indexOf(decisive.game, decisive.turn);
if (decisive !== null) {
mark(indexOf(decisive.game, decisive.turn), "match-mark-ring", 8);
mark(decisiveIndex, "match-mark-ring", 8);
}
const reviewed = summary.toReview;
if (reviewed !== null) {
mark(indexOf(reviewed.game, reviewed.turn), "match-mark-review", 5);
}
if (summary.match.over && !decisiveAtEnd(summary)) {
// Le moment décisif et la fin du match à moins de MERGE_DISTANCE l'un
// de l'autre à l'écran : l'anneau or seul tient lieu des deux marques.
const ring = at(decisiveIndex);
const end = summary.match.over ? at(points.length - 1) : null;
const merged =
ring !== null &&
end !== null &&
Math.abs(ring.x - end.x) < MERGE_DISTANCE &&
Math.abs(ring.y - end.y) < MERGE_DISTANCE;
if (end !== null && !merged) {
mark(
points.length - 1,
summary.match.winner === "player" ? "match-mark-end won" : "match-mark-end",
@@ -744,20 +767,12 @@ function drawCurve(svg: SVGSVGElement, summary: MatchSummary, selected: number |
});
bubble(svg, summary, point, x(selected), width);
}
return merged;
}
/** Le moment décisif est le dernier tour du match fini : sa marque (l'anneau
* or) tient lieu de marque de fin. */
function decisiveAtEnd(summary: MatchSummary): boolean {
const decisive = summary.decisive;
const last = summary.curve.at(-1);
return (
summary.match.over &&
decisive !== null &&
last !== undefined &&
last.game === decisive.game &&
last.turn === decisive.turn
);
/** Lance fn après deux images : la mise en page en cours est finie. */
function afterLayout(fn: () => void): void {
requestAnimationFrame(() => requestAnimationFrame(fn));
}
/** La bulle du point choisi : partie, coup, score, chances ; un lien SVG