ridder
Version:
A straightforward game engine for simple data-driven games in JavaScript
35 lines (34 loc) • 1.42 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
const fonts = [];
/**
* Load a font into the cache.
*
* Use `setFont` in the `setup` or `render` functions to use the font in the upcoming text rendering.
*
* @param id - The ID for the font in the cache.
* @param url - The url to the `.ttf` or `.otf` file.
* @param family - The name of the font family.
* @param size - The base size in pixels.
*/
export function loadFont(id, url, family, size) {
return __awaiter(this, void 0, void 0, function* () {
const font = new FontFace(family, `url(${url})`);
yield font.load();
document.fonts.add(font);
fonts[id] = `${size}px ${font.family}`;
});
}
/**
* Get a font from the cache.
*/
export function getFont(id) {
return fonts[id];
}