Files
bworld/client/skins.ts
T
2026-09-27 12:01:52 -03:00

19 lines
751 B
TypeScript

import { load_texture, type Texture } from "./renderer/mod.ts";
// a player's skin as the server sends it (a base64 64x64 png), undefined when it can't be decoded
export async function decode_skin(skin: string): Promise<Texture | undefined> {
try {
const bytes = Uint8Array.from(atob(skin), (char) => char.charCodeAt(0));
// as stored, so pngs with a color profile look the same as in an editor that ignores it, like minecraft
const image = await createImageBitmap(new Blob([bytes], { type: "image/png" }), {
colorSpaceConversion: "none",
premultiplyAlpha: "none",
});
const texture = image.width === 64 && image.height === 64 ? load_texture(image) : undefined;
image.close();
return texture;
} catch {
return undefined;
}
}