diff --git a/web/js/screens.js b/web/js/screens.js index 57b63622..ee6fbd70 100644 --- a/web/js/screens.js +++ b/web/js/screens.js @@ -425,9 +425,32 @@ function syncColours() { function loadImage(file) { if (!file || !file.type.startsWith('image/')) return; + + if (state.imageUrl) { + URL.revokeObjectURL(state.imageUrl); + state.imageUrl = null; + } + + const url = URL.createObjectURL(file); + state.imageUrl = url; + const img = new Image(); - img.onload = () => { state.image = img; renderPreview(); }; - img.src = URL.createObjectURL(file); + img.onload = () => { + if (state.imageUrl !== url) return; + URL.revokeObjectURL(url); + state.imageUrl = null; + + state.image = img; + renderPreview(); + }; + img.onerror = () => { + if (state.imageUrl === url) { + URL.revokeObjectURL(url); + state.imageUrl = null; + } + }; + img.src = url; + } function setSource(source) {