37 lines
1.3 KiB
JavaScript
37 lines
1.3 KiB
JavaScript
// Render-list helpers. A draw-op is a jco-lifted WIT variant: {tag, val}.
|
|
|
|
/** Translate a draw-op list by (dx, dy), returning new ops. */
|
|
export function translate(ops, dx, dy) {
|
|
return ops.map((op) => {
|
|
const v = op.val;
|
|
switch (op.tag) {
|
|
case 'fill-rect':
|
|
case 'stroke-rect':
|
|
return { tag: op.tag, val: { ...v, rect: { ...v.rect, x: v.rect.x + dx, y: v.rect.y + dy } } };
|
|
case 'line':
|
|
return { tag: op.tag, val: { ...v, start: { x: v.start.x + dx, y: v.start.y + dy }, end: { x: v.end.x + dx, y: v.end.y + dy } } };
|
|
case 'circle':
|
|
return { tag: op.tag, val: { ...v, center: { x: v.center.x + dx, y: v.center.y + dy } } };
|
|
case 'text':
|
|
return { tag: op.tag, val: { ...v, origin: { x: v.origin.x + dx, y: v.origin.y + dy } } };
|
|
case 'push-clip':
|
|
return { tag: op.tag, val: { ...v, x: v.x + dx, y: v.y + dy } };
|
|
case 'pop-clip':
|
|
return op;
|
|
default:
|
|
throw new Error(`unknown draw-op tag: ${op.tag}`);
|
|
}
|
|
});
|
|
}
|
|
|
|
/** Place a component's local-space ops at `rect`, clipped to it. */
|
|
export function place(ops, rect) {
|
|
return [
|
|
{ tag: 'push-clip', val: { ...rect } },
|
|
...translate(ops, rect.x, rect.y),
|
|
{ tag: 'pop-clip', val: undefined },
|
|
];
|
|
}
|
|
|
|
export const colorCss = (c) => `rgba(${c.r},${c.g},${c.b},${(c.a / 255).toFixed(3)})`;
|