react-zoom-pan-pinch 3.7.0 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs.js +785 -310
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +117 -23
- package/dist/index.esm.js +781 -302
- package/dist/index.esm.js.map +1 -1
- package/package.json +73 -62
- package/src/components/index.ts +5 -0
- package/src/components/keep-scale/keep-scale.tsx +28 -0
- package/src/components/mini-map/mini-map.tsx +292 -0
- package/src/components/mini-map/use-resize.hook.ts +74 -0
- package/src/components/transform-component/transform-component.module.css +43 -0
- package/src/components/transform-component/transform-component.tsx +96 -0
- package/src/components/transform-wrapper/transform-wrapper.tsx +39 -0
- package/src/components/virtualize/virtualize.tsx +133 -0
- package/src/components/virtualize/virtualize.utils.ts +104 -0
- package/src/constants/state.constants.ts +99 -0
- package/src/core/animations/animations.constants.ts +78 -0
- package/src/core/animations/animations.utils.ts +115 -0
- package/src/core/bounds/bounds.types.ts +8 -0
- package/src/core/bounds/bounds.utils.ts +226 -0
- package/src/core/double-click/double-click.logic.ts +127 -0
- package/src/core/handlers/handlers.logic.ts +122 -0
- package/src/core/handlers/handlers.utils.ts +243 -0
- package/src/core/instance.core.ts +704 -0
- package/src/core/pan/panning.logic.ts +122 -0
- package/src/core/pan/panning.utils.ts +216 -0
- package/src/core/pan/velocity.logic.ts +187 -0
- package/src/core/pan/velocity.utils.ts +83 -0
- package/src/core/pinch/pinch.logic.ts +139 -0
- package/src/core/pinch/pinch.utils.ts +95 -0
- package/src/core/wheel/wheel.logic.ts +161 -0
- package/src/core/wheel/wheel.utils.ts +157 -0
- package/src/core/zoom/zoom.logic.ts +71 -0
- package/src/core/zoom/zoom.utils.ts +62 -0
- package/src/hooks/index.ts +5 -0
- package/src/hooks/use-controls.tsx +9 -0
- package/src/hooks/use-transform-component.tsx +30 -0
- package/src/hooks/use-transform-context.tsx +13 -0
- package/src/hooks/use-transform-effect.tsx +22 -0
- package/src/hooks/use-transform-init.tsx +28 -0
- package/src/hooks/use-zoom-pan-pinch.tsx +48 -0
- package/src/index.ts +6 -0
- package/src/models/calculations.model.ts +34 -0
- package/src/models/context.model.ts +218 -0
- package/src/models/helpers.model.ts +15 -0
- package/src/models/index.ts +3 -0
- package/src/react-app-env.d.ts +1 -0
- package/src/stories/assets/big-image.jpeg +0 -0
- package/src/stories/assets/map.jpg +0 -0
- package/src/stories/assets/medium-image.jpg +0 -0
- package/src/stories/assets/pin.svg +1 -0
- package/src/stories/assets/small-image.jpg +0 -0
- package/src/stories/assets/svg-example.svg +14 -0
- package/src/stories/docs/DocTable.tsx +59 -0
- package/src/stories/docs/HandlerCards.tsx +102 -0
- package/src/stories/docs/Introduction.stories.mdx +135 -0
- package/src/stories/docs/PropTable.tsx +116 -0
- package/src/stories/docs/docs.module.css +370 -0
- package/src/stories/docs/handlers.stories.mdx +7 -0
- package/src/stories/docs/handlers.tsx +111 -0
- package/src/stories/docs/props.stories.mdx +11 -0
- package/src/stories/docs/props.tsx +571 -0
- package/src/stories/examples/axis-lock/axis-lock.stories.mdx +35 -0
- package/src/stories/examples/axis-lock/example.tsx +114 -0
- package/src/stories/examples/bounds/bounds.data.tsx +191 -0
- package/src/stories/examples/bounds/bounds.stories.mdx +99 -0
- package/src/stories/examples/center-zoomed-out/center-zoomed-out.stories.mdx +206 -0
- package/src/stories/examples/cinema/cinema-data.tsx +578 -0
- package/src/stories/examples/cinema/cinema.stories.mdx +30 -0
- package/src/stories/examples/cinema/example.tsx +378 -0
- package/src/stories/examples/cinema/image.png +0 -0
- package/src/stories/examples/content/content.stories.mdx +172 -0
- package/src/stories/examples/content/mixed-content.data.tsx +624 -0
- package/src/stories/examples/content-rerendering/content-updating.stories.mdx +34 -0
- package/src/stories/examples/content-rerendering/example.tsx +631 -0
- package/src/stories/examples/controls/controls.stories.mdx +120 -0
- package/src/stories/examples/image/example.tsx +149 -0
- package/src/stories/examples/image/image.stories.mdx +46 -0
- package/src/stories/examples/image-annotations/example.tsx +1442 -0
- package/src/stories/examples/image-annotations/image-annotations.stories.mdx +39 -0
- package/src/stories/examples/image-responsive/example.tsx +152 -0
- package/src/stories/examples/image-responsive/image-responsive.stories.mdx +51 -0
- package/src/stories/examples/image-zoomed-out/image-zoomed-out.stories.mdx +107 -0
- package/src/stories/examples/initial-transform/example.tsx +233 -0
- package/src/stories/examples/initial-transform/initial-transform.stories.mdx +37 -0
- package/src/stories/examples/keep-scale/example.tsx +882 -0
- package/src/stories/examples/keep-scale/keep-scale.stories.mdx +30 -0
- package/src/stories/examples/keep-scale/map.stories.mdx +28 -0
- package/src/stories/examples/medical-viewer/chest-xray.tsx +246 -0
- package/src/stories/examples/medical-viewer/example.tsx +690 -0
- package/src/stories/examples/medical-viewer/medical-viewer.stories.mdx +49 -0
- package/src/stories/examples/mini-map/example.tsx +122 -0
- package/src/stories/examples/mini-map/mini-map.stories.mdx +26 -0
- package/src/stories/examples/miro/example.tsx +1573 -0
- package/src/stories/examples/miro/miro.stories.mdx +31 -0
- package/src/stories/examples/padding/example.tsx +147 -0
- package/src/stories/examples/padding/padding.stories.mdx +35 -0
- package/src/stories/examples/product-card/example.tsx +594 -0
- package/src/stories/examples/product-card/product-card.stories.mdx +29 -0
- package/src/stories/examples/stadium/example.tsx +1040 -0
- package/src/stories/examples/stadium/stadium-data.ts +224 -0
- package/src/stories/examples/stadium/stadium.stories.mdx +32 -0
- package/src/stories/examples/stress/example.tsx +59 -0
- package/src/stories/examples/stress/stress-test.stories.mdx +28 -0
- package/src/stories/examples/svg-zoom-to-element/creativity.svg +143 -0
- package/src/stories/examples/svg-zoom-to-element/example.tsx +95 -0
- package/src/stories/examples/svg-zoom-to-element/svg-zoom-to-element.stories.mdx +28 -0
- package/src/stories/examples/text-selection/example.tsx +221 -0
- package/src/stories/examples/text-selection/text-selection.stories.mdx +31 -0
- package/src/stories/examples/velocity/example.tsx +908 -0
- package/src/stories/examples/velocity/velocity.stories.mdx +35 -0
- package/src/stories/examples/virtualize/example.tsx +239 -0
- package/src/stories/examples/virtualize/virtualize.stories.mdx +84 -0
- package/src/stories/examples/zoom-to-element/example.tsx +219 -0
- package/src/stories/examples/zoom-to-element/zoom-to-element.stories.mdx +31 -0
- package/src/stories/hooks/use-controls.stories.mdx +86 -0
- package/src/stories/hooks/use-transform-component.stories.mdx +108 -0
- package/src/stories/hooks/use-transform-context.stories.mdx +92 -0
- package/src/stories/hooks/use-transform-effect.stories.mdx +114 -0
- package/src/stories/hooks/use-transform-init.stories.mdx +105 -0
- package/src/stories/types/args.types.ts +192 -0
- package/src/stories/utils/args.utils.ts +35 -0
- package/src/stories/utils/bounds-overlay.tsx +168 -0
- package/src/stories/utils/code.utils.tsx +22 -0
- package/src/stories/utils/controls.utils.tsx +347 -0
- package/src/stories/utils/index.ts +5 -0
- package/src/stories/utils/styles.module.css +223 -0
- package/src/stories/utils/viewer.styles.ts +34 -0
- package/src/styles.css +8 -0
- package/src/typings.d.ts +21 -0
- package/src/utils/calculations.utils.ts +16 -0
- package/src/utils/callback.utils.ts +11 -0
- package/src/utils/context.utils.ts +49 -0
- package/src/utils/event.utils.ts +38 -0
- package/src/utils/helpers.utils.ts +22 -0
- package/src/utils/index.ts +8 -0
- package/src/utils/ref.utils.ts +32 -0
- package/src/utils/state.utils.ts +77 -0
- package/src/utils/styles.utils.ts +49 -0
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Meta, Story, ArgsTable, Canvas } from "@storybook/blocks";
|
|
2
|
+
|
|
3
|
+
import { TransformWrapper } from "../../../components";
|
|
4
|
+
import { argsTypes } from "../../types/args.types.ts";
|
|
5
|
+
import { Example } from "./example";
|
|
6
|
+
|
|
7
|
+
export const Template = (args) => <Example {...args} />;
|
|
8
|
+
|
|
9
|
+
<Meta
|
|
10
|
+
title="Examples/Image annotations"
|
|
11
|
+
component={TransformWrapper}
|
|
12
|
+
argTypes={argsTypes}
|
|
13
|
+
/>
|
|
14
|
+
|
|
15
|
+
# Image annotations
|
|
16
|
+
|
|
17
|
+
An **image labeling tool** built on the library: pan and zoom the photo, then
|
|
18
|
+
draw **bounding boxes** (Rectangle mode) or drop **numbered pins** (Pin mode)
|
|
19
|
+
directly on the image — even while zoomed in.
|
|
20
|
+
|
|
21
|
+
In **Rectangle** mode, click and drag to draw a bounding box for object
|
|
22
|
+
detection. In **Pin** mode, click to place a point marker. After placing, fill
|
|
23
|
+
in a **title**, **description**, and choose a **label category** with
|
|
24
|
+
color-coded presets. Click any annotation on the canvas to see its full details
|
|
25
|
+
in a popup.
|
|
26
|
+
|
|
27
|
+
Coordinates are converted from **viewport space** to **content space** using the
|
|
28
|
+
live transform state, so markers stay glued to the image at all zoom levels.
|
|
29
|
+
**Focus** in the sidebar calls **`zoomToElement`** on the annotation.
|
|
30
|
+
|
|
31
|
+
<Canvas>
|
|
32
|
+
<Story name="Image annotations">
|
|
33
|
+
{(args) => <Template {...args} />}
|
|
34
|
+
</Story>
|
|
35
|
+
</Canvas>
|
|
36
|
+
|
|
37
|
+
## Component API
|
|
38
|
+
|
|
39
|
+
<ArgsTable story="Image annotations" />
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import React, { useState, useEffect, useMemo, useCallback } from "react";
|
|
2
|
+
|
|
3
|
+
import { TransformComponent, TransformWrapper } from "components";
|
|
4
|
+
import { Controls, normalizeArgs, viewerChrome } from "stories/utils";
|
|
5
|
+
import { useTransformComponent } from "../../../hooks";
|
|
6
|
+
import exampleImg from "../../assets/medium-image.jpg";
|
|
7
|
+
|
|
8
|
+
function ScaleBadge() {
|
|
9
|
+
return useTransformComponent(({ state }) => (
|
|
10
|
+
<div
|
|
11
|
+
style={{
|
|
12
|
+
position: "absolute",
|
|
13
|
+
bottom: 16,
|
|
14
|
+
right: 16,
|
|
15
|
+
zIndex: 10,
|
|
16
|
+
padding: "5px 12px",
|
|
17
|
+
borderRadius: 8,
|
|
18
|
+
background: "rgba(10, 10, 18, 0.78)",
|
|
19
|
+
backdropFilter: "blur(12px)",
|
|
20
|
+
WebkitBackdropFilter: "blur(12px)",
|
|
21
|
+
border: "1px solid rgba(255,255,255,0.1)",
|
|
22
|
+
color: "rgba(255,255,255,0.7)",
|
|
23
|
+
fontSize: 11,
|
|
24
|
+
fontWeight: 600,
|
|
25
|
+
fontFamily: "system-ui, -apple-system, sans-serif",
|
|
26
|
+
letterSpacing: "0.02em",
|
|
27
|
+
userSelect: "none",
|
|
28
|
+
pointerEvents: "none",
|
|
29
|
+
}}
|
|
30
|
+
>
|
|
31
|
+
{state.scale.toFixed(2)}x
|
|
32
|
+
</div>
|
|
33
|
+
));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export const Example: React.FC<any> = (args: any) => {
|
|
37
|
+
const src = exampleImg;
|
|
38
|
+
const alt = "example";
|
|
39
|
+
const scaleUp = true;
|
|
40
|
+
const zoomFactor = 8;
|
|
41
|
+
|
|
42
|
+
const [container, setContainer] = useState<HTMLDivElement | null>(null);
|
|
43
|
+
|
|
44
|
+
const [containerWidth, setContainerWidth] = useState<number>(0);
|
|
45
|
+
const [containerHeight, setContainerHeight] = useState<number>(0);
|
|
46
|
+
|
|
47
|
+
const [imageNaturalWidth, setImageNaturalWidth] = useState<number>(0);
|
|
48
|
+
const [imageNaturalHeight, setImageNaturalHeight] = useState<number>(0);
|
|
49
|
+
|
|
50
|
+
const imageScale = useMemo(() => {
|
|
51
|
+
if (
|
|
52
|
+
containerWidth === 0 ||
|
|
53
|
+
containerHeight === 0 ||
|
|
54
|
+
imageNaturalWidth === 0 ||
|
|
55
|
+
imageNaturalHeight === 0
|
|
56
|
+
)
|
|
57
|
+
return 0;
|
|
58
|
+
const scale = Math.min(
|
|
59
|
+
containerWidth / imageNaturalWidth,
|
|
60
|
+
containerHeight / imageNaturalHeight,
|
|
61
|
+
);
|
|
62
|
+
return scaleUp ? scale : Math.max(scale, 1);
|
|
63
|
+
}, [
|
|
64
|
+
scaleUp,
|
|
65
|
+
containerWidth,
|
|
66
|
+
containerHeight,
|
|
67
|
+
imageNaturalWidth,
|
|
68
|
+
imageNaturalHeight,
|
|
69
|
+
]);
|
|
70
|
+
|
|
71
|
+
const applySize = useCallback((width: number, height: number) => {
|
|
72
|
+
setContainerWidth(width);
|
|
73
|
+
setContainerHeight(height);
|
|
74
|
+
}, []);
|
|
75
|
+
|
|
76
|
+
useEffect(() => {
|
|
77
|
+
if (!container) return undefined;
|
|
78
|
+
|
|
79
|
+
const measure = () => {
|
|
80
|
+
const rect = container.getBoundingClientRect();
|
|
81
|
+
applySize(rect.width, rect.height);
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
measure();
|
|
85
|
+
|
|
86
|
+
const ro = new ResizeObserver((entries) => {
|
|
87
|
+
const cr = entries[0]?.contentRect;
|
|
88
|
+
if (cr) {
|
|
89
|
+
applySize(cr.width, cr.height);
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
ro.observe(container);
|
|
93
|
+
|
|
94
|
+
return () => {
|
|
95
|
+
ro.disconnect();
|
|
96
|
+
};
|
|
97
|
+
}, [container, applySize]);
|
|
98
|
+
|
|
99
|
+
const handleImageOnLoad = (image: HTMLImageElement) => {
|
|
100
|
+
setImageNaturalWidth(image.naturalWidth);
|
|
101
|
+
setImageNaturalHeight(image.naturalHeight);
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
useEffect(() => {
|
|
105
|
+
const image = new Image();
|
|
106
|
+
image.onload = () => handleImageOnLoad(image);
|
|
107
|
+
image.src = src;
|
|
108
|
+
}, [src]);
|
|
109
|
+
|
|
110
|
+
return (
|
|
111
|
+
<div
|
|
112
|
+
style={{
|
|
113
|
+
width: "100%",
|
|
114
|
+
height: "100%",
|
|
115
|
+
minHeight: 0,
|
|
116
|
+
maxWidth: "100%",
|
|
117
|
+
boxSizing: "border-box",
|
|
118
|
+
overflow: "hidden",
|
|
119
|
+
position: "relative",
|
|
120
|
+
}}
|
|
121
|
+
ref={(el: HTMLDivElement | null) => setContainer(el)}
|
|
122
|
+
>
|
|
123
|
+
{imageScale > 0 && (
|
|
124
|
+
<TransformWrapper
|
|
125
|
+
{...normalizeArgs(args)}
|
|
126
|
+
key={`${containerWidth}x${containerHeight}`}
|
|
127
|
+
initialScale={imageScale}
|
|
128
|
+
minScale={imageScale * 0.5}
|
|
129
|
+
maxScale={imageScale * zoomFactor}
|
|
130
|
+
centerOnInit
|
|
131
|
+
centerZoomedOut
|
|
132
|
+
>
|
|
133
|
+
{(utils) => (
|
|
134
|
+
<>
|
|
135
|
+
<Controls {...utils} />
|
|
136
|
+
<ScaleBadge />
|
|
137
|
+
<TransformComponent
|
|
138
|
+
wrapperStyle={{
|
|
139
|
+
...viewerChrome,
|
|
140
|
+
width: "100%",
|
|
141
|
+
height: "100%",
|
|
142
|
+
}}
|
|
143
|
+
>
|
|
144
|
+
<img alt={alt} src={src} style={{ display: "block" }} />
|
|
145
|
+
</TransformComponent>
|
|
146
|
+
</>
|
|
147
|
+
)}
|
|
148
|
+
</TransformWrapper>
|
|
149
|
+
)}
|
|
150
|
+
</div>
|
|
151
|
+
);
|
|
152
|
+
};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { Meta, Story, ArgsTable, Canvas } from "@storybook/blocks";
|
|
2
|
+
|
|
3
|
+
import { TransformComponent, TransformWrapper } from "../../../components/";
|
|
4
|
+
import { argsTypes } from "../../types/args.types";
|
|
5
|
+
import { Example } from "./example";
|
|
6
|
+
|
|
7
|
+
export const Template = (args) => (
|
|
8
|
+
<div
|
|
9
|
+
style={{
|
|
10
|
+
width: "100%",
|
|
11
|
+
maxWidth: 640,
|
|
12
|
+
height: "min(420px, 52vh)",
|
|
13
|
+
margin: "0 auto",
|
|
14
|
+
boxSizing: "border-box",
|
|
15
|
+
}}
|
|
16
|
+
>
|
|
17
|
+
<Example {...args} />
|
|
18
|
+
</div>
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
<Meta
|
|
22
|
+
title="Basic/Responsive Image"
|
|
23
|
+
component={TransformWrapper}
|
|
24
|
+
argTypes={argsTypes}
|
|
25
|
+
/>
|
|
26
|
+
|
|
27
|
+
# Responsive Image
|
|
28
|
+
|
|
29
|
+
A **responsive** image viewer that automatically scales the image to fit its
|
|
30
|
+
container. The preview is sized to stay inside the Storybook canvas (`max-width`
|
|
31
|
+
640px, `height: min(420px, 52vh)`). A **`ResizeObserver`** on the outer frame
|
|
32
|
+
remeasures when the preview area changes, so the fit scale stays correct without
|
|
33
|
+
relying only on `window` resize.
|
|
34
|
+
|
|
35
|
+
The component measures the container and the image's natural dimensions, then
|
|
36
|
+
computes a fit scale (`Math.min(containerW / naturalW, containerH / naturalH)`).
|
|
37
|
+
That scale becomes both `initialScale` and `minScale`, so the user can zoom in
|
|
38
|
+
up to 8x but can never zoom out beyond the fitted view. The `key` prop on
|
|
39
|
+
`TransformWrapper` forces a clean remount when the measured size changes — a
|
|
40
|
+
simple pattern that keeps centering correct after layout changes.
|
|
41
|
+
|
|
42
|
+
The floating badge in the corner tracks the current scale in real time via
|
|
43
|
+
`useTransformComponent`.
|
|
44
|
+
|
|
45
|
+
<Canvas>
|
|
46
|
+
<Story name="Responsive Image">{(args) => <Template {...args} />}</Story>
|
|
47
|
+
</Canvas>
|
|
48
|
+
|
|
49
|
+
## Component API
|
|
50
|
+
|
|
51
|
+
<ArgsTable story="Responsive Image" />
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { Meta, Story, ArgsTable, Canvas } from "@storybook/blocks";
|
|
2
|
+
|
|
3
|
+
import { TransformWrapper, TransformComponent } from "../../../components";
|
|
4
|
+
import { argsTypes } from "../../types/args.types.ts";
|
|
5
|
+
import { normalizeArgs, Controls, viewerChrome } from "../../utils";
|
|
6
|
+
import { useTransformComponent } from "../../../hooks";
|
|
7
|
+
|
|
8
|
+
import exampleImg from "../../assets/medium-image.jpg";
|
|
9
|
+
|
|
10
|
+
export const ScaleBadge = () => {
|
|
11
|
+
return useTransformComponent(({ state }) => (
|
|
12
|
+
<div
|
|
13
|
+
style={{
|
|
14
|
+
position: "absolute",
|
|
15
|
+
top: 16,
|
|
16
|
+
right: 16,
|
|
17
|
+
zIndex: 10,
|
|
18
|
+
padding: "5px 12px",
|
|
19
|
+
borderRadius: 8,
|
|
20
|
+
background: "rgba(10, 10, 18, 0.78)",
|
|
21
|
+
backdropFilter: "blur(12px)",
|
|
22
|
+
WebkitBackdropFilter: "blur(12px)",
|
|
23
|
+
border: "1px solid rgba(255,255,255,0.1)",
|
|
24
|
+
color: "rgba(255,255,255,0.7)",
|
|
25
|
+
fontSize: 11,
|
|
26
|
+
fontWeight: 600,
|
|
27
|
+
fontFamily: "system-ui, -apple-system, sans-serif",
|
|
28
|
+
letterSpacing: "0.02em",
|
|
29
|
+
userSelect: "none",
|
|
30
|
+
pointerEvents: "none",
|
|
31
|
+
}}
|
|
32
|
+
>
|
|
33
|
+
{state.scale.toFixed(2)}x
|
|
34
|
+
</div>
|
|
35
|
+
));
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export const Template = (args) => (
|
|
39
|
+
<div style={{ fontFamily: "system-ui, -apple-system, sans-serif" }}>
|
|
40
|
+
<TransformWrapper
|
|
41
|
+
{...normalizeArgs(args)}
|
|
42
|
+
initialScale={0.4}
|
|
43
|
+
minScale={0.2}
|
|
44
|
+
centerOnInit={true}
|
|
45
|
+
>
|
|
46
|
+
{(utils) => (
|
|
47
|
+
<>
|
|
48
|
+
<Controls {...utils} />
|
|
49
|
+
<div style={{ position: "relative", display: "inline-block" }}>
|
|
50
|
+
<TransformComponent
|
|
51
|
+
wrapperStyle={{
|
|
52
|
+
width: "500px",
|
|
53
|
+
height: "500px",
|
|
54
|
+
maxWidth: "80vw",
|
|
55
|
+
maxHeight: "75vh",
|
|
56
|
+
...viewerChrome,
|
|
57
|
+
}}
|
|
58
|
+
>
|
|
59
|
+
<img style={{ width: "100%" }} src={exampleImg} alt="" />
|
|
60
|
+
</TransformComponent>
|
|
61
|
+
<ScaleBadge />
|
|
62
|
+
</div>
|
|
63
|
+
</>
|
|
64
|
+
)}
|
|
65
|
+
</TransformWrapper>
|
|
66
|
+
<p
|
|
67
|
+
style={{
|
|
68
|
+
fontSize: "13px",
|
|
69
|
+
color: "#888",
|
|
70
|
+
marginTop: "12px",
|
|
71
|
+
maxWidth: "500px",
|
|
72
|
+
lineHeight: 1.5,
|
|
73
|
+
}}
|
|
74
|
+
>
|
|
75
|
+
The content starts at <strong>0.4x</strong> scale and can zoom down to{" "}
|
|
76
|
+
<strong>0.2x</strong>. The checkerboard background makes it easy to see
|
|
77
|
+
the zoomed-out boundaries. Use <code>initialScale</code> to set the
|
|
78
|
+
starting zoom and <code>minScale</code> to control how far the user can
|
|
79
|
+
zoom out.
|
|
80
|
+
</p>
|
|
81
|
+
</div>
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
<Meta
|
|
85
|
+
title="Basic/Zoomed Out"
|
|
86
|
+
component={TransformWrapper}
|
|
87
|
+
argTypes={argsTypes}
|
|
88
|
+
/>
|
|
89
|
+
|
|
90
|
+
# Zoomed Out Content
|
|
91
|
+
|
|
92
|
+
Start the viewport at a **reduced scale** so users see the full picture at a
|
|
93
|
+
glance and can zoom in to explore details. This pattern works well for large
|
|
94
|
+
diagrams, floor plans, and high-resolution photos where an overview-first
|
|
95
|
+
experience is more useful than starting at 1:1.
|
|
96
|
+
|
|
97
|
+
Set `initialScale` below `1` to begin zoomed out, and `minScale` to control the
|
|
98
|
+
lower bound. The library's `centerOnInit` ensures the content is centered in the
|
|
99
|
+
viewport at load time.
|
|
100
|
+
|
|
101
|
+
<Canvas>
|
|
102
|
+
<Story name="Zoomed Out">{(args) => <Template {...args} />}</Story>
|
|
103
|
+
</Canvas>
|
|
104
|
+
|
|
105
|
+
## Component API
|
|
106
|
+
|
|
107
|
+
<ArgsTable story="Zoomed Out" />
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import React, { useState } from "react";
|
|
2
|
+
|
|
3
|
+
import { TransformWrapper, TransformComponent } from "../../../components";
|
|
4
|
+
import { Controls, normalizeArgs, viewerChrome } from "../../utils";
|
|
5
|
+
import { useTransformComponent } from "../../../hooks";
|
|
6
|
+
import exampleImg from "../../assets/medium-image.jpg";
|
|
7
|
+
|
|
8
|
+
const font = "system-ui, -apple-system, sans-serif";
|
|
9
|
+
|
|
10
|
+
const viewer: React.CSSProperties = {
|
|
11
|
+
...viewerChrome,
|
|
12
|
+
width: "600px",
|
|
13
|
+
height: "440px",
|
|
14
|
+
maxWidth: "90vw",
|
|
15
|
+
maxHeight: "75vh",
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
interface Preset {
|
|
19
|
+
label: string;
|
|
20
|
+
description: string;
|
|
21
|
+
initialScale: number;
|
|
22
|
+
initialPositionX: number;
|
|
23
|
+
initialPositionY: number;
|
|
24
|
+
centerOnInit: boolean;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const PRESETS: Preset[] = [
|
|
28
|
+
{
|
|
29
|
+
label: "Centered overview",
|
|
30
|
+
description: "centerOnInit: true — scale 1, library auto-centers",
|
|
31
|
+
initialScale: 1,
|
|
32
|
+
initialPositionX: 0,
|
|
33
|
+
initialPositionY: 0,
|
|
34
|
+
centerOnInit: true,
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
label: "Zoomed out",
|
|
38
|
+
description: "initialScale: 0.4 — see the whole image with padding",
|
|
39
|
+
initialScale: 0.4,
|
|
40
|
+
initialPositionX: 0,
|
|
41
|
+
initialPositionY: 0,
|
|
42
|
+
centerOnInit: true,
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
label: "Top-left corner",
|
|
46
|
+
description: "initialScale: 2.5 — start zoomed into the top-left",
|
|
47
|
+
initialScale: 2.5,
|
|
48
|
+
initialPositionX: 0,
|
|
49
|
+
initialPositionY: 0,
|
|
50
|
+
centerOnInit: false,
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
label: "Center detail",
|
|
54
|
+
description: "initialScale: 2 — offset to center the middle region",
|
|
55
|
+
initialScale: 2,
|
|
56
|
+
initialPositionX: -460,
|
|
57
|
+
initialPositionY: -220,
|
|
58
|
+
centerOnInit: false,
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
label: "Bottom-right",
|
|
62
|
+
description: "initialScale: 2.2 — panned to the bottom-right corner",
|
|
63
|
+
initialScale: 2.2,
|
|
64
|
+
initialPositionX: -900,
|
|
65
|
+
initialPositionY: -440,
|
|
66
|
+
centerOnInit: false,
|
|
67
|
+
},
|
|
68
|
+
];
|
|
69
|
+
|
|
70
|
+
function StateBadge() {
|
|
71
|
+
return useTransformComponent(({ state }) => (
|
|
72
|
+
<div
|
|
73
|
+
style={{
|
|
74
|
+
position: "absolute",
|
|
75
|
+
bottom: 14,
|
|
76
|
+
left: 14,
|
|
77
|
+
zIndex: 10,
|
|
78
|
+
padding: "8px 12px",
|
|
79
|
+
borderRadius: 10,
|
|
80
|
+
background: "rgba(10, 10, 18, 0.88)",
|
|
81
|
+
backdropFilter: "blur(12px)",
|
|
82
|
+
WebkitBackdropFilter: "blur(12px)",
|
|
83
|
+
border: "1px solid rgba(255,255,255,0.08)",
|
|
84
|
+
fontFamily: "'SF Mono', 'Fira Code', ui-monospace, monospace",
|
|
85
|
+
fontSize: 11,
|
|
86
|
+
lineHeight: 1.7,
|
|
87
|
+
color: "rgba(255,255,255,0.6)",
|
|
88
|
+
userSelect: "none",
|
|
89
|
+
pointerEvents: "none",
|
|
90
|
+
}}
|
|
91
|
+
>
|
|
92
|
+
<div>
|
|
93
|
+
<span style={{ color: "#818cf8" }}>scale</span>:{" "}
|
|
94
|
+
{state.scale.toFixed(2)}
|
|
95
|
+
</div>
|
|
96
|
+
<div>
|
|
97
|
+
<span style={{ color: "#34d399" }}>positionX</span>:{" "}
|
|
98
|
+
{state.positionX.toFixed(0)}
|
|
99
|
+
</div>
|
|
100
|
+
<div>
|
|
101
|
+
<span style={{ color: "#f9a8d4" }}>positionY</span>:{" "}
|
|
102
|
+
{state.positionY.toFixed(0)}
|
|
103
|
+
</div>
|
|
104
|
+
</div>
|
|
105
|
+
));
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export const Example: React.FC<Record<string, unknown>> = (args) => {
|
|
109
|
+
const normalized = normalizeArgs(args);
|
|
110
|
+
const [presetIdx, setPresetIdx] = useState(0);
|
|
111
|
+
const preset = PRESETS[presetIdx];
|
|
112
|
+
|
|
113
|
+
return (
|
|
114
|
+
<div style={{ fontFamily: font }}>
|
|
115
|
+
{/* Preset selector */}
|
|
116
|
+
<div
|
|
117
|
+
style={{
|
|
118
|
+
display: "flex",
|
|
119
|
+
flexWrap: "wrap",
|
|
120
|
+
gap: 6,
|
|
121
|
+
marginBottom: 14,
|
|
122
|
+
}}
|
|
123
|
+
>
|
|
124
|
+
{PRESETS.map((p, i) => {
|
|
125
|
+
const on = presetIdx === i;
|
|
126
|
+
return (
|
|
127
|
+
<button
|
|
128
|
+
key={p.label}
|
|
129
|
+
type="button"
|
|
130
|
+
onClick={() => setPresetIdx(i)}
|
|
131
|
+
style={{
|
|
132
|
+
padding: "8px 14px",
|
|
133
|
+
borderRadius: 10,
|
|
134
|
+
border: on
|
|
135
|
+
? "1px solid rgba(129, 140, 248, 0.5)"
|
|
136
|
+
: "1px solid rgba(255,255,255,0.1)",
|
|
137
|
+
background: on
|
|
138
|
+
? "rgba(99, 102, 241, 0.18)"
|
|
139
|
+
: "rgba(255,255,255,0.04)",
|
|
140
|
+
color: on ? "#c7d2fe" : "rgba(255,255,255,0.55)",
|
|
141
|
+
fontSize: 12,
|
|
142
|
+
fontWeight: 600,
|
|
143
|
+
fontFamily: "inherit",
|
|
144
|
+
cursor: "pointer",
|
|
145
|
+
transition: "background 0.15s ease, border-color 0.15s ease",
|
|
146
|
+
}}
|
|
147
|
+
>
|
|
148
|
+
{p.label}
|
|
149
|
+
</button>
|
|
150
|
+
);
|
|
151
|
+
})}
|
|
152
|
+
</div>
|
|
153
|
+
|
|
154
|
+
{/* Config display */}
|
|
155
|
+
<div
|
|
156
|
+
style={{
|
|
157
|
+
marginBottom: 14,
|
|
158
|
+
padding: "10px 14px",
|
|
159
|
+
borderRadius: 10,
|
|
160
|
+
background: "rgba(15, 15, 20, 0.6)",
|
|
161
|
+
border: "1px solid rgba(255,255,255,0.06)",
|
|
162
|
+
display: "flex",
|
|
163
|
+
flexWrap: "wrap",
|
|
164
|
+
gap: "6px 20px",
|
|
165
|
+
alignItems: "center",
|
|
166
|
+
}}
|
|
167
|
+
>
|
|
168
|
+
<span
|
|
169
|
+
style={{
|
|
170
|
+
fontSize: 12,
|
|
171
|
+
color: "rgba(255,255,255,0.4)",
|
|
172
|
+
fontFamily: "ui-monospace, monospace",
|
|
173
|
+
}}
|
|
174
|
+
>
|
|
175
|
+
{preset.description}
|
|
176
|
+
</span>
|
|
177
|
+
<span style={{ flex: 1 }} />
|
|
178
|
+
<code
|
|
179
|
+
style={{
|
|
180
|
+
fontSize: 11,
|
|
181
|
+
color: preset.centerOnInit ? "#34d399" : "#f87171",
|
|
182
|
+
fontFamily: "ui-monospace, monospace",
|
|
183
|
+
}}
|
|
184
|
+
>
|
|
185
|
+
centerOnInit: {preset.centerOnInit ? "true" : "false"}
|
|
186
|
+
</code>
|
|
187
|
+
</div>
|
|
188
|
+
|
|
189
|
+
{/* Viewer — key forces remount on preset change */}
|
|
190
|
+
<TransformWrapper
|
|
191
|
+
key={presetIdx}
|
|
192
|
+
{...normalized}
|
|
193
|
+
initialScale={preset.initialScale}
|
|
194
|
+
initialPositionX={preset.initialPositionX}
|
|
195
|
+
initialPositionY={preset.initialPositionY}
|
|
196
|
+
centerOnInit={preset.centerOnInit}
|
|
197
|
+
minScale={0.2}
|
|
198
|
+
maxScale={5}
|
|
199
|
+
>
|
|
200
|
+
{(utils) => (
|
|
201
|
+
<div style={{ position: "relative", display: "inline-block" }}>
|
|
202
|
+
<Controls {...utils} />
|
|
203
|
+
<StateBadge />
|
|
204
|
+
<TransformComponent wrapperStyle={viewer}>
|
|
205
|
+
<img
|
|
206
|
+
src={exampleImg}
|
|
207
|
+
alt="example"
|
|
208
|
+
style={{ display: "block", width: "100%" }}
|
|
209
|
+
/>
|
|
210
|
+
</TransformComponent>
|
|
211
|
+
</div>
|
|
212
|
+
)}
|
|
213
|
+
</TransformWrapper>
|
|
214
|
+
|
|
215
|
+
<p
|
|
216
|
+
style={{
|
|
217
|
+
margin: "14px 0 0",
|
|
218
|
+
fontSize: 12,
|
|
219
|
+
color: "rgba(148, 163, 184, 0.8)",
|
|
220
|
+
lineHeight: 1.6,
|
|
221
|
+
maxWidth: 600,
|
|
222
|
+
}}
|
|
223
|
+
>
|
|
224
|
+
Switch presets above to remount the wrapper with different{" "}
|
|
225
|
+
<code style={{ color: "#c7d2fe" }}>initialScale</code> and{" "}
|
|
226
|
+
<code style={{ color: "#c7d2fe" }}>initialPositionX/Y</code> values. The
|
|
227
|
+
state badge shows live transform values. Use{" "}
|
|
228
|
+
<strong style={{ color: "#d4d4d8" }}>Reset</strong> to return to the
|
|
229
|
+
initial configuration.
|
|
230
|
+
</p>
|
|
231
|
+
</div>
|
|
232
|
+
);
|
|
233
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { Meta, Story, ArgsTable, Canvas } from "@storybook/blocks";
|
|
2
|
+
|
|
3
|
+
import { TransformWrapper } from "../../../components";
|
|
4
|
+
import { argsTypes } from "../../types/args.types.ts";
|
|
5
|
+
import { Example } from "./example";
|
|
6
|
+
|
|
7
|
+
export const Template = (args) => <Example {...args} />;
|
|
8
|
+
|
|
9
|
+
<Meta
|
|
10
|
+
title="Basic/Initial transform"
|
|
11
|
+
component={TransformWrapper}
|
|
12
|
+
argTypes={argsTypes}
|
|
13
|
+
/>
|
|
14
|
+
|
|
15
|
+
# Initial transform
|
|
16
|
+
|
|
17
|
+
Set the starting view with **`initialScale`**, **`initialPositionX`**, and
|
|
18
|
+
**`initialPositionY`**. Positions are in the same **pixel space** as the
|
|
19
|
+
transform state (`positionX` / `positionY`).
|
|
20
|
+
|
|
21
|
+
If **`centerOnInit`** is `true` (the default in many apps), the library will
|
|
22
|
+
**center** the content after mount and can override your initial translation.
|
|
23
|
+
This story sets **`centerOnInit={false}`** so the initial scale and position
|
|
24
|
+
stay exactly as configured.
|
|
25
|
+
|
|
26
|
+
Try **Reset** or **Center** from the control bar to compare the default
|
|
27
|
+
behavior helpers with your chosen initial view.
|
|
28
|
+
|
|
29
|
+
<Canvas>
|
|
30
|
+
<Story name="Initial transform">
|
|
31
|
+
{(args) => <Template {...args} />}
|
|
32
|
+
</Story>
|
|
33
|
+
</Canvas>
|
|
34
|
+
|
|
35
|
+
## Component API
|
|
36
|
+
|
|
37
|
+
<ArgsTable story="Initial transform" />
|