Adding Opacity ui primitive

master
Dustin Swan 4 days ago
parent 441957185e
commit 12d27a1bff
Signed by: dustinswan
GPG Key ID: 30D46587E2100467

@ -53,6 +53,7 @@ export type UIValue =
| { kind: 'column', children: UIValue[], gap: number } | { kind: 'column', children: UIValue[], gap: number }
| { kind: 'clickable', child: UIValue, event: Value } | { kind: 'clickable', child: UIValue, event: Value }
| { kind: 'padding', child: UIValue, amount: number } | { kind: 'padding', child: UIValue, amount: number }
| { kind: 'opacity', child: UIValue, opacity: number }
| { kind: 'stack', children: UIValue[] } | { kind: 'stack', children: UIValue[] }
| { kind: 'text-input', value: string, placeholder: string, x: number, y: number, w: number, h: number, focused: boolean, onInput: Value, onSubmit: Value } | { kind: 'text-input', value: string, placeholder: string, x: number, y: number, w: number, h: number, focused: boolean, onInput: Value, onSubmit: Value }

@ -98,6 +98,14 @@ function renderUI(ui: UIValue, ctx: CanvasRenderingContext2D, x: number, y: numb
renderUI(ui.child, ctx, x + ui.amount, y + ui.amount); renderUI(ui.child, ctx, x + ui.amount, y + ui.amount);
break; break;
case 'opacity': {
const previousAlpha = ctx.globalAlpha;
ctx.globalAlpha = previousAlpha * ui.opacity;
renderUI(ui.child, ctx, x, y);
ctx.globalAlpha = previousAlpha;
break;
}
case 'stack': { case 'stack': {
for (const child of ui.children) { for (const child of ui.children) {
renderUI(child, ctx, x, y); renderUI(child, ctx, x, y);

@ -77,6 +77,15 @@ export function valueToUI(value: Value): UIValue {
return { kind: 'padding', amount: amount.value, child: valueToUI(child) }; return { kind: 'padding', amount: amount.value, child: valueToUI(child) };
} }
case 'Opacity': {
const { child, opacity } = fields;
if (opacity.kind !== 'int')
throw new Error('Invalid Opacity fields');
return { kind: 'opacity', opacity: opacity.value, child: valueToUI(child) };
}
case 'Stack': { case 'Stack': {
const children = fields.children; const children = fields.children;

Loading…
Cancel
Save