Skip to content

Commit bc12fe0

Browse files
author
Michael Dobekidis
authoredOct 9, 2023
fix: support for variable sized sprites (#1678)
* feat: added optional Sprite or Plane geometry for SpriteAnimator * fix: support for variable sized sprites * fix: added extra checks to change frames with variable size
1 parent 6a88b88 commit bc12fe0

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed
 

‎src/core/SpriteAnimator.tsx

+10-2
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ export const SpriteAnimator: React.FC<SpriteAnimatorProps> = (
8686

8787
const calculateAspectRatio = (width: number, height: number): Vector3 => {
8888
const aspectRatio = height / width
89-
spriteRef.current.scale.set(1, aspectRatio, 1)
89+
if (spriteRef.current) {
90+
spriteRef.current.scale.set(1, aspectRatio, 1)
91+
}
9092
return [1, aspectRatio, 1]
9193
}
9294

@@ -125,6 +127,12 @@ export const SpriteAnimator: React.FC<SpriteAnimatorProps> = (
125127
currentFrame.current = 0
126128
currentFrameName.current = frameName
127129
hasEnded.current = false
130+
modifySpritePosition()
131+
if (spriteData.current) {
132+
const { w, h } = getFirstItem(spriteData.current.frames).sourceSize
133+
const _aspect = calculateAspectRatio(w, h)
134+
setAspect(_aspect)
135+
}
128136
}
129137
}, [frameName])
130138

@@ -326,7 +334,7 @@ export const SpriteAnimator: React.FC<SpriteAnimatorProps> = (
326334
return param[0]
327335
} else if (typeof param === 'object' && param !== null) {
328336
const keys = Object.keys(param)
329-
return param[keys[0]][0]
337+
return frameName ? param[frameName][0] : param[keys[0]][0]
330338
} else {
331339
return { w: 0, h: 0 }
332340
}

1 commit comments

Comments
 (1)

vercel[bot] commented on Oct 9, 2023

@vercel[bot]
Please sign in to comment.