Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | 69x 69x 2x | import { Injectable } from '@angular/core';
export interface Dim {
width: number;
height: number;
}
export function height(width: number, ar: Dim) {
return width * ar.height / ar.width;
}
export function width(height: number, ar: Dim) {
return height * ar.width / ar.height;
}
@Injectable({
providedIn: 'root'
})
export class ImageService {
private cache = new Map<string, Promise<Dim>>();
getImage(url: string): Promise<Dim> {
if (!this.cache.has(url)) {
this.cache.set(url, this.loadUrl(url));
}
return this.cache.get(url)!;
}
private loadUrl(url: string): Promise<Dim> {
const promise = new Promise<Dim>((resolve, reject) => {
const image = new Image();
image.src = url;
image.onload = () => {
resolve({
width: image.width,
height: image.height,
});
};
image.onerror = (event) => reject(event);
});
promise.catch(() => this.cache.delete(url));
return promise;
}
}
|