2024.js/lib/html.ts

23 lines
606 B
TypeScript
Raw Normal View History

2024-01-12 20:46:09 -05:00
export function h<Name extends keyof HTMLElementTagNameMap>(
name: Name,
2024-01-23 23:27:41 -05:00
props: Partial<HTMLElementTagNameMap[Name]> = {},
2024-01-13 00:29:49 -05:00
...children: (Node | string)[]
2024-01-12 20:46:09 -05:00
): HTMLElementTagNameMap[Name] {
const element = Object.assign(document.createElement(name), props);
element.append(...children);
return element;
}
export function canvas2d(
props: Partial<HTMLCanvasElement>
): [HTMLCanvasElement, CanvasRenderingContext2D] {
const canvas = h("canvas", props);
const cx = canvas.getContext("2d");
if (!cx) {
throw new Error("2D rendering context not supported");
}
return [canvas, cx];
}