Skip to content

Commit

Permalink
Fix issues on DPR or browser zoom changes (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
RodrigoHamuy authored Oct 24, 2023
1 parent 5c66947 commit 748d7a7
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/gold-numbers-march.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-three-map": patch
---

Fix issues on DPR or browser zoom changes.
10 changes: 2 additions & 8 deletions src/core/use-on-add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,9 @@ export function useOnAdd(
})

const onResize = useFunction(() => {
if (!r3mRef.current.map) return;
if (!r3mRef.current.state) return;
const canvas = r3mRef.current.map.getCanvas();
r3mRef.current.state.setSize(
canvas.clientWidth,
canvas.clientHeight,
false
);

// because we update size out of zustand, we just want to let it know that something changed
r3mRef.current.state.set({});
})

const onRemove = useFunction(() => {
Expand Down
2 changes: 2 additions & 0 deletions src/core/use-render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export function useRender(
const render = useFunction((_gl: WebGL2RenderingContext, mapCamMx: number[]) => {
const r3m = r3mRef.current;
if (!r3m.state || !r3m.map) return;
r3m.state.size.width = r3m.state.gl.domElement.clientWidth;
r3m.state.size.height = r3m.state.gl.domElement.clientHeight;
const camera = r3m.state.camera;
const gl = r3m.state.gl;
const advance = r3m.state.advance;
Expand Down
3 changes: 2 additions & 1 deletion stories/src/screen-sizer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ const worldPos = new Vector3();
export const ScreenSizer = memo<Omit<Object3DProps, 'scale'> & {scale?: number}>(({
scale = 1, ...props
}) => {
const container = useRef<Object3D>(null as any);
const container = useRef<Object3D>(null);

useFrame((state) => {
const obj = container.current;
if(!obj) return;
const sf = calculateScaleFactor(obj.getWorldPosition(worldPos), scale, state.camera, state.size);
obj.scale.setScalar(sf * scale);
});
Expand Down

0 comments on commit 748d7a7

Please sign in to comment.