From ffcd8e1c792d6bab4d7398a41dcd58b3b16baca8 Mon Sep 17 00:00:00 2001 From: Documentation Date: Sat, 21 Aug 2021 20:16:05 +0000 Subject: [PATCH] Updates --- CardCreator.html | 267 +++++++++++++++++++++++++++++++++++++++++++- create-card.js.html | 54 +++++++-- index.html | 2 +- 3 files changed, 310 insertions(+), 13 deletions(-) diff --git a/CardCreator.html b/CardCreator.html index e40b64c..cb1d8c3 100644 --- a/CardCreator.html +++ b/CardCreator.html @@ -139,6 +139,82 @@ +

Members

+ + + +

canvasSize :CardCreator~CanvasDimensions

+ + + + +
+ The canvas's dimensions. +
+ + + +
Type:
+ + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + +

Methods

@@ -149,7 +225,7 @@ -

(async) createCard(charaId) → {Promise.<Buffer>}

+

(async) createCard(charaId, customImage) → {Promise.<Buffer>}

@@ -216,6 +292,42 @@ + + + + customImage + + + + + +string +| + +Buffer +| + +null +| + +undefined + + + + + + + + + + Optional parameter providing a custom +image to be drawn between the background of the character card and the black information boxes. +The image should be the same resolution as the default image. The default image size can be +retrieved with CardCreator#canvasSize. May be a URL, `data: `URI, a local file path, +or a Buffer instance. + + + @@ -253,7 +365,7 @@
Source:
@@ -380,7 +492,7 @@ generation methods.
Source:
@@ -436,6 +548,153 @@ generation methods. +

Type Definitions

+ + + +

CanvasDimensions

+ + + + + + +
Type:
+ + + + + + +
Properties:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescription
width + + +number + + + + The width of the canvas.
height + + +number + + + + The height of the canvas.
+ + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ + + + + + + +
+ + + + + + + + @@ -454,7 +713,7 @@ generation methods.
diff --git a/create-card.js.html b/create-card.js.html index f902367..b8eb454 100644 --- a/create-card.js.html +++ b/create-card.js.html @@ -116,6 +116,23 @@ class CardCreator { this.isInit = false; } + /** + * @typedef {Object} CardCreator~CanvasDimensions + * @property {number} width The width of the canvas. + * @property {number} height The height of the canvas. + */ + + /** + * The canvas's dimensions. + * @type {CardCreator~CanvasDimensions} + */ + get canvasSize() { + return { + width: 890, + height: 720, + }; + } + /** * Ensures that the instance is ready to generate character cards. * This function must be resolved before using character card @@ -276,7 +293,10 @@ class CardCreator { if (cnt == 0) return 0; - return this.pad(Math.floor(ilvl / cnt), 4); + // ilvl division is always out of 13 items + // mainhand counts twice if there's no offhand + // job stones are ignored + return this.pad(Math.floor(ilvl / 13), 4); } pad(num, size) { @@ -288,6 +308,11 @@ class CardCreator { /** * Creates a character card for a character. * @param {number | string} charaId The Lodestone ID of the character to generate a card for. + * @param {string | Buffer | null | undefined} customImage Optional parameter providing a custom + * image to be drawn between the background of the character card and the black information boxes. + * The image should be the same resolution as the default image. The default image size can be + * retrieved with {@link CardCreator#canvasSize}. May be a URL, `data: `URI, a local file path, + * or a Buffer instance. * @example * const fs = require("fs"); * @@ -302,19 +327,32 @@ class CardCreator { * }); * @returns {Promise<Buffer>} A promise representating the construction of the card's image data. */ - async createCard(charaId) { - var response = await fetch(`https://xivapi.com/character/${charaId}?extended=1&data=FC,mimo`); - var data = await response.json(); + async createCard(charaId, customImage) { + const characterInfoUrl = `https://xivapi.com/character/${charaId}?extended=1&data=FC,mimo`; + const response = await fetch(characterInfoUrl); + if (!response.ok) { + // Retry once if the request fails + response = await fetch(characterInfoUrl); + } - const canvas = createCanvas(890, 720); + const data = await response.json(); + + const canvasSize = this.canvasSize; + const canvas = createCanvas(canvasSize.width, canvasSize.height); const ctx = canvas.getContext("2d"); - var portrait = await loadImage(data.Character.Portrait); + const portrait = await loadImage(data.Character.Portrait); - ctx.drawImage(this.bgImage, 0, 0, 890, 722); + ctx.drawImage(this.bgImage, 0, 0, canvasSize.width, canvasSize.height + 2); ctx.drawImage(portrait, 0, 120, 441, 600); + if (customImage != null) { + const bg = await loadImage(customImage); + + ctx.drawImage(bg, 0, 0, canvasSize.width, canvasSize.height); + } + ctx.strokeStyle = white; ctx.fillStyle = black; ctx.beginPath(); @@ -653,7 +691,7 @@ exports.CardCreator = CardCreator;
diff --git a/index.html b/index.html index c9776b1..4d0eae8 100644 --- a/index.html +++ b/index.html @@ -118,7 +118,7 @@ example((err, response) => {