diff --git a/src/core/useSpriteLoader.tsx b/src/core/useSpriteLoader.tsx index a6eca0685..14cf8f1e8 100644 --- a/src/core/useSpriteLoader.tsx +++ b/src/core/useSpriteLoader.tsx @@ -90,13 +90,20 @@ export function useSpriteLoader( } function loadStandaloneSprite(textureUrl?: string) { - if (textureUrl || input) { - new Promise((resolve) => { - textureLoader.load(textureUrl ?? input!, resolve) - }).then((texture) => { - parseSpriteData(null, texture) - }) + if (!textureUrl && !input) { + throw new Error('Either textureUrl or input must be provided') } + + const validUrl = textureUrl ?? input + if (!validUrl) { + throw new Error('A valid texture URL must be provided') + } + + new Promise((resolve) => { + textureLoader.load(validUrl, resolve) + }).then((texture) => { + parseSpriteData(null, texture) + }) } /** @@ -203,7 +210,11 @@ export function useSpriteLoader( const getRowsAndColumns = (texture: THREE.Texture, totalFrames: number) => { if (texture.image) { const canvas = document.createElement('canvas') - const ctx = canvas.getContext('2d')! + const ctx = canvas.getContext('2d') + + if (!ctx) { + throw new Error('Failed to get 2d context') + } canvas.width = texture.image.width canvas.height = texture.image.height