@lancercomet/zoom-pan 0.1.1 → 0.1.3

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.
@@ -0,0 +1,68 @@
1
+ import type { ICommand } from './types';
2
+ /**
3
+ * Interface for objects that can capture and restore snapshots.
4
+ * CanvasLayer implements this interface.
5
+ */
6
+ interface ISnapshotable {
7
+ captureSnapshot(region?: ISnapshotRegion): ImageData | null;
8
+ restoreSnapshot(data: ImageData, position?: {
9
+ x: number;
10
+ y: number;
11
+ }): void;
12
+ }
13
+ interface ISnapshotRegion {
14
+ x: number;
15
+ y: number;
16
+ width: number;
17
+ height: number;
18
+ }
19
+ interface SnapshotCommandOptions {
20
+ /**
21
+ * The region of the snapshot. If not provided, uses full canvas.
22
+ */
23
+ region?: ISnapshotRegion;
24
+ }
25
+ /**
26
+ * A generic command that stores before/after snapshots for undo/redo.
27
+ * This command is decoupled from any specific layer implementation.
28
+ */
29
+ declare class SnapshotCommand implements ICommand {
30
+ readonly type = "snapshot";
31
+ private readonly _target;
32
+ private readonly _beforeData;
33
+ private readonly _afterData;
34
+ private readonly _region?;
35
+ private _isExecuted;
36
+ /**
37
+ * Create a snapshot command.
38
+ *
39
+ * @param target - The object to restore snapshots to (must implement ISnapshotable)
40
+ * @param beforeData - The snapshot before the change
41
+ * @param afterData - The snapshot after the change
42
+ * @param options - Optional configuration
43
+ */
44
+ constructor(target: ISnapshotable, beforeData: ImageData, afterData: ImageData, options?: SnapshotCommandOptions);
45
+ execute(): void;
46
+ undo(): void;
47
+ canMerge(): boolean;
48
+ merge(): ICommand;
49
+ }
50
+ /**
51
+ * Helper function to create a snapshot command.
52
+ *
53
+ * Usage:
54
+ * ```typescript
55
+ * // Before starting the action
56
+ * const before = layer.captureSnapshot()
57
+ *
58
+ * // ... perform some drawing ...
59
+ *
60
+ * // After the action is complete
61
+ * const after = layer.captureSnapshot()
62
+ * const command = createSnapshotCommand(layer, before, after)
63
+ * historyManager.addCommand(command)
64
+ * ```
65
+ */
66
+ declare function createSnapshotCommand(target: ISnapshotable, beforeData: ImageData | null, afterData: ImageData | null, options?: SnapshotCommandOptions): SnapshotCommand | null;
67
+ export { SnapshotCommand, createSnapshotCommand };
68
+ export type { ISnapshotable, ISnapshotRegion, SnapshotCommandOptions };
@@ -0,0 +1,9 @@
1
+ type CommandType = 'snapshot' | string;
2
+ interface ICommand {
3
+ readonly type: CommandType;
4
+ execute(): void;
5
+ undo(): void;
6
+ canMerge?(other: ICommand): boolean;
7
+ merge?(other: ICommand): ICommand;
8
+ }
9
+ export type { CommandType, ICommand };
@@ -0,0 +1,4 @@
1
+ export * from './types';
2
+ export * from './interaction';
3
+ export * from './document';
4
+ export * from './history';
@@ -0,0 +1,76 @@
1
+ import type { ViewManager } from '../../core/view-manager';
2
+ import type { Plugin } from '../types';
3
+ interface InteractionPluginOptions {
4
+ /**
5
+ * Enable pan (drag to move). Default true.
6
+ */
7
+ panEnabled?: boolean;
8
+ /**
9
+ * Enable zoom (wheel to scale). Default true.
10
+ */
11
+ zoomEnabled?: boolean;
12
+ /**
13
+ * Friction for inertia (per frame). Default 0.92.
14
+ */
15
+ friction?: number;
16
+ /**
17
+ * Stop speed threshold (px/ms). Default 20/1000.
18
+ */
19
+ stopSpeed?: number;
20
+ /**
21
+ * EMA alpha for velocity smoothing. Default 0.25.
22
+ */
23
+ emaAlpha?: number;
24
+ /**
25
+ * Idle time (ms) before clearing inertia on pointer up. Default 120.
26
+ */
27
+ idleNoInertiaMs?: number;
28
+ /**
29
+ * Wheel sensitivity (pixel -> log step multiplier). Default 0.0015.
30
+ */
31
+ wheelSensitivity?: number;
32
+ }
33
+ /**
34
+ * Interaction Plugin
35
+ *
36
+ * Handles pan (drag), zoom (wheel), and inertia.
37
+ * Can be enabled/disabled at runtime.
38
+ */
39
+ declare class InteractionPlugin implements Plugin {
40
+ readonly name = "interaction";
41
+ private _view;
42
+ private _options;
43
+ private _panEnabled;
44
+ private _zoomEnabled;
45
+ private _dragging;
46
+ private _vx;
47
+ private _vy;
48
+ private _lastMoveTs;
49
+ private _activePointerId;
50
+ private _onDownBound;
51
+ private _onMoveBound;
52
+ private _onUpBound;
53
+ private _onWheelBound;
54
+ constructor(options?: InteractionPluginOptions);
55
+ install(view: ViewManager): void;
56
+ destroy(): void;
57
+ isPanEnabled(): boolean;
58
+ isZoomEnabled(): boolean;
59
+ setPanEnabled(enabled: boolean): void;
60
+ setZoomEnabled(enabled: boolean): void;
61
+ setWheelSensitivity(s: number): void;
62
+ isDragging(): boolean;
63
+ private _onUpdate;
64
+ private _onPointerDown;
65
+ private _onPointerMove;
66
+ private _onPointerUp;
67
+ private _getLineHeightPx;
68
+ private _normalizeWheelDelta;
69
+ private _onWheel;
70
+ }
71
+ /**
72
+ * Create an interaction plugin instance.
73
+ */
74
+ declare function createInteractionPlugin(options?: InteractionPluginOptions): InteractionPlugin;
75
+ export { InteractionPlugin, createInteractionPlugin };
76
+ export type { InteractionPluginOptions };
@@ -0,0 +1,29 @@
1
+ import type { ViewManager } from '../core/view-manager';
2
+ /**
3
+ * Plugin interface for ViewManager.
4
+ * Plugins can extend the core functionality with additional features.
5
+ */
6
+ interface Plugin {
7
+ /**
8
+ * Unique name of the plugin.
9
+ */
10
+ readonly name: string;
11
+ /**
12
+ * Called when the plugin is installed on a ViewManager instance.
13
+ * @param view The ViewManager instance
14
+ */
15
+ install(view: ViewManager): void;
16
+ /**
17
+ * Called when the plugin is uninstalled or the ViewManager is destroyed.
18
+ */
19
+ destroy(): void;
20
+ }
21
+ /**
22
+ * Plugin constructor type for creating plugin instances.
23
+ */
24
+ type PluginConstructor<T extends Plugin = Plugin, O = unknown> = new (options?: O) => T;
25
+ /**
26
+ * Plugin factory function type.
27
+ */
28
+ type PluginFactory<T extends Plugin = Plugin, O = unknown> = (options?: O) => T;
29
+ export type { Plugin, PluginConstructor, PluginFactory };
@@ -0,0 +1,4 @@
1
+ type AnchorType = 'topLeft' | 'center';
2
+ type SpaceType = 'world' | 'screen';
3
+ type BlendMode = GlobalCompositeOperation;
4
+ export type { AnchorType, SpaceType, BlendMode };
@@ -0,0 +1,3 @@
1
+ declare const loadImage: (src: string | File | Blob, crossOrigin?: "" | "anonymous" | "use-credentials") => Promise<HTMLImageElement>;
2
+ declare const clamp: (value: number, min: number, max: number) => number;
3
+ export { clamp, loadImage };
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@lancercomet/zoom-pan",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "Yet another web 2D rendering lib.",
5
- "main": "./dist/index.umd.js",
6
- "module": "./dist/index.mjs",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.module.js",
7
7
  "files": [
8
8
  "dist/**/*",
9
9
  "README.md"
@@ -11,7 +11,7 @@
11
11
  "scripts": {
12
12
  "dev": "vite",
13
13
  "build:example": "vite build --mode example",
14
- "build:lib": "vite build --mode lib"
14
+ "build:lib": "rollup -c"
15
15
  },
16
16
  "keywords": [],
17
17
  "author": "",
@@ -20,6 +20,10 @@
20
20
  "@lancercomet/eslint-config-eslint-rules": "^0.2.0",
21
21
  "@rollup/plugin-terser": "^0.4.4",
22
22
  "@vitejs/plugin-vue-jsx": "^5.1.1",
23
+ "rollup": "^4.53.3",
24
+ "rollup-plugin-commonjs": "^10.1.0",
25
+ "rollup-plugin-delete": "^2.0.0",
26
+ "rollup-plugin-typescript2": "^0.31.2",
23
27
  "stylus": "^0.64.0",
24
28
  "terser": "^5.44.1",
25
29
  "typescript": "^5.9.2",