@vkcha/svg-core 0.1.2 → 1.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.d.ts CHANGED
@@ -5,6 +5,18 @@ type PanZoomState = {
5
5
  };
6
6
  type PanZoomListener = (state: Readonly<PanZoomState>) => void;
7
7
  type PanZoomWheelMode = "zoom" | "pan";
8
+ type WorldGroupConfig = {
9
+ /** Custom ID for the world group (e.g., "canvasgroup"). */
10
+ id?: string;
11
+ /** Static attributes to set on the world group. */
12
+ attributes?: Record<string, string>;
13
+ /**
14
+ * Dynamic attribute that updates with zoom level.
15
+ * The callback receives the current zoom and returns the attribute value.
16
+ * Example: { "data-canvas-zoom": (zoom) => String(Math.round(zoom)) }
17
+ */
18
+ dynamicAttributes?: Record<string, (zoom: number) => string>;
19
+ };
8
20
  type PanZoomOptions = {
9
21
  /** Default: "pan" (current behavior). Figma/Mac trackpad feel often uses "pan". */
10
22
  wheelMode: PanZoomWheelMode;
@@ -17,10 +29,14 @@ type PanZoomOptions = {
17
29
  maxZoom: number;
18
30
  /** Zoom speed multiplier for wheel (higher = faster). */
19
31
  zoomSpeed: number;
32
+ /** Extra zoom speed multiplier when ctrl/cmd is held (pinch on macOS). */
33
+ pinchZoomSpeed: number;
20
34
  /** If true, invert wheel direction for zoom. */
21
35
  invertZoom: boolean;
22
36
  /** If true, invert wheel direction for pan. */
23
37
  invertPan: boolean;
38
+ /** Configuration for the world group element. */
39
+ worldGroup?: WorldGroupConfig;
24
40
  };
25
41
  /**
26
42
  * Minimal SVG "canvas" with pan/zoom.
@@ -39,7 +55,10 @@ declare class PanZoomCanvas {
39
55
  private notifyScheduled;
40
56
  private dragPointerId;
41
57
  private panStart;
58
+ private isPanning;
42
59
  private isSpaceDown;
60
+ private static readonly DRAG_THRESHOLD_PX;
61
+ private animationFrameId;
43
62
  private windowKeyDownHandler;
44
63
  private windowKeyUpHandler;
45
64
  private svgWheelHandler;
@@ -49,6 +68,15 @@ declare class PanZoomCanvas {
49
68
  private svgPointerCancelHandler;
50
69
  private svgPointerLeaveHandler;
51
70
  constructor(svg: SVGSVGElement, opts?: Partial<PanZoomOptions>);
71
+ private applyWorldGroupConfig;
72
+ /**
73
+ * Create a new <g> layer inside the world.
74
+ * Useful when you want pan/zoom only and manage your own SVG content.
75
+ */
76
+ createLayer(name?: string, opts?: {
77
+ position?: "front" | "back";
78
+ pointerEvents?: string;
79
+ }): SVGGElement;
52
80
  setOptions(next: Partial<PanZoomOptions>): void;
53
81
  /**
54
82
  * Subscribe to pan/zoom state changes (event-driven, no polling).
@@ -58,6 +86,14 @@ declare class PanZoomCanvas {
58
86
  */
59
87
  subscribe(fn: PanZoomListener): () => void;
60
88
  setState(next: Partial<PanZoomState>): void;
89
+ /**
90
+ * Animate to a target state over a duration.
91
+ * Uses linear easing by default. Pass a custom easing function for different curves.
92
+ * Example: `(t) => 1 - Math.pow(1 - t, 3)` for ease-out cubic.
93
+ */
94
+ animateTo(target: Partial<PanZoomState>, durationMs?: number, easing?: (t: number) => number): Promise<void>;
95
+ /** Stop any running animation. */
96
+ stopAnimation(): void;
61
97
  reset(): void;
62
98
  /**
63
99
  * Clean up all event listeners and resources.
@@ -125,6 +161,7 @@ type InitOptions = {
125
161
  panZoom?: Partial<PanZoomOptions>;
126
162
  culling?: boolean | CullingOptions;
127
163
  };
164
+ type WorldLayerPosition = "below-nodes" | "above-nodes";
128
165
  /**
129
166
  * SvgCore entrypoint.
130
167
  *
@@ -152,11 +189,20 @@ declare class SvgCore {
152
189
  get svg(): SVGSVGElement;
153
190
  /** World layer (<g>) that you draw into. */
154
191
  get world(): SVGGElement;
192
+ /**
193
+ * Create a custom <g> layer inside the world.
194
+ * Useful when you want to add your own SVG content.
195
+ */
196
+ createWorldLayer(name?: string, opts?: {
197
+ position?: WorldLayerPosition;
198
+ pointerEvents?: string;
199
+ }): SVGGElement;
155
200
  /** Current pan/zoom state. */
156
201
  get state(): PanZoomState;
157
202
  /** Current pan/zoom options (includes minZoom/maxZoom). */
158
203
  get panZoomOptions(): Readonly<PanZoomOptions>;
159
204
  constructor(svg: SVGSVGElement, opts?: InitOptions);
205
+ constructor(canvas: PanZoomCanvas, opts?: InitOptions);
160
206
  /**
161
207
  * Set zoom while keeping a chosen screen-space anchor stable.
162
208
  * By default anchors at the viewport center.
@@ -231,4 +277,4 @@ type FragmentMetrics = {
231
277
  declare function measureFragmentMetrics(markup: string): FragmentMetrics | null;
232
278
 
233
279
  export { Node, PanZoomCanvas, SvgCore, measureFragmentMetrics };
234
- export type { CullingOptions, CullingStats, FragmentMetrics, InitOptions, NodeId, NodeOptions, PanZoomOptions, PanZoomState, PanZoomWheelMode };
280
+ export type { CullingOptions, CullingStats, FragmentMetrics, InitOptions, NodeId, NodeOptions, PanZoomOptions, PanZoomState, PanZoomWheelMode, WorldGroupConfig, WorldLayerPosition };