Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 39x 3x 3x 3x 1x 1x 3x 39x 39x 39x 39x 39x 8x 5x 5x 5x 3x 39x 3x 3x 3x 3x 3x 3x 3x 39x 3x 3x 13x 39x 39x 39x 39x 39x 14x 14x 39x | import { layoutTabularRun, type BeMusicSongInfoLayer, type BeMusicSongInfoText } from '@be-music/player-skin-sdk';
/** Plain's palette, type, and a few small drawing helpers shared by its three screens. */
export const INK = '#111114';
export const PANEL = '#1c1c21';
export const LINE = '#3a3a42';
export const TEXT = '#f2f2f2';
export const MUTED = '#8c8c96';
export const ACCENT = '#3d7bff';
export const DANGER = '#ff4d5e';
/** Lane backgrounds and note colours by lane kind. */
/** Per lane kind. The 24 / 48 KEY wheel lanes take the scratch colours: they are the side's outer, odd-one-out lane. */
export const LANE_FILL = {
white: '#1d1d22',
black: '#16161a',
scratch: '#16161a',
'wheel-up': '#16161a',
'wheel-down': '#16161a',
} as const;
export const NOTE_FILL = {
white: '#f2f2f2',
black: '#5b8cff',
scratch: '#ff4d5e',
'wheel-up': '#ff4d5e',
'wheel-down': '#ff4d5e',
} as const;
/** Colours the PERFECT judgement (shown as GREAT) cycles through. */
export const FLASHING_GREAT = [0xffffff, 0x5b8cff, 0xffd84d] as const;
/** The system's sans-serif: Plain loads no web fonts, so it draws the same on any host. */
export const FONT = 'ui-sans-serif, system-ui, sans-serif';
const FONTS = new Map<string, string>();
/** A CSS font for `ctx.font` (the same string for the same size and weight). */
export function font(size: number, weight = 500): string {
const key = `${weight}/${size}`;
let value = FONTS.get(key);
if (!value) {
value = `${weight} ${size}px ${FONT}`;
FONTS.set(key, value);
}
return value;
}
/** The font last set on each context: the browser re-parses `ctx.font` on every assignment, so skip repeats. */
const CURRENT_FONTS = new WeakMap<CanvasRenderingContext2D, string>();
/** `0xrrggbb` → `#rrggbb`, for colours the SDK hands out as numbers. */
export function css(color: number): string {
return `#${color.toString(16).padStart(6, '0')}`;
}
/** The narrowest a `maxWidth` text is squeezed; anything longer is clipped to the box and scrolls instead. */
export const MIN_SQUEEZE = 0.85;
/** Marquee timing: rest at the start so it reads, scroll at a steady pace, rest at the end, then jump back. */
const MARQUEE_START_HOLD_MS = 1500;
const MARQUEE_SPEED_PX_PER_S = 32;
const MARQUEE_END_HOLD_MS = 1000;
/** How far (px) a text overflowing its box by `overflow` px has scrolled `elapsedMs` after it appeared. */
export function marqueeOffset(elapsedMs: number, overflow: number): number {
if (!(overflow > 0) || !(elapsedMs > 0)) return 0;
const scrollMs = (overflow / MARQUEE_SPEED_PX_PER_S) * 1000;
const t = elapsedMs % (MARQUEE_START_HOLD_MS + scrollMs + MARQUEE_END_HOLD_MS);
if (t <= MARQUEE_START_HOLD_MS) return 0;
return Math.min(overflow, ((t - MARQUEE_START_HOLD_MS) * MARQUEE_SPEED_PX_PER_S) / 1000);
}
/** When each scrolling string was first drawn, so it starts from its beginning; forgotten once it leaves the screen. */
const MARQUEE_SINCE = new Map<string, { startMs: number; seenMs: number }>();
function marqueeElapsed(value: string, nowMs: number): number {
let since = MARQUEE_SINCE.get(value);
if (!since || nowMs - since.seenMs > 400) {
if (MARQUEE_SINCE.size > 64) {
for (const [key, entry] of MARQUEE_SINCE) if (nowMs - entry.seenMs > 400) MARQUEE_SINCE.delete(key);
}
since = { startMs: nowMs, seenMs: nowMs };
MARQUEE_SINCE.set(value, since);
}
since.seenMs = nowMs;
return nowMs - since.startMs;
}
export interface TextOptions {
size: number;
color?: string;
weight?: number;
align?: CanvasTextAlign;
/**
* Box the text must fit: squeezed down to {@link MIN_SQUEEZE}, then clipped to the box. With `nowMs` (the frame's
* clock) a clipped text scrolls through the box like a marquee; without it, it shows its start.
*/
maxWidth?: number;
nowMs?: number;
}
/** Draws `text` with its left edge (or `align` point) at `x` and its middle at `y`. */
export function text(ctx: CanvasRenderingContext2D, value: string, x: number, y: number, options: TextOptions): void {
const wanted = font(options.size, options.weight);
if (CURRENT_FONTS.get(ctx) !== wanted) {
ctx.font = wanted;
CURRENT_FONTS.set(ctx, wanted);
}
ctx.fillStyle = options.color ?? TEXT;
ctx.textAlign = options.align ?? 'left';
ctx.textBaseline = 'middle';
const { maxWidth } = options;
if (maxWidth === undefined) {
ctx.fillText(value, x, y);
return;
}
const overflow = ctx.measureText(value).width * MIN_SQUEEZE - maxWidth;
if (overflow <= 0.5) {
// Fits as it is, or squeezed a little: `fillText` squeezes to `maxWidth` by itself.
ctx.fillText(value, x, y, maxWidth);
return;
}
// Too long: draw it at the minimum squeeze, clipped to the box, scrolled by the time it has been on screen.
const align = options.align ?? 'left';
const left = align === 'right' || align === 'end' ? x - maxWidth : align === 'center' ? x - maxWidth / 2 : x;
const offset = options.nowMs === undefined ? 0 : marqueeOffset(marqueeElapsed(value, options.nowMs), overflow);
ctx.save();
ctx.beginPath();
ctx.rect(left, y - options.size, maxWidth, options.size * 2);
ctx.clip();
ctx.translate(left - offset, y);
ctx.scale(MIN_SQUEEZE, 1);
ctx.textAlign = 'left';
ctx.fillText(value, 0, 0);
ctx.restore();
}
/** How {@link stat} fits its value, and how big its caption is. */
export interface StatOptions {
/** Box the value must fit (it scrolls with `nowMs` when it cannot). */
maxWidth?: number;
nowMs?: number;
/** Caption size; 9 px on the landscape stage, 10 on the portrait one (which a phone shows smaller). */
labelSize?: number;
}
/** A label above a value: the small grey caption and the figure under it, the value fitted to `maxWidth`. */
export function stat(
ctx: CanvasRenderingContext2D,
label: string,
value: string,
x: number,
y: number,
options: StatOptions = {},
): void {
text(ctx, label, x, y, { size: options.labelSize ?? 9, color: MUTED, weight: 700 });
text(ctx, value, x, y + 16, { size: 16, maxWidth: options.maxWidth, nowMs: options.nowMs });
}
/**
* Draws a NONSTOP play's song info crossfading (`resolveSongInfoLayers`): `draw` once per layer, at that layer's opacity.
* The opacity goes on `globalAlpha` and back, not through `save` / `restore`, which would undo the font `text` keeps.
*/
export function drawSongInfoLayers(
ctx: CanvasRenderingContext2D,
layers: readonly BeMusicSongInfoLayer[],
draw: (info: BeMusicSongInfoText) => void,
): void {
const base = ctx.globalAlpha;
for (const { info, alpha } of layers) {
ctx.globalAlpha = base * alpha;
draw(info);
}
ctx.globalAlpha = base;
}
/**
* {@link text} with tabular figures: every digit in a cell as wide as the widest digit, so a changing number (a judge
* detail's milliseconds) keeps its place. Canvas 2D has no `font-variant-numeric`, so the cells are laid out by hand.
*/
export function tabularText(
ctx: CanvasRenderingContext2D,
value: string,
x: number,
y: number,
options: Omit<TextOptions, 'maxWidth' | 'nowMs'>,
): void {
const wanted = font(options.size, options.weight);
Eif (CURRENT_FONTS.get(ctx) !== wanted) {
ctx.font = wanted;
CURRENT_FONTS.set(ctx, wanted);
}
ctx.fillStyle = options.color ?? TEXT;
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
const run = layoutTabularRun(value, (char) => measureTabularChar(ctx, wanted, char));
const align = options.align ?? 'left';
const left = align === 'center' ? x - run.width / 2 : align === 'right' || align === 'end' ? x - run.width : x;
for (const glyph of run.glyphs) Eif (glyph.char !== ' ') ctx.fillText(glyph.char, left + glyph.x + glyph.w / 2, y);
}
/** Each font's measured character advances, for {@link tabularText}: a judge detail redraws every frame. */
const TABULAR_WIDTHS = new Map<string, Map<string, number>>();
function measureTabularChar(ctx: CanvasRenderingContext2D, font: string, char: string): number {
let widths = TABULAR_WIDTHS.get(font);
if (!widths) TABULAR_WIDTHS.set(font, (widths = new Map()));
let width = widths.get(char);
if (width === undefined) {
width = ctx.measureText(char).width;
widths.set(char, width);
}
return width;
}
|