-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #98. Allows zooming via: - Pinch on mobile devices (this was a tough one because it required simultaneous zooming and panning, but only if `pan` hasn't been set to false). - Scrolling on desktop (but to avoid interrupting scrolling, there is a 500ms timeout where Mafs waits for scroll idle). - Pinching on desktop (like with a trackpad). This introduces a new matrix into Mafs' pile of matrices, `camera`, which is used to transform `(xMin, yMin)` and `(xMax, yMax)`. This counterintuitively means that translations and scaling are reversed—if `camera` has a scale of 2, that's equivalent to being "zoomed 0.5x".
- Loading branch information
1 parent
10c1deb
commit 7051e5a
Showing
16 changed files
with
344 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
docs/components/guide-examples/display/viewbox/ZoomExample.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Mafs, Coordinates, Circle, Text } from "mafs" | ||
|
||
export default function ZoomExample() { | ||
return ( | ||
<Mafs | ||
zoom={{ min: 0.1, max: 2 }} | ||
viewBox={{ | ||
x: [-0.25, 0.25], | ||
y: [-0.25, 0.25], | ||
padding: 0, | ||
}} | ||
height={400} | ||
> | ||
<Coordinates.Cartesian subdivisions={5} /> | ||
<Circle center={[0, 0]} radius={1} /> | ||
<Text x={1.1} y={0.1} attach="ne"> | ||
Oh hi! | ||
</Text> | ||
</Mafs> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+5.95 KB
...snapshots/guide-examples-display-viewbox-ZoomExample-1-Mobile-Chrome-darwin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+9.87 KB
...snapshots/guide-examples-display-viewbox-ZoomExample-1-Mobile-Safari-darwin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+11.7 KB
....tsx-snapshots/guide-examples-display-viewbox-ZoomExample-1-chromium-darwin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+33.7 KB
...c.tsx-snapshots/guide-examples-display-viewbox-ZoomExample-1-firefox-darwin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+26.6 KB
...ec.tsx-snapshots/guide-examples-display-viewbox-ZoomExample-1-webkit-darwin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import * as React from "react" | ||
import { clamp } from "../math" | ||
import { vec } from "../vec" | ||
|
||
export function useCamera({ minZoom, maxZoom }: { minZoom: number; maxZoom: number }) { | ||
const [matrix, setMatrix] = React.useState<vec.Matrix>(vec.identity) | ||
const initialMatrix = React.useRef<vec.Matrix>(vec.identity) | ||
|
||
return { | ||
matrix: matrix, | ||
setBase() { | ||
initialMatrix.current = matrix | ||
}, | ||
move({ zoom, pan }: { zoom?: { at: vec.Vector2; scale?: number }; pan?: vec.Vector2 }) { | ||
const scale = 1 / (zoom?.scale ?? 1) | ||
const zoomAt = zoom?.at ?? [0, 0] | ||
|
||
const currentScale = initialMatrix.current[0] | ||
const minScale = 1 / maxZoom / currentScale | ||
const maxScale = 1 / minZoom / currentScale | ||
|
||
/** | ||
* Represents the amount of scaling to apply such that we never exceed the | ||
* minimum or maximum zoom level. | ||
*/ | ||
const clampedScale = clamp(scale, minScale, maxScale) | ||
|
||
const newCamera = vec | ||
.matrixBuilder(initialMatrix.current) | ||
.translate(...vec.scale(zoomAt, -1)) | ||
.scale(clampedScale, clampedScale) | ||
.translate(...vec.scale(zoomAt, 1)) | ||
.translate(...(pan ?? [0, 0])) | ||
.get() | ||
|
||
setMatrix(newCamera) | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import * as React from "react" | ||
|
||
/** | ||
* A custom hook that makes the `wheel` event not interrupt scrolling. It will | ||
* only allow the Mafs viewport to be zoomed using the wheel if the user hasn't | ||
* scrolled the page for 500ms, or if they are hovering over the Mafs viewport. | ||
*/ | ||
export function useWheelEnabler(zoomEnabled: boolean) { | ||
const [wheelEnabled, setWheelEnabled] = React.useState(false) | ||
|
||
const timer = React.useRef<number>(0) | ||
|
||
React.useEffect(() => { | ||
if (!zoomEnabled) return | ||
|
||
function handleWindowScroll() { | ||
setWheelEnabled(false) | ||
|
||
clearTimeout(timer.current) | ||
timer.current = setTimeout(() => { | ||
setWheelEnabled(true) | ||
}, 500) as unknown as number | ||
} | ||
|
||
window.addEventListener("scroll", handleWindowScroll) | ||
return () => window.removeEventListener("scroll", handleWindowScroll) | ||
}, [zoomEnabled]) | ||
|
||
return { | ||
wheelEnabled: zoomEnabled ? wheelEnabled : false, | ||
handleMouseMove() { | ||
setWheelEnabled(true) | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
7051e5a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
mafs – ./
mafs-stevenpetryk.vercel.app
mafs-git-main-stevenpetryk.vercel.app
mafs-docs.vercel.app
www.mafs.dev
mafs.dev