react-zoom-pan-pinch 3.6.1 → 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.
Files changed (139) hide show
  1. package/dist/index.cjs.js +770 -277
  2. package/dist/index.cjs.js.map +1 -1
  3. package/dist/index.d.ts +116 -22
  4. package/dist/index.esm.js +766 -269
  5. package/dist/index.esm.js.map +1 -1
  6. package/package.json +73 -60
  7. package/src/components/index.ts +5 -0
  8. package/src/components/keep-scale/keep-scale.tsx +28 -0
  9. package/src/components/mini-map/mini-map.tsx +292 -0
  10. package/src/components/mini-map/use-resize.hook.ts +74 -0
  11. package/src/components/transform-component/transform-component.module.css +43 -0
  12. package/src/components/transform-component/transform-component.tsx +96 -0
  13. package/src/components/transform-wrapper/transform-wrapper.tsx +39 -0
  14. package/src/components/virtualize/virtualize.tsx +133 -0
  15. package/src/components/virtualize/virtualize.utils.ts +104 -0
  16. package/src/constants/state.constants.ts +99 -0
  17. package/src/core/animations/animations.constants.ts +78 -0
  18. package/src/core/animations/animations.utils.ts +115 -0
  19. package/src/core/bounds/bounds.types.ts +8 -0
  20. package/src/core/bounds/bounds.utils.ts +226 -0
  21. package/src/core/double-click/double-click.logic.ts +127 -0
  22. package/src/core/handlers/handlers.logic.ts +122 -0
  23. package/src/core/handlers/handlers.utils.ts +243 -0
  24. package/src/core/instance.core.ts +704 -0
  25. package/src/core/pan/panning.logic.ts +122 -0
  26. package/src/core/pan/panning.utils.ts +216 -0
  27. package/src/core/pan/velocity.logic.ts +187 -0
  28. package/src/core/pan/velocity.utils.ts +83 -0
  29. package/src/core/pinch/pinch.logic.ts +139 -0
  30. package/src/core/pinch/pinch.utils.ts +95 -0
  31. package/src/core/wheel/wheel.logic.ts +161 -0
  32. package/src/core/wheel/wheel.utils.ts +157 -0
  33. package/src/core/zoom/zoom.logic.ts +71 -0
  34. package/src/core/zoom/zoom.utils.ts +62 -0
  35. package/src/hooks/index.ts +5 -0
  36. package/src/hooks/use-controls.tsx +9 -0
  37. package/src/hooks/use-transform-component.tsx +30 -0
  38. package/src/hooks/use-transform-context.tsx +13 -0
  39. package/src/hooks/use-transform-effect.tsx +22 -0
  40. package/src/hooks/use-transform-init.tsx +28 -0
  41. package/src/hooks/use-zoom-pan-pinch.tsx +48 -0
  42. package/src/index.ts +6 -0
  43. package/src/models/calculations.model.ts +34 -0
  44. package/src/models/context.model.ts +218 -0
  45. package/src/models/helpers.model.ts +15 -0
  46. package/src/models/index.ts +3 -0
  47. package/src/react-app-env.d.ts +1 -0
  48. package/src/stories/assets/big-image.jpeg +0 -0
  49. package/src/stories/assets/map.jpg +0 -0
  50. package/src/stories/assets/medium-image.jpg +0 -0
  51. package/src/stories/assets/pin.svg +1 -0
  52. package/src/stories/assets/small-image.jpg +0 -0
  53. package/src/stories/assets/svg-example.svg +14 -0
  54. package/src/stories/docs/DocTable.tsx +59 -0
  55. package/src/stories/docs/HandlerCards.tsx +102 -0
  56. package/src/stories/docs/Introduction.stories.mdx +135 -0
  57. package/src/stories/docs/PropTable.tsx +116 -0
  58. package/src/stories/docs/docs.module.css +370 -0
  59. package/src/stories/docs/handlers.stories.mdx +7 -0
  60. package/src/stories/docs/handlers.tsx +111 -0
  61. package/src/stories/docs/props.stories.mdx +11 -0
  62. package/src/stories/docs/props.tsx +571 -0
  63. package/src/stories/examples/axis-lock/axis-lock.stories.mdx +35 -0
  64. package/src/stories/examples/axis-lock/example.tsx +114 -0
  65. package/src/stories/examples/bounds/bounds.data.tsx +191 -0
  66. package/src/stories/examples/bounds/bounds.stories.mdx +99 -0
  67. package/src/stories/examples/center-zoomed-out/center-zoomed-out.stories.mdx +206 -0
  68. package/src/stories/examples/cinema/cinema-data.tsx +578 -0
  69. package/src/stories/examples/cinema/cinema.stories.mdx +30 -0
  70. package/src/stories/examples/cinema/example.tsx +378 -0
  71. package/src/stories/examples/cinema/image.png +0 -0
  72. package/src/stories/examples/content/content.stories.mdx +172 -0
  73. package/src/stories/examples/content/mixed-content.data.tsx +624 -0
  74. package/src/stories/examples/content-rerendering/content-updating.stories.mdx +34 -0
  75. package/src/stories/examples/content-rerendering/example.tsx +631 -0
  76. package/src/stories/examples/controls/controls.stories.mdx +120 -0
  77. package/src/stories/examples/image/example.tsx +149 -0
  78. package/src/stories/examples/image/image.stories.mdx +46 -0
  79. package/src/stories/examples/image-annotations/example.tsx +1442 -0
  80. package/src/stories/examples/image-annotations/image-annotations.stories.mdx +39 -0
  81. package/src/stories/examples/image-responsive/example.tsx +152 -0
  82. package/src/stories/examples/image-responsive/image-responsive.stories.mdx +51 -0
  83. package/src/stories/examples/image-zoomed-out/image-zoomed-out.stories.mdx +107 -0
  84. package/src/stories/examples/initial-transform/example.tsx +233 -0
  85. package/src/stories/examples/initial-transform/initial-transform.stories.mdx +37 -0
  86. package/src/stories/examples/keep-scale/example.tsx +882 -0
  87. package/src/stories/examples/keep-scale/keep-scale.stories.mdx +30 -0
  88. package/src/stories/examples/keep-scale/map.stories.mdx +28 -0
  89. package/src/stories/examples/medical-viewer/chest-xray.tsx +246 -0
  90. package/src/stories/examples/medical-viewer/example.tsx +690 -0
  91. package/src/stories/examples/medical-viewer/medical-viewer.stories.mdx +49 -0
  92. package/src/stories/examples/mini-map/example.tsx +122 -0
  93. package/src/stories/examples/mini-map/mini-map.stories.mdx +26 -0
  94. package/src/stories/examples/miro/example.tsx +1573 -0
  95. package/src/stories/examples/miro/miro.stories.mdx +31 -0
  96. package/src/stories/examples/padding/example.tsx +147 -0
  97. package/src/stories/examples/padding/padding.stories.mdx +35 -0
  98. package/src/stories/examples/product-card/example.tsx +594 -0
  99. package/src/stories/examples/product-card/product-card.stories.mdx +29 -0
  100. package/src/stories/examples/stadium/example.tsx +1040 -0
  101. package/src/stories/examples/stadium/stadium-data.ts +224 -0
  102. package/src/stories/examples/stadium/stadium.stories.mdx +32 -0
  103. package/src/stories/examples/stress/example.tsx +59 -0
  104. package/src/stories/examples/stress/stress-test.stories.mdx +28 -0
  105. package/src/stories/examples/svg-zoom-to-element/creativity.svg +143 -0
  106. package/src/stories/examples/svg-zoom-to-element/example.tsx +95 -0
  107. package/src/stories/examples/svg-zoom-to-element/svg-zoom-to-element.stories.mdx +28 -0
  108. package/src/stories/examples/text-selection/example.tsx +221 -0
  109. package/src/stories/examples/text-selection/text-selection.stories.mdx +31 -0
  110. package/src/stories/examples/velocity/example.tsx +908 -0
  111. package/src/stories/examples/velocity/velocity.stories.mdx +35 -0
  112. package/src/stories/examples/virtualize/example.tsx +239 -0
  113. package/src/stories/examples/virtualize/virtualize.stories.mdx +84 -0
  114. package/src/stories/examples/zoom-to-element/example.tsx +219 -0
  115. package/src/stories/examples/zoom-to-element/zoom-to-element.stories.mdx +31 -0
  116. package/src/stories/hooks/use-controls.stories.mdx +86 -0
  117. package/src/stories/hooks/use-transform-component.stories.mdx +108 -0
  118. package/src/stories/hooks/use-transform-context.stories.mdx +92 -0
  119. package/src/stories/hooks/use-transform-effect.stories.mdx +114 -0
  120. package/src/stories/hooks/use-transform-init.stories.mdx +105 -0
  121. package/src/stories/types/args.types.ts +192 -0
  122. package/src/stories/utils/args.utils.ts +35 -0
  123. package/src/stories/utils/bounds-overlay.tsx +168 -0
  124. package/src/stories/utils/code.utils.tsx +22 -0
  125. package/src/stories/utils/controls.utils.tsx +347 -0
  126. package/src/stories/utils/index.ts +5 -0
  127. package/src/stories/utils/styles.module.css +223 -0
  128. package/src/stories/utils/viewer.styles.ts +34 -0
  129. package/src/styles.css +8 -0
  130. package/src/typings.d.ts +21 -0
  131. package/src/utils/calculations.utils.ts +16 -0
  132. package/src/utils/callback.utils.ts +11 -0
  133. package/src/utils/context.utils.ts +49 -0
  134. package/src/utils/event.utils.ts +38 -0
  135. package/src/utils/helpers.utils.ts +22 -0
  136. package/src/utils/index.ts +8 -0
  137. package/src/utils/ref.utils.ts +32 -0
  138. package/src/utils/state.utils.ts +77 -0
  139. package/src/utils/styles.utils.ts +49 -0
