hippofish/packages/frontend/src/scripts/favicon-dot.ts

112 lines
3.1 KiB
TypeScript
Raw Normal View History

class FavIconDot {
2024-05-06 13:54:43 +02:00
canvas: HTMLCanvasElement;
src: string | null = null;
ctx: CanvasRenderingContext2D | null = null;
faviconImage: HTMLImageElement | null = null;
faviconEL: HTMLLinkElement | undefined;
hasLoaded: Promise<void> | undefined;
constructor() {
this.canvas = document.createElement('canvas');
2024-05-02 17:22:30 +02:00
}
2024-05-06 13:54:43 +02:00
/**
* Must be called before calling any other functions
*/
2024-05-02 17:22:30 +02:00
public async setup() {
2024-05-06 13:54:43 +02:00
const element: HTMLLinkElement = await this.getOrMakeFaviconElement();
2024-05-02 17:22:30 +02:00
this.faviconEL = element;
this.src = this.faviconEL.getAttribute('href');
this.ctx = this.canvas.getContext('2d');
2024-05-02 17:22:30 +02:00
this.faviconImage = document.createElement('img');
this.hasLoaded = new Promise((resolve, reject) => {
2024-05-03 11:27:41 +02:00
(this.faviconImage as HTMLImageElement).addEventListener('load', () => {
2024-05-02 17:22:30 +02:00
this.canvas.width = (this.faviconImage as HTMLImageElement).width;
this.canvas.height = (this.faviconImage as HTMLImageElement).height;
resolve();
2024-05-03 11:27:41 +02:00
});
(this.faviconImage as HTMLImageElement).addEventListener('error', () => {
2024-05-02 17:22:30 +02:00
reject('Failed to create favicon img element');
2024-05-03 11:27:41 +02:00
});
2024-05-02 17:22:30 +02:00
});
2024-05-02 17:38:16 +02:00
this.faviconImage.src = this.faviconEL.href;
2024-05-02 17:22:30 +02:00
}
2024-05-06 13:54:43 +02:00
private async getOrMakeFaviconElement(): Promise<HTMLLinkElement> {
2024-05-02 17:22:30 +02:00
return new Promise((resolve, reject) => {
2024-05-06 13:54:43 +02:00
const favicon = (document.querySelector('link[rel=icon]') ?? this.createFaviconElem()) as HTMLLinkElement;
2024-05-03 11:27:41 +02:00
favicon.addEventListener('load', () => {
2024-05-02 17:38:16 +02:00
resolve(favicon);
2024-05-03 11:27:41 +02:00
});
2024-05-02 17:22:30 +02:00
2024-05-02 17:38:16 +02:00
favicon.onerror = () => {
reject('Failed to load favicon');
};
2024-05-02 17:22:30 +02:00
resolve(favicon);
});
}
2024-05-06 13:54:43 +02:00
private createFaviconElem() {
const newLink = document.createElement('link');
2024-05-03 12:20:58 +02:00
newLink.setAttribute('rel', 'icon');
newLink.setAttribute('href', '/favicon.ico');
newLink.setAttribute('type', 'image/x-icon');
document.head.appendChild(newLink);
return newLink;
}
2024-05-06 13:54:43 +02:00
private drawIcon() {
2024-05-02 17:22:30 +02:00
if (!this.ctx || !this.faviconImage) return;
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
2024-05-02 17:22:30 +02:00
this.ctx.drawImage(this.faviconImage, 0, 0, this.faviconImage.width, this.faviconImage.height);
}
2024-05-06 13:54:43 +02:00
private drawDot() {
2024-05-02 17:22:30 +02:00
if (!this.ctx || !this.faviconImage) return;
this.ctx.beginPath();
2024-05-02 17:22:30 +02:00
this.ctx.arc(this.faviconImage.width - 10, 10, 10, 0, 2 * Math.PI);
this.ctx.fillStyle = getComputedStyle(document.documentElement).getPropertyValue('--navIndicator');
this.ctx.strokeStyle = 'white';
this.ctx.fill();
this.ctx.stroke();
}
2024-05-06 13:54:43 +02:00
private setFavicon() {
2024-05-02 17:22:30 +02:00
if (this.faviconEL) this.faviconEL.href = this.canvas.toDataURL('image/png');
}
2024-05-06 13:54:43 +02:00
async setVisible(isVisible: boolean) {
// Wait for it to have loaded the icon
await this.hasLoaded;
2024-05-06 13:54:43 +02:00
this.drawIcon();
if (isVisible) this.drawDot();
this.setFavicon();
}
}
2024-05-02 17:22:30 +02:00
let icon: FavIconDot | undefined = undefined;
export function setFavIconDot(visible: boolean) {
2024-05-02 17:22:30 +02:00
const setIconVisibility = async () => {
if (!icon) {
icon = new FavIconDot();
await icon.setup();
}
(icon as FavIconDot).setVisible(visible);
};
// If document is already loaded, set visibility immediately
if (document.readyState === 'complete') {
setIconVisibility();
} else {
// Otherwise, set visibility when window loads
2024-05-03 11:27:41 +02:00
window.addEventListener('load', setIconVisibility);
}
}