dominionator/src/draw.ts

140 lines
3.1 KiB
TypeScript
Raw Normal View History

2024-12-29 23:00:38 -05:00
import { DominionCard } from "./types.ts";
2023-12-27 11:37:37 -08:00
2024-12-31 11:53:45 -05:00
const imageCache: Record<string, HTMLImageElement> = {};
export const loadImage = (
key: string,
src: string
): Promise<HTMLImageElement> => {
2025-01-04 22:32:17 -05:00
console.log("2", key);
console.log(src);
2024-12-31 11:53:45 -05:00
return new Promise((resolve) => {
if (key in imageCache && imageCache[key]) {
resolve(imageCache[key]);
}
const img = new Image();
img.onload = () => {
imageCache[key] = img;
resolve(img);
};
2025-01-04 22:32:17 -05:00
img.onerror = (e) => {
console.log("err", e);
};
2024-12-31 11:53:45 -05:00
img.src = src;
});
};
const imageList = [
{
2025-01-04 22:32:17 -05:00
key: "card-color-1",
src: "/static/assets/CardColorOne.png",
},
{
key: "card-brown",
src: "/static/assets/CardBrown.png",
},
{
key: "card-gray",
src: "/static/assets/CardGray.png",
},
{
key: "card-description-focus",
src: "/static/assets/DescriptionFocus.png",
2024-12-31 11:53:45 -05:00
},
];
2025-01-04 22:32:17 -05:00
export const loadImages = async () => {
for (const imageInfo of imageList) {
const { key, src } = imageInfo;
console.log(key);
await loadImage(key, src);
}
};
2024-12-31 11:53:45 -05:00
export const getImage = (key: string) => {
const image = imageCache[key];
if (!image) {
throw Error(`Tried to get an invalid image ${key}`);
}
return image;
};
2025-01-04 22:32:17 -05:00
export const colorImage = (
image: HTMLImageElement,
color: string
): HTMLCanvasElement => {
const canvas = document.createElement("canvas");
canvas.width = image.width;
canvas.height = image.height;
const context = canvas.getContext("2d")!;
context.save();
context.drawImage(image, 0, 0);
context.globalCompositeOperation = "multiply";
context.fillStyle = color;
context.fillRect(0, 0, canvas.width, canvas.height);
context.globalCompositeOperation = "destination-atop"; // restore transparency
context.drawImage(image, 0, 0);
context.restore();
return canvas;
};
2024-12-29 23:00:38 -05:00
export const drawCard = (
context: CanvasRenderingContext2D,
card: DominionCard
): Promise<void> => {
2023-12-27 11:37:37 -08:00
if (card.orientation === "card") {
2024-12-29 23:00:38 -05:00
return drawStandardCard(context, card);
2023-12-27 11:37:37 -08:00
} else {
2024-12-29 23:00:38 -05:00
return drawLandscapeCard(context, card);
2023-12-27 11:37:37 -08:00
}
2024-12-29 23:00:38 -05:00
};
2023-12-27 11:37:37 -08:00
2025-01-05 10:12:27 -05:00
const drawText = (
context: CanvasRenderingContext2D,
text: string,
x: number,
y: number,
options?: {
maxWidth?: number;
maxHeight?: number;
allowWrap?: boolean;
font?: string;
color?: string;
}
) => {
const { maxWidth = undefined } = options ?? {};
context.font = "bold 48px serif";
context.fillText(text, x, y, maxWidth);
};
2024-12-29 23:00:38 -05:00
const drawStandardCard = async (
context: CanvasRenderingContext2D,
card: DominionCard
): Promise<void> => {
const w = context.canvas.width;
const h = context.canvas.height;
context.save();
2025-01-04 22:32:17 -05:00
// Draw the image
// Draw the card base
2025-01-05 10:12:27 -05:00
const color = "#ffffff"; // "#ffbc55";
context.drawImage(colorImage(getImage("card-color-1"), color), 0, 0);
2025-01-04 22:32:17 -05:00
context.drawImage(getImage("card-gray"), 0, 0);
context.drawImage(colorImage(getImage("card-brown"), "#ff9911"), 0, 0);
context.drawImage(getImage("card-description-focus"), 44, 1094);
// Draw the name
2025-01-05 10:12:27 -05:00
drawText(context, card.title, 300, 300);
2025-01-04 22:32:17 -05:00
// Draw the description
// Draw the types
// Draw the cost
// Draw the preview
// Draw the icon
// Draw the credit
2024-12-29 23:00:38 -05:00
context.restore();
};
2023-12-27 11:37:37 -08:00
2024-12-29 23:00:38 -05:00
const drawLandscapeCard = async (
context: CanvasRenderingContext2D,
card: DominionCard
): Promise<void> => {
2023-12-27 11:37:37 -08:00
// TODO: everything
2024-12-29 23:00:38 -05:00
};