xonecas.ioDon't click! Sean Caetano Martin Sign in
tiny-adventure / web / js / frames.js 50 lines1.7 kB Raw
1import { drawText } from './text.js';
2import { T } from './theme.js';
3
4const RADIUS = 4;
5
6function box(ctx, x, y, w, h, fill, line) {
7  ctx.beginPath();
8  ctx.roundRect(x + 0.5, y + 0.5, w - 1, h - 1, RADIUS);
9  ctx.fillStyle = fill;
10  ctx.fill();
11  if (!line) return;
12  ctx.lineWidth = 1;
13  ctx.strokeStyle = line;
14  ctx.stroke();
15}
16
17export function createFrames() {
18  const panel = (ctx, x, y, w, h, role = 'surface', line = T.line) => box(ctx, x, y, w, h, T[role], line);
19
20  const button = (ctx, x, y, w, h, label, { hover = false, focus = false, primary = false, disabled = false, key = '', size = 14 } = {}) => {
21    const fill = disabled ? T.surface : primary ? T.accent : hover ? T.line : T.raised;
22    box(ctx, x, y, w, h, fill, focus || (hover && primary) ? T.fg : primary ? null : T.line);
23    const color = disabled ? T.faint : primary ? T.accentFg : T.fg;
24    const keyW = key ? 22 : 0;
25    drawText(ctx, label, x + (w - keyW) / 2, y, { h, size, align: 'center', weight: 'bold', color, maxW: w - 12 - keyW, fit: true });
26    if (key) drawText(ctx, key, x + w - 14, y, { h, size: 12, color: primary ? T.accentFg : T.muted, align: 'center' });
27  };
28
29  const bar = (ctx, x, y, w, h, ratio, color) => {
30    ctx.fillStyle = T.bg;
31    ctx.fillRect(x, y, w, h);
32    ctx.fillStyle = color;
33    ctx.fillRect(x, y, Math.max(0, w * Math.min(1, Math.max(0, ratio))), h);
34  };
35
36  const rule = (ctx, x, y, w) => {
37    ctx.fillStyle = T.line;
38    ctx.fillRect(x, y, w, 1);
39  };
40
41  const scrollbar = (ctx, x, y, h, start, shown, total) => {
42    if (shown >= total) return;
43    ctx.fillStyle = T.line;
44    ctx.fillRect(x, y, 3, h);
45    ctx.fillStyle = T.muted;
46    ctx.fillRect(x, y + (h * start) / total, 3, (h * shown) / total);
47  };
48
49  return { panel, button, bar, rule, scrollbar };
50}