Skip to content

Commit

Permalink
fix: support for variable sized sprites (#1678)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
netgfx authored Oct 9, 2023
1 parent 6a88b88 commit bc12fe0
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/core/SpriteAnimator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ export const SpriteAnimator: React.FC<SpriteAnimatorProps> = (

const calculateAspectRatio = (width: number, height: number): Vector3 => {
const aspectRatio = height / width
spriteRef.current.scale.set(1, aspectRatio, 1)
if (spriteRef.current) {
spriteRef.current.scale.set(1, aspectRatio, 1)
}
return [1, aspectRatio, 1]
}

Expand Down Expand Up @@ -125,6 +127,12 @@ export const SpriteAnimator: React.FC<SpriteAnimatorProps> = (
currentFrame.current = 0
currentFrameName.current = frameName
hasEnded.current = false
modifySpritePosition()
if (spriteData.current) {
const { w, h } = getFirstItem(spriteData.current.frames).sourceSize
const _aspect = calculateAspectRatio(w, h)
setAspect(_aspect)
}
}
}, [frameName])

Expand Down Expand Up @@ -326,7 +334,7 @@ export const SpriteAnimator: React.FC<SpriteAnimatorProps> = (
return param[0]
} else if (typeof param === 'object' && param !== null) {
const keys = Object.keys(param)
return param[keys[0]][0]
return frameName ? param[frameName][0] : param[keys[0]][0]
} else {
return { w: 0, h: 0 }
}
Expand Down

1 comment on commit bc12fe0

@vercel
Copy link

@vercel vercel bot commented on bc12fe0 Oct 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.