diff --git a/config/default.json b/config/default.json index 67aa516f5..9aef69048 100644 --- a/config/default.json +++ b/config/default.json @@ -216,8 +216,8 @@ "name": "Halloween", "enabled": true, "images": [ - "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dc/Fluent_Emoji_flat_1f383.svg/640px-Fluent_Emoji_flat_1f383.svg.png", - "https://upload.wikimedia.org/wikipedia/commons/thumb/f/fc/Ghost.svg/640px-Ghost.svg.png" + "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dc/Fluent_Emoji_flat_1f383.svg/120px-Fluent_Emoji_flat_1f383.svg.png", + "https://upload.wikimedia.org/wikipedia/commons/thumb/f/fc/Ghost.svg/120px-Ghost.svg.png" ], "imageScale": 2, "startDay": 29, diff --git a/src/features/holiday/HolidayAnimations.js b/src/features/holiday/HolidayAnimations.js index ec566e39d..cb4e15740 100644 --- a/src/features/holiday/HolidayAnimations.js +++ b/src/features/holiday/HolidayAnimations.js @@ -9,6 +9,8 @@ export class HolidayAnimations { this.imageWidth = 15 * scale this.baseImages = images this.images = [] + /** @type {Map} */ + this.loaded = new Map() this.minScale = 0.3 this.interval = null this.canvas = /** @type {HTMLCanvasElement} */ ( @@ -39,12 +41,10 @@ export class HolidayAnimations { this.setCanvasSize() this.ctx.clearRect(0, 0, this.w, this.h) this.images.forEach((item) => { - item.image = new Image() - item.image.style.height = item.height - item.image.style.width = item.width - item.image.src = item.src + const image = this.loaded.get(item.src) + if (!image.complete || !image.naturalWidth) return this.ctx.globalAlpha = item.opacity - this.ctx.drawImage(item.image, item.x, item.y, item.width, item.height) + this.ctx.drawImage(image, item.x, item.y, item.width, item.height) }) this.move() } @@ -54,6 +54,18 @@ export class HolidayAnimations { return } this.setCanvasSize() + // Load each source once; creating a new Image every frame re-requested + // any URL that failed to load, every frame + this.baseImages.forEach((src) => { + if (this.loaded.has(src)) return + const image = new Image() + image.onerror = () => { + // eslint-disable-next-line no-console + console.warn('[HolidayEffect] failed to load image', src) + } + image.src = src + this.loaded.set(src, image) + }) for (let a = 0; a < 30; a += 1) { const scale = Math.random() * (1 - this.minScale) + this.minScale this.images.push({