1
0
Fork 0

feat(ribbon): render stripe bands in either direction with accent lines

RibbonDesign gains optional stripeOrientation and lines keys; stored
designs without them keep rendering exactly as before. computeLineRects
clamps overhanging lines to the face and skips malformed entries so
legacy or hand-edited JSON can never crash the shared renderer.
This commit is contained in:
Jason Fraley 2026-12-01 09:29:06 -05:00
parent 51ead07da0
commit 749bef0258
3 changed files with 98 additions and 7 deletions

View file

@ -13,6 +13,9 @@
* ribbons keep the device set authored in the designer.
* - `computeDeviceSlots(...)` lays the devices out as one centered cluster on
* the ribbon face, evenly spaced and shrunk to fit narrow ribbons.
*
* Stripes run either direction (`stripeOrientation`) and optional horizontal
* accent lines (`lines`) draw over the bands but under the overlay and devices.
*/
import type { ReactNode } from "react";
import { RIBBON_CONSTANTS, DEFAULT_RIBBON } from "./constants";
@ -165,6 +168,50 @@ export function DeviceGlyph({
);
}
// ─── Accent lines ───────────────────────────────────────────────────
export interface LineRect {
x: number;
y: number;
width: number;
height: number;
color: string;
}
/**
* Convert authored accent lines (percentages of ribbon height, `positionY`
* at the line's center) into full-width SVG rects, clamped to the canvas.
* Malformed entries are skipped so stored junk can never crash the renderer.
*/
export function computeLineRects(
lines: RibbonDesign["lines"],
width: number,
height: number,
): LineRect[] {
if (!Array.isArray(lines)) return [];
const rects: LineRect[] = [];
for (const line of lines) {
if (!line || typeof line !== "object") continue;
const { positionY, thickness, color } = line;
if (
typeof positionY !== "number" ||
!Number.isFinite(positionY) ||
typeof thickness !== "number" ||
!Number.isFinite(thickness) ||
thickness <= 0 ||
typeof color !== "string"
) {
continue;
}
const clampPct = (v: number) => Math.max(0, Math.min(1, v / 100));
const top = clampPct(positionY - thickness / 2) * height;
const bottom = clampPct(positionY + thickness / 2) * height;
if (bottom <= top) continue;
rects.push({ x: 0, y: top, width, height: bottom - top, color });
}
return rects;
}
// ─── Shared renderer ────────────────────────────────────────────────
export interface RibbonArtProps {
@ -179,9 +226,9 @@ export interface RibbonArtProps {
}
/**
* Render a ribbon (stripes + overlay + vector devices) at the canonical
* `RIBBON_WIDTH x PREVIEW_HEIGHT` viewBox, scaled responsively by CSS. The
* devices shown come from `resolveDisplayedDeviceTypes(design, tier)` and are
* Render a ribbon (stripes + accent lines + overlay + vector devices) at the
* canonical `RIBBON_WIDTH x PREVIEW_HEIGHT` viewBox, scaled responsively by CSS.
* The devices shown come from `resolveDisplayedDeviceTypes(design, tier)` and are
* auto-laid-out by `computeDeviceSlots`, so this component owns all layout.
*/
export function RibbonArt({
@ -195,7 +242,11 @@ export function RibbonArt({
design && typeof design === "object" && Array.isArray(design.stripes) ? design : DEFAULT_RIBBON;
const { stripes, mirrored } = resolved;
if (stripes.length === 0) return null;
const orientation = resolved.stripeOrientation === "horizontal" ? "horizontal" : "vertical";
// Mirroring reflects the band sequence. For vertical bands that is a
// left-right mirror; for horizontal bands it stacks symmetrically
// top-to-bottom, keeping the pattern balanced in either orientation.
const displayStripes = mirrored ? [...stripes, ...stripes.slice(0, -1).reverse()] : stripes;
const totalWeight = displayStripes.reduce((sum, s) => sum + s.width, 0);
const viewWidth = RIBBON_CONSTANTS.RIBBON_WIDTH;
@ -203,12 +254,19 @@ export function RibbonArt({
const deviceTypes = resolveDisplayedDeviceTypes(resolved, tier);
const slots = computeDeviceSlots(deviceTypes.length, viewWidth, viewHeight);
const lineRects = computeLineRects(resolved.lines, viewWidth, viewHeight);
let stripeX = 0;
let stripeOffset = 0;
const stripeRects = displayStripes.map((stripe, i) => {
const w = (stripe.width / totalWeight) * viewWidth;
const el = <rect key={i} x={stripeX} y={0} width={w} height={viewHeight} fill={stripe.color} />;
stripeX += w;
const span =
(stripe.width / totalWeight) * (orientation === "horizontal" ? viewHeight : viewWidth);
const el =
orientation === "horizontal" ? (
<rect key={i} x={0} y={stripeOffset} width={viewWidth} height={span} fill={stripe.color} />
) : (
<rect key={i} x={stripeOffset} y={0} width={span} height={viewHeight} fill={stripe.color} />
);
stripeOffset += span;
return el;
});
@ -220,6 +278,17 @@ export function RibbonArt({
>
<rect x={0} y={0} width={viewWidth} height={viewHeight} rx={2} fill="#18181b" />
{stripeRects}
{lineRects.map((rect, i) => (
<rect
key={`line-${i}`}
data-line="true"
x={rect.x}
y={rect.y}
width={rect.width}
height={rect.height}
fill={rect.color}
/>
))}
{showOverlay && (
<image href="/images/ribbon-overlay.png" className="mix-blend-multiply w-full" />
)}

View file

@ -12,6 +12,7 @@ export const DEFAULT_RIBBON: RibbonDesign = {
],
devices: [],
mirrored: false,
lines: [],
};
export const MILITARY_COLORS: { name: string; hex: string }[] = [

View file

@ -14,8 +14,29 @@ export type Stripe = {
width: number;
};
/** Direction the stripe bands run across the ribbon face. */
export type StripeOrientation = "vertical" | "horizontal";
/**
* A horizontal accent line drawn across the stripe pattern, independent of
* stripe orientation. `positionY` marks the line's center as a percentage of
* ribbon height (0 = top edge, 100 = bottom edge) and `thickness` is also a
* percentage of ribbon height, so hairline pinstripes and bold bars share
* one representation.
*/
export type RibbonLine = {
id: string;
color: string;
positionY: number;
thickness: number;
};
export type RibbonDesign = {
stripes: Stripe[];
devices: Device[];
mirrored: boolean;
/** Band layout direction. Legacy designs without this key render vertical. */
stripeOrientation?: StripeOrientation;
/** Accent lines over the stripes, under the overlay and devices. Absent means none. */
lines?: RibbonLine[];
};