From 0e9661f14621dbbbcaa30fe4f14a38fd58234750 Mon Sep 17 00:00:00 2001 From: Dylan Pizzo Date: Mon, 6 Jan 2025 23:34:41 -0500 Subject: [PATCH] Add image drawing --- src/dominiontext.ts | 4 +-- src/draw.ts | 63 +++++++++++++++++++++++++++++++++++++++------ src/sampleData.ts | 11 ++++---- 3 files changed, 62 insertions(+), 16 deletions(-) diff --git a/src/dominiontext.ts b/src/dominiontext.ts index 9aa5396..3db715a 100644 --- a/src/dominiontext.ts +++ b/src/dominiontext.ts @@ -235,13 +235,13 @@ export const measureDominionText = async ( } line.width = line.pieces .map((piece) => piece.measure.width) - .reduce((a, b) => a + b); + .reduce((a, b) => a + b, 0); return line; }), width: Math.max(...lines.map((line) => line.width)), height: lines .map((line) => line.ascent + line.descent) - .reduce((a, b) => a + b), + .reduce((a, b) => a + b, 0), }; }; diff --git a/src/draw.ts b/src/draw.ts index 2a169f5..58aa40e 100644 --- a/src/draw.ts +++ b/src/draw.ts @@ -3,25 +3,28 @@ import { parse, renderDominionText, } from "./dominiontext.ts"; -import { DominionCardType, DominionColor, TYPE_ACTION } from "./types.ts"; +import { DominionCardType, TYPE_ACTION } from "./types.ts"; import { DominionCard } from "./types.ts"; const imageCache: Record = {}; export const loadImage = ( - key: string, - src: string -): Promise => { + src: string, + key?: string +): Promise => { return new Promise((resolve) => { - if (key in imageCache && imageCache[key]) { + if (key && key in imageCache && imageCache[key]) { resolve(imageCache[key]); } const img = new Image(); img.onload = () => { - imageCache[key] = img; + if (key) { + imageCache[key] = img; + } resolve(img); }; img.onerror = (e) => { console.log("err", e); + resolve(null); }; img.src = src; }); @@ -53,7 +56,7 @@ const imageList = [ export const loadImages = async () => { for (const imageInfo of imageList) { const { key, src } = imageInfo; - await loadImage(key, src); + await loadImage(src, key); } }; @@ -123,6 +126,24 @@ const drawStandardCard = async ( const h = context.canvas.height; context.save(); // Draw the image + const image = await loadImage(card.image); + if (image) { + const cx = w / 2; + const cy = 704; + const windowHeight = 830; + const windowWidth = 1194; + const scale = Math.max( + windowHeight / image.height, + windowWidth / image.width + ); + context.drawImage( + image, + cx - (scale * image.width) / 2, + cy - (scale * image.height) / 2, + scale * image.width, + scale * image.height + ); + } // Draw the card base const color = getColors(card.types).primary; // "#ffbc55"; context.drawImage(colorImage(getImage("card-color-1"), color), 0, 0); @@ -178,7 +199,33 @@ const drawStandardCard = async ( ); } // Draw the icon - // Draw the credit + // Draw the author credit + context.fillStyle = "white"; + context.font = "31pt DominionText"; + const authorMeasure = await measureDominionText( + context, + parse(card.author) + ); + await renderDominionText( + context, + parse(card.author), + w - 150 - authorMeasure.width / 2, + 2035, + w / 2 - 150 + ); + // Draw the artist credit + const artistMeasure = await measureDominionText( + context, + parse(card.artist) + ); + await renderDominionText( + context, + parse(card.artist), + 155 + artistMeasure.width / 2, + 2035, + w / 2 - 150 + ); + // Restore the context context.restore(); }; diff --git a/src/sampleData.ts b/src/sampleData.ts index be4f6ca..bc8f992 100644 --- a/src/sampleData.ts +++ b/src/sampleData.ts @@ -1,7 +1,6 @@ import { DominionCard, TYPE_ACTION, - TYPE_ATTACK, TYPE_REACTION, TYPE_TREASURE, } from "./types.ts"; @@ -11,9 +10,9 @@ export const sampleCard1: DominionCard = { title: "Title", description: "Hello, world.", types: [TYPE_ACTION, TYPE_REACTION], - image: "", - artist: "", - author: "", + image: "https://wiki.dominionstrategy.com/images/7/76/AdventurerArt.jpg", + artist: "Dall-E", + author: "John Doe", version: "", cost: "$", preview: "", @@ -25,8 +24,8 @@ export const sampleCard2: DominionCard = { description: "+1 Card\n+1 Action\n+1 Buy\n+$1", types: [TYPE_ACTION], image: "", - artist: "", - author: "", + artist: "Leonardo DaVinci", + author: "Jane Smith", version: "", cost: "$4", preview: "",