2025-01-06 23:01:01 -05:00
|
|
|
import {
|
|
|
|
measureDominionText,
|
|
|
|
parse,
|
|
|
|
renderDominionText,
|
|
|
|
} from "./dominiontext.ts";
|
|
|
|
import { DominionCardType, DominionColor, TYPE_ACTION } from "./types.ts";
|
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> => {
|
|
|
|
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-06 21:12:28 -05:00
|
|
|
{
|
|
|
|
key: "coin",
|
|
|
|
src: "/static/assets/Coin.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;
|
|
|
|
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,
|
2025-01-06 00:12:39 -05:00
|
|
|
color?: string
|
2025-01-04 22:32:17 -05:00
|
|
|
): 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";
|
2025-01-06 00:12:39 -05:00
|
|
|
context.fillStyle = color ?? "white";
|
2025-01-04 22:32:17 -05:00
|
|
|
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-06 23:01:01 -05:00
|
|
|
const getColors = (types: DominionCardType[]): { primary: string } => {
|
|
|
|
const byPriority = [...types]
|
|
|
|
.filter((type) => type.color)
|
|
|
|
.sort((a, b) => b.color!.priority - a.color!.priority);
|
|
|
|
if (byPriority.length === 0) {
|
|
|
|
return { primary: "white" };
|
|
|
|
}
|
|
|
|
const priority = byPriority[0]!;
|
|
|
|
if (priority !== TYPE_ACTION) {
|
|
|
|
return { primary: priority.color!.value };
|
|
|
|
} else {
|
|
|
|
const overriders = byPriority.filter((t) => t.color!.overridesAction);
|
|
|
|
if (overriders.length) {
|
|
|
|
return { primary: overriders[0]!.color!.value };
|
|
|
|
} else {
|
|
|
|
return { primary: priority.color!.value };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-12-29 23:00:38 -05:00
|
|
|
const drawStandardCard = async (
|
|
|
|
context: CanvasRenderingContext2D,
|
2025-01-06 22:28:57 -05:00
|
|
|
card: DominionCard & { orientation: "card" }
|
2024-12-29 23:00:38 -05:00
|
|
|
): 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-06 23:01:01 -05:00
|
|
|
const color = getColors(card.types).primary; // "#ffbc55";
|
2025-01-05 10:12:27 -05:00
|
|
|
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-06 23:01:01 -05:00
|
|
|
context.font = "78pt DominionTitle";
|
2025-01-06 22:13:53 -05:00
|
|
|
await renderDominionText(context, parse(card.title), w / 2, 220, 1100);
|
2025-01-04 22:32:17 -05:00
|
|
|
// Draw the description
|
2025-01-06 21:12:28 -05:00
|
|
|
context.font = "60pt DominionText";
|
|
|
|
await renderDominionText(
|
2025-01-05 23:55:22 -05:00
|
|
|
context,
|
2025-01-06 22:13:53 -05:00
|
|
|
parse(card.description),
|
2025-01-05 23:55:22 -05:00
|
|
|
w / 2,
|
|
|
|
1520,
|
2025-01-06 21:12:28 -05:00
|
|
|
1100
|
2025-01-05 23:55:22 -05:00
|
|
|
);
|
2025-01-04 22:32:17 -05:00
|
|
|
// Draw the types
|
2025-01-06 23:01:01 -05:00
|
|
|
let size = 65;
|
|
|
|
context.font = `${size}pt DominionTitle`;
|
|
|
|
while (
|
|
|
|
(
|
|
|
|
await measureDominionText(
|
|
|
|
context,
|
|
|
|
parse(card.types.map((t) => t.name).join(" - "))
|
|
|
|
)
|
|
|
|
).width > 800
|
|
|
|
) {
|
|
|
|
size -= 1;
|
|
|
|
context.font = `${size}pt DominionTitle`;
|
|
|
|
}
|
|
|
|
await renderDominionText(
|
|
|
|
context,
|
|
|
|
parse(card.types.map((t) => t.name).join(" - ")),
|
|
|
|
w / 2,
|
|
|
|
1930,
|
|
|
|
800
|
|
|
|
);
|
2025-01-04 22:32:17 -05:00
|
|
|
// Draw the cost
|
2025-01-06 22:13:53 -05:00
|
|
|
context.font = "90pt DominionText";
|
2025-01-06 22:14:58 -05:00
|
|
|
await renderDominionText(context, parse(card.cost), 210, 1940, 200);
|
2025-01-04 22:32:17 -05:00
|
|
|
// Draw the preview
|
2025-01-06 22:28:57 -05:00
|
|
|
if (card.preview) {
|
|
|
|
context.font = "90pt DominionText";
|
|
|
|
await renderDominionText(context, parse(card.preview), 200, 210, 200);
|
|
|
|
await renderDominionText(
|
|
|
|
context,
|
|
|
|
parse(card.preview),
|
|
|
|
w - 200,
|
|
|
|
210,
|
|
|
|
200
|
|
|
|
);
|
|
|
|
}
|
2025-01-04 22:32:17 -05:00
|
|
|
// 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,
|
2025-01-06 22:28:57 -05:00
|
|
|
card: DominionCard & { orientation: "landscape" }
|
2024-12-29 23:00:38 -05:00
|
|
|
): Promise<void> => {
|
2023-12-27 11:37:37 -08:00
|
|
|
// TODO: everything
|
2024-12-29 23:00:38 -05:00
|
|
|
};
|