Skip to content

Commit 02b8fd4

Browse files
fix: joystick window resize issue on safari
1 parent 5bb35dc commit 02b8fd4

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ecctrl",
3-
"version": "1.0.49",
3+
"version": "1.0.50",
44
"author": "Erdong Chen",
55
"license": "MIT",
66
"description": "A floating rigibody character controller for R3F",

src/EcctrlJoystick.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ const JoystickComponents = (props: EcctrlJoystickProps) => {
9797
resetJoystick()
9898
}
9999

100+
// Reset window size function
101+
const onWindowResize = () => {
102+
setWindowSize({ innerHeight: window.innerHeight, innerWidth: window.innerWidth })
103+
}
104+
100105
useEffect(() => {
101106
const joystickPositionX = joystickDiv.getBoundingClientRect().x
102107
const joystickPositionY = joystickDiv.getBoundingClientRect().y
@@ -111,12 +116,12 @@ const JoystickComponents = (props: EcctrlJoystickProps) => {
111116
joystickDiv.addEventListener("touchmove", onTouchMove, { passive: false })
112117
joystickDiv.addEventListener("touchend", onTouchEnd)
113118

114-
window.addEventListener("resize", () => { setWindowSize({ innerHeight: window.innerHeight, innerWidth: window.innerWidth }) })
119+
window.visualViewport.addEventListener("resize", onWindowResize)
115120

116121
return () => {
117122
joystickDiv.removeEventListener("touchmove", onTouchMove)
118123
joystickDiv.removeEventListener("touchend", onTouchEnd)
119-
window.removeEventListener("resize", () => { })
124+
window.visualViewport.removeEventListener("resize", onWindowResize)
120125
}
121126
})
122127

0 commit comments

Comments
 (0)