Skip to content

Commit

Permalink
[drawer] rename images to preloadedImages
Browse files Browse the repository at this point in the history
  • Loading branch information
nau7ilus committed Aug 22, 2024
1 parent 28e3ff1 commit c41317f
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions drawer/lib/drawer.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,24 +69,24 @@ const RANK_BAR_Y = 720;
const RANK_BAR_BG_COLOR = '#2A313A';
const RANK_BAR_RADII = 5;

let images = null;
let preloadedImages = null;

async function preload() {
registerFont(path.join(__dirname, './assets/junegull.ttf'), { family: 'Junegull' });
const foundImages = addPath('./assets', 'png', await findImages());
const loadPromises = Object.values(foundImages).map((p) => loadImage(p));
const loaded = await Promise.all(loadPromises);
const imagesEntries = Object.keys(foundImages).map((k, i) => [k, loaded[i]]);
images = Object.fromEntries(imagesEntries);
preloadedImages = Object.fromEntries(imagesEntries);
}

async function initCanvas(type, locale) {
// If the pictures have not loaded yet, do this
if (!images) await preload();
if (!preloadedImages) await preload();
const canvas = createCanvas(WIDTH, HEIGHT);
const ctx = canvas.getContext('2d');
// Set background preset
const background = images[`base/${locale}/${type}Bg`];
const background = preloadedImages[`base/${locale}/${type}Bg`];
ctx.drawImage(background, 0, 0);
return { canvas, ctx };
}
Expand Down Expand Up @@ -131,7 +131,7 @@ function setAttemptsLeft(ctx, attemptsLeft) {
}

function addHangman(ctx, type) {
const hangman = images[`sprites/${type}`];
const hangman = preloadedImages[`sprites/${type}`];
ctx.drawImage(hangman, ...HANGMAN_COORDS);
}

Expand Down

0 comments on commit c41317f

Please sign in to comment.