This commit is contained in:
Documentation 2021-08-21 20:16:05 +00:00
parent 0cd5a07962
commit ffcd8e1c79
3 changed files with 310 additions and 13 deletions

View file

@ -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;
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.7</a> on Sun May 30 2021 14:07:32 GMT+0000 (Coordinated Universal Time)
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.7</a> on Sat Aug 21 2021 20:16:04 GMT+0000 (Coordinated Universal Time)
</footer>
<script> prettyPrint(); </script>