mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-27 17:51:20 -08:00
34 lines
744 B
TypeScript
34 lines
744 B
TypeScript
export default class Poster {
|
|
private image: HTMLImageElement | null = null
|
|
|
|
cancel(): void {
|
|
if (!this.image)
|
|
return
|
|
this.image.onload = null
|
|
this.image.onerror = null
|
|
this.image = null
|
|
}
|
|
|
|
draw(source: string, current: () => boolean, draw: (image: HTMLImageElement) => void, failure: (error: unknown) => void): void {
|
|
this.cancel()
|
|
const image = new Image()
|
|
this.image = image
|
|
image.onload = () => {
|
|
if (this.image !== image || !current())
|
|
return
|
|
this.cancel()
|
|
try {
|
|
draw(image)
|
|
}
|
|
catch (error) {
|
|
failure(error)
|
|
}
|
|
}
|
|
image.onerror = () => {
|
|
if (this.image === image)
|
|
this.cancel()
|
|
}
|
|
image.src = source
|
|
}
|
|
}
|