Skip to content

Commit 748d7a7

Browse files
authored
Fix issues on DPR or browser zoom changes (#74)
1 parent 5c66947 commit 748d7a7

File tree

4 files changed

+11
-9
lines changed

4 files changed

+11
-9
lines changed

.changeset/gold-numbers-march.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"react-three-map": patch
3+
---
4+
5+
Fix issues on DPR or browser zoom changes.

src/core/use-on-add.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,9 @@ export function useOnAdd(
7474
})
7575

7676
const onResize = useFunction(() => {
77-
if (!r3mRef.current.map) return;
7877
if (!r3mRef.current.state) return;
79-
const canvas = r3mRef.current.map.getCanvas();
80-
r3mRef.current.state.setSize(
81-
canvas.clientWidth,
82-
canvas.clientHeight,
83-
false
84-
);
85-
78+
// because we update size out of zustand, we just want to let it know that something changed
79+
r3mRef.current.state.set({});
8680
})
8781

8882
const onRemove = useFunction(() => {

src/core/use-render.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ export function useRender(
1111
const render = useFunction((_gl: WebGL2RenderingContext, mapCamMx: number[]) => {
1212
const r3m = r3mRef.current;
1313
if (!r3m.state || !r3m.map) return;
14+
r3m.state.size.width = r3m.state.gl.domElement.clientWidth;
15+
r3m.state.size.height = r3m.state.gl.domElement.clientHeight;
1416
const camera = r3m.state.camera;
1517
const gl = r3m.state.gl;
1618
const advance = r3m.state.advance;

stories/src/screen-sizer.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ const worldPos = new Vector3();
88
export const ScreenSizer = memo<Omit<Object3DProps, 'scale'> & {scale?: number}>(({
99
scale = 1, ...props
1010
}) => {
11-
const container = useRef<Object3D>(null as any);
11+
const container = useRef<Object3D>(null);
1212

1313
useFrame((state) => {
1414
const obj = container.current;
15+
if(!obj) return;
1516
const sf = calculateScaleFactor(obj.getWorldPosition(worldPos), scale, state.camera, state.size);
1617
obj.scale.setScalar(sf * scale);
1718
});

0 commit comments

Comments
 (0)