wip
@@ -1,5 +1,43 @@
|
|||||||
import { DominionCard } from "./types.ts";
|
import { DominionCard } from "./types.ts";
|
||||||
|
|
||||||
|
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);
|
||||||
|
};
|
||||||
|
img.src = src;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const imageList = [
|
||||||
|
{
|
||||||
|
key: "card",
|
||||||
|
src: "/assets/CardColorOne.png",
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
for (const imageInfo of imageList) {
|
||||||
|
const { key, src } = imageInfo;
|
||||||
|
await loadImage(key, src);
|
||||||
|
}
|
||||||
|
|
||||||
|
export const getImage = (key: string) => {
|
||||||
|
const image = imageCache[key];
|
||||||
|
if (!image) {
|
||||||
|
throw Error(`Tried to get an invalid image ${key}`);
|
||||||
|
}
|
||||||
|
return image;
|
||||||
|
};
|
||||||
|
|
||||||
export const drawCard = (
|
export const drawCard = (
|
||||||
context: CanvasRenderingContext2D,
|
context: CanvasRenderingContext2D,
|
||||||
card: DominionCard
|
card: DominionCard
|
||||||
@@ -19,7 +57,7 @@ const drawStandardCard = async (
|
|||||||
const h = context.canvas.height;
|
const h = context.canvas.height;
|
||||||
context.save();
|
context.save();
|
||||||
context.fillStyle = "brown";
|
context.fillStyle = "brown";
|
||||||
context.fillRect(0, 0, w, h);
|
context.drawImage(getImage("card"), 0, 0);
|
||||||
context.restore();
|
context.restore();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
|
After Width: | Height: | Size: 624 KiB |
|
After Width: | Height: | Size: 484 KiB |
|
After Width: | Height: | Size: 80 KiB |
|
After Width: | Height: | Size: 36 KiB |
|
After Width: | Height: | Size: 142 KiB |
|
After Width: | Height: | Size: 1.6 MiB |
|
After Width: | Height: | Size: 1.0 MiB |
|
After Width: | Height: | Size: 1.0 MiB |
|
After Width: | Height: | Size: 1.2 MiB |
|
After Width: | Height: | Size: 1.3 MiB |
|
After Width: | Height: | Size: 1.0 MiB |
|
After Width: | Height: | Size: 117 KiB |
|
After Width: | Height: | Size: 48 KiB |
|
After Width: | Height: | Size: 49 KiB |
|
After Width: | Height: | Size: 41 KiB |
|
After Width: | Height: | Size: 1.2 MiB |
|
After Width: | Height: | Size: 960 KiB |
|
After Width: | Height: | Size: 466 KiB |
|
After Width: | Height: | Size: 265 KiB |
|
After Width: | Height: | Size: 90 KiB |
|
After Width: | Height: | Size: 353 KiB |
|
After Width: | Height: | Size: 291 KiB |
|
After Width: | Height: | Size: 200 KiB |
|
After Width: | Height: | Size: 175 KiB |
|
After Width: | Height: | Size: 157 KiB |
|
After Width: | Height: | Size: 113 KiB |
|
After Width: | Height: | Size: 131 KiB |
|
After Width: | Height: | Size: 916 KiB |
|
After Width: | Height: | Size: 735 KiB |
|
After Width: | Height: | Size: 80 KiB |
|
After Width: | Height: | Size: 23 KiB |
|
After Width: | Height: | Size: 32 KiB |
|
After Width: | Height: | Size: 316 KiB |
|
After Width: | Height: | Size: 88 KiB |
|
After Width: | Height: | Size: 384 KiB |
|
After Width: | Height: | Size: 140 KiB |
|
After Width: | Height: | Size: 88 KiB |
|
After Width: | Height: | Size: 50 KiB |
|
After Width: | Height: | Size: 38 KiB |