@@ -0,0 +1,908 @@
1
+ import React, { useCallback, useEffect, useRef, useState } from "react";
2
+
3
+ import { TransformWrapper, TransformComponent } from "../../../components";
4
+ import { Controls, normalizeArgs, viewerChrome } from "../../utils";
5
+ import type { ReactZoomPanPinchRef } from "../../../models/context.model";
6
+
7
+ const font = 'Inter, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif';
8
+
9
+ const CANVAS_W = 4200;
10
+ const CANVAS_H = 2800;
11
+
12
+ /* ── Procedural starfield ──────────────────────────────────── */
13
+
14
+ function hash(i: number) {
15
+ let n = i;
16
+ // eslint-disable-next-line no-bitwise
17
+ n = ((n >> 16) ^ n) * 0x45d9f3b;
18
+ // eslint-disable-next-line no-bitwise
19
+ n = ((n >> 16) ^ n) * 0x45d9f3b;
20
+ // eslint-disable-next-line no-bitwise
21
+ return ((n >> 16) ^ n) >>> 0;
22
+ }
23
+
24
+ interface Star {
25
+ x: number;
26
+ y: number;
27
+ r: number;
28
+ o: number;
29
+ }
30
+
31
+ const STARS: Star[] = Array.from({ length: 320 }, (_, i) => {
32
+ const h = hash(i + 7);
33
+ return {
34
+ x: h % CANVAS_W,
35
+ y: hash(i * 3 + 11) % CANVAS_H,
36
+ r: 0.6 + (h % 30) / 20,
37
+ o: 0.15 + (h % 60) / 100,
38
+ };
39
+ });
40
+
41
+ interface Nebula {
42
+ x: number;
43
+ y: number;
44
+ rx: number;
45
+ ry: number;
46
+ hue: number;
47
+ sat: number;
48
+ opacity: number;
49
+ }
50
+
51
+ const NEBULAE: Nebula[] = [
52
+ { x: 700, y: 500, rx: 420, ry: 280, hue: 270, sat: 65, opacity: 0.18 },
53
+ { x: 2100, y: 1400, rx: 500, ry: 320, hue: 340, sat: 60, opacity: 0.16 },
54
+ { x: 3200, y: 700, rx: 360, ry: 440, hue: 190, sat: 55, opacity: 0.2 },
55
+ { x: 1400, y: 2100, rx: 380, ry: 240, hue: 30, sat: 50, opacity: 0.14 },
56
+ { x: 3500, y: 2200, rx: 340, ry: 280, hue: 210, sat: 58, opacity: 0.17 },
57
+ { x: 500, y: 1800, rx: 280, ry: 320, hue: 150, sat: 45, opacity: 0.12 },
58
+ ];
59
+
60
+ interface CelestialObject {
61
+ x: number;
62
+ y: number;
63
+ name: string;
64
+ sub: string;
65
+ hue: number;
66
+ size: number;
67
+ }
68
+
69
+ const OBJECTS: CelestialObject[] = [
70
+ {
71
+ x: 700,
72
+ y: 500,
73
+ name: "Orion Nebula",
74
+ sub: "M42 · Emission nebula",
75
+ hue: 270,
76
+ size: 14,
77
+ },
78
+ {
79
+ x: 2100,
80
+ y: 1400,
81
+ name: "Carina Cluster",
82
+ sub: "NGC 3372 · Star cluster",
83
+ hue: 340,
84
+ size: 16,
85
+ },
86
+ {
87
+ x: 3200,
88
+ y: 700,
89
+ name: "Eagle Nebula",
90
+ sub: "M16 · Pillars of Creation",
91
+ hue: 190,
92
+ size: 12,
93
+ },
94
+ {
95
+ x: 1400,
96
+ y: 2100,
97
+ name: "Flame Nebula",
98
+ sub: "NGC 2024 · H II region",
99
+ hue: 30,
100
+ size: 10,
101
+ },
102
+ {
103
+ x: 3500,
104
+ y: 2200,
105
+ name: "Helix Nebula",
106
+ sub: "NGC 7293 · Planetary",
107
+ hue: 210,
108
+ size: 13,
109
+ },
110
+ {
111
+ x: 500,
112
+ y: 1800,
113
+ name: "Rosette Nebula",
114
+ sub: "NGC 2237 · Emission",
115
+ hue: 150,
116
+ size: 11,
117
+ },
118
+ ];
119
+
120
+ /* Constellation lines */
121
+ const CONSTELLATIONS: [number, number, number, number][] = [
122
+ [640, 460, 820, 380],
123
+ [820, 380, 960, 520],
124
+ [960, 520, 1050, 430],
125
+ [2050, 1350, 2200, 1280],
126
+ [2200, 1280, 2300, 1400],
127
+ [3140, 650, 3300, 580],
128
+ [3300, 580, 3380, 720],
129
+ [3380, 720, 3280, 800],
130
+ [1350, 2060, 1500, 1980],
131
+ [1500, 1980, 1480, 2160],
132
+ ];
133
+
134
+ /* ── Planet (CSS sphere) ───────────────────────────────────── */
135
+
136
+ function Planet(props: {
137
+ x: number;
138
+ y: number;
139
+ size: number;
140
+ hue: number;
141
+ name: string;
142
+ rings: boolean;
143
+ }) {
144
+ const { x, y, size, hue, name, rings } = props;
145
+ return (
146
+ <div style={{ position: "absolute", left: x - size, top: y - size }}>
147
+ <div
148
+ style={{
149
+ width: size * 2,
150
+ height: size * 2,
151
+ borderRadius: "50%",
152
+ background: `radial-gradient(
153
+ circle at 35% 30%,
154
+ hsl(${hue}, 55%, 72%) 0%,
155
+ hsl(${hue}, 50%, 48%) 40%,
156
+ hsl(${hue}, 45%, 22%) 80%,
157
+ hsl(${hue}, 40%, 10%) 100%
158
+ )`,
159
+ boxShadow: `
160
+ 0 0 ${size}px hsla(${hue}, 60%, 50%, 0.35),
161
+ 0 0 ${size * 3}px hsla(${hue}, 60%, 40%, 0.15)
162
+ `,
163
+ position: "relative",
164
+ }}
165
+ >
166
+ {rings && (
167
+ <div
168
+ style={{
169
+ position: "absolute",
170
+ left: -size * 0.7,
171
+ top: "50%",
172
+ width: size * 3.4,
173
+ height: size * 0.7,
174
+ marginTop: -size * 0.15,
175
+ borderRadius: "50%",
176
+ border: `2px solid hsla(${hue}, 40%, 60%, 0.3)`,
177
+ background: `linear-gradient(
178
+ 180deg,
179
+ transparent 40%,
180
+ hsla(${hue}, 35%, 55%, 0.08) 50%,
181
+ transparent 60%
182
+ )`,
183
+ transform: "rotateX(70deg)",
184
+ }}
185
+ />
186
+ )}
187
+ </div>
188
+ <div
189
+ style={{
190
+ textAlign: "center",
191
+ marginTop: 10,
192
+ fontSize: 11,
193
+ fontWeight: 600,
194
+ color: `hsla(${hue}, 50%, 80%, 0.7)`,
195
+ letterSpacing: "0.05em",
196
+ textTransform: "uppercase",
197
+ fontFamily: font,
198
+ }}
199
+ >
200
+ {name}
201
+ </div>
202
+ </div>
203
+ );
204
+ }
205
+
206
+ /* ── Space canvas ──────────────────────────────────────────── */
207
+
208
+ const SpaceCanvas = React.memo(function SpaceCanvas() {
209
+ return (
210
+ <div
211
+ style={{
212
+ position: "relative",
213
+ width: CANVAS_W,
214
+ height: CANVAS_H,
215
+ background:
216
+ "radial-gradient(ellipse 80% 60% at 40% 35%, #0c1229 0%, #060a18 50%, #020510 100%)",
217
+ overflow: "hidden",
218
+ }}
219
+ >
220
+ {/* Nebulae */}
221
+ {NEBULAE.map((n) => (
222
+ <div
223
+ key={`nebula-${n.x}-${n.y}`}
224
+ style={{
225
+ position: "absolute",
226
+ left: n.x - n.rx,
227
+ top: n.y - n.ry,
228
+ width: n.rx * 2,
229
+ height: n.ry * 2,
230
+ borderRadius: "50%",
231
+ background: `radial-gradient(
232
+ ellipse at center,
233
+ hsla(${n.hue}, ${n.sat}%, 55%, ${n.opacity}) 0%,
234
+ hsla(${n.hue}, ${n.sat}%, 40%, ${n.opacity * 0.5}) 35%,
235
+ hsla(${n.hue}, ${n.sat}%, 30%, ${n.opacity * 0.15}) 65%,
236
+ transparent 100%
237
+ )`,
238
+ filter: "blur(8px)",
239
+ pointerEvents: "none",
240
+ }}
241
+ />
242
+ ))}
243
+
244
+ {/* Stars SVG */}
245
+ <svg
246
+ width={CANVAS_W}
247
+ height={CANVAS_H}
248
+ style={{ position: "absolute", inset: 0, pointerEvents: "none" }}
249
+ aria-hidden
250
+ >
251
+ {STARS.map((s) => (
252
+ <circle
253
+ key={`star-${s.x}-${s.y}`}
254
+ cx={s.x}
255
+ cy={s.y}
256
+ r={s.r}
257
+ fill={`rgba(220, 230, 255, ${s.o})`}
258
+ />
259
+ ))}
260
+
261
+ {/* Constellation lines + vertex stars */}
262
+ {CONSTELLATIONS.map(([x1, y1, x2, y2]) => (
263
+ <g key={`${x1}-${y1}-${x2}-${y2}`}>
264
+ <line
265
+ x1={x1}
266
+ y1={y1}
267
+ x2={x2}
268
+ y2={y2}
269
+ stroke="rgba(148, 163, 184, 0.18)"
270
+ strokeWidth={0.8}
271
+ strokeDasharray="4 6"
272
+ />
273
+ <circle cx={x1} cy={y1} r={2} fill="rgba(200, 215, 240, 0.35)" />
274
+ <circle cx={x2} cy={y2} r={2} fill="rgba(200, 215, 240, 0.35)" />
275
+ </g>
276
+ ))}
277
+ </svg>
278
+
279
+ {/* Planets */}
280
+ <Planet
281
+ x={1700}
282
+ y={800}
283
+ size={48}
284
+ hue={25}
285
+ name="Kepler-442b"
286
+ rings={false}
287
+ />
288
+ <Planet
289
+ x={2900}
290
+ y={1800}
291
+ size={64}
292
+ hue={220}
293
+ name="Neptune Prime"
294
+ rings
295
+ />
296
+ <Planet
297
+ x={900}
298
+ y={1300}
299
+ size={32}
300
+ hue={0}
301
+ name="Proxima c"
302
+ rings={false}
303
+ />
304
+
305
+ {/* Celestial object labels */}
306
+ {OBJECTS.map((obj) => (
307
+ <div
308
+ key={obj.name}
309
+ style={{ position: "absolute", left: obj.x + 20, top: obj.y - 12 }}
310
+ >
311
+ <div
312
+ style={{
313
+ display: "flex",
314
+ alignItems: "center",
315
+ gap: 8,
316
+ marginBottom: 2,
317
+ }}
318
+ >
319
+ <div
320
+ style={{
321
+ width: obj.size,
322
+ height: obj.size,
323
+ borderRadius: "50%",
324
+ background: `radial-gradient(
325
+ circle at 40% 35%,
326
+ hsla(${obj.hue}, 80%, 80%, 0.9),
327
+ hsla(${obj.hue}, 70%, 50%, 0.7)
328
+ )`,
329
+ boxShadow: `0 0 ${obj.size}px hsla(${obj.hue}, 60%, 60%, 0.4)`,
330
+ flexShrink: 0,
331
+ }}
332
+ />
333
+ <span
334
+ style={{
335
+ fontSize: 14,
336
+ fontWeight: 700,
337
+ color: "rgba(226, 232, 240, 0.9)",
338
+ letterSpacing: "-0.01em",
339
+ fontFamily: font,
340
+ whiteSpace: "nowrap",
341
+ }}
342
+ >
343
+ {obj.name}
344
+ </span>
345
+ </div>
346
+ <span
347
+ style={{
348
+ fontSize: 11,
349
+ color: "rgba(148, 163, 184, 0.6)",
350
+ fontFamily: font,
351
+ marginLeft: obj.size + 8,
352
+ }}
353
+ >
354
+ {obj.sub}
355
+ </span>
356
+ </div>
357
+ ))}
358
+
359
+ {/* Center title */}
360
+ <div
361
+ style={{
362
+ position: "absolute",
363
+ left: "50%",
364
+ top: "50%",
365
+ transform: "translate(-50%, -50%)",
366
+ textAlign: "center",
367
+ pointerEvents: "none",
368
+ }}
369
+ >
370
+ <div
371
+ style={{
372
+ fontSize: 72,
373
+ fontWeight: 900,
374
+ letterSpacing: "0.2em",
375
+ fontFamily: font,
376
+ userSelect: "none",
377
+ color: "transparent",
378
+ background:
379
+ "linear-gradient(180deg, rgba(148, 163, 184, 0.18) 0%, rgba(100, 116, 139, 0.08) 100%)",
380
+ WebkitBackgroundClip: "text",
381
+ WebkitTextFillColor: "transparent",
382
+ textShadow: "0 0 60px rgba(56, 189, 248, 0.06)",
383
+ }}
384
+ >
385
+ EXPLORE
386
+ </div>
387
+ <div
388
+ style={{
389
+ fontSize: 12,
390
+ fontWeight: 500,
391
+ letterSpacing: "0.25em",
392
+ color: "rgba(148, 163, 184, 0.18)",
393
+ textTransform: "uppercase",
394
+ marginTop: 4,
395
+ }}
396
+ >
397
+ Deep space explorer
398
+ </div>
399
+ </div>
400
+ </div>
401
+ );
402
+ });
403
+
404
+ /* ── Velocity gauge (DOM-driven, no re-renders) ──────────── */
405
+
406
+ function VelocityGauge() {
407
+ const barRef = useRef<HTMLDivElement>(null);
408
+ const labelRef = useRef<HTMLSpanElement>(null);
409
+ const dotRef = useRef<HTMLDivElement>(null);
410
+ const lastRef = useRef({ x: 0, y: 0, t: 0 });
411
+ const speedRef = useRef(0);
412
+ const rafRef = useRef(0);
413
+
414
+ const updateDOM = useCallback((v: number) => {
415
+ const pct = Math.min(v / 12, 1) * 100;
416
+ if (barRef.current) {
417
+ barRef.current.style.width = `${pct}%`;
418
+ barRef.current.style.opacity = pct > 0 ? "1" : "0.3";
419
+ }
420
+ if (labelRef.current) {
421
+ labelRef.current.textContent = `${Math.round(v * 10) / 10}`;
422
+ }
423
+ if (dotRef.current) {
424
+ dotRef.current.style.boxShadow =
425
+ v > 2
426
+ ? "0 0 8px rgba(56, 189, 248, 0.6)"
427
+ : "0 0 4px rgba(56, 189, 248, 0.2)";
428
+ dotRef.current.style.background =
429
+ v > 2 ? "rgba(56, 189, 248, 0.9)" : "rgba(56, 189, 248, 0.4)";
430
+ }
431
+ }, []);
432
+
433
+ const decay = useCallback(() => {
434
+ speedRef.current *= 0.92;
435
+ if (speedRef.current < 0.5) speedRef.current = 0;
436
+ updateDOM(speedRef.current);
437
+ if (speedRef.current > 0) {
438
+ rafRef.current = requestAnimationFrame(decay);
439
+ }
440
+ }, [updateDOM]);
441
+
442
+ useEffect(() => {
443
+ return () => cancelAnimationFrame(rafRef.current);
444
+ }, []);
445
+
446
+ const onTransform = useCallback(
447
+ (
448
+ _ref: ReactZoomPanPinchRef,
449
+ st: { positionX: number; positionY: number },
450
+ ) => {
451
+ const now = performance.now();
452
+ const dt = now - lastRef.current.t;
453
+ if (dt > 0 && lastRef.current.t > 0) {
454
+ const dx = st.positionX - lastRef.current.x;
455
+ const dy = st.positionY - lastRef.current.y;
456
+ speedRef.current =
457
+ (Math.sqrt(dx * dx + dy * dy) / Math.max(dt, 1)) * 16;
458
+ }
459
+ lastRef.current = { x: st.positionX, y: st.positionY, t: now };
460
+ cancelAnimationFrame(rafRef.current);
461
+ updateDOM(speedRef.current);
462
+ rafRef.current = requestAnimationFrame(decay);
463
+ },
464
+ [decay, updateDOM],
465
+ );
466
+
467
+ return { onTransform, barRef, labelRef, dotRef };
468
+ }
469
+
470
+ /* ── Toggle pill ───────────────────────────────────────────── */
471
+
472
+ function TogglePill(props: { on: boolean; onToggle: (v: boolean) => void }) {
473
+ const { on, onToggle } = props;
474
+
475
+ return (
476
+ <div
477
+ style={{
478
+ display: "inline-flex",
479
+ borderRadius: 12,
480
+ background: "rgba(15, 23, 42, 0.85)",
481
+ border: "1px solid rgba(255,255,255,0.12)",
482
+ backdropFilter: "blur(12px)",
483
+ padding: 4,
484
+ gap: 2,
485
+ fontFamily: font,
486
+ }}
487
+ >
488
+ <button
489
+ type="button"
490
+ onClick={() => onToggle(true)}
491
+ style={{
492
+ padding: "8px 18px",
493
+ borderRadius: 9,
494
+ border: "none",
495
+ fontSize: 13,
496
+ fontWeight: 700,
497
+ fontFamily: font,
498
+ cursor: "pointer",
499
+ transition: "all 0.2s ease",
500
+ background: on
501
+ ? "linear-gradient(135deg, rgba(16, 185, 129, 0.35), rgba(52, 211, 153, 0.2))"
502
+ : "transparent",
503
+ color: on ? "#6ee7b7" : "rgba(203, 213, 225, 0.7)",
504
+ boxShadow: on
505
+ ? "0 0 0 1px rgba(52, 211, 153, 0.4) inset, 0 2px 8px rgba(16, 185, 129, 0.2)"
506
+ : "none",
507
+ display: "flex",
508
+ alignItems: "center",
509
+ gap: 7,
510
+ }}
511
+ >
512
+ <svg width="14" height="14" viewBox="0 0 14 14" fill="none">
513
+ <path
514
+ d="M2 7C2 7 4 3.5 7 3.5S12 7 12 7"
515
+ stroke="currentColor"
516
+ strokeWidth="1.5"
517
+ strokeLinecap="round"
518
+ />
519
+ <path
520
+ d="M3 9.5C4 10.5 5.5 11 7 11S10 10.5 11 9.5"
521
+ stroke="currentColor"
522
+ strokeWidth="1.2"
523
+ strokeLinecap="round"
524
+ opacity="0.5"
525
+ />
526
+ </svg>
527
+ Momentum
528
+ </button>
529
+ <button
530
+ type="button"
531
+ onClick={() => onToggle(false)}
532
+ style={{
533
+ padding: "8px 18px",
534
+ borderRadius: 9,
535
+ border: "none",
536
+ fontSize: 13,
537
+ fontWeight: 700,
538
+ fontFamily: font,
539
+ cursor: "pointer",
540
+ transition: "all 0.2s ease",
541
+ background: !on
542
+ ? "linear-gradient(135deg, rgba(239, 68, 68, 0.3), rgba(248, 113, 113, 0.18))"
543
+ : "transparent",
544
+ color: !on ? "#fca5a5" : "rgba(203, 213, 225, 0.7)",
545
+ boxShadow: !on
546
+ ? "0 0 0 1px rgba(248, 113, 113, 0.4) inset, 0 2px 8px rgba(239, 68, 68, 0.15)"
547
+ : "none",
548
+ display: "flex",
549
+ alignItems: "center",
550
+ gap: 7,
551
+ }}
552
+ >
553
+ <svg width="14" height="14" viewBox="0 0 14 14" fill="none">
554
+ <rect
555
+ x="3"
556
+ y="3"
557
+ width="8"
558
+ height="8"
559
+ rx="2"
560
+ stroke="currentColor"
561
+ strokeWidth="1.5"
562
+ />
563
+ <path
564
+ d="M6 6L8 8M8 6L6 8"
565
+ stroke="currentColor"
566
+ strokeWidth="1.3"
567
+ strokeLinecap="round"
568
+ />
569
+ </svg>
570
+ Locked
571
+ </button>
572
+ </div>
573
+ );
574
+ }
575
+
576
+ /* ── Sensitivity slider ────────────────────────────────────── */
577
+
578
+ function ParamSlider(props: {
579
+ label: string;
580
+ value: number;
581
+ min: number;
582
+ max: number;
583
+ step: number;
584
+ onChange: (v: number) => void;
585
+ display: (v: number) => string;
586
+ }) {
587
+ const { label, value, min, max, step, onChange, display } = props;
588
+ const inputId = `param-slider-${label.toLowerCase().replace(/\s+/g, "-")}`;
589
+ return (
590
+ <label
591
+ htmlFor={inputId}
592
+ style={{
593
+ display: "flex",
594
+ alignItems: "center",
595
+ gap: 10,
596
+ fontSize: 12,
597
+ fontWeight: 700,
598
+ color: "rgba(226, 232, 240, 0.85)",
599
+ fontFamily: font,
600
+ cursor: "default",
601
+ }}
602
+ >
603
+ {label}
604
+ <input
605
+ id={inputId}
606
+ type="range"
607
+ min={min}
608
+ max={max}
609
+ step={step}
610
+ value={value}
611
+ onChange={(e) => onChange(Number(e.target.value))}
612
+ style={{
613
+ width: 100,
614
+ accentColor: "#38bdf8",
615
+ cursor: "pointer",
616
+ }}
617
+ />
618
+ <span
619
+ style={{
620
+ fontSize: 11,
621
+ color: "#e2e8f0",
622
+ fontVariantNumeric: "tabular-nums",
623
+ fontWeight: 600,
624
+ minWidth: 32,
625
+ }}
626
+ >
627
+ {display(value)}
628
+ </span>
629
+ </label>
630
+ );
631
+ }
632
+
633
+ /* ── Main ──────────────────────────────────────────────────── */
634
+
635
+ const viewer: React.CSSProperties = {
636
+ ...viewerChrome,
637
+ width: "100%",
638
+ height: "clamp(480px, calc(100vh - 320px), 720px)",
639
+ };
640
+
641
+ export const Example: React.FC<Record<string, unknown>> = (args) => {
642
+ const normalized = normalizeArgs(args);
643
+ const [inertiaOn, setInertiaOn] = useState(true);
644
+ const [sensitivity, setSensitivity] = useState(1);
645
+ const [friction, setFriction] = useState(0.93);
646
+
647
+ const velocityAnimation = {
648
+ ...(normalized.velocityAnimation ?? {}),
649
+ disabled: !inertiaOn,
650
+ sensitivityMouse: sensitivity,
651
+ inertia: friction,
652
+ };
653
+
654
+ const gauge = VelocityGauge();
655
+
656
+ return (
657
+ <div style={{ fontFamily: font, maxWidth: 960 }}>
658
+ {/* ── Top control strip ────────────────────────────── */}
659
+ <div
660
+ style={{
661
+ display: "flex",
662
+ flexWrap: "wrap",
663
+ gap: 14,
664
+ marginBottom: 14,
665
+ alignItems: "center",
666
+ }}
667
+ >
668
+ <TogglePill on={inertiaOn} onToggle={setInertiaOn} />
669
+
670
+ <div
671
+ style={{
672
+ display: "flex",
673
+ flexWrap: "wrap",
674
+ gap: 16,
675
+ alignItems: "center",
676
+ padding: "8px 16px",
677
+ borderRadius: 12,
678
+ background: "rgba(15, 23, 42, 0.85)",
679
+ border: "1px solid rgba(255,255,255,0.12)",
680
+ }}
681
+ >
682
+ <ParamSlider
683
+ label="Sensitivity"
684
+ value={sensitivity}
685
+ min={0.2}
686
+ max={3}
687
+ step={0.1}
688
+ onChange={setSensitivity}
689
+ display={(v) => `${v.toFixed(1)}×`}
690
+ />
691
+ <ParamSlider
692
+ label="Friction"
693
+ value={friction}
694
+ min={0.8}
695
+ max={0.99}
696
+ step={0.01}
697
+ onChange={setFriction}
698
+ display={(v) => `${(v * 100).toFixed(0)}%`}
699
+ />
700
+ </div>
701
+ </div>
702
+
703
+ {/* ── Viewer ───────────────────────────────────────── */}
704
+ <div style={{ position: "relative" }}>
705
+ <TransformWrapper
706
+ {...normalized}
707
+ centerOnInit
708
+ limitToBounds={false}
709
+ velocityAnimation={velocityAnimation}
710
+ doubleClick={{ ...normalized.doubleClick, disabled: true }}
711
+ onTransform={gauge.onTransform}
712
+ >
713
+ {(utils) => (
714
+ <div style={{ position: "relative" }}>
715
+ <Controls {...utils} />
716
+
717
+ {/* Velocity HUD */}
718
+ <div
719
+ style={{
720
+ position: "absolute",
721
+ bottom: 14,
722
+ right: 14,
723
+ zIndex: 4,
724
+ display: "flex",
725
+ alignItems: "center",
726
+ gap: 8,
727
+ padding: "7px 14px",
728
+ borderRadius: 10,
729
+ background: "rgba(2, 6, 23, 0.75)",
730
+ border: "1px solid rgba(255,255,255,0.08)",
731
+ backdropFilter: "blur(8px)",
732
+ fontFamily: font,
733
+ fontSize: 12,
734
+ fontWeight: 600,
735
+ color: "rgba(148, 163, 184, 0.8)",
736
+ fontVariantNumeric: "tabular-nums",
737
+ }}
738
+ >
739
+ <div
740
+ ref={gauge.dotRef}
741
+ style={{
742
+ width: 8,
743
+ height: 8,
744
+ borderRadius: "50%",
745
+ background: "rgba(56, 189, 248, 0.4)",
746
+ transition: "background 0.15s, box-shadow 0.15s",
747
+ flexShrink: 0,
748
+ }}
749
+ />
750
+ <span style={{ color: "rgba(226, 232, 240, 0.65)" }}>vel</span>
751
+ <span
752
+ ref={gauge.labelRef}
753
+ style={{
754
+ color: "rgba(226, 232, 240, 0.9)",
755
+ minWidth: 24,
756
+ textAlign: "right",
757
+ }}
758
+ >
759
+ 0
760
+ </span>
761
+ <div
762
+ style={{
763
+ width: 60,
764
+ height: 4,
765
+ borderRadius: 2,
766
+ background: "rgba(255,255,255,0.06)",
767
+ overflow: "hidden",
768
+ }}
769
+ >
770
+ <div
771
+ ref={gauge.barRef}
772
+ style={{
773
+ height: "100%",
774
+ width: "0%",
775
+ borderRadius: 2,
776
+ background:
777
+ "linear-gradient(90deg, rgba(56, 189, 248, 0.7), rgba(139, 92, 246, 0.7))",
778
+ transition: "width 0.08s linear",
779
+ }}
780
+ />
781
+ </div>
782
+ </div>
783
+
784
+ <TransformComponent
785
+ wrapperStyle={viewer}
786
+ contentStyle={{ width: CANVAS_W, height: CANVAS_H }}
787
+ >
788
+ <SpaceCanvas />
789
+ </TransformComponent>
790
+ </div>
791
+ )}
792
+ </TransformWrapper>
793
+
794
+ {/* Instruction overlay */}
795
+ <div
796
+ style={{
797
+ position: "absolute",
798
+ bottom: 56,
799
+ left: "50%",
800
+ transform: "translateX(-50%)",
801
+ padding: "8px 20px",
802
+ borderRadius: 999,
803
+ background: "rgba(2, 6, 23, 0.7)",
804
+ border: "1px solid rgba(255,255,255,0.06)",
805
+ backdropFilter: "blur(8px)",
806
+ fontSize: 12,
807
+ fontWeight: 500,
808
+ color: "rgba(148, 163, 184, 0.7)",
809
+ fontFamily: font,
810
+ pointerEvents: "none",
811
+ whiteSpace: "nowrap",
812
+ zIndex: 3,
813
+ }}
814
+ >
815
+ Drag quickly and{" "}
816
+ <strong style={{ color: "rgba(226, 232, 240, 0.85)" }}>
817
+ release
818
+ </strong>{" "}
819
+ to feel the momentum
820
+ </div>
821
+ </div>
822
+
823
+ {/* ── API reference ────────────────────────────────── */}
824
+ <div
825
+ style={{
826
+ marginTop: 16,
827
+ padding: "16px 20px",
828
+ borderRadius: 14,
829
+ background: "rgba(15, 23, 42, 0.7)",
830
+ border: "1px solid rgba(56, 189, 248, 0.15)",
831
+ fontFamily: font,
832
+ display: "flex",
833
+ alignItems: "flex-start",
834
+ gap: 14,
835
+ }}
836
+ >
837
+ <span
838
+ style={{
839
+ flexShrink: 0,
840
+ width: 28,
841
+ height: 28,
842
+ borderRadius: 8,
843
+ background: "linear-gradient(135deg, #0ea5e9, #6366f1)",
844
+ display: "flex",
845
+ alignItems: "center",
846
+ justifyContent: "center",
847
+ fontSize: 13,
848
+ fontWeight: 800,
849
+ color: "#fff",
850
+ marginTop: 2,
851
+ }}
852
+ >
853
+ v
854
+ </span>
855
+ <div
856
+ style={{
857
+ display: "flex",
858
+ flexDirection: "column",
859
+ gap: 10,
860
+ fontSize: 13,
861
+ lineHeight: 1.55,
862
+ color: "#94a3b8",
863
+ }}
864
+ >
865
+ {[
866
+ {
867
+ prop: "velocityAnimation.disabled",
868
+ desc: "Enable or disable post-release momentum",
869
+ accent: "#a78bfa",
870
+ },
871
+ {
872
+ prop: "sensitivityMouse",
873
+ desc: "How much initial velocity is captured from gesture speed",
874
+ accent: "#38bdf8",
875
+ },
876
+ {
877
+ prop: "inertia",
878
+ desc: "Decay coefficient — higher values produce a longer, smoother glide",
879
+ accent: "#38bdf8",
880
+ },
881
+ ].map((item) => (
882
+ <div
883
+ key={item.prop}
884
+ style={{ display: "flex", alignItems: "baseline", gap: 8 }}
885
+ >
886
+ <code
887
+ style={{
888
+ flexShrink: 0,
889
+ padding: "2px 8px",
890
+ borderRadius: 5,
891
+ background: `${item.accent}15`,
892
+ border: `1px solid ${item.accent}30`,
893
+ fontSize: 11,
894
+ fontWeight: 600,
895
+ fontFamily: "'SF Mono', ui-monospace, monospace",
896
+ color: item.accent,
897
+ }}
898
+ >
899
+ {item.prop}
900
+ </code>
901
+ <span style={{ color: "#cbd5e1" }}>{item.desc}</span>
902
+ </div>
903
+ ))}
904
+ </div>
905
+ </div>
906
+ </div>
907
+ );
908
+ };