@lancercomet/zoom-pan 0.1.5 → 0.2.1

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/README.md CHANGED
@@ -1,25 +1,16 @@
1
1
  # ZoomPan
2
2
 
3
- A lightweight **camera & input control core** for 2D rendering systems.
4
- It provides smooth **panning**, **zooming**, and **inertia** behaviors — completely independent of any rendering engine.
3
+ A lightweight **canvas viewport control library** for 2D applications.
4
+ Provides smooth **panning**, **zooming**, **pinch-to-zoom**, and **inertia** behaviors.
5
5
 
6
- You can think of it as a **camera control module** that separates *input dynamics* from *rendering logic*, giving you full control over how the view moves.
6
+ ## Features
7
7
 
8
- Although it currently uses **Canvas2D** to render images, it can technically work with any rendering engine such as **Pixi.js** or others.
9
-
10
- I built this to replace **Fabric.js** in my own project.
11
-
12
- ## Design Goals
13
-
14
- | Goal | Description |
15
- |------|--------------|
16
- | **Independent coordinate space** | Keeps world coordinates separate from screen space |
17
- | **Smooth and physical motion** | Uses exponential decay and EMA (Exponential Moving Average) for velocity smoothing |
18
- | **Engine-agnostic** | Works without depending on any specific graphics library |
19
- | **Continuous motion** | Supports inertia and smooth decay after user input ends |
20
- | **Predictable zooming** | Logarithmic interpolation ensures consistent zoom behavior |
21
- | **Touch-friendly** | Supports both mouse and multi-touch pinch gestures |
22
- | **Layer-based system** | Designed like Photoshop-style layers for world and UI separation |
8
+ - Mouse pan & wheel zoom
9
+ - Touch pan & pinch-to-zoom (always works, even in drawing mode)
10
+ - Pen/stylus support for drawing apps
11
+ - Layer-based rendering system
12
+ - Built-in undo/redo support
13
+ - Document bounds with margins
23
14
 
24
15
  ## Installation
25
16
 
@@ -29,359 +20,206 @@ npm install @lancercomet/zoom-pan
29
20
 
30
21
  ## Quick Start
31
22
 
32
- ### Basic Viewer (Read-only)
23
+ ### Image Viewer
33
24
 
34
25
  ```typescript
35
- import { ViewManager, ContentLayerManager } from '@lancercomet/zoom-pan'
26
+ import { ViewManager, ContentLayerManager, createInteractionPlugin } from '@lancercomet/zoom-pan'
36
27
 
37
- // Create layer manager
38
28
  const layerManager = new ContentLayerManager()
39
29
 
40
- // Initialize view
41
- const view = new ViewManager(
42
- canvas,
43
- (view) => {
44
- layerManager.renderAllLayersIn(view)
45
- },
46
- {
47
- minZoom: 0.2,
48
- maxZoom: 10,
49
- background: '#ffffff'
50
- }
51
- )
52
-
53
- // Register layer manager
30
+ const view = new ViewManager(canvas, () => {
31
+ layerManager.renderAllLayersIn(view)
32
+ })
33
+
54
34
  view.registerLayerManager(layerManager)
35
+ view.use(createInteractionPlugin())
55
36
 
56
- // Add an image layer
37
+ // Add an image
57
38
  await layerManager.createImageLayer({
58
- src: 'path/to/image.png',
39
+ src: 'image.png',
59
40
  x: 0,
60
41
  y: 0
61
42
  })
62
43
  ```
63
44
 
64
- ### Painter with Undo/Redo
45
+ ### Drawing App
65
46
 
66
47
  ```typescript
67
- import {
68
- ViewManager,
69
- ContentLayerManager,
48
+ import {
49
+ ViewManager,
50
+ ContentLayerManager,
70
51
  CanvasLayer,
71
52
  HistoryManager,
72
- createSnapshotCommand,
73
53
  createInteractionPlugin,
74
- createDocumentPlugin
54
+ createDocumentPlugin,
55
+ createSnapshotCommand
75
56
  } from '@lancercomet/zoom-pan'
76
57
 
77
58
  const layerManager = new ContentLayerManager()
78
59
  const historyManager = new HistoryManager({ maxHistorySize: 50 })
79
60
 
80
- const view = new ViewManager(canvas, (view) => {
61
+ const view = new ViewManager(canvas, () => {
81
62
  layerManager.renderAllLayersIn(view)
82
63
  })
83
64
 
84
65
  view.registerLayerManager(layerManager)
85
66
 
86
- // Use plugins for interaction
87
- const interactionPlugin = createInteractionPlugin()
88
- view.use(interactionPlugin)
67
+ // Interaction plugin: controls pan/zoom
68
+ // - Touch gestures (pan/pinch) always work
69
+ // - Mouse/pen pan controlled by setPanEnabled()
70
+ const interaction = view.use(createInteractionPlugin())
71
+
72
+ // Document plugin: sets canvas bounds
73
+ const doc = view.use(createDocumentPlugin({
74
+ rect: { x: 0, y: 0, width: 1200, height: 800 },
75
+ margins: { left: 50, right: 50, top: 50, bottom: 50 }
76
+ }))
77
+ doc.zoomToFit()
89
78
 
90
- // Create a drawable canvas layer
79
+ // Create drawable layer
91
80
  const drawLayer = new CanvasLayer({
92
81
  width: 1200,
93
- height: 800,
94
- x: 0,
95
- y: 0
82
+ height: 800
96
83
  })
97
84
  layerManager.addLayer(drawLayer)
98
85
 
99
- // Drawing with snapshot-based undo/redo
86
+ // Switch to drawing mode (disable mouse/pen pan, touch still works)
87
+ interaction.setPanEnabled(false)
88
+
89
+ // Drawing with undo support
100
90
  let snapshotBefore: ImageData | null = null
101
91
 
102
- function onPointerDown(wx: number, wy: number) {
92
+ canvas.onpointerdown = (e) => {
93
+ if (e.pointerType === 'touch') return // Let plugin handle touch
94
+
95
+ const { wx, wy } = view.toWorld(e.offsetX, e.offsetY)
103
96
  snapshotBefore = drawLayer.captureSnapshot()
104
97
  drawLayer.beginStroke(wx, wy)
105
98
  }
106
99
 
107
- function onPointerMove(wx: number, wy: number, pressure: number) {
108
- drawLayer.stroke(wx, wy, '#ff0000', 10, pressure, 'brush')
100
+ canvas.onpointermove = (e) => {
101
+ if (e.pointerType === 'touch') return
102
+ if (e.buttons !== 1) return
103
+
104
+ const { wx, wy } = view.toWorld(e.offsetX, e.offsetY)
105
+ drawLayer.stroke(wx, wy, '#000', 10, e.pressure, 'brush')
106
+ view.requestRender()
109
107
  }
110
108
 
111
- function onPointerUp() {
109
+ canvas.onpointerup = () => {
112
110
  drawLayer.endStroke()
113
111
  const snapshotAfter = drawLayer.captureSnapshot()
114
- const command = createSnapshotCommand(drawLayer, snapshotBefore, snapshotAfter)
115
- if (command) {
116
- historyManager.addCommand(command)
117
- }
112
+ const cmd = createSnapshotCommand(drawLayer, snapshotBefore, snapshotAfter)
113
+ if (cmd) historyManager.addCommand(cmd)
118
114
  }
119
115
 
120
- // Undo / Redo
121
- historyManager.undo()
122
- historyManager.redo()
116
+ // Undo/Redo
117
+ document.onkeydown = (e) => {
118
+ if (e.ctrlKey && e.key === 'z') historyManager.undo()
119
+ if (e.ctrlKey && e.key === 'y') historyManager.redo()
120
+ }
123
121
  ```
124
122
 
125
- ## API Reference
123
+ ## Core Concepts
126
124
 
127
125
  ### ViewManager
128
126
 
129
- The core class that manages the canvas viewport.
130
-
131
- ```typescript
132
- const view = new ViewManager(canvas, renderFn, options?)
133
- ```
134
-
135
- #### Options
136
-
137
- | Option | Type | Default | Description |
138
- |--------|------|---------|-------------|
139
- | `minZoom` | `number` | `0.5` | Minimum zoom level |
140
- | `maxZoom` | `number` | `10` | Maximum zoom level |
141
- | `wheelSensitivity` | `number` | `0.0015` | Mouse wheel zoom sensitivity |
142
- | `friction` | `number` | `0.92` | Inertia friction (per frame) |
143
- | `background` | `string` | `'#fff'` | Canvas background color |
144
- | `drawDocBorder` | `boolean` | `false` | Draw border around document |
145
- | `panClampMode` | `'margin' \| 'minVisible'` | `'minVisible'` | Pan restriction mode |
146
-
147
- #### Methods
127
+ The main viewport controller. Handles coordinate transformation and rendering.
148
128
 
149
129
  ```typescript
150
- // Zoom
151
- view.zoomToAtScreen(anchorX, anchorY, zoom) // Zoom to absolute level at screen point
152
- view.zoomByFactorAtScreen(anchorX, anchorY, factor) // Zoom by factor at screen point
153
- view.zoomInAtCenter() // Zoom in at canvas center
154
- view.zoomOutAtCenter() // Zoom out at canvas center
130
+ const view = new ViewManager(canvas, renderFn, {
131
+ minZoom: 0.2,
132
+ maxZoom: 10,
133
+ background: '#fff'
134
+ })
155
135
 
156
136
  // Coordinate conversion
157
- view.toWorld(screenX, screenY) // Screen → World coordinates
158
- view.toScreen(worldX, worldY) // World → Screen coordinates
137
+ const { wx, wy } = view.toWorld(screenX, screenY)
138
+ const { sx, sy } = view.toScreen(worldX, worldY)
159
139
 
160
- // Pan control
161
- view.enablePan()
162
- view.disablePan()
163
-
164
- // Zoom control
165
- view.enableZoom()
166
- view.disableZoom()
167
-
168
- // Document mode (constrained panning)
169
- view.setDocument(x, y, width, height)
170
- view.setDocumentMargins(left, right, top, bottom)
171
-
172
- // Reset
173
- view.reset() // Smooth reset to initial state
174
-
175
- // Cleanup
176
- view.destroy()
140
+ // Programmatic zoom
141
+ view.zoomToAtScreen(anchorX, anchorY, 2.0)
142
+ view.zoomByFactorAtScreen(anchorX, anchorY, 1.5)
177
143
  ```
178
144
 
179
- ### ContentLayerManager
145
+ ### InteractionPlugin
180
146
 
181
- Manages layers that render in world space.
147
+ Handles user input for pan and zoom.
182
148
 
183
149
  ```typescript
184
- const layerManager = new ContentLayerManager()
185
-
186
- // Create image layer from URL/File/Blob
187
- const imageLayer = await layerManager.createImageLayer({
188
- src: 'image.png',
189
- x: 0,
190
- y: 0,
191
- width: 500, // optional, uses natural size if omitted
192
- height: 300, // optional
193
- anchor: 'center', // 'topLeft' | 'center'
194
- rotation: 0 // radians
195
- })
196
-
197
- // Add custom layer
198
- layerManager.addLayer(layer)
150
+ const interaction = view.use(createInteractionPlugin())
199
151
 
200
- // Remove layer
201
- layerManager.removeLayer(layer)
152
+ // Pan enabled affects mouse/pen only
153
+ // Touch gestures (single-finger pan, two-finger pinch) always work
154
+ interaction.setPanEnabled(true) // Mouse/pen can pan
155
+ interaction.setPanEnabled(false) // Mouse/pen cannot pan (for drawing mode)
202
156
 
203
- // Cleanup
204
- layerManager.destroy()
157
+ interaction.setZoomEnabled(true) // Wheel zoom & pinch zoom enabled
205
158
  ```
206
159
 
207
- ### CanvasLayer
160
+ ### DocumentPlugin
208
161
 
209
- A drawable layer with an offscreen canvas.
162
+ Defines document bounds and provides pan clamping.
210
163
 
211
164
  ```typescript
212
- const layer = new CanvasLayer({
213
- width: 1200,
214
- height: 800,
215
- x: 0,
216
- y: 0,
217
- anchor: 'topLeft',
218
- space: 'world' // 'world' | 'screen'
219
- })
220
-
221
- // Drawing API
222
- layer.beginStroke(worldX, worldY)
223
- layer.stroke(worldX, worldY, '#000000', 10, pressure, 'brush') // color, size, pressure, mode
224
- layer.endStroke()
225
-
226
- // Snapshot API for undo/redo
227
- const snapshot = layer.captureSnapshot() // Capture current state
228
- layer.restoreSnapshot(snapshot) // Restore to snapshot
229
-
230
- // Direct canvas access
231
- layer.context.fillRect(0, 0, 100, 100)
165
+ const doc = view.use(createDocumentPlugin({
166
+ rect: { x: 0, y: 0, width: 1200, height: 800 },
167
+ margins: { left: 50, right: 50, top: 50, bottom: 50 },
168
+ drawBorder: true,
169
+ background: '#f0f0f0',
170
+ shadow: { blur: 20, color: 'rgba(0,0,0,0.3)', offsetX: 0, offsetY: 5 }
171
+ }))
172
+
173
+ doc.zoomToFit() // Fit document to viewport
174
+ doc.setPanClampMode('minVisible') // 'margin' | 'minVisible'
232
175
  ```
233
176
 
234
- ### BitmapLayer
235
-
236
- Extends `CanvasLayer` for image display.
177
+ ### Layer System
237
178
 
238
179
  ```typescript
239
- const layer = await BitmapLayer.fromImage({
240
- src: 'image.png', // URL, File, or Blob
241
- x: 0,
242
- y: 0,
243
- width: 500,
244
- height: 300,
245
- anchor: 'center',
246
- rotation: Math.PI / 4
247
- })
180
+ // Content layers (world space)
181
+ const contentManager = new ContentLayerManager()
182
+ view.registerLayerManager(contentManager)
248
183
 
249
- // Replace image source
250
- await layer.setSource('new-image.png')
184
+ // Canvas layer for drawing
185
+ const layer = new CanvasLayer({ width: 1200, height: 800 })
186
+ contentManager.addLayer(layer)
251
187
 
252
- // Paint on the bitmap
253
- layer.paint((ctx, canvas) => {
254
- ctx.fillStyle = 'red'
255
- ctx.fillRect(10, 10, 50, 50)
256
- })
188
+ // Image layer
189
+ const imgLayer = await contentManager.createImageLayer({ src: 'image.png' })
257
190
 
258
- // Pixel access
259
- const imageData = layer.getImageData()
260
- layer.putImageData(imageData, 0, 0)
261
- ```
262
-
263
- ### HistoryManager
264
-
265
- Command-based undo/redo system.
266
-
267
- ```typescript
268
- const historyManager = new HistoryManager({
269
- maxHistorySize: 50,
270
- undoStack: [], // optional, pass reactive array for UI binding
271
- redoStack: []
272
- })
273
-
274
- // Add a command (already executed)
275
- historyManager.addCommand(command)
276
-
277
- // Execute and add a command
278
- historyManager.executeCommand(command)
191
+ // Remove layer (destroys it)
192
+ contentManager.removeLayer(layer.id)
279
193
 
280
- historyManager.undo()
281
- historyManager.redo()
282
- historyManager.canUndo()
283
- historyManager.canRedo()
284
- historyManager.clear()
194
+ // Detach layer (keeps it for undo)
195
+ contentManager.detachLayer(layer.id)
285
196
  ```
286
197
 
287
- ### SnapshotCommand
288
-
289
- Built-in command for canvas content undo/redo.
198
+ ### History (Undo/Redo)
290
199
 
291
200
  ```typescript
292
- import { createSnapshotCommand } from '@lancercomet/zoom-pan'
201
+ const history = new HistoryManager({ maxHistorySize: 50 })
293
202
 
294
- // Capture before action
203
+ // Snapshot-based undo for drawing
295
204
  const before = layer.captureSnapshot()
296
-
297
- // ... perform drawing ...
298
-
299
- // Capture after action
205
+ // ... draw ...
300
206
  const after = layer.captureSnapshot()
207
+ const cmd = createSnapshotCommand(layer, before, after)
208
+ if (cmd) history.addCommand(cmd)
301
209
 
302
- // Create command and add to history
303
- const command = createSnapshotCommand(layer, before, after)
304
- if (command) {
305
- historyManager.addCommand(command)
306
- }
307
- ```
308
-
309
- ### Plugins
310
-
311
- The library uses a plugin system for extensibility.
312
-
313
- #### InteractionPlugin
314
-
315
- Handles pan, zoom, wheel, and inertia.
316
-
317
- ```typescript
318
- import { createInteractionPlugin } from '@lancercomet/zoom-pan'
319
-
320
- const interactionPlugin = createInteractionPlugin()
321
- view.use(interactionPlugin)
210
+ // Layer commands
211
+ import { CreateLayerCommand, DeleteLayerCommand } from '@lancercomet/zoom-pan'
322
212
 
323
- // Control pan/zoom
324
- interactionPlugin.setPanEnabled(true)
325
- interactionPlugin.setZoomEnabled(true)
326
- ```
327
-
328
- #### DocumentPlugin
329
-
330
- Handles document bounds and pan clamping.
331
-
332
- ```typescript
333
- import { createDocumentPlugin } from '@lancercomet/zoom-pan'
334
-
335
- const documentPlugin = createDocumentPlugin()
336
- view.use(documentPlugin)
337
-
338
- // Set document bounds
339
- documentPlugin.setRect(0, 0, 1200, 800)
340
- documentPlugin.setMargins(100, 100, 100, 100)
341
- documentPlugin.setPanClampMode('minVisible') // 'margin' | 'minVisible'
342
- documentPlugin.zoomToFit()
343
- ```
344
-
345
- ### Custom Commands
346
-
347
- Implement `ICommand` interface for custom undo/redo actions.
348
-
349
- ```typescript
350
- import { ICommand } from '@lancercomet/zoom-pan'
351
-
352
- class MyCommand implements ICommand {
353
- readonly type = 'my-command'
354
-
355
- execute(): void {
356
- // Do the action
357
- }
358
-
359
- undo(): void {
360
- // Reverse the action
361
- }
362
- }
213
+ history.undo()
214
+ history.redo()
215
+ history.canUndo()
216
+ history.canRedo()
363
217
  ```
364
218
 
365
219
  ## Examples
366
220
 
367
- Check out the `examples/` directory:
368
-
369
- - **`examples/viewer/`** - Basic image viewer with pan & zoom
370
- - **`examples/painter/`** - Full-featured painter with brush, eraser, undo/redo
371
-
372
- ### Painter Hotkeys
373
-
374
- | Key | Action |
375
- |-----|--------|
376
- | `B` | Brush tool |
377
- | `E` | Eraser tool |
378
- | `H` | Pan tool |
379
- | `Z` | Zoom tool |
380
- | `Space` (hold) | Temporary pan mode |
381
- | `Alt` (hold) | Color picker |
382
- | `Ctrl+Z` | Undo |
383
- | `Ctrl+Y` / `Ctrl+Shift+Z` | Redo |
384
-
385
- ## License
221
+ See `examples/` folder:
386
222
 
387
- MIT
223
+ - **`examples/viewer/`** - Simple image viewer
224
+ - **`examples/painter/`** - Full drawing app with brush, eraser, layers, undo/redo
225
+ - **`examples/doc/`** - Document mode demo with background and shadow
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";function t(t,e,i,s){return new(i||(i=Promise))(function(n,h){function a(t){try{r(s.next(t))}catch(t){h(t)}}function o(t){try{r(s.throw(t))}catch(t){h(t)}}function r(t){var e;t.done?n(t.value):(e=t.value,e instanceof i?e:new i(function(t){t(e)})).then(a,o)}r((s=s.apply(t,e||[])).next())})}function e(t,e,i,s){if("a"===i&&!s)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof e?t!==e||!s:!e.has(t))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===i?s:"a"===i?s.call(t):s?s.value:e.get(t)}function i(t,e,i,s,n){if("m"===s)throw new TypeError("Private method is not writable");if("a"===s&&!n)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof e?t!==e||!n:!e.has(t))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===s?n.call(t,i):n?n.value=i:e.set(t,i),i}"function"==typeof SuppressedError&&SuppressedError;const s=(e,i)=>t(void 0,void 0,void 0,function*(){return new Promise((t,s)=>{const n=new Image;"string"==typeof e?(void 0!==i&&(n.crossOrigin=i),n.src=e):n.src=URL.createObjectURL(e),n.onload=()=>{t(n)},n.onerror=()=>{s(new Error("Image load failed"))}})}),n=(t,e,i)=>Math.min(Math.max(t,e),i);let h=0;class a{constructor(t,e,i="world"){this.space="world",this.visible=!0,this.opacity=1,this.blend="source-over",this.name=t,this.id=`layer_${e}_${++h}`,this.type=e,this.space=i}}class o extends a{beginStroke(t,e){const{lx:i,ly:s}=this.toLocalPoint(t,e);this._lastX=i,this._lastY=s,this._drawing=!0}stroke(t,e,i,s,n=1,h="brush"){if(!this._drawing)return;const{lx:a,ly:o}=this.toLocalPoint(t,e);this.context.beginPath(),this.context.moveTo(this._lastX,this._lastY),this.context.lineTo(a,o),"eraser"===h?(this.context.globalCompositeOperation="destination-out",this.context.strokeStyle="rgba(0, 0, 0, 1)"):(this.context.globalCompositeOperation="source-over",this.context.strokeStyle=i),this.context.lineWidth=s*n,this.context.lineCap="round",this.context.lineJoin="round",this.context.stroke(),this.context.closePath(),this._lastX=a,this._lastY=o}endStroke(){this._drawing=!1}isDrawing(){return this._drawing}captureSnapshot(t){try{if(t){const{x:e,y:i,width:s,height:n}=t,h=Math.max(0,Math.floor(e)),a=Math.max(0,Math.floor(i)),o=Math.min(this.canvas.width-h,Math.ceil(s)),r=Math.min(this.canvas.height-a,Math.ceil(n));return o<=0||r<=0?null:this.context.getImageData(h,a,o,r)}return this.context.getImageData(0,0,this.canvas.width,this.canvas.height)}catch(t){return null}}restoreSnapshot(t,e){var i,s;const n=null!==(i=null==e?void 0:e.x)&&void 0!==i?i:0,h=null!==(s=null==e?void 0:e.y)&&void 0!==s?s:0;this.context.putImageData(t,n,h)}clearRegion(t){t?this.context.clearRect(t.x,t.y,t.width,t.height):this.context.clearRect(0,0,this.canvas.width,this.canvas.height)}requestRedraw(){var t;null===(t=this._redraw)||void 0===t||t.call(this,this.context,this.canvas)}drawImage(t,e,i,s,n){this.context.drawImage(t,e,i,null!=s?s:t.width,null!=n?n:t.height)}hitTest(t,e){const{lx:i,ly:s}=this.toLocalPoint(t,e);return i>=0&&i<=this.canvas.width&&s>=0&&s<=this.canvas.height}toLocalPoint(t,e){const i=t-this.x,s=e-this.y,n=Math.cos(-this.rotation),h=Math.sin(-this.rotation),a=i*h+s*n,o=(i*n-s*h)/this.scale,r=a/this.scale;return{lx:o+("center"===this.anchor?this.canvas.width/2:0),ly:r+("center"===this.anchor?this.canvas.height/2:0)}}render(t,e){if(!this.visible)return;const i=this.canvas.width*this.scale,s=this.canvas.height*this.scale,n="center"===this.anchor?-i/2:0,h="center"===this.anchor?-s/2:0;t.save(),t.globalAlpha=this.opacity,t.globalCompositeOperation=this.blend,t.translate(this.x,this.y),t.rotate(this.rotation),t.drawImage(this.canvas,n,h,i,s),t.restore()}cropTo(t){const e=Math.max(1,Math.floor(t.width)),i=Math.max(1,Math.floor(t.height));if(e===this.canvas.width&&i===this.canvas.height)return;const s=this._cloneCanvas(),n=Math.min(s.width,e),h=Math.min(s.height,i);this._setCanvasSize(e,i),n>0&&h>0&&this.context.drawImage(s,0,0,n,h,0,0,n,h)}resizeTo(t){const e=Math.max(1,Math.floor(t.width)),i=Math.max(1,Math.floor(t.height));if(e===this.canvas.width&&i===this.canvas.height)return;const s=this._cloneCanvas();this._setCanvasSize(e,i),s.width>0&&s.height>0&&this.context.drawImage(s,0,0,s.width,s.height,0,0,e,i)}_cloneCanvas(){const t=document.createElement("canvas");if(t.width=this.canvas.width,t.height=this.canvas.height,0===t.width||0===t.height)return t;const e=t.getContext("2d");if(!e)throw new Error("Offscreen 2D context unavailable");return e.drawImage(this.canvas,0,0),t}_setCanvasSize(t,e){this.canvas.width=t,this.canvas.height=e,this.context.setTransform(1,0,0,1,0,0),this.context.clearRect(0,0,t,e)}destroy(){this.canvas.width=0,this.canvas.height=0}constructor(t){var e,i;if(super(t.name||"","canvas",null!==(e=t.space)&&void 0!==e?e:"world"),this.x=0,this.y=0,this.scale=1,this.rotation=0,this.anchor="topLeft",this._drawing=!1,this._lastX=0,this._lastY=0,"canvas"in t&&t.canvas)this.canvas=t.canvas;else{const e=t;this.canvas=document.createElement("canvas"),this.canvas.width=e.width,this.canvas.height=e.height}const s=this.canvas.getContext("2d",{willReadFrequently:!0});if(!s)throw new Error("Offscreen 2D context unavailable");this.context=s,this.x=t.x||0,this.y=t.y||0,this.scale=null!==(i=t.scale)&&void 0!==i?i:1,this.rotation=t.rotation||0,t.anchor&&(this.anchor=t.anchor),this._redraw=t.redraw,this._redraw&&this._redraw(this.context,this.canvas)}}var r;class c extends o{static fromImage(e){return t(this,void 0,void 0,function*(){var t,n,h,a,o,_,l,d;const g=yield s(e.src,e.crossOrigin),u=null!==(t=e.width)&&void 0!==t?t:g.naturalWidth,m=null!==(n=e.height)&&void 0!==n?n:g.naturalHeight,p=new c({name:e.name,space:null!==(h=e.space)&&void 0!==h?h:"world",x:null!==(a=e.x)&&void 0!==a?a:0,y:null!==(o=e.y)&&void 0!==o?o:0,scale:null!==(_=e.scale)&&void 0!==_?_:1,rotation:null!==(l=e.rotation)&&void 0!==l?l:0,anchor:null!==(d=e.anchor)&&void 0!==d?d:"topLeft",width:u,height:m});return p.context.clearRect(0,0,p.canvas.width,p.canvas.height),p.context.drawImage(g,0,0,u,m),"string"!=typeof e.src&&i(p,r,g.src.startsWith("blob:")?g.src:null,"f"),p})}setSource(n,h){return t(this,void 0,void 0,function*(){const t=yield s(n,h);if(this.canvas.width=t.naturalWidth,this.canvas.height=t.naturalHeight,this.context.setTransform(1,0,0,1,0,0),this.context.clearRect(0,0,this.canvas.width,this.canvas.height),this.context.drawImage(t,0,0),e(this,r,"f")){try{URL.revokeObjectURL(e(this,r,"f"))}catch(t){}i(this,r,null,"f")}"string"!=typeof n&&i(this,r,t.src.startsWith("blob:")?t.src:null,"f")})}paint(t){t(this.context,this.canvas)}getImageData(t=0,e=0,i=this.canvas.width,s=this.canvas.height){return this.context.getImageData(t,e,i,s)}putImageData(t,e=0,i=0){this.context.putImageData(t,e,i)}toDataURL(t="image/png",e){return this.canvas.toDataURL(t,e)}toImageBitmap(t){return createImageBitmap(this.canvas,null!=t?t:{})}destroy(){var t;if(null===(t=super.destroy)||void 0===t||t.call(this),e(this,r,"f")){try{URL.revokeObjectURL(e(this,r,"f"))}catch(t){}i(this,r,null,"f")}}constructor(t){var e,i;super({name:t.name,space:null!==(e=t.space)&&void 0!==e?e:"world",x:t.x,y:t.y,scale:t.scale,rotation:t.rotation,anchor:null!==(i=t.anchor)&&void 0!==i?i:"topLeft",width:t.width,height:t.height}),r.set(this,null),this.type="bitmap"}}r=new WeakMap;class _{constructor(){this._worldLayers=[],this._screenLayers=[]}_renderAllLayersIn(t,e){e.save(),t.applyWorldTransform(e);for(const i of this._worldLayers)!i.visible||i.opacity<=0||(e.save(),i.render(e,t),e.restore());e.restore(),e.save(),t.applyScreenTransform(e);for(const i of this._screenLayers)!i.visible||i.opacity<=0||(e.save(),i.render(e,t),e.restore());e.restore()}addLayer(t,e){const i="world"===t.space?this._worldLayers:this._screenLayers;return"number"==typeof e&&e>=0&&e<i.length?(i.splice(e,0,t),t.id):(i.push(t),t.id)}createImageLayer(e){return t(this,void 0,void 0,function*(){const t=yield c.fromImage(e);return this.addLayer(t),t})}createCanvasLayer(t){const e=new o(t);return this.addLayer(e),e}removeLayer(t){var e,i,s,n;const h=this._worldLayers.findIndex(e=>e.id===t);if(h>=0)return null===(i=(e=this._worldLayers[h]).destroy)||void 0===i||i.call(e),void this._worldLayers.splice(h,1);const a=this._screenLayers.findIndex(e=>e.id===t);a>=0&&(null===(n=(s=this._screenLayers[a]).destroy)||void 0===n||n.call(s),this._screenLayers.splice(a,1))}moveLayer(t,e){const i=this._worldLayers.findIndex(e=>e.id===t);if(i>=0){const[t]=this._worldLayers.splice(i,1),s=Math.max(0,Math.min(e,this._worldLayers.length));return void this._worldLayers.splice(s,0,t)}const s=this._screenLayers.findIndex(e=>e.id===t);if(s>=0){const[t]=this._screenLayers.splice(s,1),i=Math.max(0,Math.min(e,this._screenLayers.length));this._screenLayers.splice(i,0,t)}}getLayer(t){return this._worldLayers.find(e=>e.id===t)||this._screenLayers.find(e=>e.id===t)}getAllLayers(t){return t?("world"===t?this._worldLayers:this._screenLayers).slice():[...this._worldLayers,...this._screenLayers]}hitTest(t,e,i="world"){const s=this.getAllLayers(i);for(let i=s.length-1;i>=0;i--){const n=s[i];if(n.hitTest&&n.hitTest(t,e))return n}}destroy(){var t;for(const e of[...this._worldLayers,...this._screenLayers])null===(t=e.destroy)||void 0===t||t.call(e);this._worldLayers=[],this._screenLayers=[]}}class l{constructor(t){this.name="interaction",this._view=null,this._dragging=!1,this._vx=0,this._vy=0,this._lastMoveTs=0,this._activePointerId=null,this._onDownBound=t=>this._onPointerDown(t),this._onMoveBound=t=>this._onPointerMove(t),this._onUpBound=()=>this._onPointerUp(),this._onWheelBound=t=>this._onWheel(t),this._onUpdate=t=>{if(!this._view)return;const{friction:e,stopSpeed:i}=this._options,s=Math.hypot(this._vx,this._vy)>=i;if(!this._dragging&&this._panEnabled&&s){const s=this._vx*t,n=this._vy*t;this._view.panBy(s,n),this._vx*=e,this._vy*=e,Math.hypot(this._vx,this._vy)<i&&(this._vx=0,this._vy=0)}else this._panEnabled||(this._vx=0,this._vy=0)},this._options=Object.assign({panEnabled:!0,zoomEnabled:!0,friction:.92,stopSpeed:.02,emaAlpha:.25,idleNoInertiaMs:120,wheelSensitivity:.0015},t),this._panEnabled=this._options.panEnabled,this._zoomEnabled=this._options.zoomEnabled}install(t){this._view=t;const e=t.canvas;e.addEventListener("wheel",this._onWheelBound,{passive:!1}),e.addEventListener("pointerdown",this._onDownBound),window.addEventListener("pointermove",this._onMoveBound),window.addEventListener("pointerup",this._onUpBound),t.onUpdate(this._onUpdate)}destroy(){if(!this._view)return;const t=this._view.canvas;t.removeEventListener("wheel",this._onWheelBound),t.removeEventListener("pointerdown",this._onDownBound),window.removeEventListener("pointermove",this._onMoveBound),window.removeEventListener("pointerup",this._onUpBound),this._view.offUpdate(this._onUpdate),this._view=null}isPanEnabled(){return this._panEnabled}isZoomEnabled(){return this._zoomEnabled}setPanEnabled(t){this._panEnabled!==t&&(this._panEnabled=t,t||(this._dragging=!1,this._vx=0,this._vy=0))}setZoomEnabled(t){this._zoomEnabled=t}setWheelSensitivity(t){this._options.wheelSensitivity=t}isDragging(){return this._dragging}_onPointerDown(t){var e;if(0===t.button&&this._panEnabled){this._dragging=!0,this._vx=0,this._vy=0,this._lastMoveTs=performance.now(),this._activePointerId=t.pointerId;try{null===(e=this._view)||void 0===e||e.canvas.setPointerCapture(t.pointerId)}catch(t){}}}_onPointerMove(t){if(!this._dragging||!this._panEnabled||!this._view)return;const e=performance.now(),i=Math.max(1,e-(this._lastMoveTs||e-16));this._lastMoveTs=e;const s=t.movementX,n=t.movementY;this._view.panBy(s,n);const h=this._options.emaAlpha,a=s/i,o=n/i;this._vx=(1-h)*this._vx+h*a,this._vy=(1-h)*this._vy+h*o}_onPointerUp(){if(!this._dragging)return;this._dragging=!1;const t=performance.now(),e=this._lastMoveTs?t-this._lastMoveTs:1/0;if(null!=this._activePointerId&&this._view){try{this._view.canvas.releasePointerCapture(this._activePointerId)}catch(t){}this._activePointerId=null}if(e>=this._options.idleNoInertiaMs)this._vx=0,this._vy=0;else{const t=Math.pow(this._options.friction,e/16);this._vx*=t,this._vy*=t}Math.hypot(this._vx,this._vy)<this._options.stopSpeed&&(this._vx=0,this._vy=0)}_getLineHeightPx(){if(!this._view)return 16;const t=getComputedStyle(this._view.canvas).lineHeight;if(!t||"normal"===t)return 16;const e=parseFloat(t);return Number.isFinite(e)?e:16}_normalizeWheelDelta(t){if(!this._view)return 0;let e=t.deltaY;if(1===t.deltaMode)e*=this._getLineHeightPx();else if(2===t.deltaMode){e*=this._view.canvas.clientHeight||window.innerHeight||800}return e}_onWheel(t){if(!this._zoomEnabled||!this._view)return;t.preventDefault(),t.stopPropagation();const e=this._normalizeWheelDelta(t),i=this._view.canvas.getBoundingClientRect(),s=t.clientX-i.left,n=t.clientY-i.top;let h=-e*this._options.wheelSensitivity;t.ctrlKey||t.metaKey?h*=1.6:t.shiftKey&&(h*=.6),this._view.zoomByLogAtScreen(s,n,h)}}class d{constructor(t){var e,i,s,n,h;this.name="document",this._view=null,this._enabled=!1,this._x=0,this._y=0,this._width=0,this._height=0,this._marginL=0,this._marginR=0,this._marginT=0,this._marginB=0,this._panClampMode="minVisible",this._background=null,this._shadow=null,this._onUpdate=()=>{this._enabled&&this._view&&this._clampPan()},this._onBeforeRender=t=>{var e,i,s,n,h;this._enabled&&this._view&&(this._shadow?(t.save(),t.shadowColor=null!==(e=this._shadow.color)&&void 0!==e?e:d._defaultShadow.color,t.shadowBlur=null!==(i=this._shadow.blur)&&void 0!==i?i:d._defaultShadow.blur,t.shadowOffsetX=null!==(s=this._shadow.offsetX)&&void 0!==s?s:d._defaultShadow.offsetX,t.shadowOffsetY=null!==(n=this._shadow.offsetY)&&void 0!==n?n:d._defaultShadow.offsetY,t.fillStyle=null!==(h=this._background)&&void 0!==h?h:"#ffffff",t.fillRect(this._x,this._y,this._width,this._height),t.restore()):this._background&&(t.save(),t.fillStyle=this._background,t.fillRect(this._x,this._y,this._width,this._height),t.restore()),t.save(),t.beginPath(),t.rect(this._x,this._y,this._width,this._height),t.clip())},this._onAfterRender=t=>{if(this._enabled&&this._view&&(t.restore(),this._options.drawBorder)){const e=this._view.zoom;t.save(),t.lineWidth=1/e,t.strokeStyle="#cfcfcf",t.strokeRect(this._x,this._y,this._width,this._height),t.restore()}},this._options=Object.assign({rect:{x:0,y:0,width:0,height:0},margins:{},drawBorder:!1,minVisiblePx:30,panClampMode:"minVisible",background:null,shadow:!1},t),(null==t?void 0:t.rect)&&(this._enabled=!0,this._x=t.rect.x,this._y=t.rect.y,this._width=t.rect.width,this._height=t.rect.height),(null==t?void 0:t.margins)&&(this._marginL=null!==(e=t.margins.left)&&void 0!==e?e:0,this._marginR=null!==(i=t.margins.right)&&void 0!==i?i:0,this._marginT=null!==(s=t.margins.top)&&void 0!==s?s:0,this._marginB=null!==(n=t.margins.bottom)&&void 0!==n?n:0),this._panClampMode=this._options.panClampMode,this._background=null!==(h=this._options.background)&&void 0!==h?h:null,!0===this._options.shadow?this._shadow=Object.assign({},d._defaultShadow):this._options.shadow&&"object"==typeof this._options.shadow&&(this._shadow=Object.assign(Object.assign({},d._defaultShadow),this._options.shadow))}install(t){this._view=t,t.onUpdate(this._onUpdate),t.onBeforeRender(this._onBeforeRender),t.onAfterRender(this._onAfterRender)}destroy(){this._view&&(this._view.offUpdate(this._onUpdate),this._view.offBeforeRender(this._onBeforeRender),this._view.offAfterRender(this._onAfterRender),this._view=null)}isEnabled(){return this._enabled}getRect(){return{x:this._x,y:this._y,width:this._width,height:this._height}}setRect(t,e,i,s){this._enabled=!0,this._x=t,this._y=e,this._width=i,this._height=s}clearRect(){this._enabled=!1}setMargins(t){var e,i,s,n;this._marginL=null!==(e=t.left)&&void 0!==e?e:this._marginL,this._marginR=null!==(i=t.right)&&void 0!==i?i:this._marginR,this._marginT=null!==(s=t.top)&&void 0!==s?s:this._marginT,this._marginB=null!==(n=t.bottom)&&void 0!==n?n:this._marginB}getMargins(){return{left:this._marginL,right:this._marginR,top:this._marginT,bottom:this._marginB}}setPanClampMode(t){this._panClampMode=t}getPanClampMode(){return this._panClampMode}cropTo(t){this._doResize("crop",t)}resizeTo(t){this._doResize("resize",t)}zoomToFit(t="contain",e=!0){if(!this._enabled||!this._view)return;const i=this._view.dpr,s=this._view.canvas.width/i,n=this._view.canvas.height/i,h=Math.max(1,s-(this._marginL+this._marginR)),a=Math.max(1,n-(this._marginT+this._marginB));let o;const r=h/this._width,c=a/this._height;o="contain"===t?Math.min(r,c):"cover"===t?Math.max(r,c):"fitWidth"===t?r:c,o=Math.min(this._view.maxZoom,Math.max(this._view.minZoom,o));const _=this._marginL+(h-o*this._width)/2,l=this._marginT+(a-o*this._height)/2,d=_-o*this._x,g=l-o*this._y;e?this._view.setTransformSmooth(o,d,g):this._view.setTransform(o,d,g)}isPointInDocument(t,e){return!this._enabled||t>=this._x&&t<=this._x+this._width&&e>=this._y&&e<=this._y+this._height}get background(){return this._background}setBackground(t){var e;this._background=t,null===(e=this._view)||void 0===e||e.requestRender()}get shadow(){return this._shadow?Object.assign({},this._shadow):null}setShadow(t){var e;this._shadow=!0===t?Object.assign({},d._defaultShadow):t&&"object"==typeof t?Object.assign(Object.assign({},d._defaultShadow),t):null,null===(e=this._view)||void 0===e||e.requestRender()}_clampPan(){if(!this._view)return;const{zoom:t,tx:e,ty:i}=this._view.getTransform(),s=t,n=this._view.dpr,h=this._view.canvas.width/n,a=this._view.canvas.height/n,o=this._x,r=this._y,c=this._x+this._width,_=this._y+this._height;let l=e,d=i;if("margin"===this._panClampMode){const t=this._marginL-s*o,n=h-this._marginR-s*c,g=this._marginT-s*r,u=a-this._marginB-s*_,m=Math.max(1,h-(this._marginL+this._marginR)),p=Math.max(1,a-(this._marginT+this._marginB));l=s*this._width<=m?this._marginL+(m-s*this._width)/2-s*this._x:Math.min(t,Math.max(n,e)),d=s*this._height<=p?this._marginT+(p-s*this._height)/2-s*this._y:Math.min(g,Math.max(u,i))}else if("minVisible"===this._panClampMode){const t=s*this._width,n=s*this._height,g=Math.min(this._options.minVisiblePx,t),u=Math.min(this._options.minVisiblePx,n),m=h-g-s*o,p=g-s*c,v=a-u-s*r,f=u-s*_;l=p<=m?Math.min(m,Math.max(p,e)):(p+m)/2,d=f<=v?Math.min(v,Math.max(f,i)):(f+v)/2}l===e&&d===i||this._view.setPan(l,d)}_doResize(t,e){if(!this._view)return;const i=Math.max(1,Math.floor(e.width)),s=Math.max(1,Math.floor(e.height)),n={width:i,height:s},h=this._view.getLayerManagers();for(const e of h){const i=e.getAllLayers("world");for(const e of i){const i=e;"function"==typeof i.cropTo&&"function"==typeof i.resizeTo&&("crop"===t?i.cropTo(n):i.resizeTo(n))}}this._width=i,this._height=s,this._enabled&&this._clampPan()}}d._defaultShadow={color:"rgba(0, 0, 0, 0.3)",blur:20,offsetX:0,offsetY:4};class g{constructor(t,e,i,s){this.type="snapshot",this._isExecuted=!0,this._target=t,this._beforeData=e,this._afterData=i,this._region=null==s?void 0:s.region}execute(){if(this._isExecuted)return;const t=this._region?{x:this._region.x,y:this._region.y}:void 0;this._target.restoreSnapshot(this._afterData,t),this._isExecuted=!0}undo(){if(!this._isExecuted)return;const t=this._region?{x:this._region.x,y:this._region.y}:void 0;this._target.restoreSnapshot(this._beforeData,t),this._isExecuted=!1}canMerge(){return!1}merge(){return this}}exports.BitmapLayer=c,exports.CanvasLayer=o,exports.ContentLayerManager=class extends _{constructor(){super(...arguments),this._compositeCache=null,this._compositeCacheCtx=null,this._compositeDirty=!0,this._lastCacheWidth=0,this._lastCacheHeight=0,this._cachedBoundsMinX=0,this._cachedBoundsMinY=0}markDirty(){this._compositeDirty=!0}addLayer(t,e){return this._compositeDirty=!0,super.addLayer(t,e)}removeLayer(t){this._compositeDirty=!0,super.removeLayer(t)}removeAllLayers(t){var e,i;if(!t||"world"===t){for(const t of this._worldLayers)null===(e=t.destroy)||void 0===e||e.call(t);this._worldLayers=[]}if(!t||"screen"===t){for(const t of this._screenLayers)null===(i=t.destroy)||void 0===i||i.call(t);this._screenLayers=[]}}moveLayer(t,e){this._compositeDirty=!0,super.moveLayer(t,e)}renderAllLayersIn(t){const e=t.contentContext,i=this._worldLayers;0!==i.length&&(this._compositeDirty&&this._rebuildCompositeCache(i),this._compositeCache&&this._compositeCacheCtx&&e.drawImage(this._compositeCache,this._cachedBoundsMinX,this._cachedBoundsMinY))}_rebuildCompositeCache(t){let e=0,i=0,s=0,n=0,h=!1;for(const a of t){const t=a,o=t.canvas.width*t.scale,r=t.canvas.height*t.scale,c="center"===t.anchor?-o/2:0,_="center"===t.anchor?-r/2:0,l=Math.cos(t.rotation),d=Math.sin(t.rotation),g=[{x:c,y:_},{x:c+o,y:_},{x:c,y:_+r},{x:c+o,y:_+r}];let u=1/0,m=1/0,p=-1/0,v=-1/0;for(const e of g){const i=e.x*l-e.y*d+t.x,s=e.x*d+e.y*l+t.y;u=Math.min(u,i),m=Math.min(m,s),p=Math.max(p,i),v=Math.max(v,s)}h?(e=Math.min(e,u),i=Math.min(i,m),s=Math.max(s,p),n=Math.max(n,v)):(e=u,i=m,s=p,n=v,h=!0)}if(!h)return void(this._compositeDirty=!1);const a=Math.ceil(s-e),o=Math.ceil(n-i);this._compositeCache||(this._compositeCache=document.createElement("canvas"),this._compositeCacheCtx=this._compositeCache.getContext("2d",{alpha:!0})),this._lastCacheWidth===a&&this._lastCacheHeight===o||(this._compositeCache.width=a,this._compositeCache.height=o,this._lastCacheWidth=a,this._lastCacheHeight=o),this._cachedBoundsMinX=e,this._cachedBoundsMinY=i;const r=this._compositeCacheCtx;r.clearRect(0,0,a,o),r.save(),r.translate(-e,-i);for(const e of t)!e.visible||e.opacity<=0||(r.save(),e.render(r),r.restore());r.restore(),this._compositeDirty=!1}destroy(){super.destroy(),this._compositeCache=null,this._compositeCacheCtx=null}},exports.DocumentPlugin=d,exports.HistoryManager=class{executeCommand(t){t.execute(),this.addCommand(t)}addCommand(t){var e,i;this._redoStack=[];const s=this._undoStack[this._undoStack.length-1];if(s&&(null===(e=s.canMerge)||void 0===e?void 0:e.call(s,t))&&s.merge){const e=null!==(i=s.merge(t))&&void 0!==i?i:s;return void(e!==s&&(this._undoStack[this._undoStack.length-1]=e))}this._undoStack.push(t),this._undoStack.length>this._maxHistorySize&&this._undoStack.shift()}undo(){if(0===this._undoStack.length)return null;const t=this._undoStack.pop();return t.undo(),this._redoStack.push(t),t}redo(){if(0===this._redoStack.length)return null;const t=this._redoStack.pop();return t.execute(),this._undoStack.push(t),t}canUndo(){return this._undoStack.length>0}canRedo(){return this._redoStack.length>0}clear(){this._undoStack=[],this._redoStack=[]}setMaxHistorySize(t){this._maxHistorySize=Math.max(1,t),this._undoStack.length>this._maxHistorySize&&(this._undoStack=this._undoStack.slice(-this._maxHistorySize))}constructor(t){var e,i,s;this._undoStack=[],this._redoStack=[],this._maxHistorySize=null!==(e=null==t?void 0:t.maxHistorySize)&&void 0!==e?e:50,this._undoStack=null!==(i=null==t?void 0:t.undoStack)&&void 0!==i?i:[],this._redoStack=null!==(s=null==t?void 0:t.redoStack)&&void 0!==s?s:[]}},exports.InteractionPlugin=l,exports.LayerBase=a,exports.LayerManagerBase=_,exports.SnapshotCommand=g,exports.TopScreenLayerManager=class extends _{renderAllLayersIn(t){const e=t.topScreenContext;this._renderAllLayersIn(t,e)}},exports.ViewManager=class{get zoom(){return Math.exp(this._currentLogZ)}get minZoom(){return this._options.minZoom}get maxZoom(){return this._options.maxZoom}get dpr(){return this._dpr}use(t){return this._plugins.has(t.name)?(console.warn(`Plugin "${t.name}" is already installed.`),t):(this._plugins.set(t.name,t),t.install(this),t)}unuse(t){const e=this._plugins.get(t);e&&(e.destroy(),this._plugins.delete(t))}getPlugin(t){return this._plugins.get(t)}onUpdate(t){this._updateCallbacks.add(t)}offUpdate(t){this._updateCallbacks.delete(t)}onBeforeRender(t){this._beforeRenderCallbacks.add(t)}offBeforeRender(t){this._beforeRenderCallbacks.delete(t)}onAfterRender(t){this._afterRenderCallbacks.add(t)}offAfterRender(t){this._afterRenderCallbacks.delete(t)}requestRender(){this._needsRender=!0}_clampLog(t){return n(t,this.LOG_MIN,this.LOG_MAX)}_setTargetLogZoomAtScreen(t,e,i){Number.isFinite(i)&&(this._anchorX=t,this._anchorY=e,this._targetLogZ=this._clampLog(i),this._needsRender=!0)}zoomToAtScreen(t,e,i){this._setTargetLogZoomAtScreen(t,e,Math.log(i))}zoomToAtScreenRaw(t,e,i){if(!Number.isFinite(i))return;const s=Math.max(1e-8,this._options.minZoom),h=this._options.maxZoom,a=n(i,s,h),o=Math.exp(this._currentLogZ),r=a;if(!Number.isFinite(o)||o<=0)return;if(Math.abs(r-o)<1e-12)return;const c=Math.log(a);this._currentLogZ=c,this._targetLogZ=c;const _=r/o;this._tx=t-(t-this._tx)*_,this._ty=e-(e-this._ty)*_,this._needsRender=!0}zoomToAtWorld(t,e,i){const{x:s,y:n}=this.toScreen(t,e);this.zoomToAtScreen(s,n,i)}zoomByFactorAtScreen(t,e,i){if(i<=0||!Number.isFinite(i))return;const s=Math.log(i);this._setTargetLogZoomAtScreen(t,e,this._targetLogZ+s)}zoomByLogAtScreen(t,e,i){this._setTargetLogZoomAtScreen(t,e,this._targetLogZ+i)}zoomByFactorAtWorld(t,e,i){const{x:s,y:n}=this.toScreen(t,e);this.zoomByFactorAtScreen(s,n,i)}panBy(t,e){this._tx+=t,this._ty+=e,this._needsRender=!0}setPan(t,e){this._tx=t,this._ty=e,this._needsRender=!0}setTransform(t,e,i){const s=n(t,this._options.minZoom,this._options.maxZoom);this._currentLogZ=Math.log(s),this._targetLogZ=this._currentLogZ,this._tx=e,this._ty=i,this._targetTx=null,this._targetTy=null,this._needsRender=!0}setTransformSmooth(t,e,i){const s=n(t,this._options.minZoom,this._options.maxZoom);this._targetLogZ=Math.log(s),this._targetTx=e,this._targetTy=i,this._needsRender=!0}_ensureOffscreenSizeLike(t,e){t.width===e.width&&t.height===e.height||(t.width=e.width,t.height=e.height)}_loop(){if(this._isResizing)return void(this._raf=requestAnimationFrame(()=>this._loop()));const t=performance.now(),e=Math.max(1,t-this._lastFrameTs);this._lastFrameTs=t;const{approachKZoom:i}=this._options,s=Math.exp(this._currentLogZ),n=this._targetLogZ-this._currentLogZ,h=Math.abs(n)>1e-6;if(h){const t=1-Math.exp(-i*e);this._currentLogZ+=n*t}const a=Math.exp(this._currentLogZ);if(a!==s){const t=this._anchorX,e=this._anchorY,i=a/s;this._tx=t-(t-this._tx)*i,this._ty=e-(e-this._ty)*i}let o=!1;if(null!==this._targetTx&&null!==this._targetTy){const t=1-Math.exp(-this._options.approachKPan*e),i=this._targetTx-this._tx,s=this._targetTy-this._ty;o=Math.abs(i)>.5||Math.abs(s)>.5,o?(this._tx+=i*t,this._ty+=s*t):(this._tx=this._targetTx,this._ty=this._targetTy,this._targetTx=null,this._targetTy=null)}if(this._isResetting){const t=1-Math.exp(-this._options.approachKPan*e);this._tx+=(0-this._tx)*t,this._ty+=(0-this._ty)*t;const i=Math.abs(this._currentLogZ)<.001&&Math.abs(this._targetLogZ)<1e-6,s=Math.abs(this._tx)<.5&&Math.abs(this._ty)<.5;i&&s&&(this._currentLogZ=0,this._targetLogZ=0,this._tx=0,this._ty=0,this._isResetting=!1)}for(const t of this._updateCallbacks)t(e);if(!(this._needsRender||h||o||this._isResetting))return void(this._raf=requestAnimationFrame(()=>this._loop()));this._needsRender=!1;const r=this.contentCanvas,c=this.contentContext,_=this.topScreenCanvas,l=this.topScreenContext,d=this.canvas,g=this.context;this._ensureOffscreenSizeLike(r,d),this._ensureOffscreenSizeLike(_,d),c.setTransform(1,0,0,1,0,0),l.setTransform(1,0,0,1,0,0),g.setTransform(1,0,0,1,0,0);const u=this._options.background;"string"==typeof u&&""!==u.trim()&&"transparent"!==u.toLowerCase()?(g.fillStyle=u,g.fillRect(0,0,d.width,d.height)):g.clearRect(0,0,d.width,d.height),c.clearRect(0,0,r.width,r.height),l.clearRect(0,0,_.width,_.height),c.setTransform(this._dpr*a,0,0,this._dpr*a,this._dpr*this._tx,this._dpr*this._ty);for(const t of this._beforeRenderCallbacks)t(c);this._render(this);for(const t of this._afterRenderCallbacks)t(c);g.drawImage(r,0,0),g.drawImage(_,0,0),this._raf=requestAnimationFrame(()=>this._loop())}applyWorldTransform(t){const e=Math.exp(this._currentLogZ);t.setTransform(this._dpr*e,0,0,this._dpr*e,this._dpr*this._tx,this._dpr*this._ty)}applyScreenTransform(t){t.setTransform(this._dpr,0,0,this._dpr,0,0)}getPixelColorAtScreen(t,e){const i=Math.floor(t*this._dpr),s=Math.floor(e*this._dpr);if(i<0||s<0||i>=this.canvas.width||s>=this.canvas.height)return{r:0,g:0,b:0,a:0,rgba:"rgba(0,0,0,0)",hex:"#000000"};const n=this.contentContext.getImageData(i,s,1,1).data,h=n[0],a=n[1],o=n[2],r=n[3]/255,c=t=>t.toString(16).padStart(2,"0"),_=`#${c(h)}${c(a)}${c(o)}`;return{r:h,g:a,b:o,a:r,rgba:`rgba(${h},${a},${o},${r.toFixed(3)})`,hex:_}}getPixelColorAtWorld(t,e){const{x:i,y:s}=this.toScreen(t,e);return this.getPixelColorAtScreen(i,s)}registerLayerManager(t){t&&(this._layerManagers.includes(t)||(this._layerManagers.push(t),this._needsRender=!0))}unregisterLayerManager(t){const e=this._layerManagers.indexOf(t);e>=0&&(this._layerManagers.splice(e,1),this._needsRender=!0)}getLayerManagers(){return[...this._layerManagers]}resetSmooth(){this._isResetting=!0,this._targetLogZ=0,this._needsRender=!0}resetInstant(){this._currentLogZ=0,this._targetLogZ=0,this._tx=0,this._ty=0,this._needsRender=!0}toWorld(t,e){const i=Math.exp(this._currentLogZ);return{wx:(t-this._tx)/i,wy:(e-this._ty)/i}}toScreen(t,e){const i=Math.exp(this._currentLogZ);return{x:t*i+this._tx,y:e*i+this._ty}}getTransform(){return{zoom:Math.exp(this._currentLogZ),tx:this._tx,ty:this._ty}}getViewportBounds(){const t=Math.exp(this._currentLogZ),e=this.canvas.width/this._dpr,i=this.canvas.height/this._dpr,s=-this._tx/t,n=-this._ty/t,h=(e-this._tx)/t,a=(i-this._ty)/t;return{left:s,top:n,right:h,bottom:a,width:h-s,height:a-n}}setZoomRange(t,e){this._options.minZoom=t,this._options.maxZoom=e,this.LOG_MIN=Math.log(t),this.LOG_MAX=Math.log(e),this._targetLogZ=Math.min(this.LOG_MAX,Math.max(this.LOG_MIN,this._targetLogZ))}resizeToParent(){this._isResizing=!0;const t=(this.canvas.parentElement||this.canvas).getBoundingClientRect();this._dpr=Math.max(1,window.devicePixelRatio||1);const e=Math.max(1,Math.round(t.width)),i=Math.max(1,Math.round(t.height));this.canvas.width=Math.round(e*this._dpr),this.canvas.height=Math.round(i*this._dpr),this.canvas.style.width=`${e}px`,this.canvas.style.height=`${i}px`,this._ensureOffscreenSizeLike(this.contentCanvas,this.canvas),this._ensureOffscreenSizeLike(this.topScreenCanvas,this.canvas),clearTimeout(this._resizeReleaseTimer),this._resizeReleaseTimer=window.setTimeout(()=>{this._isResizing=!1},50)}destroy(){cancelAnimationFrame(this._raf);for(const t of this._plugins.values())t.destroy();this._plugins.clear(),this._resizeObserver&&this._resizeObserver.disconnect(),this._layerManagers=[],this._updateCallbacks.clear(),this._beforeRenderCallbacks.clear(),this._afterRenderCallbacks.clear()}constructor(t,e,i){this._layerManagers=[],this._plugins=new Map,this._isResetting=!1,this._isResizing=!1,this._needsRender=!0,this._raf=0,this._lastFrameTs=performance.now(),this._tx=0,this._ty=0,this._anchorX=0,this._anchorY=0,this._currentLogZ=Math.log(1),this._targetLogZ=Math.log(1),this._targetTx=null,this._targetTy=null,this._updateCallbacks=new Set,this._beforeRenderCallbacks=new Set,this._afterRenderCallbacks=new Set,this._dpr=Math.max(1,window.devicePixelRatio||1);const s=t.getContext("2d",{willReadFrequently:!0,alpha:!0});if(!s)throw new Error("2D context not available");this.canvas=t,this.context=s,this._render=e,this.contentCanvas=document.createElement("canvas"),this.contentCanvas.width=t.width,this.contentCanvas.height=t.height,this.contentContext=this.contentCanvas.getContext("2d",{alpha:!0,willReadFrequently:!0}),this.topScreenCanvas=document.createElement("canvas"),this.topScreenCanvas.width=t.width,this.topScreenCanvas.height=t.height,this.topScreenContext=this.topScreenCanvas.getContext("2d",{alpha:!0}),this._options=Object.assign({minZoom:.5,maxZoom:10,approachKZoom:.022,approachKPan:.022,autoResize:!0,background:"#fff"},i),this.LOG_MIN=Math.log(this._options.minZoom),this.LOG_MAX=Math.log(this._options.maxZoom),this._options.autoResize&&(this._resizeObserver=new ResizeObserver(()=>this.resizeToParent()),this._resizeObserver.observe(this.canvas.parentElement||this.canvas)),this.resizeToParent(),this._lastFrameTs=performance.now(),this._raf=requestAnimationFrame(()=>this._loop())}},exports.createDocumentPlugin=function(t){return new d(t)},exports.createInteractionPlugin=function(t){return new l(t)},exports.createSnapshotCommand=function(t,e,i,s){return e&&i?new g(t,e,i,s):null};
1
+ "use strict";function t(t,e,i,s){return new(i||(i=Promise))(function(n,h){function a(t){try{r(s.next(t))}catch(t){h(t)}}function o(t){try{r(s.throw(t))}catch(t){h(t)}}function r(t){var e;t.done?n(t.value):(e=t.value,e instanceof i?e:new i(function(t){t(e)})).then(a,o)}r((s=s.apply(t,e||[])).next())})}function e(t,e,i,s){if("a"===i&&!s)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof e?t!==e||!s:!e.has(t))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===i?s:"a"===i?s.call(t):s?s.value:e.get(t)}function i(t,e,i,s,n){if("m"===s)throw new TypeError("Private method is not writable");if("a"===s&&!n)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof e?t!==e||!n:!e.has(t))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===s?n.call(t,i):n?n.value=i:e.set(t,i),i}"function"==typeof SuppressedError&&SuppressedError;const s=(e,i)=>t(void 0,void 0,void 0,function*(){return new Promise((t,s)=>{const n=new Image;"string"==typeof e?(void 0!==i&&(n.crossOrigin=i),n.src=e):n.src=URL.createObjectURL(e),n.onload=()=>{t(n)},n.onerror=()=>{s(new Error("Image load failed"))}})}),n=(t,e,i)=>Math.min(Math.max(t,e),i);let h=0;class a{constructor(t,e,i="world"){this.space="world",this.visible=!0,this.opacity=1,this.blend="source-over",this.name=t,this.id=`layer_${e}_${++h}`,this.type=e,this.space=i}}class o extends a{beginStroke(t,e){const{lx:i,ly:s}=this.toLocalPoint(t,e);this._lastX=i,this._lastY=s,this._drawing=!0}stroke(t,e,i,s,n=1,h="brush"){if(!this._drawing)return;const{lx:a,ly:o}=this.toLocalPoint(t,e);this.context.beginPath(),this.context.moveTo(this._lastX,this._lastY),this.context.lineTo(a,o),"eraser"===h?(this.context.globalCompositeOperation="destination-out",this.context.strokeStyle="rgba(0, 0, 0, 1)"):(this.context.globalCompositeOperation="source-over",this.context.strokeStyle=i),this.context.lineWidth=s*n,this.context.lineCap="round",this.context.lineJoin="round",this.context.stroke(),this.context.closePath(),this._lastX=a,this._lastY=o}endStroke(){this._drawing=!1}isDrawing(){return this._drawing}captureSnapshot(t){try{if(t){const{x:e,y:i,width:s,height:n}=t,h=Math.max(0,Math.floor(e)),a=Math.max(0,Math.floor(i)),o=Math.min(this.canvas.width-h,Math.ceil(s)),r=Math.min(this.canvas.height-a,Math.ceil(n));return o<=0||r<=0?null:this.context.getImageData(h,a,o,r)}return this.context.getImageData(0,0,this.canvas.width,this.canvas.height)}catch(t){return null}}restoreSnapshot(t,e){var i,s;const n=null!==(i=null==e?void 0:e.x)&&void 0!==i?i:0,h=null!==(s=null==e?void 0:e.y)&&void 0!==s?s:0;this.context.putImageData(t,n,h)}clearRegion(t){t?this.context.clearRect(t.x,t.y,t.width,t.height):this.context.clearRect(0,0,this.canvas.width,this.canvas.height)}requestRedraw(){var t;null===(t=this._redraw)||void 0===t||t.call(this,this.context,this.canvas)}drawImage(t,e,i,s,n){this.context.drawImage(t,e,i,null!=s?s:t.width,null!=n?n:t.height)}hitTest(t,e){const{lx:i,ly:s}=this.toLocalPoint(t,e);return i>=0&&i<=this.canvas.width&&s>=0&&s<=this.canvas.height}toLocalPoint(t,e){const i=t-this.x,s=e-this.y,n=Math.cos(-this.rotation),h=Math.sin(-this.rotation),a=i*h+s*n,o=(i*n-s*h)/this.scale,r=a/this.scale;return{lx:o+("center"===this.anchor?this.canvas.width/2:0),ly:r+("center"===this.anchor?this.canvas.height/2:0)}}render(t,e){if(!this.visible)return;const i=this.canvas.width*this.scale,s=this.canvas.height*this.scale,n="center"===this.anchor?-i/2:0,h="center"===this.anchor?-s/2:0;t.save(),t.globalAlpha=this.opacity,t.globalCompositeOperation=this.blend,t.translate(this.x,this.y),t.rotate(this.rotation),t.drawImage(this.canvas,n,h,i,s),t.restore()}cropTo(t){const e=Math.max(1,Math.floor(t.width)),i=Math.max(1,Math.floor(t.height));if(e===this.canvas.width&&i===this.canvas.height)return;const s=this._cloneCanvas(),n=Math.min(s.width,e),h=Math.min(s.height,i);this._setCanvasSize(e,i),n>0&&h>0&&this.context.drawImage(s,0,0,n,h,0,0,n,h)}resizeTo(t){const e=Math.max(1,Math.floor(t.width)),i=Math.max(1,Math.floor(t.height));if(e===this.canvas.width&&i===this.canvas.height)return;const s=this._cloneCanvas();this._setCanvasSize(e,i),s.width>0&&s.height>0&&this.context.drawImage(s,0,0,s.width,s.height,0,0,e,i)}_cloneCanvas(){const t=document.createElement("canvas");if(t.width=this.canvas.width,t.height=this.canvas.height,0===t.width||0===t.height)return t;const e=t.getContext("2d");if(!e)throw new Error("Offscreen 2D context unavailable");return e.drawImage(this.canvas,0,0),t}_setCanvasSize(t,e){this.canvas.width=t,this.canvas.height=e,this.context.setTransform(1,0,0,1,0,0),this.context.clearRect(0,0,t,e)}destroy(){this.canvas.width=0,this.canvas.height=0}constructor(t){var e,i;if(super(t.name||"","canvas",null!==(e=t.space)&&void 0!==e?e:"world"),this.x=0,this.y=0,this.scale=1,this.rotation=0,this.anchor="topLeft",this._drawing=!1,this._lastX=0,this._lastY=0,"canvas"in t&&t.canvas)this.canvas=t.canvas;else{const e=t;this.canvas=document.createElement("canvas"),this.canvas.width=e.width,this.canvas.height=e.height}const s=this.canvas.getContext("2d",{willReadFrequently:!0});if(!s)throw new Error("Offscreen 2D context unavailable");this.context=s,this.x=t.x||0,this.y=t.y||0,this.scale=null!==(i=t.scale)&&void 0!==i?i:1,this.rotation=t.rotation||0,t.anchor&&(this.anchor=t.anchor),this._redraw=t.redraw,this._redraw&&this._redraw(this.context,this.canvas)}}var r;class c extends o{static fromImage(e){return t(this,void 0,void 0,function*(){var t,n,h,a,o,_,l,d;const u=yield s(e.src,e.crossOrigin),g=null!==(t=e.width)&&void 0!==t?t:u.naturalWidth,p=null!==(n=e.height)&&void 0!==n?n:u.naturalHeight,v=new c({name:e.name,space:null!==(h=e.space)&&void 0!==h?h:"world",x:null!==(a=e.x)&&void 0!==a?a:0,y:null!==(o=e.y)&&void 0!==o?o:0,scale:null!==(_=e.scale)&&void 0!==_?_:1,rotation:null!==(l=e.rotation)&&void 0!==l?l:0,anchor:null!==(d=e.anchor)&&void 0!==d?d:"topLeft",width:g,height:p});return v.context.clearRect(0,0,v.canvas.width,v.canvas.height),v.context.drawImage(u,0,0,g,p),"string"!=typeof e.src&&i(v,r,u.src.startsWith("blob:")?u.src:null,"f"),v})}setSource(n,h){return t(this,void 0,void 0,function*(){const t=yield s(n,h);if(this.canvas.width=t.naturalWidth,this.canvas.height=t.naturalHeight,this.context.setTransform(1,0,0,1,0,0),this.context.clearRect(0,0,this.canvas.width,this.canvas.height),this.context.drawImage(t,0,0),e(this,r,"f")){try{URL.revokeObjectURL(e(this,r,"f"))}catch(t){}i(this,r,null,"f")}"string"!=typeof n&&i(this,r,t.src.startsWith("blob:")?t.src:null,"f")})}paint(t){t(this.context,this.canvas)}getImageData(t=0,e=0,i=this.canvas.width,s=this.canvas.height){return this.context.getImageData(t,e,i,s)}putImageData(t,e=0,i=0){this.context.putImageData(t,e,i)}toDataURL(t="image/png",e){return this.canvas.toDataURL(t,e)}toImageBitmap(t){return createImageBitmap(this.canvas,null!=t?t:{})}destroy(){var t;if(null===(t=super.destroy)||void 0===t||t.call(this),e(this,r,"f")){try{URL.revokeObjectURL(e(this,r,"f"))}catch(t){}i(this,r,null,"f")}}constructor(t){var e,i;super({name:t.name,space:null!==(e=t.space)&&void 0!==e?e:"world",x:t.x,y:t.y,scale:t.scale,rotation:t.rotation,anchor:null!==(i=t.anchor)&&void 0!==i?i:"topLeft",width:t.width,height:t.height}),r.set(this,null),this.type="bitmap"}}r=new WeakMap;class _{constructor(){this._worldLayers=[],this._screenLayers=[]}_renderAllLayersIn(t,e){e.save(),t.applyWorldTransform(e);for(const i of this._worldLayers)!i.visible||i.opacity<=0||(e.save(),i.render(e,t),e.restore());e.restore(),e.save(),t.applyScreenTransform(e);for(const i of this._screenLayers)!i.visible||i.opacity<=0||(e.save(),i.render(e,t),e.restore());e.restore()}addLayer(t,e){const i="world"===t.space?this._worldLayers:this._screenLayers;return"number"==typeof e&&e>=0&&e<i.length?(i.splice(e,0,t),t.id):(i.push(t),t.id)}createImageLayer(e){return t(this,void 0,void 0,function*(){const t=yield c.fromImage(e);return this.addLayer(t),t})}createCanvasLayer(t){const e=new o(t);return this.addLayer(e),e}removeLayer(t){var e,i,s,n;const h=this._worldLayers.findIndex(e=>e.id===t);if(h>=0)return null===(i=(e=this._worldLayers[h]).destroy)||void 0===i||i.call(e),void this._worldLayers.splice(h,1);const a=this._screenLayers.findIndex(e=>e.id===t);a>=0&&(null===(n=(s=this._screenLayers[a]).destroy)||void 0===n||n.call(s),this._screenLayers.splice(a,1))}detachLayer(t){const e=this._worldLayers.findIndex(e=>e.id===t);if(e>=0)return void this._worldLayers.splice(e,1);const i=this._screenLayers.findIndex(e=>e.id===t);i>=0&&this._screenLayers.splice(i,1)}moveLayer(t,e){const i=this._worldLayers.findIndex(e=>e.id===t);if(i>=0){const[t]=this._worldLayers.splice(i,1),s=Math.max(0,Math.min(e,this._worldLayers.length));return void this._worldLayers.splice(s,0,t)}const s=this._screenLayers.findIndex(e=>e.id===t);if(s>=0){const[t]=this._screenLayers.splice(s,1),i=Math.max(0,Math.min(e,this._screenLayers.length));this._screenLayers.splice(i,0,t)}}reorderLayers(t){const e=[];for(const i of t){const t=this._worldLayers.find(t=>t.id===i);t&&e.push(t)}for(const i of this._worldLayers)t.includes(i.id)||e.push(i);this._worldLayers=e}getLayer(t){return this._worldLayers.find(e=>e.id===t)||this._screenLayers.find(e=>e.id===t)}getAllLayers(t){return t?("world"===t?this._worldLayers:this._screenLayers).slice():[...this._worldLayers,...this._screenLayers]}hitTest(t,e,i="world"){const s=this.getAllLayers(i);for(let i=s.length-1;i>=0;i--){const n=s[i];if(n.hitTest&&n.hitTest(t,e))return n}}destroy(){var t;for(const e of[...this._worldLayers,...this._screenLayers])null===(t=e.destroy)||void 0===t||t.call(e);this._worldLayers=[],this._screenLayers=[]}}class l{constructor(t){this.name="interaction",this._view=null,this._dragging=!1,this._vx=0,this._vy=0,this._lastMoveTs=0,this._activePointerId=null,this._lastPointerX=0,this._lastPointerY=0,this._touchPointers=new Map,this._isTouchPanning=!1,this._isPinching=!1,this._lastPinchDistance=0,this._lastPinchCenterX=0,this._lastPinchCenterY=0,this._lastTouchX=0,this._lastTouchY=0,this._onDownBound=t=>this._onPointerDown(t),this._onMoveBound=t=>this._onPointerMove(t),this._onUpBound=t=>this._onPointerUp(t),this._onCancelBound=t=>this._onPointerCancel(t),this._onWheelBound=t=>this._onWheel(t),this._onUpdate=t=>{if(!this._view)return;const{friction:e,stopSpeed:i}=this._options,s=Math.hypot(this._vx,this._vy)>=i;if(!(this._dragging||this._isTouchPanning||this._isPinching)&&s){const s=this._vx*t,n=this._vy*t;this._view.panBy(s,n),this._vx*=e,this._vy*=e,Math.hypot(this._vx,this._vy)<i&&(this._vx=0,this._vy=0)}},this._options=Object.assign({panEnabled:!0,zoomEnabled:!0,friction:.92,stopSpeed:.02,emaAlpha:.25,idleNoInertiaMs:120,wheelSensitivity:.0015},t),this._panEnabled=this._options.panEnabled,this._zoomEnabled=this._options.zoomEnabled}install(t){this._view=t;const e=t.canvas;e.style.touchAction="none",e.addEventListener("wheel",this._onWheelBound,{passive:!1}),e.addEventListener("pointerdown",this._onDownBound),window.addEventListener("pointermove",this._onMoveBound),window.addEventListener("pointerup",this._onUpBound),window.addEventListener("pointercancel",this._onCancelBound),t.onUpdate(this._onUpdate)}destroy(){if(!this._view)return;const t=this._view.canvas;t.removeEventListener("wheel",this._onWheelBound),t.removeEventListener("pointerdown",this._onDownBound),window.removeEventListener("pointermove",this._onMoveBound),window.removeEventListener("pointerup",this._onUpBound),window.removeEventListener("pointercancel",this._onCancelBound),this._touchPointers.clear(),this._isPinching=!1,this._isTouchPanning=!1,this._view.offUpdate(this._onUpdate),this._view=null}isPanEnabled(){return this._panEnabled}isZoomEnabled(){return this._zoomEnabled}setPanEnabled(t){this._panEnabled!==t&&(this._panEnabled=t,t||null===this._activePointerId||(this._dragging=!1,this._vx=0,this._vy=0))}setZoomEnabled(t){this._zoomEnabled!==t&&(this._zoomEnabled=t,!t&&this._isPinching&&this._endPinch())}setWheelSensitivity(t){this._options.wheelSensitivity=t}isDragging(){return this._dragging||this._isTouchPanning||this._isPinching}_onPointerDown(t){var e;if("touch"!==t.pointerType){if(("mouse"!==t.pointerType||0===t.button)&&this._panEnabled&&null===this._activePointerId){t.preventDefault(),this._dragging=!0,this._vx=0,this._vy=0,this._lastMoveTs=performance.now(),this._activePointerId=t.pointerId,this._lastPointerX=t.clientX,this._lastPointerY=t.clientY;try{null===(e=this._view)||void 0===e||e.canvas.setPointerCapture(t.pointerId)}catch(t){}}}else this._onTouchDown(t)}_onPointerMove(t){if("touch"===t.pointerType)return void this._onTouchMove(t);if(t.pointerId!==this._activePointerId)return;if(!this._dragging||!this._panEnabled||!this._view)return;const e=performance.now(),i=Math.max(1,e-(this._lastMoveTs||e-16));this._lastMoveTs=e;const s=t.clientX-this._lastPointerX,n=t.clientY-this._lastPointerY;this._lastPointerX=t.clientX,this._lastPointerY=t.clientY,this._view.panBy(s,n);const h=this._options.emaAlpha,a=s/i,o=n/i;this._vx=(1-h)*this._vx+h*a,this._vy=(1-h)*this._vy+h*o}_onPointerUp(t){if("touch"===t.pointerType)return void this._onTouchUp(t);if(t.pointerId!==this._activePointerId)return;if(!this._dragging)return;this._dragging=!1;const e=performance.now(),i=this._lastMoveTs?e-this._lastMoveTs:1/0;if(null!=this._activePointerId&&this._view){try{this._view.canvas.releasePointerCapture(this._activePointerId)}catch(t){}this._activePointerId=null}if(i>=this._options.idleNoInertiaMs)this._vx=0,this._vy=0;else{const t=Math.pow(this._options.friction,i/16);this._vx*=t,this._vy*=t}Math.hypot(this._vx,this._vy)<this._options.stopSpeed&&(this._vx=0,this._vy=0)}_onPointerCancel(t){"touch"!==t.pointerType?t.pointerId===this._activePointerId&&(this._dragging=!1,this._vx=0,this._vy=0,this._activePointerId=null):this._onTouchCancel(t)}_onTouchDown(t){if(t.preventDefault(),this._touchPointers.set(t.pointerId,{x:t.clientX,y:t.clientY}),2===this._touchPointers.size&&this._zoomEnabled)return this._isTouchPanning=!1,void this._startPinch();1===this._touchPointers.size&&(this._isTouchPanning=!0,this._vx=0,this._vy=0,this._lastMoveTs=performance.now(),this._lastTouchX=t.clientX,this._lastTouchY=t.clientY)}_onTouchMove(t){if(this._touchPointers.has(t.pointerId))if(this._touchPointers.set(t.pointerId,{x:t.clientX,y:t.clientY}),this._isPinching&&2===this._touchPointers.size)this._handlePinchMove();else if(this._isTouchPanning&&1===this._touchPointers.size&&this._view){const e=performance.now(),i=Math.max(1,e-(this._lastMoveTs||e-16));this._lastMoveTs=e;const s=t.clientX-this._lastTouchX,n=t.clientY-this._lastTouchY;this._lastTouchX=t.clientX,this._lastTouchY=t.clientY,this._view.panBy(s,n);const h=this._options.emaAlpha,a=s/i,o=n/i;this._vx=(1-h)*this._vx+h*a,this._vy=(1-h)*this._vy+h*o}}_onTouchUp(t){if(this._touchPointers.delete(t.pointerId),this._isPinching){if(this._endPinch(),1===this._touchPointers.size){const[[,t]]=this._touchPointers.entries();this._isTouchPanning=!0,this._lastTouchX=t.x,this._lastTouchY=t.y,this._vx=0,this._vy=0,this._lastMoveTs=performance.now()}}else if(this._isTouchPanning&&0===this._touchPointers.size){this._isTouchPanning=!1;const t=performance.now(),e=this._lastMoveTs?t-this._lastMoveTs:1/0;if(e>=this._options.idleNoInertiaMs)this._vx=0,this._vy=0;else{const t=Math.pow(this._options.friction,e/16);this._vx*=t,this._vy*=t}Math.hypot(this._vx,this._vy)<this._options.stopSpeed&&(this._vx=0,this._vy=0)}}_onTouchCancel(t){this._touchPointers.delete(t.pointerId),this._isPinching&&this._endPinch(),0===this._touchPointers.size&&(this._isTouchPanning=!1,this._vx=0,this._vy=0)}_startPinch(){this._isPinching=!0;const[t,e]=Array.from(this._touchPointers.values());this._lastPinchDistance=Math.hypot(e.x-t.x,e.y-t.y),this._lastPinchCenterX=(t.x+e.x)/2,this._lastPinchCenterY=(t.y+e.y)/2}_handlePinchMove(){if(!this._view||!this._zoomEnabled)return;const t=Array.from(this._touchPointers.values());if(2!==t.length)return;const[e,i]=t,s=Math.hypot(i.x-e.x,i.y-e.y),n=(e.x+i.x)/2,h=(e.y+i.y)/2;if(this._lastPinchDistance>0){const t=s/this._lastPinchDistance,e=this._view.canvas.getBoundingClientRect(),i=n-e.left,a=h-e.top;this._view.zoomByFactorAtScreen(i,a,t);const o=n-this._lastPinchCenterX,r=h-this._lastPinchCenterY;this._view.panBy(o,r)}this._lastPinchDistance=s,this._lastPinchCenterX=n,this._lastPinchCenterY=h}_endPinch(){this._isPinching=!1,this._lastPinchDistance=0,this._lastPinchCenterX=0,this._lastPinchCenterY=0}_getLineHeightPx(){if(!this._view)return 16;const t=getComputedStyle(this._view.canvas).lineHeight;if(!t||"normal"===t)return 16;const e=parseFloat(t);return Number.isFinite(e)?e:16}_normalizeWheelDelta(t){if(!this._view)return 0;let e=t.deltaY;if(1===t.deltaMode)e*=this._getLineHeightPx();else if(2===t.deltaMode){e*=this._view.canvas.clientHeight||window.innerHeight||800}return e}_onWheel(t){if(!this._zoomEnabled||!this._view)return;t.preventDefault(),t.stopPropagation();const e=this._normalizeWheelDelta(t),i=this._view.canvas.getBoundingClientRect(),s=t.clientX-i.left,n=t.clientY-i.top;let h=-e*this._options.wheelSensitivity;t.ctrlKey||t.metaKey?h*=1.6:t.shiftKey&&(h*=.6),this._view.zoomByLogAtScreen(s,n,h)}}class d{constructor(t){var e,i,s,n,h;this.name="document",this._view=null,this._enabled=!1,this._x=0,this._y=0,this._width=0,this._height=0,this._marginL=0,this._marginR=0,this._marginT=0,this._marginB=0,this._panClampMode="minVisible",this._background=null,this._shadow=null,this._onUpdate=()=>{this._enabled&&this._view&&this._clampPan()},this._onBeforeRender=t=>{var e,i,s,n,h;this._enabled&&this._view&&(this._shadow?(t.save(),t.shadowColor=null!==(e=this._shadow.color)&&void 0!==e?e:d._defaultShadow.color,t.shadowBlur=null!==(i=this._shadow.blur)&&void 0!==i?i:d._defaultShadow.blur,t.shadowOffsetX=null!==(s=this._shadow.offsetX)&&void 0!==s?s:d._defaultShadow.offsetX,t.shadowOffsetY=null!==(n=this._shadow.offsetY)&&void 0!==n?n:d._defaultShadow.offsetY,t.fillStyle=null!==(h=this._background)&&void 0!==h?h:"#ffffff",t.fillRect(this._x,this._y,this._width,this._height),t.restore()):this._background&&(t.save(),t.fillStyle=this._background,t.fillRect(this._x,this._y,this._width,this._height),t.restore()),t.save(),t.beginPath(),t.rect(this._x,this._y,this._width,this._height),t.clip())},this._onAfterRender=t=>{if(this._enabled&&this._view&&(t.restore(),this._options.drawBorder)){const e=this._view.zoom;t.save(),t.lineWidth=1/e,t.strokeStyle="#cfcfcf",t.strokeRect(this._x,this._y,this._width,this._height),t.restore()}},this._options=Object.assign({rect:{x:0,y:0,width:0,height:0},margins:{},drawBorder:!1,minVisiblePx:30,panClampMode:"minVisible",background:null,shadow:!1},t),(null==t?void 0:t.rect)&&(this._enabled=!0,this._x=t.rect.x,this._y=t.rect.y,this._width=t.rect.width,this._height=t.rect.height),(null==t?void 0:t.margins)&&(this._marginL=null!==(e=t.margins.left)&&void 0!==e?e:0,this._marginR=null!==(i=t.margins.right)&&void 0!==i?i:0,this._marginT=null!==(s=t.margins.top)&&void 0!==s?s:0,this._marginB=null!==(n=t.margins.bottom)&&void 0!==n?n:0),this._panClampMode=this._options.panClampMode,this._background=null!==(h=this._options.background)&&void 0!==h?h:null,!0===this._options.shadow?this._shadow=Object.assign({},d._defaultShadow):this._options.shadow&&"object"==typeof this._options.shadow&&(this._shadow=Object.assign(Object.assign({},d._defaultShadow),this._options.shadow))}install(t){this._view=t,t.onUpdate(this._onUpdate),t.onBeforeRender(this._onBeforeRender),t.onAfterRender(this._onAfterRender)}destroy(){this._view&&(this._view.offUpdate(this._onUpdate),this._view.offBeforeRender(this._onBeforeRender),this._view.offAfterRender(this._onAfterRender),this._view=null)}isEnabled(){return this._enabled}getRect(){return{x:this._x,y:this._y,width:this._width,height:this._height}}setRect(t,e,i,s){this._enabled=!0,this._x=t,this._y=e,this._width=i,this._height=s}clearRect(){this._enabled=!1}setMargins(t){var e,i,s,n;this._marginL=null!==(e=t.left)&&void 0!==e?e:this._marginL,this._marginR=null!==(i=t.right)&&void 0!==i?i:this._marginR,this._marginT=null!==(s=t.top)&&void 0!==s?s:this._marginT,this._marginB=null!==(n=t.bottom)&&void 0!==n?n:this._marginB}getMargins(){return{left:this._marginL,right:this._marginR,top:this._marginT,bottom:this._marginB}}setPanClampMode(t){this._panClampMode=t}getPanClampMode(){return this._panClampMode}cropTo(t){this._doResize("crop",t)}resizeTo(t){this._doResize("resize",t)}zoomToFit(t="contain",e=!0){if(!this._enabled||!this._view)return;const i=this._view.dpr,s=this._view.canvas.width/i,n=this._view.canvas.height/i,h=Math.max(1,s-(this._marginL+this._marginR)),a=Math.max(1,n-(this._marginT+this._marginB));let o;const r=h/this._width,c=a/this._height;o="contain"===t?Math.min(r,c):"cover"===t?Math.max(r,c):"fitWidth"===t?r:c,o=Math.min(this._view.maxZoom,Math.max(this._view.minZoom,o));const _=this._marginL+(h-o*this._width)/2,l=this._marginT+(a-o*this._height)/2,d=_-o*this._x,u=l-o*this._y;e?this._view.setTransformSmooth(o,d,u):this._view.setTransform(o,d,u)}isPointInDocument(t,e){return!this._enabled||t>=this._x&&t<=this._x+this._width&&e>=this._y&&e<=this._y+this._height}get background(){return this._background}setBackground(t){var e;this._background=t,null===(e=this._view)||void 0===e||e.requestRender()}get shadow(){return this._shadow?Object.assign({},this._shadow):null}setShadow(t){var e;this._shadow=!0===t?Object.assign({},d._defaultShadow):t&&"object"==typeof t?Object.assign(Object.assign({},d._defaultShadow),t):null,null===(e=this._view)||void 0===e||e.requestRender()}_clampPan(){if(!this._view)return;const{zoom:t,tx:e,ty:i}=this._view.getTransform(),s=t,n=this._view.dpr,h=this._view.canvas.width/n,a=this._view.canvas.height/n,o=this._x,r=this._y,c=this._x+this._width,_=this._y+this._height;let l=e,d=i;if("margin"===this._panClampMode){const t=this._marginL-s*o,n=h-this._marginR-s*c,u=this._marginT-s*r,g=a-this._marginB-s*_,p=Math.max(1,h-(this._marginL+this._marginR)),v=Math.max(1,a-(this._marginT+this._marginB));l=s*this._width<=p?this._marginL+(p-s*this._width)/2-s*this._x:Math.min(t,Math.max(n,e)),d=s*this._height<=v?this._marginT+(v-s*this._height)/2-s*this._y:Math.min(u,Math.max(g,i))}else if("minVisible"===this._panClampMode){const t=s*this._width,n=s*this._height,u=Math.min(this._options.minVisiblePx,t),g=Math.min(this._options.minVisiblePx,n),p=h-u-s*o,v=u-s*c,m=a-g-s*r,y=g-s*_;l=v<=p?Math.min(p,Math.max(v,e)):(v+p)/2,d=y<=m?Math.min(m,Math.max(y,i)):(y+m)/2}l===e&&d===i||this._view.setPan(l,d)}_doResize(t,e){if(!this._view)return;const i=Math.max(1,Math.floor(e.width)),s=Math.max(1,Math.floor(e.height)),n={width:i,height:s},h=this._view.getLayerManagers();for(const e of h){const i=e.getAllLayers("world");for(const e of i){const i=e;"function"==typeof i.cropTo&&"function"==typeof i.resizeTo&&("crop"===t?i.cropTo(n):i.resizeTo(n))}}this._width=i,this._height=s,this._enabled&&this._clampPan()}}d._defaultShadow={color:"rgba(0, 0, 0, 0.3)",blur:20,offsetX:0,offsetY:4};class u{constructor(t,e,i,s){this.type="snapshot",this._isExecuted=!0,this._target=t,this._beforeData=e,this._afterData=i,this._region=null==s?void 0:s.region}execute(){if(this._isExecuted)return;const t=this._region?{x:this._region.x,y:this._region.y}:void 0;this._target.restoreSnapshot(this._afterData,t),this._isExecuted=!0}undo(){if(!this._isExecuted)return;const t=this._region?{x:this._region.x,y:this._region.y}:void 0;this._target.restoreSnapshot(this._beforeData,t),this._isExecuted=!1}canMerge(){return!1}merge(){return this}}exports.BitmapLayer=c,exports.CanvasLayer=o,exports.ContentLayerManager=class extends _{constructor(){super(...arguments),this._compositeCache=null,this._compositeCacheCtx=null,this._compositeDirty=!0,this._lastCacheWidth=0,this._lastCacheHeight=0,this._cachedBoundsMinX=0,this._cachedBoundsMinY=0}markDirty(){this._compositeDirty=!0}addLayer(t,e){return this._compositeDirty=!0,super.addLayer(t,e)}removeLayer(t){this._compositeDirty=!0,super.removeLayer(t)}detachLayer(t){this._compositeDirty=!0,super.detachLayer(t)}removeAllLayers(t){var e,i;if(!t||"world"===t){for(const t of this._worldLayers)null===(e=t.destroy)||void 0===e||e.call(t);this._worldLayers=[]}if(!t||"screen"===t){for(const t of this._screenLayers)null===(i=t.destroy)||void 0===i||i.call(t);this._screenLayers=[]}}moveLayer(t,e){this._compositeDirty=!0,super.moveLayer(t,e)}renderAllLayersIn(t){const e=t.contentContext,i=this._worldLayers;0!==i.length&&(this._compositeDirty&&this._rebuildCompositeCache(i),this._compositeCache&&this._compositeCacheCtx&&e.drawImage(this._compositeCache,this._cachedBoundsMinX,this._cachedBoundsMinY))}_rebuildCompositeCache(t){let e=0,i=0,s=0,n=0,h=!1;for(const a of t){const t=a,o=t.canvas.width*t.scale,r=t.canvas.height*t.scale,c="center"===t.anchor?-o/2:0,_="center"===t.anchor?-r/2:0,l=Math.cos(t.rotation),d=Math.sin(t.rotation),u=[{x:c,y:_},{x:c+o,y:_},{x:c,y:_+r},{x:c+o,y:_+r}];let g=1/0,p=1/0,v=-1/0,m=-1/0;for(const e of u){const i=e.x*l-e.y*d+t.x,s=e.x*d+e.y*l+t.y;g=Math.min(g,i),p=Math.min(p,s),v=Math.max(v,i),m=Math.max(m,s)}h?(e=Math.min(e,g),i=Math.min(i,p),s=Math.max(s,v),n=Math.max(n,m)):(e=g,i=p,s=v,n=m,h=!0)}if(!h)return void(this._compositeDirty=!1);const a=Math.ceil(s-e),o=Math.ceil(n-i);this._compositeCache||(this._compositeCache=document.createElement("canvas"),this._compositeCacheCtx=this._compositeCache.getContext("2d",{alpha:!0})),this._lastCacheWidth===a&&this._lastCacheHeight===o||(this._compositeCache.width=a,this._compositeCache.height=o,this._lastCacheWidth=a,this._lastCacheHeight=o),this._cachedBoundsMinX=e,this._cachedBoundsMinY=i;const r=this._compositeCacheCtx;r.clearRect(0,0,a,o),r.save(),r.translate(-e,-i);for(const e of t)!e.visible||e.opacity<=0||(r.save(),e.render(r),r.restore());r.restore(),this._compositeDirty=!1}destroy(){super.destroy(),this._compositeCache=null,this._compositeCacheCtx=null}},exports.CreateLayerCommand=class{constructor(t){this.type="create-layer",this._layer=t.layer,this._layerManager=t.layerManager,this._insertIndex=t.insertIndex,this._previousSelectedId=t.previousSelectedId,this._getLayerList=t.getLayerList,this._setSelectedId=t.setSelectedId}execute(){this._layerManager.addLayer(this._layer,this._insertIndex);this._getLayerList().splice(this._insertIndex,0,this._layer),this._setSelectedId(this._layer.id)}undo(){this._layerManager.detachLayer(this._layer.id);const t=this._getLayerList(),e=t.findIndex(t=>t.id===this._layer.id);e>-1&&t.splice(e,1),this._setSelectedId(this._previousSelectedId)}},exports.DeleteLayerCommand=class{constructor(t){this.type="delete-layer",this._layer=t.layer,this._layerManager=t.layerManager,this._originalIndex=t.originalIndex,this._previousSelectedId=t.previousSelectedId,this._newSelectedId=t.newSelectedId,this._getLayerList=t.getLayerList,this._setSelectedId=t.setSelectedId}execute(){this._layerManager.detachLayer(this._layer.id);const t=this._getLayerList(),e=t.findIndex(t=>t.id===this._layer.id);e>-1&&t.splice(e,1),this._setSelectedId(this._newSelectedId)}undo(){this._layerManager.addLayer(this._layer,this._originalIndex);this._getLayerList().splice(this._originalIndex,0,this._layer),this._setSelectedId(this._previousSelectedId)}},exports.DocumentPlugin=d,exports.HistoryManager=class{executeCommand(t){t.execute(),this.addCommand(t)}addCommand(t){var e,i;this._redoStack=[];const s=this._undoStack[this._undoStack.length-1];if(s&&(null===(e=s.canMerge)||void 0===e?void 0:e.call(s,t))&&s.merge){const e=null!==(i=s.merge(t))&&void 0!==i?i:s;return void(e!==s&&(this._undoStack[this._undoStack.length-1]=e))}this._undoStack.push(t),this._undoStack.length>this._maxHistorySize&&this._undoStack.shift()}undo(){if(0===this._undoStack.length)return null;const t=this._undoStack.pop();return t.undo(),this._redoStack.push(t),t}redo(){if(0===this._redoStack.length)return null;const t=this._redoStack.pop();return t.execute(),this._undoStack.push(t),t}canUndo(){return this._undoStack.length>0}canRedo(){return this._redoStack.length>0}clear(){this._undoStack=[],this._redoStack=[]}setMaxHistorySize(t){this._maxHistorySize=Math.max(1,t),this._undoStack.length>this._maxHistorySize&&(this._undoStack=this._undoStack.slice(-this._maxHistorySize))}constructor(t){var e,i,s;this._undoStack=[],this._redoStack=[],this._maxHistorySize=null!==(e=null==t?void 0:t.maxHistorySize)&&void 0!==e?e:50,this._undoStack=null!==(i=null==t?void 0:t.undoStack)&&void 0!==i?i:[],this._redoStack=null!==(s=null==t?void 0:t.redoStack)&&void 0!==s?s:[]}},exports.InteractionPlugin=l,exports.LayerBase=a,exports.LayerManagerBase=_,exports.SnapshotCommand=u,exports.TopScreenLayerManager=class extends _{renderAllLayersIn(t){const e=t.topScreenContext;this._renderAllLayersIn(t,e)}},exports.ViewManager=class{get zoom(){return Math.exp(this._currentLogZ)}get minZoom(){return this._options.minZoom}get maxZoom(){return this._options.maxZoom}get dpr(){return this._dpr}use(t){return this._plugins.has(t.name)?(console.warn(`Plugin "${t.name}" is already installed.`),t):(this._plugins.set(t.name,t),t.install(this),t)}unuse(t){const e=this._plugins.get(t);e&&(e.destroy(),this._plugins.delete(t))}getPlugin(t){return this._plugins.get(t)}onUpdate(t){this._updateCallbacks.add(t)}offUpdate(t){this._updateCallbacks.delete(t)}onBeforeRender(t){this._beforeRenderCallbacks.add(t)}offBeforeRender(t){this._beforeRenderCallbacks.delete(t)}onAfterRender(t){this._afterRenderCallbacks.add(t)}offAfterRender(t){this._afterRenderCallbacks.delete(t)}requestRender(){this._needsRender=!0}_clampLog(t){return n(t,this.LOG_MIN,this.LOG_MAX)}_setTargetLogZoomAtScreen(t,e,i){Number.isFinite(i)&&(this._anchorX=t,this._anchorY=e,this._targetLogZ=this._clampLog(i),this._needsRender=!0)}zoomToAtScreen(t,e,i){this._setTargetLogZoomAtScreen(t,e,Math.log(i))}zoomToAtScreenRaw(t,e,i){if(!Number.isFinite(i))return;const s=Math.max(1e-8,this._options.minZoom),h=this._options.maxZoom,a=n(i,s,h),o=Math.exp(this._currentLogZ),r=a;if(!Number.isFinite(o)||o<=0)return;if(Math.abs(r-o)<1e-12)return;const c=Math.log(a);this._currentLogZ=c,this._targetLogZ=c;const _=r/o;this._tx=t-(t-this._tx)*_,this._ty=e-(e-this._ty)*_,this._needsRender=!0}zoomToAtWorld(t,e,i){const{x:s,y:n}=this.toScreen(t,e);this.zoomToAtScreen(s,n,i)}zoomByFactorAtScreen(t,e,i){if(i<=0||!Number.isFinite(i))return;const s=Math.log(i);this._setTargetLogZoomAtScreen(t,e,this._targetLogZ+s)}zoomByLogAtScreen(t,e,i){this._setTargetLogZoomAtScreen(t,e,this._targetLogZ+i)}zoomByFactorAtWorld(t,e,i){const{x:s,y:n}=this.toScreen(t,e);this.zoomByFactorAtScreen(s,n,i)}panBy(t,e){this._tx+=t,this._ty+=e,this._needsRender=!0}setPan(t,e){this._tx=t,this._ty=e,this._needsRender=!0}setTransform(t,e,i){const s=n(t,this._options.minZoom,this._options.maxZoom);this._currentLogZ=Math.log(s),this._targetLogZ=this._currentLogZ,this._tx=e,this._ty=i,this._targetTx=null,this._targetTy=null,this._needsRender=!0}setTransformSmooth(t,e,i){const s=n(t,this._options.minZoom,this._options.maxZoom);this._targetLogZ=Math.log(s),this._targetTx=e,this._targetTy=i,this._needsRender=!0}_ensureOffscreenSizeLike(t,e){t.width===e.width&&t.height===e.height||(t.width=e.width,t.height=e.height)}_loop(){if(this._isResizing)return void(this._raf=requestAnimationFrame(()=>this._loop()));const t=performance.now(),e=Math.max(1,t-this._lastFrameTs);this._lastFrameTs=t;const{approachKZoom:i}=this._options,s=Math.exp(this._currentLogZ),n=this._targetLogZ-this._currentLogZ,h=Math.abs(n)>1e-6;if(h){const t=1-Math.exp(-i*e);this._currentLogZ+=n*t}const a=Math.exp(this._currentLogZ);if(a!==s){const t=this._anchorX,e=this._anchorY,i=a/s;this._tx=t-(t-this._tx)*i,this._ty=e-(e-this._ty)*i}let o=!1;if(null!==this._targetTx&&null!==this._targetTy){const t=1-Math.exp(-this._options.approachKPan*e),i=this._targetTx-this._tx,s=this._targetTy-this._ty;o=Math.abs(i)>.5||Math.abs(s)>.5,o?(this._tx+=i*t,this._ty+=s*t):(this._tx=this._targetTx,this._ty=this._targetTy,this._targetTx=null,this._targetTy=null)}if(this._isResetting){const t=1-Math.exp(-this._options.approachKPan*e);this._tx+=(0-this._tx)*t,this._ty+=(0-this._ty)*t;const i=Math.abs(this._currentLogZ)<.001&&Math.abs(this._targetLogZ)<1e-6,s=Math.abs(this._tx)<.5&&Math.abs(this._ty)<.5;i&&s&&(this._currentLogZ=0,this._targetLogZ=0,this._tx=0,this._ty=0,this._isResetting=!1)}for(const t of this._updateCallbacks)t(e);if(!(this._needsRender||h||o||this._isResetting))return void(this._raf=requestAnimationFrame(()=>this._loop()));this._needsRender=!1;const r=this.contentCanvas,c=this.contentContext,_=this.topScreenCanvas,l=this.topScreenContext,d=this.canvas,u=this.context;this._ensureOffscreenSizeLike(r,d),this._ensureOffscreenSizeLike(_,d),c.setTransform(1,0,0,1,0,0),l.setTransform(1,0,0,1,0,0),u.setTransform(1,0,0,1,0,0);const g=this._options.background;"string"==typeof g&&""!==g.trim()&&"transparent"!==g.toLowerCase()?(u.fillStyle=g,u.fillRect(0,0,d.width,d.height)):u.clearRect(0,0,d.width,d.height),c.clearRect(0,0,r.width,r.height),l.clearRect(0,0,_.width,_.height),c.setTransform(this._dpr*a,0,0,this._dpr*a,this._dpr*this._tx,this._dpr*this._ty);for(const t of this._beforeRenderCallbacks)t(c);this._render(this);for(const t of this._afterRenderCallbacks)t(c);u.drawImage(r,0,0),u.drawImage(_,0,0),this._raf=requestAnimationFrame(()=>this._loop())}applyWorldTransform(t){const e=Math.exp(this._currentLogZ);t.setTransform(this._dpr*e,0,0,this._dpr*e,this._dpr*this._tx,this._dpr*this._ty)}applyScreenTransform(t){t.setTransform(this._dpr,0,0,this._dpr,0,0)}getPixelColorAtScreen(t,e){const i=Math.floor(t*this._dpr),s=Math.floor(e*this._dpr);if(i<0||s<0||i>=this.canvas.width||s>=this.canvas.height)return{r:0,g:0,b:0,a:0,rgba:"rgba(0,0,0,0)",hex:"#000000"};const n=this.contentContext.getImageData(i,s,1,1).data,h=n[0],a=n[1],o=n[2],r=n[3]/255,c=t=>t.toString(16).padStart(2,"0"),_=`#${c(h)}${c(a)}${c(o)}`;return{r:h,g:a,b:o,a:r,rgba:`rgba(${h},${a},${o},${r.toFixed(3)})`,hex:_}}getPixelColorAtWorld(t,e){const{x:i,y:s}=this.toScreen(t,e);return this.getPixelColorAtScreen(i,s)}registerLayerManager(t){t&&(this._layerManagers.includes(t)||(this._layerManagers.push(t),this._needsRender=!0))}unregisterLayerManager(t){const e=this._layerManagers.indexOf(t);e>=0&&(this._layerManagers.splice(e,1),this._needsRender=!0)}getLayerManagers(){return[...this._layerManagers]}resetSmooth(){this._isResetting=!0,this._targetLogZ=0,this._needsRender=!0}resetInstant(){this._currentLogZ=0,this._targetLogZ=0,this._tx=0,this._ty=0,this._needsRender=!0}toWorld(t,e){const i=Math.exp(this._currentLogZ);return{wx:(t-this._tx)/i,wy:(e-this._ty)/i}}toScreen(t,e){const i=Math.exp(this._currentLogZ);return{x:t*i+this._tx,y:e*i+this._ty}}getTransform(){return{zoom:Math.exp(this._currentLogZ),tx:this._tx,ty:this._ty}}getViewportBounds(){const t=Math.exp(this._currentLogZ),e=this.canvas.width/this._dpr,i=this.canvas.height/this._dpr,s=-this._tx/t,n=-this._ty/t,h=(e-this._tx)/t,a=(i-this._ty)/t;return{left:s,top:n,right:h,bottom:a,width:h-s,height:a-n}}setZoomRange(t,e){this._options.minZoom=t,this._options.maxZoom=e,this.LOG_MIN=Math.log(t),this.LOG_MAX=Math.log(e),this._targetLogZ=Math.min(this.LOG_MAX,Math.max(this.LOG_MIN,this._targetLogZ))}resizeToParent(){this._isResizing=!0;const t=(this.canvas.parentElement||this.canvas).getBoundingClientRect();this._dpr=Math.max(1,window.devicePixelRatio||1);const e=Math.max(1,Math.round(t.width)),i=Math.max(1,Math.round(t.height));this.canvas.width=Math.round(e*this._dpr),this.canvas.height=Math.round(i*this._dpr),this.canvas.style.width=`${e}px`,this.canvas.style.height=`${i}px`,this._ensureOffscreenSizeLike(this.contentCanvas,this.canvas),this._ensureOffscreenSizeLike(this.topScreenCanvas,this.canvas),clearTimeout(this._resizeReleaseTimer),this._resizeReleaseTimer=window.setTimeout(()=>{this._isResizing=!1},50)}destroy(){cancelAnimationFrame(this._raf);for(const t of this._plugins.values())t.destroy();this._plugins.clear(),this._resizeObserver&&this._resizeObserver.disconnect(),this._layerManagers=[],this._updateCallbacks.clear(),this._beforeRenderCallbacks.clear(),this._afterRenderCallbacks.clear()}constructor(t,e,i){this._layerManagers=[],this._plugins=new Map,this._isResetting=!1,this._isResizing=!1,this._needsRender=!0,this._raf=0,this._lastFrameTs=performance.now(),this._tx=0,this._ty=0,this._anchorX=0,this._anchorY=0,this._currentLogZ=Math.log(1),this._targetLogZ=Math.log(1),this._targetTx=null,this._targetTy=null,this._updateCallbacks=new Set,this._beforeRenderCallbacks=new Set,this._afterRenderCallbacks=new Set,this._dpr=Math.max(1,window.devicePixelRatio||1);const s=t.getContext("2d",{willReadFrequently:!0,alpha:!0});if(!s)throw new Error("2D context not available");this.canvas=t,this.context=s,this._render=e,this.contentCanvas=document.createElement("canvas"),this.contentCanvas.width=t.width,this.contentCanvas.height=t.height,this.contentContext=this.contentCanvas.getContext("2d",{alpha:!0,willReadFrequently:!0}),this.topScreenCanvas=document.createElement("canvas"),this.topScreenCanvas.width=t.width,this.topScreenCanvas.height=t.height,this.topScreenContext=this.topScreenCanvas.getContext("2d",{alpha:!0}),this._options=Object.assign({minZoom:.5,maxZoom:10,approachKZoom:.022,approachKPan:.022,autoResize:!0,background:"#fff"},i),this.LOG_MIN=Math.log(this._options.minZoom),this.LOG_MAX=Math.log(this._options.maxZoom),this._options.autoResize&&(this._resizeObserver=new ResizeObserver(()=>this.resizeToParent()),this._resizeObserver.observe(this.canvas.parentElement||this.canvas)),this.resizeToParent(),this._lastFrameTs=performance.now(),this._raf=requestAnimationFrame(()=>this._loop())}},exports.createDocumentPlugin=function(t){return new d(t)},exports.createInteractionPlugin=function(t){return new l(t)},exports.createSnapshotCommand=function(t,e,i,s){return e&&i?new u(t,e,i,s):null};
@@ -1 +1 @@
1
- function t(t,e,i,s){return new(i||(i=Promise))(function(h,n){function a(t){try{r(s.next(t))}catch(t){n(t)}}function o(t){try{r(s.throw(t))}catch(t){n(t)}}function r(t){var e;t.done?h(t.value):(e=t.value,e instanceof i?e:new i(function(t){t(e)})).then(a,o)}r((s=s.apply(t,e||[])).next())})}function e(t,e,i,s){if("a"===i&&!s)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof e?t!==e||!s:!e.has(t))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===i?s:"a"===i?s.call(t):s?s.value:e.get(t)}function i(t,e,i,s,h){if("m"===s)throw new TypeError("Private method is not writable");if("a"===s&&!h)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof e?t!==e||!h:!e.has(t))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===s?h.call(t,i):h?h.value=i:e.set(t,i),i}"function"==typeof SuppressedError&&SuppressedError;const s=(e,i)=>t(void 0,void 0,void 0,function*(){return new Promise((t,s)=>{const h=new Image;"string"==typeof e?(void 0!==i&&(h.crossOrigin=i),h.src=e):h.src=URL.createObjectURL(e),h.onload=()=>{t(h)},h.onerror=()=>{s(new Error("Image load failed"))}})}),h=(t,e,i)=>Math.min(Math.max(t,e),i);class n{get zoom(){return Math.exp(this._currentLogZ)}get minZoom(){return this._options.minZoom}get maxZoom(){return this._options.maxZoom}get dpr(){return this._dpr}use(t){return this._plugins.has(t.name)?(console.warn(`Plugin "${t.name}" is already installed.`),t):(this._plugins.set(t.name,t),t.install(this),t)}unuse(t){const e=this._plugins.get(t);e&&(e.destroy(),this._plugins.delete(t))}getPlugin(t){return this._plugins.get(t)}onUpdate(t){this._updateCallbacks.add(t)}offUpdate(t){this._updateCallbacks.delete(t)}onBeforeRender(t){this._beforeRenderCallbacks.add(t)}offBeforeRender(t){this._beforeRenderCallbacks.delete(t)}onAfterRender(t){this._afterRenderCallbacks.add(t)}offAfterRender(t){this._afterRenderCallbacks.delete(t)}requestRender(){this._needsRender=!0}_clampLog(t){return h(t,this.LOG_MIN,this.LOG_MAX)}_setTargetLogZoomAtScreen(t,e,i){Number.isFinite(i)&&(this._anchorX=t,this._anchorY=e,this._targetLogZ=this._clampLog(i),this._needsRender=!0)}zoomToAtScreen(t,e,i){this._setTargetLogZoomAtScreen(t,e,Math.log(i))}zoomToAtScreenRaw(t,e,i){if(!Number.isFinite(i))return;const s=Math.max(1e-8,this._options.minZoom),n=this._options.maxZoom,a=h(i,s,n),o=Math.exp(this._currentLogZ),r=a;if(!Number.isFinite(o)||o<=0)return;if(Math.abs(r-o)<1e-12)return;const c=Math.log(a);this._currentLogZ=c,this._targetLogZ=c;const _=r/o;this._tx=t-(t-this._tx)*_,this._ty=e-(e-this._ty)*_,this._needsRender=!0}zoomToAtWorld(t,e,i){const{x:s,y:h}=this.toScreen(t,e);this.zoomToAtScreen(s,h,i)}zoomByFactorAtScreen(t,e,i){if(i<=0||!Number.isFinite(i))return;const s=Math.log(i);this._setTargetLogZoomAtScreen(t,e,this._targetLogZ+s)}zoomByLogAtScreen(t,e,i){this._setTargetLogZoomAtScreen(t,e,this._targetLogZ+i)}zoomByFactorAtWorld(t,e,i){const{x:s,y:h}=this.toScreen(t,e);this.zoomByFactorAtScreen(s,h,i)}panBy(t,e){this._tx+=t,this._ty+=e,this._needsRender=!0}setPan(t,e){this._tx=t,this._ty=e,this._needsRender=!0}setTransform(t,e,i){const s=h(t,this._options.minZoom,this._options.maxZoom);this._currentLogZ=Math.log(s),this._targetLogZ=this._currentLogZ,this._tx=e,this._ty=i,this._targetTx=null,this._targetTy=null,this._needsRender=!0}setTransformSmooth(t,e,i){const s=h(t,this._options.minZoom,this._options.maxZoom);this._targetLogZ=Math.log(s),this._targetTx=e,this._targetTy=i,this._needsRender=!0}_ensureOffscreenSizeLike(t,e){t.width===e.width&&t.height===e.height||(t.width=e.width,t.height=e.height)}_loop(){if(this._isResizing)return void(this._raf=requestAnimationFrame(()=>this._loop()));const t=performance.now(),e=Math.max(1,t-this._lastFrameTs);this._lastFrameTs=t;const{approachKZoom:i}=this._options,s=Math.exp(this._currentLogZ),h=this._targetLogZ-this._currentLogZ,n=Math.abs(h)>1e-6;if(n){const t=1-Math.exp(-i*e);this._currentLogZ+=h*t}const a=Math.exp(this._currentLogZ);if(a!==s){const t=this._anchorX,e=this._anchorY,i=a/s;this._tx=t-(t-this._tx)*i,this._ty=e-(e-this._ty)*i}let o=!1;if(null!==this._targetTx&&null!==this._targetTy){const t=1-Math.exp(-this._options.approachKPan*e),i=this._targetTx-this._tx,s=this._targetTy-this._ty;o=Math.abs(i)>.5||Math.abs(s)>.5,o?(this._tx+=i*t,this._ty+=s*t):(this._tx=this._targetTx,this._ty=this._targetTy,this._targetTx=null,this._targetTy=null)}if(this._isResetting){const t=1-Math.exp(-this._options.approachKPan*e);this._tx+=(0-this._tx)*t,this._ty+=(0-this._ty)*t;const i=Math.abs(this._currentLogZ)<.001&&Math.abs(this._targetLogZ)<1e-6,s=Math.abs(this._tx)<.5&&Math.abs(this._ty)<.5;i&&s&&(this._currentLogZ=0,this._targetLogZ=0,this._tx=0,this._ty=0,this._isResetting=!1)}for(const t of this._updateCallbacks)t(e);if(!(this._needsRender||n||o||this._isResetting))return void(this._raf=requestAnimationFrame(()=>this._loop()));this._needsRender=!1;const r=this.contentCanvas,c=this.contentContext,_=this.topScreenCanvas,l=this.topScreenContext,d=this.canvas,g=this.context;this._ensureOffscreenSizeLike(r,d),this._ensureOffscreenSizeLike(_,d),c.setTransform(1,0,0,1,0,0),l.setTransform(1,0,0,1,0,0),g.setTransform(1,0,0,1,0,0);const u=this._options.background;"string"==typeof u&&""!==u.trim()&&"transparent"!==u.toLowerCase()?(g.fillStyle=u,g.fillRect(0,0,d.width,d.height)):g.clearRect(0,0,d.width,d.height),c.clearRect(0,0,r.width,r.height),l.clearRect(0,0,_.width,_.height),c.setTransform(this._dpr*a,0,0,this._dpr*a,this._dpr*this._tx,this._dpr*this._ty);for(const t of this._beforeRenderCallbacks)t(c);this._render(this);for(const t of this._afterRenderCallbacks)t(c);g.drawImage(r,0,0),g.drawImage(_,0,0),this._raf=requestAnimationFrame(()=>this._loop())}applyWorldTransform(t){const e=Math.exp(this._currentLogZ);t.setTransform(this._dpr*e,0,0,this._dpr*e,this._dpr*this._tx,this._dpr*this._ty)}applyScreenTransform(t){t.setTransform(this._dpr,0,0,this._dpr,0,0)}getPixelColorAtScreen(t,e){const i=Math.floor(t*this._dpr),s=Math.floor(e*this._dpr);if(i<0||s<0||i>=this.canvas.width||s>=this.canvas.height)return{r:0,g:0,b:0,a:0,rgba:"rgba(0,0,0,0)",hex:"#000000"};const h=this.contentContext.getImageData(i,s,1,1).data,n=h[0],a=h[1],o=h[2],r=h[3]/255,c=t=>t.toString(16).padStart(2,"0"),_=`#${c(n)}${c(a)}${c(o)}`;return{r:n,g:a,b:o,a:r,rgba:`rgba(${n},${a},${o},${r.toFixed(3)})`,hex:_}}getPixelColorAtWorld(t,e){const{x:i,y:s}=this.toScreen(t,e);return this.getPixelColorAtScreen(i,s)}registerLayerManager(t){t&&(this._layerManagers.includes(t)||(this._layerManagers.push(t),this._needsRender=!0))}unregisterLayerManager(t){const e=this._layerManagers.indexOf(t);e>=0&&(this._layerManagers.splice(e,1),this._needsRender=!0)}getLayerManagers(){return[...this._layerManagers]}resetSmooth(){this._isResetting=!0,this._targetLogZ=0,this._needsRender=!0}resetInstant(){this._currentLogZ=0,this._targetLogZ=0,this._tx=0,this._ty=0,this._needsRender=!0}toWorld(t,e){const i=Math.exp(this._currentLogZ);return{wx:(t-this._tx)/i,wy:(e-this._ty)/i}}toScreen(t,e){const i=Math.exp(this._currentLogZ);return{x:t*i+this._tx,y:e*i+this._ty}}getTransform(){return{zoom:Math.exp(this._currentLogZ),tx:this._tx,ty:this._ty}}getViewportBounds(){const t=Math.exp(this._currentLogZ),e=this.canvas.width/this._dpr,i=this.canvas.height/this._dpr,s=-this._tx/t,h=-this._ty/t,n=(e-this._tx)/t,a=(i-this._ty)/t;return{left:s,top:h,right:n,bottom:a,width:n-s,height:a-h}}setZoomRange(t,e){this._options.minZoom=t,this._options.maxZoom=e,this.LOG_MIN=Math.log(t),this.LOG_MAX=Math.log(e),this._targetLogZ=Math.min(this.LOG_MAX,Math.max(this.LOG_MIN,this._targetLogZ))}resizeToParent(){this._isResizing=!0;const t=(this.canvas.parentElement||this.canvas).getBoundingClientRect();this._dpr=Math.max(1,window.devicePixelRatio||1);const e=Math.max(1,Math.round(t.width)),i=Math.max(1,Math.round(t.height));this.canvas.width=Math.round(e*this._dpr),this.canvas.height=Math.round(i*this._dpr),this.canvas.style.width=`${e}px`,this.canvas.style.height=`${i}px`,this._ensureOffscreenSizeLike(this.contentCanvas,this.canvas),this._ensureOffscreenSizeLike(this.topScreenCanvas,this.canvas),clearTimeout(this._resizeReleaseTimer),this._resizeReleaseTimer=window.setTimeout(()=>{this._isResizing=!1},50)}destroy(){cancelAnimationFrame(this._raf);for(const t of this._plugins.values())t.destroy();this._plugins.clear(),this._resizeObserver&&this._resizeObserver.disconnect(),this._layerManagers=[],this._updateCallbacks.clear(),this._beforeRenderCallbacks.clear(),this._afterRenderCallbacks.clear()}constructor(t,e,i){this._layerManagers=[],this._plugins=new Map,this._isResetting=!1,this._isResizing=!1,this._needsRender=!0,this._raf=0,this._lastFrameTs=performance.now(),this._tx=0,this._ty=0,this._anchorX=0,this._anchorY=0,this._currentLogZ=Math.log(1),this._targetLogZ=Math.log(1),this._targetTx=null,this._targetTy=null,this._updateCallbacks=new Set,this._beforeRenderCallbacks=new Set,this._afterRenderCallbacks=new Set,this._dpr=Math.max(1,window.devicePixelRatio||1);const s=t.getContext("2d",{willReadFrequently:!0,alpha:!0});if(!s)throw new Error("2D context not available");this.canvas=t,this.context=s,this._render=e,this.contentCanvas=document.createElement("canvas"),this.contentCanvas.width=t.width,this.contentCanvas.height=t.height,this.contentContext=this.contentCanvas.getContext("2d",{alpha:!0,willReadFrequently:!0}),this.topScreenCanvas=document.createElement("canvas"),this.topScreenCanvas.width=t.width,this.topScreenCanvas.height=t.height,this.topScreenContext=this.topScreenCanvas.getContext("2d",{alpha:!0}),this._options=Object.assign({minZoom:.5,maxZoom:10,approachKZoom:.022,approachKPan:.022,autoResize:!0,background:"#fff"},i),this.LOG_MIN=Math.log(this._options.minZoom),this.LOG_MAX=Math.log(this._options.maxZoom),this._options.autoResize&&(this._resizeObserver=new ResizeObserver(()=>this.resizeToParent()),this._resizeObserver.observe(this.canvas.parentElement||this.canvas)),this.resizeToParent(),this._lastFrameTs=performance.now(),this._raf=requestAnimationFrame(()=>this._loop())}}let a=0;class o{constructor(t,e,i="world"){this.space="world",this.visible=!0,this.opacity=1,this.blend="source-over",this.name=t,this.id=`layer_${e}_${++a}`,this.type=e,this.space=i}}class r extends o{beginStroke(t,e){const{lx:i,ly:s}=this.toLocalPoint(t,e);this._lastX=i,this._lastY=s,this._drawing=!0}stroke(t,e,i,s,h=1,n="brush"){if(!this._drawing)return;const{lx:a,ly:o}=this.toLocalPoint(t,e);this.context.beginPath(),this.context.moveTo(this._lastX,this._lastY),this.context.lineTo(a,o),"eraser"===n?(this.context.globalCompositeOperation="destination-out",this.context.strokeStyle="rgba(0, 0, 0, 1)"):(this.context.globalCompositeOperation="source-over",this.context.strokeStyle=i),this.context.lineWidth=s*h,this.context.lineCap="round",this.context.lineJoin="round",this.context.stroke(),this.context.closePath(),this._lastX=a,this._lastY=o}endStroke(){this._drawing=!1}isDrawing(){return this._drawing}captureSnapshot(t){try{if(t){const{x:e,y:i,width:s,height:h}=t,n=Math.max(0,Math.floor(e)),a=Math.max(0,Math.floor(i)),o=Math.min(this.canvas.width-n,Math.ceil(s)),r=Math.min(this.canvas.height-a,Math.ceil(h));return o<=0||r<=0?null:this.context.getImageData(n,a,o,r)}return this.context.getImageData(0,0,this.canvas.width,this.canvas.height)}catch(t){return null}}restoreSnapshot(t,e){var i,s;const h=null!==(i=null==e?void 0:e.x)&&void 0!==i?i:0,n=null!==(s=null==e?void 0:e.y)&&void 0!==s?s:0;this.context.putImageData(t,h,n)}clearRegion(t){t?this.context.clearRect(t.x,t.y,t.width,t.height):this.context.clearRect(0,0,this.canvas.width,this.canvas.height)}requestRedraw(){var t;null===(t=this._redraw)||void 0===t||t.call(this,this.context,this.canvas)}drawImage(t,e,i,s,h){this.context.drawImage(t,e,i,null!=s?s:t.width,null!=h?h:t.height)}hitTest(t,e){const{lx:i,ly:s}=this.toLocalPoint(t,e);return i>=0&&i<=this.canvas.width&&s>=0&&s<=this.canvas.height}toLocalPoint(t,e){const i=t-this.x,s=e-this.y,h=Math.cos(-this.rotation),n=Math.sin(-this.rotation),a=i*n+s*h,o=(i*h-s*n)/this.scale,r=a/this.scale;return{lx:o+("center"===this.anchor?this.canvas.width/2:0),ly:r+("center"===this.anchor?this.canvas.height/2:0)}}render(t,e){if(!this.visible)return;const i=this.canvas.width*this.scale,s=this.canvas.height*this.scale,h="center"===this.anchor?-i/2:0,n="center"===this.anchor?-s/2:0;t.save(),t.globalAlpha=this.opacity,t.globalCompositeOperation=this.blend,t.translate(this.x,this.y),t.rotate(this.rotation),t.drawImage(this.canvas,h,n,i,s),t.restore()}cropTo(t){const e=Math.max(1,Math.floor(t.width)),i=Math.max(1,Math.floor(t.height));if(e===this.canvas.width&&i===this.canvas.height)return;const s=this._cloneCanvas(),h=Math.min(s.width,e),n=Math.min(s.height,i);this._setCanvasSize(e,i),h>0&&n>0&&this.context.drawImage(s,0,0,h,n,0,0,h,n)}resizeTo(t){const e=Math.max(1,Math.floor(t.width)),i=Math.max(1,Math.floor(t.height));if(e===this.canvas.width&&i===this.canvas.height)return;const s=this._cloneCanvas();this._setCanvasSize(e,i),s.width>0&&s.height>0&&this.context.drawImage(s,0,0,s.width,s.height,0,0,e,i)}_cloneCanvas(){const t=document.createElement("canvas");if(t.width=this.canvas.width,t.height=this.canvas.height,0===t.width||0===t.height)return t;const e=t.getContext("2d");if(!e)throw new Error("Offscreen 2D context unavailable");return e.drawImage(this.canvas,0,0),t}_setCanvasSize(t,e){this.canvas.width=t,this.canvas.height=e,this.context.setTransform(1,0,0,1,0,0),this.context.clearRect(0,0,t,e)}destroy(){this.canvas.width=0,this.canvas.height=0}constructor(t){var e,i;if(super(t.name||"","canvas",null!==(e=t.space)&&void 0!==e?e:"world"),this.x=0,this.y=0,this.scale=1,this.rotation=0,this.anchor="topLeft",this._drawing=!1,this._lastX=0,this._lastY=0,"canvas"in t&&t.canvas)this.canvas=t.canvas;else{const e=t;this.canvas=document.createElement("canvas"),this.canvas.width=e.width,this.canvas.height=e.height}const s=this.canvas.getContext("2d",{willReadFrequently:!0});if(!s)throw new Error("Offscreen 2D context unavailable");this.context=s,this.x=t.x||0,this.y=t.y||0,this.scale=null!==(i=t.scale)&&void 0!==i?i:1,this.rotation=t.rotation||0,t.anchor&&(this.anchor=t.anchor),this._redraw=t.redraw,this._redraw&&this._redraw(this.context,this.canvas)}}var c;class _ extends r{static fromImage(e){return t(this,void 0,void 0,function*(){var t,h,n,a,o,r,l,d;const g=yield s(e.src,e.crossOrigin),u=null!==(t=e.width)&&void 0!==t?t:g.naturalWidth,m=null!==(h=e.height)&&void 0!==h?h:g.naturalHeight,v=new _({name:e.name,space:null!==(n=e.space)&&void 0!==n?n:"world",x:null!==(a=e.x)&&void 0!==a?a:0,y:null!==(o=e.y)&&void 0!==o?o:0,scale:null!==(r=e.scale)&&void 0!==r?r:1,rotation:null!==(l=e.rotation)&&void 0!==l?l:0,anchor:null!==(d=e.anchor)&&void 0!==d?d:"topLeft",width:u,height:m});return v.context.clearRect(0,0,v.canvas.width,v.canvas.height),v.context.drawImage(g,0,0,u,m),"string"!=typeof e.src&&i(v,c,g.src.startsWith("blob:")?g.src:null,"f"),v})}setSource(h,n){return t(this,void 0,void 0,function*(){const t=yield s(h,n);if(this.canvas.width=t.naturalWidth,this.canvas.height=t.naturalHeight,this.context.setTransform(1,0,0,1,0,0),this.context.clearRect(0,0,this.canvas.width,this.canvas.height),this.context.drawImage(t,0,0),e(this,c,"f")){try{URL.revokeObjectURL(e(this,c,"f"))}catch(t){}i(this,c,null,"f")}"string"!=typeof h&&i(this,c,t.src.startsWith("blob:")?t.src:null,"f")})}paint(t){t(this.context,this.canvas)}getImageData(t=0,e=0,i=this.canvas.width,s=this.canvas.height){return this.context.getImageData(t,e,i,s)}putImageData(t,e=0,i=0){this.context.putImageData(t,e,i)}toDataURL(t="image/png",e){return this.canvas.toDataURL(t,e)}toImageBitmap(t){return createImageBitmap(this.canvas,null!=t?t:{})}destroy(){var t;if(null===(t=super.destroy)||void 0===t||t.call(this),e(this,c,"f")){try{URL.revokeObjectURL(e(this,c,"f"))}catch(t){}i(this,c,null,"f")}}constructor(t){var e,i;super({name:t.name,space:null!==(e=t.space)&&void 0!==e?e:"world",x:t.x,y:t.y,scale:t.scale,rotation:t.rotation,anchor:null!==(i=t.anchor)&&void 0!==i?i:"topLeft",width:t.width,height:t.height}),c.set(this,null),this.type="bitmap"}}c=new WeakMap;class l{constructor(){this._worldLayers=[],this._screenLayers=[]}_renderAllLayersIn(t,e){e.save(),t.applyWorldTransform(e);for(const i of this._worldLayers)!i.visible||i.opacity<=0||(e.save(),i.render(e,t),e.restore());e.restore(),e.save(),t.applyScreenTransform(e);for(const i of this._screenLayers)!i.visible||i.opacity<=0||(e.save(),i.render(e,t),e.restore());e.restore()}addLayer(t,e){const i="world"===t.space?this._worldLayers:this._screenLayers;return"number"==typeof e&&e>=0&&e<i.length?(i.splice(e,0,t),t.id):(i.push(t),t.id)}createImageLayer(e){return t(this,void 0,void 0,function*(){const t=yield _.fromImage(e);return this.addLayer(t),t})}createCanvasLayer(t){const e=new r(t);return this.addLayer(e),e}removeLayer(t){var e,i,s,h;const n=this._worldLayers.findIndex(e=>e.id===t);if(n>=0)return null===(i=(e=this._worldLayers[n]).destroy)||void 0===i||i.call(e),void this._worldLayers.splice(n,1);const a=this._screenLayers.findIndex(e=>e.id===t);a>=0&&(null===(h=(s=this._screenLayers[a]).destroy)||void 0===h||h.call(s),this._screenLayers.splice(a,1))}moveLayer(t,e){const i=this._worldLayers.findIndex(e=>e.id===t);if(i>=0){const[t]=this._worldLayers.splice(i,1),s=Math.max(0,Math.min(e,this._worldLayers.length));return void this._worldLayers.splice(s,0,t)}const s=this._screenLayers.findIndex(e=>e.id===t);if(s>=0){const[t]=this._screenLayers.splice(s,1),i=Math.max(0,Math.min(e,this._screenLayers.length));this._screenLayers.splice(i,0,t)}}getLayer(t){return this._worldLayers.find(e=>e.id===t)||this._screenLayers.find(e=>e.id===t)}getAllLayers(t){return t?("world"===t?this._worldLayers:this._screenLayers).slice():[...this._worldLayers,...this._screenLayers]}hitTest(t,e,i="world"){const s=this.getAllLayers(i);for(let i=s.length-1;i>=0;i--){const h=s[i];if(h.hitTest&&h.hitTest(t,e))return h}}destroy(){var t;for(const e of[...this._worldLayers,...this._screenLayers])null===(t=e.destroy)||void 0===t||t.call(e);this._worldLayers=[],this._screenLayers=[]}}class d extends l{constructor(){super(...arguments),this._compositeCache=null,this._compositeCacheCtx=null,this._compositeDirty=!0,this._lastCacheWidth=0,this._lastCacheHeight=0,this._cachedBoundsMinX=0,this._cachedBoundsMinY=0}markDirty(){this._compositeDirty=!0}addLayer(t,e){return this._compositeDirty=!0,super.addLayer(t,e)}removeLayer(t){this._compositeDirty=!0,super.removeLayer(t)}removeAllLayers(t){var e,i;if(!t||"world"===t){for(const t of this._worldLayers)null===(e=t.destroy)||void 0===e||e.call(t);this._worldLayers=[]}if(!t||"screen"===t){for(const t of this._screenLayers)null===(i=t.destroy)||void 0===i||i.call(t);this._screenLayers=[]}}moveLayer(t,e){this._compositeDirty=!0,super.moveLayer(t,e)}renderAllLayersIn(t){const e=t.contentContext,i=this._worldLayers;0!==i.length&&(this._compositeDirty&&this._rebuildCompositeCache(i),this._compositeCache&&this._compositeCacheCtx&&e.drawImage(this._compositeCache,this._cachedBoundsMinX,this._cachedBoundsMinY))}_rebuildCompositeCache(t){let e=0,i=0,s=0,h=0,n=!1;for(const a of t){const t=a,o=t.canvas.width*t.scale,r=t.canvas.height*t.scale,c="center"===t.anchor?-o/2:0,_="center"===t.anchor?-r/2:0,l=Math.cos(t.rotation),d=Math.sin(t.rotation),g=[{x:c,y:_},{x:c+o,y:_},{x:c,y:_+r},{x:c+o,y:_+r}];let u=1/0,m=1/0,v=-1/0,p=-1/0;for(const e of g){const i=e.x*l-e.y*d+t.x,s=e.x*d+e.y*l+t.y;u=Math.min(u,i),m=Math.min(m,s),v=Math.max(v,i),p=Math.max(p,s)}n?(e=Math.min(e,u),i=Math.min(i,m),s=Math.max(s,v),h=Math.max(h,p)):(e=u,i=m,s=v,h=p,n=!0)}if(!n)return void(this._compositeDirty=!1);const a=Math.ceil(s-e),o=Math.ceil(h-i);this._compositeCache||(this._compositeCache=document.createElement("canvas"),this._compositeCacheCtx=this._compositeCache.getContext("2d",{alpha:!0})),this._lastCacheWidth===a&&this._lastCacheHeight===o||(this._compositeCache.width=a,this._compositeCache.height=o,this._lastCacheWidth=a,this._lastCacheHeight=o),this._cachedBoundsMinX=e,this._cachedBoundsMinY=i;const r=this._compositeCacheCtx;r.clearRect(0,0,a,o),r.save(),r.translate(-e,-i);for(const e of t)!e.visible||e.opacity<=0||(r.save(),e.render(r),r.restore());r.restore(),this._compositeDirty=!1}destroy(){super.destroy(),this._compositeCache=null,this._compositeCacheCtx=null}}class g extends l{renderAllLayersIn(t){const e=t.topScreenContext;this._renderAllLayersIn(t,e)}}class u{constructor(t){this.name="interaction",this._view=null,this._dragging=!1,this._vx=0,this._vy=0,this._lastMoveTs=0,this._activePointerId=null,this._onDownBound=t=>this._onPointerDown(t),this._onMoveBound=t=>this._onPointerMove(t),this._onUpBound=()=>this._onPointerUp(),this._onWheelBound=t=>this._onWheel(t),this._onUpdate=t=>{if(!this._view)return;const{friction:e,stopSpeed:i}=this._options,s=Math.hypot(this._vx,this._vy)>=i;if(!this._dragging&&this._panEnabled&&s){const s=this._vx*t,h=this._vy*t;this._view.panBy(s,h),this._vx*=e,this._vy*=e,Math.hypot(this._vx,this._vy)<i&&(this._vx=0,this._vy=0)}else this._panEnabled||(this._vx=0,this._vy=0)},this._options=Object.assign({panEnabled:!0,zoomEnabled:!0,friction:.92,stopSpeed:.02,emaAlpha:.25,idleNoInertiaMs:120,wheelSensitivity:.0015},t),this._panEnabled=this._options.panEnabled,this._zoomEnabled=this._options.zoomEnabled}install(t){this._view=t;const e=t.canvas;e.addEventListener("wheel",this._onWheelBound,{passive:!1}),e.addEventListener("pointerdown",this._onDownBound),window.addEventListener("pointermove",this._onMoveBound),window.addEventListener("pointerup",this._onUpBound),t.onUpdate(this._onUpdate)}destroy(){if(!this._view)return;const t=this._view.canvas;t.removeEventListener("wheel",this._onWheelBound),t.removeEventListener("pointerdown",this._onDownBound),window.removeEventListener("pointermove",this._onMoveBound),window.removeEventListener("pointerup",this._onUpBound),this._view.offUpdate(this._onUpdate),this._view=null}isPanEnabled(){return this._panEnabled}isZoomEnabled(){return this._zoomEnabled}setPanEnabled(t){this._panEnabled!==t&&(this._panEnabled=t,t||(this._dragging=!1,this._vx=0,this._vy=0))}setZoomEnabled(t){this._zoomEnabled=t}setWheelSensitivity(t){this._options.wheelSensitivity=t}isDragging(){return this._dragging}_onPointerDown(t){var e;if(0===t.button&&this._panEnabled){this._dragging=!0,this._vx=0,this._vy=0,this._lastMoveTs=performance.now(),this._activePointerId=t.pointerId;try{null===(e=this._view)||void 0===e||e.canvas.setPointerCapture(t.pointerId)}catch(t){}}}_onPointerMove(t){if(!this._dragging||!this._panEnabled||!this._view)return;const e=performance.now(),i=Math.max(1,e-(this._lastMoveTs||e-16));this._lastMoveTs=e;const s=t.movementX,h=t.movementY;this._view.panBy(s,h);const n=this._options.emaAlpha,a=s/i,o=h/i;this._vx=(1-n)*this._vx+n*a,this._vy=(1-n)*this._vy+n*o}_onPointerUp(){if(!this._dragging)return;this._dragging=!1;const t=performance.now(),e=this._lastMoveTs?t-this._lastMoveTs:1/0;if(null!=this._activePointerId&&this._view){try{this._view.canvas.releasePointerCapture(this._activePointerId)}catch(t){}this._activePointerId=null}if(e>=this._options.idleNoInertiaMs)this._vx=0,this._vy=0;else{const t=Math.pow(this._options.friction,e/16);this._vx*=t,this._vy*=t}Math.hypot(this._vx,this._vy)<this._options.stopSpeed&&(this._vx=0,this._vy=0)}_getLineHeightPx(){if(!this._view)return 16;const t=getComputedStyle(this._view.canvas).lineHeight;if(!t||"normal"===t)return 16;const e=parseFloat(t);return Number.isFinite(e)?e:16}_normalizeWheelDelta(t){if(!this._view)return 0;let e=t.deltaY;if(1===t.deltaMode)e*=this._getLineHeightPx();else if(2===t.deltaMode){e*=this._view.canvas.clientHeight||window.innerHeight||800}return e}_onWheel(t){if(!this._zoomEnabled||!this._view)return;t.preventDefault(),t.stopPropagation();const e=this._normalizeWheelDelta(t),i=this._view.canvas.getBoundingClientRect(),s=t.clientX-i.left,h=t.clientY-i.top;let n=-e*this._options.wheelSensitivity;t.ctrlKey||t.metaKey?n*=1.6:t.shiftKey&&(n*=.6),this._view.zoomByLogAtScreen(s,h,n)}}function m(t){return new u(t)}class v{constructor(t){var e,i,s,h,n;this.name="document",this._view=null,this._enabled=!1,this._x=0,this._y=0,this._width=0,this._height=0,this._marginL=0,this._marginR=0,this._marginT=0,this._marginB=0,this._panClampMode="minVisible",this._background=null,this._shadow=null,this._onUpdate=()=>{this._enabled&&this._view&&this._clampPan()},this._onBeforeRender=t=>{var e,i,s,h,n;this._enabled&&this._view&&(this._shadow?(t.save(),t.shadowColor=null!==(e=this._shadow.color)&&void 0!==e?e:v._defaultShadow.color,t.shadowBlur=null!==(i=this._shadow.blur)&&void 0!==i?i:v._defaultShadow.blur,t.shadowOffsetX=null!==(s=this._shadow.offsetX)&&void 0!==s?s:v._defaultShadow.offsetX,t.shadowOffsetY=null!==(h=this._shadow.offsetY)&&void 0!==h?h:v._defaultShadow.offsetY,t.fillStyle=null!==(n=this._background)&&void 0!==n?n:"#ffffff",t.fillRect(this._x,this._y,this._width,this._height),t.restore()):this._background&&(t.save(),t.fillStyle=this._background,t.fillRect(this._x,this._y,this._width,this._height),t.restore()),t.save(),t.beginPath(),t.rect(this._x,this._y,this._width,this._height),t.clip())},this._onAfterRender=t=>{if(this._enabled&&this._view&&(t.restore(),this._options.drawBorder)){const e=this._view.zoom;t.save(),t.lineWidth=1/e,t.strokeStyle="#cfcfcf",t.strokeRect(this._x,this._y,this._width,this._height),t.restore()}},this._options=Object.assign({rect:{x:0,y:0,width:0,height:0},margins:{},drawBorder:!1,minVisiblePx:30,panClampMode:"minVisible",background:null,shadow:!1},t),(null==t?void 0:t.rect)&&(this._enabled=!0,this._x=t.rect.x,this._y=t.rect.y,this._width=t.rect.width,this._height=t.rect.height),(null==t?void 0:t.margins)&&(this._marginL=null!==(e=t.margins.left)&&void 0!==e?e:0,this._marginR=null!==(i=t.margins.right)&&void 0!==i?i:0,this._marginT=null!==(s=t.margins.top)&&void 0!==s?s:0,this._marginB=null!==(h=t.margins.bottom)&&void 0!==h?h:0),this._panClampMode=this._options.panClampMode,this._background=null!==(n=this._options.background)&&void 0!==n?n:null,!0===this._options.shadow?this._shadow=Object.assign({},v._defaultShadow):this._options.shadow&&"object"==typeof this._options.shadow&&(this._shadow=Object.assign(Object.assign({},v._defaultShadow),this._options.shadow))}install(t){this._view=t,t.onUpdate(this._onUpdate),t.onBeforeRender(this._onBeforeRender),t.onAfterRender(this._onAfterRender)}destroy(){this._view&&(this._view.offUpdate(this._onUpdate),this._view.offBeforeRender(this._onBeforeRender),this._view.offAfterRender(this._onAfterRender),this._view=null)}isEnabled(){return this._enabled}getRect(){return{x:this._x,y:this._y,width:this._width,height:this._height}}setRect(t,e,i,s){this._enabled=!0,this._x=t,this._y=e,this._width=i,this._height=s}clearRect(){this._enabled=!1}setMargins(t){var e,i,s,h;this._marginL=null!==(e=t.left)&&void 0!==e?e:this._marginL,this._marginR=null!==(i=t.right)&&void 0!==i?i:this._marginR,this._marginT=null!==(s=t.top)&&void 0!==s?s:this._marginT,this._marginB=null!==(h=t.bottom)&&void 0!==h?h:this._marginB}getMargins(){return{left:this._marginL,right:this._marginR,top:this._marginT,bottom:this._marginB}}setPanClampMode(t){this._panClampMode=t}getPanClampMode(){return this._panClampMode}cropTo(t){this._doResize("crop",t)}resizeTo(t){this._doResize("resize",t)}zoomToFit(t="contain",e=!0){if(!this._enabled||!this._view)return;const i=this._view.dpr,s=this._view.canvas.width/i,h=this._view.canvas.height/i,n=Math.max(1,s-(this._marginL+this._marginR)),a=Math.max(1,h-(this._marginT+this._marginB));let o;const r=n/this._width,c=a/this._height;o="contain"===t?Math.min(r,c):"cover"===t?Math.max(r,c):"fitWidth"===t?r:c,o=Math.min(this._view.maxZoom,Math.max(this._view.minZoom,o));const _=this._marginL+(n-o*this._width)/2,l=this._marginT+(a-o*this._height)/2,d=_-o*this._x,g=l-o*this._y;e?this._view.setTransformSmooth(o,d,g):this._view.setTransform(o,d,g)}isPointInDocument(t,e){return!this._enabled||t>=this._x&&t<=this._x+this._width&&e>=this._y&&e<=this._y+this._height}get background(){return this._background}setBackground(t){var e;this._background=t,null===(e=this._view)||void 0===e||e.requestRender()}get shadow(){return this._shadow?Object.assign({},this._shadow):null}setShadow(t){var e;this._shadow=!0===t?Object.assign({},v._defaultShadow):t&&"object"==typeof t?Object.assign(Object.assign({},v._defaultShadow),t):null,null===(e=this._view)||void 0===e||e.requestRender()}_clampPan(){if(!this._view)return;const{zoom:t,tx:e,ty:i}=this._view.getTransform(),s=t,h=this._view.dpr,n=this._view.canvas.width/h,a=this._view.canvas.height/h,o=this._x,r=this._y,c=this._x+this._width,_=this._y+this._height;let l=e,d=i;if("margin"===this._panClampMode){const t=this._marginL-s*o,h=n-this._marginR-s*c,g=this._marginT-s*r,u=a-this._marginB-s*_,m=Math.max(1,n-(this._marginL+this._marginR)),v=Math.max(1,a-(this._marginT+this._marginB));l=s*this._width<=m?this._marginL+(m-s*this._width)/2-s*this._x:Math.min(t,Math.max(h,e)),d=s*this._height<=v?this._marginT+(v-s*this._height)/2-s*this._y:Math.min(g,Math.max(u,i))}else if("minVisible"===this._panClampMode){const t=s*this._width,h=s*this._height,g=Math.min(this._options.minVisiblePx,t),u=Math.min(this._options.minVisiblePx,h),m=n-g-s*o,v=g-s*c,p=a-u-s*r,f=u-s*_;l=v<=m?Math.min(m,Math.max(v,e)):(v+m)/2,d=f<=p?Math.min(p,Math.max(f,i)):(f+p)/2}l===e&&d===i||this._view.setPan(l,d)}_doResize(t,e){if(!this._view)return;const i=Math.max(1,Math.floor(e.width)),s=Math.max(1,Math.floor(e.height)),h={width:i,height:s},n=this._view.getLayerManagers();for(const e of n){const i=e.getAllLayers("world");for(const e of i){const i=e;"function"==typeof i.cropTo&&"function"==typeof i.resizeTo&&("crop"===t?i.cropTo(h):i.resizeTo(h))}}this._width=i,this._height=s,this._enabled&&this._clampPan()}}function p(t){return new v(t)}v._defaultShadow={color:"rgba(0, 0, 0, 0.3)",blur:20,offsetX:0,offsetY:4};class f{constructor(t,e,i,s){this.type="snapshot",this._isExecuted=!0,this._target=t,this._beforeData=e,this._afterData=i,this._region=null==s?void 0:s.region}execute(){if(this._isExecuted)return;const t=this._region?{x:this._region.x,y:this._region.y}:void 0;this._target.restoreSnapshot(this._afterData,t),this._isExecuted=!0}undo(){if(!this._isExecuted)return;const t=this._region?{x:this._region.x,y:this._region.y}:void 0;this._target.restoreSnapshot(this._beforeData,t),this._isExecuted=!1}canMerge(){return!1}merge(){return this}}function w(t,e,i,s){return e&&i?new f(t,e,i,s):null}class y{executeCommand(t){t.execute(),this.addCommand(t)}addCommand(t){var e,i;this._redoStack=[];const s=this._undoStack[this._undoStack.length-1];if(s&&(null===(e=s.canMerge)||void 0===e?void 0:e.call(s,t))&&s.merge){const e=null!==(i=s.merge(t))&&void 0!==i?i:s;return void(e!==s&&(this._undoStack[this._undoStack.length-1]=e))}this._undoStack.push(t),this._undoStack.length>this._maxHistorySize&&this._undoStack.shift()}undo(){if(0===this._undoStack.length)return null;const t=this._undoStack.pop();return t.undo(),this._redoStack.push(t),t}redo(){if(0===this._redoStack.length)return null;const t=this._redoStack.pop();return t.execute(),this._undoStack.push(t),t}canUndo(){return this._undoStack.length>0}canRedo(){return this._redoStack.length>0}clear(){this._undoStack=[],this._redoStack=[]}setMaxHistorySize(t){this._maxHistorySize=Math.max(1,t),this._undoStack.length>this._maxHistorySize&&(this._undoStack=this._undoStack.slice(-this._maxHistorySize))}constructor(t){var e,i,s;this._undoStack=[],this._redoStack=[],this._maxHistorySize=null!==(e=null==t?void 0:t.maxHistorySize)&&void 0!==e?e:50,this._undoStack=null!==(i=null==t?void 0:t.undoStack)&&void 0!==i?i:[],this._redoStack=null!==(s=null==t?void 0:t.redoStack)&&void 0!==s?s:[]}}export{_ as BitmapLayer,r as CanvasLayer,d as ContentLayerManager,v as DocumentPlugin,y as HistoryManager,u as InteractionPlugin,o as LayerBase,l as LayerManagerBase,f as SnapshotCommand,g as TopScreenLayerManager,n as ViewManager,p as createDocumentPlugin,m as createInteractionPlugin,w as createSnapshotCommand};
1
+ function t(t,e,i,s){return new(i||(i=Promise))(function(n,h){function o(t){try{r(s.next(t))}catch(t){h(t)}}function a(t){try{r(s.throw(t))}catch(t){h(t)}}function r(t){var e;t.done?n(t.value):(e=t.value,e instanceof i?e:new i(function(t){t(e)})).then(o,a)}r((s=s.apply(t,e||[])).next())})}function e(t,e,i,s){if("a"===i&&!s)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof e?t!==e||!s:!e.has(t))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===i?s:"a"===i?s.call(t):s?s.value:e.get(t)}function i(t,e,i,s,n){if("m"===s)throw new TypeError("Private method is not writable");if("a"===s&&!n)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof e?t!==e||!n:!e.has(t))throw new TypeError("Cannot write private member to an object whose class did not declare it");return"a"===s?n.call(t,i):n?n.value=i:e.set(t,i),i}"function"==typeof SuppressedError&&SuppressedError;const s=(e,i)=>t(void 0,void 0,void 0,function*(){return new Promise((t,s)=>{const n=new Image;"string"==typeof e?(void 0!==i&&(n.crossOrigin=i),n.src=e):n.src=URL.createObjectURL(e),n.onload=()=>{t(n)},n.onerror=()=>{s(new Error("Image load failed"))}})}),n=(t,e,i)=>Math.min(Math.max(t,e),i);class h{get zoom(){return Math.exp(this._currentLogZ)}get minZoom(){return this._options.minZoom}get maxZoom(){return this._options.maxZoom}get dpr(){return this._dpr}use(t){return this._plugins.has(t.name)?(console.warn(`Plugin "${t.name}" is already installed.`),t):(this._plugins.set(t.name,t),t.install(this),t)}unuse(t){const e=this._plugins.get(t);e&&(e.destroy(),this._plugins.delete(t))}getPlugin(t){return this._plugins.get(t)}onUpdate(t){this._updateCallbacks.add(t)}offUpdate(t){this._updateCallbacks.delete(t)}onBeforeRender(t){this._beforeRenderCallbacks.add(t)}offBeforeRender(t){this._beforeRenderCallbacks.delete(t)}onAfterRender(t){this._afterRenderCallbacks.add(t)}offAfterRender(t){this._afterRenderCallbacks.delete(t)}requestRender(){this._needsRender=!0}_clampLog(t){return n(t,this.LOG_MIN,this.LOG_MAX)}_setTargetLogZoomAtScreen(t,e,i){Number.isFinite(i)&&(this._anchorX=t,this._anchorY=e,this._targetLogZ=this._clampLog(i),this._needsRender=!0)}zoomToAtScreen(t,e,i){this._setTargetLogZoomAtScreen(t,e,Math.log(i))}zoomToAtScreenRaw(t,e,i){if(!Number.isFinite(i))return;const s=Math.max(1e-8,this._options.minZoom),h=this._options.maxZoom,o=n(i,s,h),a=Math.exp(this._currentLogZ),r=o;if(!Number.isFinite(a)||a<=0)return;if(Math.abs(r-a)<1e-12)return;const c=Math.log(o);this._currentLogZ=c,this._targetLogZ=c;const _=r/a;this._tx=t-(t-this._tx)*_,this._ty=e-(e-this._ty)*_,this._needsRender=!0}zoomToAtWorld(t,e,i){const{x:s,y:n}=this.toScreen(t,e);this.zoomToAtScreen(s,n,i)}zoomByFactorAtScreen(t,e,i){if(i<=0||!Number.isFinite(i))return;const s=Math.log(i);this._setTargetLogZoomAtScreen(t,e,this._targetLogZ+s)}zoomByLogAtScreen(t,e,i){this._setTargetLogZoomAtScreen(t,e,this._targetLogZ+i)}zoomByFactorAtWorld(t,e,i){const{x:s,y:n}=this.toScreen(t,e);this.zoomByFactorAtScreen(s,n,i)}panBy(t,e){this._tx+=t,this._ty+=e,this._needsRender=!0}setPan(t,e){this._tx=t,this._ty=e,this._needsRender=!0}setTransform(t,e,i){const s=n(t,this._options.minZoom,this._options.maxZoom);this._currentLogZ=Math.log(s),this._targetLogZ=this._currentLogZ,this._tx=e,this._ty=i,this._targetTx=null,this._targetTy=null,this._needsRender=!0}setTransformSmooth(t,e,i){const s=n(t,this._options.minZoom,this._options.maxZoom);this._targetLogZ=Math.log(s),this._targetTx=e,this._targetTy=i,this._needsRender=!0}_ensureOffscreenSizeLike(t,e){t.width===e.width&&t.height===e.height||(t.width=e.width,t.height=e.height)}_loop(){if(this._isResizing)return void(this._raf=requestAnimationFrame(()=>this._loop()));const t=performance.now(),e=Math.max(1,t-this._lastFrameTs);this._lastFrameTs=t;const{approachKZoom:i}=this._options,s=Math.exp(this._currentLogZ),n=this._targetLogZ-this._currentLogZ,h=Math.abs(n)>1e-6;if(h){const t=1-Math.exp(-i*e);this._currentLogZ+=n*t}const o=Math.exp(this._currentLogZ);if(o!==s){const t=this._anchorX,e=this._anchorY,i=o/s;this._tx=t-(t-this._tx)*i,this._ty=e-(e-this._ty)*i}let a=!1;if(null!==this._targetTx&&null!==this._targetTy){const t=1-Math.exp(-this._options.approachKPan*e),i=this._targetTx-this._tx,s=this._targetTy-this._ty;a=Math.abs(i)>.5||Math.abs(s)>.5,a?(this._tx+=i*t,this._ty+=s*t):(this._tx=this._targetTx,this._ty=this._targetTy,this._targetTx=null,this._targetTy=null)}if(this._isResetting){const t=1-Math.exp(-this._options.approachKPan*e);this._tx+=(0-this._tx)*t,this._ty+=(0-this._ty)*t;const i=Math.abs(this._currentLogZ)<.001&&Math.abs(this._targetLogZ)<1e-6,s=Math.abs(this._tx)<.5&&Math.abs(this._ty)<.5;i&&s&&(this._currentLogZ=0,this._targetLogZ=0,this._tx=0,this._ty=0,this._isResetting=!1)}for(const t of this._updateCallbacks)t(e);if(!(this._needsRender||h||a||this._isResetting))return void(this._raf=requestAnimationFrame(()=>this._loop()));this._needsRender=!1;const r=this.contentCanvas,c=this.contentContext,_=this.topScreenCanvas,l=this.topScreenContext,d=this.canvas,u=this.context;this._ensureOffscreenSizeLike(r,d),this._ensureOffscreenSizeLike(_,d),c.setTransform(1,0,0,1,0,0),l.setTransform(1,0,0,1,0,0),u.setTransform(1,0,0,1,0,0);const g=this._options.background;"string"==typeof g&&""!==g.trim()&&"transparent"!==g.toLowerCase()?(u.fillStyle=g,u.fillRect(0,0,d.width,d.height)):u.clearRect(0,0,d.width,d.height),c.clearRect(0,0,r.width,r.height),l.clearRect(0,0,_.width,_.height),c.setTransform(this._dpr*o,0,0,this._dpr*o,this._dpr*this._tx,this._dpr*this._ty);for(const t of this._beforeRenderCallbacks)t(c);this._render(this);for(const t of this._afterRenderCallbacks)t(c);u.drawImage(r,0,0),u.drawImage(_,0,0),this._raf=requestAnimationFrame(()=>this._loop())}applyWorldTransform(t){const e=Math.exp(this._currentLogZ);t.setTransform(this._dpr*e,0,0,this._dpr*e,this._dpr*this._tx,this._dpr*this._ty)}applyScreenTransform(t){t.setTransform(this._dpr,0,0,this._dpr,0,0)}getPixelColorAtScreen(t,e){const i=Math.floor(t*this._dpr),s=Math.floor(e*this._dpr);if(i<0||s<0||i>=this.canvas.width||s>=this.canvas.height)return{r:0,g:0,b:0,a:0,rgba:"rgba(0,0,0,0)",hex:"#000000"};const n=this.contentContext.getImageData(i,s,1,1).data,h=n[0],o=n[1],a=n[2],r=n[3]/255,c=t=>t.toString(16).padStart(2,"0"),_=`#${c(h)}${c(o)}${c(a)}`;return{r:h,g:o,b:a,a:r,rgba:`rgba(${h},${o},${a},${r.toFixed(3)})`,hex:_}}getPixelColorAtWorld(t,e){const{x:i,y:s}=this.toScreen(t,e);return this.getPixelColorAtScreen(i,s)}registerLayerManager(t){t&&(this._layerManagers.includes(t)||(this._layerManagers.push(t),this._needsRender=!0))}unregisterLayerManager(t){const e=this._layerManagers.indexOf(t);e>=0&&(this._layerManagers.splice(e,1),this._needsRender=!0)}getLayerManagers(){return[...this._layerManagers]}resetSmooth(){this._isResetting=!0,this._targetLogZ=0,this._needsRender=!0}resetInstant(){this._currentLogZ=0,this._targetLogZ=0,this._tx=0,this._ty=0,this._needsRender=!0}toWorld(t,e){const i=Math.exp(this._currentLogZ);return{wx:(t-this._tx)/i,wy:(e-this._ty)/i}}toScreen(t,e){const i=Math.exp(this._currentLogZ);return{x:t*i+this._tx,y:e*i+this._ty}}getTransform(){return{zoom:Math.exp(this._currentLogZ),tx:this._tx,ty:this._ty}}getViewportBounds(){const t=Math.exp(this._currentLogZ),e=this.canvas.width/this._dpr,i=this.canvas.height/this._dpr,s=-this._tx/t,n=-this._ty/t,h=(e-this._tx)/t,o=(i-this._ty)/t;return{left:s,top:n,right:h,bottom:o,width:h-s,height:o-n}}setZoomRange(t,e){this._options.minZoom=t,this._options.maxZoom=e,this.LOG_MIN=Math.log(t),this.LOG_MAX=Math.log(e),this._targetLogZ=Math.min(this.LOG_MAX,Math.max(this.LOG_MIN,this._targetLogZ))}resizeToParent(){this._isResizing=!0;const t=(this.canvas.parentElement||this.canvas).getBoundingClientRect();this._dpr=Math.max(1,window.devicePixelRatio||1);const e=Math.max(1,Math.round(t.width)),i=Math.max(1,Math.round(t.height));this.canvas.width=Math.round(e*this._dpr),this.canvas.height=Math.round(i*this._dpr),this.canvas.style.width=`${e}px`,this.canvas.style.height=`${i}px`,this._ensureOffscreenSizeLike(this.contentCanvas,this.canvas),this._ensureOffscreenSizeLike(this.topScreenCanvas,this.canvas),clearTimeout(this._resizeReleaseTimer),this._resizeReleaseTimer=window.setTimeout(()=>{this._isResizing=!1},50)}destroy(){cancelAnimationFrame(this._raf);for(const t of this._plugins.values())t.destroy();this._plugins.clear(),this._resizeObserver&&this._resizeObserver.disconnect(),this._layerManagers=[],this._updateCallbacks.clear(),this._beforeRenderCallbacks.clear(),this._afterRenderCallbacks.clear()}constructor(t,e,i){this._layerManagers=[],this._plugins=new Map,this._isResetting=!1,this._isResizing=!1,this._needsRender=!0,this._raf=0,this._lastFrameTs=performance.now(),this._tx=0,this._ty=0,this._anchorX=0,this._anchorY=0,this._currentLogZ=Math.log(1),this._targetLogZ=Math.log(1),this._targetTx=null,this._targetTy=null,this._updateCallbacks=new Set,this._beforeRenderCallbacks=new Set,this._afterRenderCallbacks=new Set,this._dpr=Math.max(1,window.devicePixelRatio||1);const s=t.getContext("2d",{willReadFrequently:!0,alpha:!0});if(!s)throw new Error("2D context not available");this.canvas=t,this.context=s,this._render=e,this.contentCanvas=document.createElement("canvas"),this.contentCanvas.width=t.width,this.contentCanvas.height=t.height,this.contentContext=this.contentCanvas.getContext("2d",{alpha:!0,willReadFrequently:!0}),this.topScreenCanvas=document.createElement("canvas"),this.topScreenCanvas.width=t.width,this.topScreenCanvas.height=t.height,this.topScreenContext=this.topScreenCanvas.getContext("2d",{alpha:!0}),this._options=Object.assign({minZoom:.5,maxZoom:10,approachKZoom:.022,approachKPan:.022,autoResize:!0,background:"#fff"},i),this.LOG_MIN=Math.log(this._options.minZoom),this.LOG_MAX=Math.log(this._options.maxZoom),this._options.autoResize&&(this._resizeObserver=new ResizeObserver(()=>this.resizeToParent()),this._resizeObserver.observe(this.canvas.parentElement||this.canvas)),this.resizeToParent(),this._lastFrameTs=performance.now(),this._raf=requestAnimationFrame(()=>this._loop())}}let o=0;class a{constructor(t,e,i="world"){this.space="world",this.visible=!0,this.opacity=1,this.blend="source-over",this.name=t,this.id=`layer_${e}_${++o}`,this.type=e,this.space=i}}class r extends a{beginStroke(t,e){const{lx:i,ly:s}=this.toLocalPoint(t,e);this._lastX=i,this._lastY=s,this._drawing=!0}stroke(t,e,i,s,n=1,h="brush"){if(!this._drawing)return;const{lx:o,ly:a}=this.toLocalPoint(t,e);this.context.beginPath(),this.context.moveTo(this._lastX,this._lastY),this.context.lineTo(o,a),"eraser"===h?(this.context.globalCompositeOperation="destination-out",this.context.strokeStyle="rgba(0, 0, 0, 1)"):(this.context.globalCompositeOperation="source-over",this.context.strokeStyle=i),this.context.lineWidth=s*n,this.context.lineCap="round",this.context.lineJoin="round",this.context.stroke(),this.context.closePath(),this._lastX=o,this._lastY=a}endStroke(){this._drawing=!1}isDrawing(){return this._drawing}captureSnapshot(t){try{if(t){const{x:e,y:i,width:s,height:n}=t,h=Math.max(0,Math.floor(e)),o=Math.max(0,Math.floor(i)),a=Math.min(this.canvas.width-h,Math.ceil(s)),r=Math.min(this.canvas.height-o,Math.ceil(n));return a<=0||r<=0?null:this.context.getImageData(h,o,a,r)}return this.context.getImageData(0,0,this.canvas.width,this.canvas.height)}catch(t){return null}}restoreSnapshot(t,e){var i,s;const n=null!==(i=null==e?void 0:e.x)&&void 0!==i?i:0,h=null!==(s=null==e?void 0:e.y)&&void 0!==s?s:0;this.context.putImageData(t,n,h)}clearRegion(t){t?this.context.clearRect(t.x,t.y,t.width,t.height):this.context.clearRect(0,0,this.canvas.width,this.canvas.height)}requestRedraw(){var t;null===(t=this._redraw)||void 0===t||t.call(this,this.context,this.canvas)}drawImage(t,e,i,s,n){this.context.drawImage(t,e,i,null!=s?s:t.width,null!=n?n:t.height)}hitTest(t,e){const{lx:i,ly:s}=this.toLocalPoint(t,e);return i>=0&&i<=this.canvas.width&&s>=0&&s<=this.canvas.height}toLocalPoint(t,e){const i=t-this.x,s=e-this.y,n=Math.cos(-this.rotation),h=Math.sin(-this.rotation),o=i*h+s*n,a=(i*n-s*h)/this.scale,r=o/this.scale;return{lx:a+("center"===this.anchor?this.canvas.width/2:0),ly:r+("center"===this.anchor?this.canvas.height/2:0)}}render(t,e){if(!this.visible)return;const i=this.canvas.width*this.scale,s=this.canvas.height*this.scale,n="center"===this.anchor?-i/2:0,h="center"===this.anchor?-s/2:0;t.save(),t.globalAlpha=this.opacity,t.globalCompositeOperation=this.blend,t.translate(this.x,this.y),t.rotate(this.rotation),t.drawImage(this.canvas,n,h,i,s),t.restore()}cropTo(t){const e=Math.max(1,Math.floor(t.width)),i=Math.max(1,Math.floor(t.height));if(e===this.canvas.width&&i===this.canvas.height)return;const s=this._cloneCanvas(),n=Math.min(s.width,e),h=Math.min(s.height,i);this._setCanvasSize(e,i),n>0&&h>0&&this.context.drawImage(s,0,0,n,h,0,0,n,h)}resizeTo(t){const e=Math.max(1,Math.floor(t.width)),i=Math.max(1,Math.floor(t.height));if(e===this.canvas.width&&i===this.canvas.height)return;const s=this._cloneCanvas();this._setCanvasSize(e,i),s.width>0&&s.height>0&&this.context.drawImage(s,0,0,s.width,s.height,0,0,e,i)}_cloneCanvas(){const t=document.createElement("canvas");if(t.width=this.canvas.width,t.height=this.canvas.height,0===t.width||0===t.height)return t;const e=t.getContext("2d");if(!e)throw new Error("Offscreen 2D context unavailable");return e.drawImage(this.canvas,0,0),t}_setCanvasSize(t,e){this.canvas.width=t,this.canvas.height=e,this.context.setTransform(1,0,0,1,0,0),this.context.clearRect(0,0,t,e)}destroy(){this.canvas.width=0,this.canvas.height=0}constructor(t){var e,i;if(super(t.name||"","canvas",null!==(e=t.space)&&void 0!==e?e:"world"),this.x=0,this.y=0,this.scale=1,this.rotation=0,this.anchor="topLeft",this._drawing=!1,this._lastX=0,this._lastY=0,"canvas"in t&&t.canvas)this.canvas=t.canvas;else{const e=t;this.canvas=document.createElement("canvas"),this.canvas.width=e.width,this.canvas.height=e.height}const s=this.canvas.getContext("2d",{willReadFrequently:!0});if(!s)throw new Error("Offscreen 2D context unavailable");this.context=s,this.x=t.x||0,this.y=t.y||0,this.scale=null!==(i=t.scale)&&void 0!==i?i:1,this.rotation=t.rotation||0,t.anchor&&(this.anchor=t.anchor),this._redraw=t.redraw,this._redraw&&this._redraw(this.context,this.canvas)}}var c;class _ extends r{static fromImage(e){return t(this,void 0,void 0,function*(){var t,n,h,o,a,r,l,d;const u=yield s(e.src,e.crossOrigin),g=null!==(t=e.width)&&void 0!==t?t:u.naturalWidth,v=null!==(n=e.height)&&void 0!==n?n:u.naturalHeight,p=new _({name:e.name,space:null!==(h=e.space)&&void 0!==h?h:"world",x:null!==(o=e.x)&&void 0!==o?o:0,y:null!==(a=e.y)&&void 0!==a?a:0,scale:null!==(r=e.scale)&&void 0!==r?r:1,rotation:null!==(l=e.rotation)&&void 0!==l?l:0,anchor:null!==(d=e.anchor)&&void 0!==d?d:"topLeft",width:g,height:v});return p.context.clearRect(0,0,p.canvas.width,p.canvas.height),p.context.drawImage(u,0,0,g,v),"string"!=typeof e.src&&i(p,c,u.src.startsWith("blob:")?u.src:null,"f"),p})}setSource(n,h){return t(this,void 0,void 0,function*(){const t=yield s(n,h);if(this.canvas.width=t.naturalWidth,this.canvas.height=t.naturalHeight,this.context.setTransform(1,0,0,1,0,0),this.context.clearRect(0,0,this.canvas.width,this.canvas.height),this.context.drawImage(t,0,0),e(this,c,"f")){try{URL.revokeObjectURL(e(this,c,"f"))}catch(t){}i(this,c,null,"f")}"string"!=typeof n&&i(this,c,t.src.startsWith("blob:")?t.src:null,"f")})}paint(t){t(this.context,this.canvas)}getImageData(t=0,e=0,i=this.canvas.width,s=this.canvas.height){return this.context.getImageData(t,e,i,s)}putImageData(t,e=0,i=0){this.context.putImageData(t,e,i)}toDataURL(t="image/png",e){return this.canvas.toDataURL(t,e)}toImageBitmap(t){return createImageBitmap(this.canvas,null!=t?t:{})}destroy(){var t;if(null===(t=super.destroy)||void 0===t||t.call(this),e(this,c,"f")){try{URL.revokeObjectURL(e(this,c,"f"))}catch(t){}i(this,c,null,"f")}}constructor(t){var e,i;super({name:t.name,space:null!==(e=t.space)&&void 0!==e?e:"world",x:t.x,y:t.y,scale:t.scale,rotation:t.rotation,anchor:null!==(i=t.anchor)&&void 0!==i?i:"topLeft",width:t.width,height:t.height}),c.set(this,null),this.type="bitmap"}}c=new WeakMap;class l{constructor(){this._worldLayers=[],this._screenLayers=[]}_renderAllLayersIn(t,e){e.save(),t.applyWorldTransform(e);for(const i of this._worldLayers)!i.visible||i.opacity<=0||(e.save(),i.render(e,t),e.restore());e.restore(),e.save(),t.applyScreenTransform(e);for(const i of this._screenLayers)!i.visible||i.opacity<=0||(e.save(),i.render(e,t),e.restore());e.restore()}addLayer(t,e){const i="world"===t.space?this._worldLayers:this._screenLayers;return"number"==typeof e&&e>=0&&e<i.length?(i.splice(e,0,t),t.id):(i.push(t),t.id)}createImageLayer(e){return t(this,void 0,void 0,function*(){const t=yield _.fromImage(e);return this.addLayer(t),t})}createCanvasLayer(t){const e=new r(t);return this.addLayer(e),e}removeLayer(t){var e,i,s,n;const h=this._worldLayers.findIndex(e=>e.id===t);if(h>=0)return null===(i=(e=this._worldLayers[h]).destroy)||void 0===i||i.call(e),void this._worldLayers.splice(h,1);const o=this._screenLayers.findIndex(e=>e.id===t);o>=0&&(null===(n=(s=this._screenLayers[o]).destroy)||void 0===n||n.call(s),this._screenLayers.splice(o,1))}detachLayer(t){const e=this._worldLayers.findIndex(e=>e.id===t);if(e>=0)return void this._worldLayers.splice(e,1);const i=this._screenLayers.findIndex(e=>e.id===t);i>=0&&this._screenLayers.splice(i,1)}moveLayer(t,e){const i=this._worldLayers.findIndex(e=>e.id===t);if(i>=0){const[t]=this._worldLayers.splice(i,1),s=Math.max(0,Math.min(e,this._worldLayers.length));return void this._worldLayers.splice(s,0,t)}const s=this._screenLayers.findIndex(e=>e.id===t);if(s>=0){const[t]=this._screenLayers.splice(s,1),i=Math.max(0,Math.min(e,this._screenLayers.length));this._screenLayers.splice(i,0,t)}}reorderLayers(t){const e=[];for(const i of t){const t=this._worldLayers.find(t=>t.id===i);t&&e.push(t)}for(const i of this._worldLayers)t.includes(i.id)||e.push(i);this._worldLayers=e}getLayer(t){return this._worldLayers.find(e=>e.id===t)||this._screenLayers.find(e=>e.id===t)}getAllLayers(t){return t?("world"===t?this._worldLayers:this._screenLayers).slice():[...this._worldLayers,...this._screenLayers]}hitTest(t,e,i="world"){const s=this.getAllLayers(i);for(let i=s.length-1;i>=0;i--){const n=s[i];if(n.hitTest&&n.hitTest(t,e))return n}}destroy(){var t;for(const e of[...this._worldLayers,...this._screenLayers])null===(t=e.destroy)||void 0===t||t.call(e);this._worldLayers=[],this._screenLayers=[]}}class d extends l{constructor(){super(...arguments),this._compositeCache=null,this._compositeCacheCtx=null,this._compositeDirty=!0,this._lastCacheWidth=0,this._lastCacheHeight=0,this._cachedBoundsMinX=0,this._cachedBoundsMinY=0}markDirty(){this._compositeDirty=!0}addLayer(t,e){return this._compositeDirty=!0,super.addLayer(t,e)}removeLayer(t){this._compositeDirty=!0,super.removeLayer(t)}detachLayer(t){this._compositeDirty=!0,super.detachLayer(t)}removeAllLayers(t){var e,i;if(!t||"world"===t){for(const t of this._worldLayers)null===(e=t.destroy)||void 0===e||e.call(t);this._worldLayers=[]}if(!t||"screen"===t){for(const t of this._screenLayers)null===(i=t.destroy)||void 0===i||i.call(t);this._screenLayers=[]}}moveLayer(t,e){this._compositeDirty=!0,super.moveLayer(t,e)}renderAllLayersIn(t){const e=t.contentContext,i=this._worldLayers;0!==i.length&&(this._compositeDirty&&this._rebuildCompositeCache(i),this._compositeCache&&this._compositeCacheCtx&&e.drawImage(this._compositeCache,this._cachedBoundsMinX,this._cachedBoundsMinY))}_rebuildCompositeCache(t){let e=0,i=0,s=0,n=0,h=!1;for(const o of t){const t=o,a=t.canvas.width*t.scale,r=t.canvas.height*t.scale,c="center"===t.anchor?-a/2:0,_="center"===t.anchor?-r/2:0,l=Math.cos(t.rotation),d=Math.sin(t.rotation),u=[{x:c,y:_},{x:c+a,y:_},{x:c,y:_+r},{x:c+a,y:_+r}];let g=1/0,v=1/0,p=-1/0,m=-1/0;for(const e of u){const i=e.x*l-e.y*d+t.x,s=e.x*d+e.y*l+t.y;g=Math.min(g,i),v=Math.min(v,s),p=Math.max(p,i),m=Math.max(m,s)}h?(e=Math.min(e,g),i=Math.min(i,v),s=Math.max(s,p),n=Math.max(n,m)):(e=g,i=v,s=p,n=m,h=!0)}if(!h)return void(this._compositeDirty=!1);const o=Math.ceil(s-e),a=Math.ceil(n-i);this._compositeCache||(this._compositeCache=document.createElement("canvas"),this._compositeCacheCtx=this._compositeCache.getContext("2d",{alpha:!0})),this._lastCacheWidth===o&&this._lastCacheHeight===a||(this._compositeCache.width=o,this._compositeCache.height=a,this._lastCacheWidth=o,this._lastCacheHeight=a),this._cachedBoundsMinX=e,this._cachedBoundsMinY=i;const r=this._compositeCacheCtx;r.clearRect(0,0,o,a),r.save(),r.translate(-e,-i);for(const e of t)!e.visible||e.opacity<=0||(r.save(),e.render(r),r.restore());r.restore(),this._compositeDirty=!1}destroy(){super.destroy(),this._compositeCache=null,this._compositeCacheCtx=null}}class u extends l{renderAllLayersIn(t){const e=t.topScreenContext;this._renderAllLayersIn(t,e)}}class g{constructor(t){this.name="interaction",this._view=null,this._dragging=!1,this._vx=0,this._vy=0,this._lastMoveTs=0,this._activePointerId=null,this._lastPointerX=0,this._lastPointerY=0,this._touchPointers=new Map,this._isTouchPanning=!1,this._isPinching=!1,this._lastPinchDistance=0,this._lastPinchCenterX=0,this._lastPinchCenterY=0,this._lastTouchX=0,this._lastTouchY=0,this._onDownBound=t=>this._onPointerDown(t),this._onMoveBound=t=>this._onPointerMove(t),this._onUpBound=t=>this._onPointerUp(t),this._onCancelBound=t=>this._onPointerCancel(t),this._onWheelBound=t=>this._onWheel(t),this._onUpdate=t=>{if(!this._view)return;const{friction:e,stopSpeed:i}=this._options,s=Math.hypot(this._vx,this._vy)>=i;if(!(this._dragging||this._isTouchPanning||this._isPinching)&&s){const s=this._vx*t,n=this._vy*t;this._view.panBy(s,n),this._vx*=e,this._vy*=e,Math.hypot(this._vx,this._vy)<i&&(this._vx=0,this._vy=0)}},this._options=Object.assign({panEnabled:!0,zoomEnabled:!0,friction:.92,stopSpeed:.02,emaAlpha:.25,idleNoInertiaMs:120,wheelSensitivity:.0015},t),this._panEnabled=this._options.panEnabled,this._zoomEnabled=this._options.zoomEnabled}install(t){this._view=t;const e=t.canvas;e.style.touchAction="none",e.addEventListener("wheel",this._onWheelBound,{passive:!1}),e.addEventListener("pointerdown",this._onDownBound),window.addEventListener("pointermove",this._onMoveBound),window.addEventListener("pointerup",this._onUpBound),window.addEventListener("pointercancel",this._onCancelBound),t.onUpdate(this._onUpdate)}destroy(){if(!this._view)return;const t=this._view.canvas;t.removeEventListener("wheel",this._onWheelBound),t.removeEventListener("pointerdown",this._onDownBound),window.removeEventListener("pointermove",this._onMoveBound),window.removeEventListener("pointerup",this._onUpBound),window.removeEventListener("pointercancel",this._onCancelBound),this._touchPointers.clear(),this._isPinching=!1,this._isTouchPanning=!1,this._view.offUpdate(this._onUpdate),this._view=null}isPanEnabled(){return this._panEnabled}isZoomEnabled(){return this._zoomEnabled}setPanEnabled(t){this._panEnabled!==t&&(this._panEnabled=t,t||null===this._activePointerId||(this._dragging=!1,this._vx=0,this._vy=0))}setZoomEnabled(t){this._zoomEnabled!==t&&(this._zoomEnabled=t,!t&&this._isPinching&&this._endPinch())}setWheelSensitivity(t){this._options.wheelSensitivity=t}isDragging(){return this._dragging||this._isTouchPanning||this._isPinching}_onPointerDown(t){var e;if("touch"!==t.pointerType){if(("mouse"!==t.pointerType||0===t.button)&&this._panEnabled&&null===this._activePointerId){t.preventDefault(),this._dragging=!0,this._vx=0,this._vy=0,this._lastMoveTs=performance.now(),this._activePointerId=t.pointerId,this._lastPointerX=t.clientX,this._lastPointerY=t.clientY;try{null===(e=this._view)||void 0===e||e.canvas.setPointerCapture(t.pointerId)}catch(t){}}}else this._onTouchDown(t)}_onPointerMove(t){if("touch"===t.pointerType)return void this._onTouchMove(t);if(t.pointerId!==this._activePointerId)return;if(!this._dragging||!this._panEnabled||!this._view)return;const e=performance.now(),i=Math.max(1,e-(this._lastMoveTs||e-16));this._lastMoveTs=e;const s=t.clientX-this._lastPointerX,n=t.clientY-this._lastPointerY;this._lastPointerX=t.clientX,this._lastPointerY=t.clientY,this._view.panBy(s,n);const h=this._options.emaAlpha,o=s/i,a=n/i;this._vx=(1-h)*this._vx+h*o,this._vy=(1-h)*this._vy+h*a}_onPointerUp(t){if("touch"===t.pointerType)return void this._onTouchUp(t);if(t.pointerId!==this._activePointerId)return;if(!this._dragging)return;this._dragging=!1;const e=performance.now(),i=this._lastMoveTs?e-this._lastMoveTs:1/0;if(null!=this._activePointerId&&this._view){try{this._view.canvas.releasePointerCapture(this._activePointerId)}catch(t){}this._activePointerId=null}if(i>=this._options.idleNoInertiaMs)this._vx=0,this._vy=0;else{const t=Math.pow(this._options.friction,i/16);this._vx*=t,this._vy*=t}Math.hypot(this._vx,this._vy)<this._options.stopSpeed&&(this._vx=0,this._vy=0)}_onPointerCancel(t){"touch"!==t.pointerType?t.pointerId===this._activePointerId&&(this._dragging=!1,this._vx=0,this._vy=0,this._activePointerId=null):this._onTouchCancel(t)}_onTouchDown(t){if(t.preventDefault(),this._touchPointers.set(t.pointerId,{x:t.clientX,y:t.clientY}),2===this._touchPointers.size&&this._zoomEnabled)return this._isTouchPanning=!1,void this._startPinch();1===this._touchPointers.size&&(this._isTouchPanning=!0,this._vx=0,this._vy=0,this._lastMoveTs=performance.now(),this._lastTouchX=t.clientX,this._lastTouchY=t.clientY)}_onTouchMove(t){if(this._touchPointers.has(t.pointerId))if(this._touchPointers.set(t.pointerId,{x:t.clientX,y:t.clientY}),this._isPinching&&2===this._touchPointers.size)this._handlePinchMove();else if(this._isTouchPanning&&1===this._touchPointers.size&&this._view){const e=performance.now(),i=Math.max(1,e-(this._lastMoveTs||e-16));this._lastMoveTs=e;const s=t.clientX-this._lastTouchX,n=t.clientY-this._lastTouchY;this._lastTouchX=t.clientX,this._lastTouchY=t.clientY,this._view.panBy(s,n);const h=this._options.emaAlpha,o=s/i,a=n/i;this._vx=(1-h)*this._vx+h*o,this._vy=(1-h)*this._vy+h*a}}_onTouchUp(t){if(this._touchPointers.delete(t.pointerId),this._isPinching){if(this._endPinch(),1===this._touchPointers.size){const[[,t]]=this._touchPointers.entries();this._isTouchPanning=!0,this._lastTouchX=t.x,this._lastTouchY=t.y,this._vx=0,this._vy=0,this._lastMoveTs=performance.now()}}else if(this._isTouchPanning&&0===this._touchPointers.size){this._isTouchPanning=!1;const t=performance.now(),e=this._lastMoveTs?t-this._lastMoveTs:1/0;if(e>=this._options.idleNoInertiaMs)this._vx=0,this._vy=0;else{const t=Math.pow(this._options.friction,e/16);this._vx*=t,this._vy*=t}Math.hypot(this._vx,this._vy)<this._options.stopSpeed&&(this._vx=0,this._vy=0)}}_onTouchCancel(t){this._touchPointers.delete(t.pointerId),this._isPinching&&this._endPinch(),0===this._touchPointers.size&&(this._isTouchPanning=!1,this._vx=0,this._vy=0)}_startPinch(){this._isPinching=!0;const[t,e]=Array.from(this._touchPointers.values());this._lastPinchDistance=Math.hypot(e.x-t.x,e.y-t.y),this._lastPinchCenterX=(t.x+e.x)/2,this._lastPinchCenterY=(t.y+e.y)/2}_handlePinchMove(){if(!this._view||!this._zoomEnabled)return;const t=Array.from(this._touchPointers.values());if(2!==t.length)return;const[e,i]=t,s=Math.hypot(i.x-e.x,i.y-e.y),n=(e.x+i.x)/2,h=(e.y+i.y)/2;if(this._lastPinchDistance>0){const t=s/this._lastPinchDistance,e=this._view.canvas.getBoundingClientRect(),i=n-e.left,o=h-e.top;this._view.zoomByFactorAtScreen(i,o,t);const a=n-this._lastPinchCenterX,r=h-this._lastPinchCenterY;this._view.panBy(a,r)}this._lastPinchDistance=s,this._lastPinchCenterX=n,this._lastPinchCenterY=h}_endPinch(){this._isPinching=!1,this._lastPinchDistance=0,this._lastPinchCenterX=0,this._lastPinchCenterY=0}_getLineHeightPx(){if(!this._view)return 16;const t=getComputedStyle(this._view.canvas).lineHeight;if(!t||"normal"===t)return 16;const e=parseFloat(t);return Number.isFinite(e)?e:16}_normalizeWheelDelta(t){if(!this._view)return 0;let e=t.deltaY;if(1===t.deltaMode)e*=this._getLineHeightPx();else if(2===t.deltaMode){e*=this._view.canvas.clientHeight||window.innerHeight||800}return e}_onWheel(t){if(!this._zoomEnabled||!this._view)return;t.preventDefault(),t.stopPropagation();const e=this._normalizeWheelDelta(t),i=this._view.canvas.getBoundingClientRect(),s=t.clientX-i.left,n=t.clientY-i.top;let h=-e*this._options.wheelSensitivity;t.ctrlKey||t.metaKey?h*=1.6:t.shiftKey&&(h*=.6),this._view.zoomByLogAtScreen(s,n,h)}}function v(t){return new g(t)}class p{constructor(t){var e,i,s,n,h;this.name="document",this._view=null,this._enabled=!1,this._x=0,this._y=0,this._width=0,this._height=0,this._marginL=0,this._marginR=0,this._marginT=0,this._marginB=0,this._panClampMode="minVisible",this._background=null,this._shadow=null,this._onUpdate=()=>{this._enabled&&this._view&&this._clampPan()},this._onBeforeRender=t=>{var e,i,s,n,h;this._enabled&&this._view&&(this._shadow?(t.save(),t.shadowColor=null!==(e=this._shadow.color)&&void 0!==e?e:p._defaultShadow.color,t.shadowBlur=null!==(i=this._shadow.blur)&&void 0!==i?i:p._defaultShadow.blur,t.shadowOffsetX=null!==(s=this._shadow.offsetX)&&void 0!==s?s:p._defaultShadow.offsetX,t.shadowOffsetY=null!==(n=this._shadow.offsetY)&&void 0!==n?n:p._defaultShadow.offsetY,t.fillStyle=null!==(h=this._background)&&void 0!==h?h:"#ffffff",t.fillRect(this._x,this._y,this._width,this._height),t.restore()):this._background&&(t.save(),t.fillStyle=this._background,t.fillRect(this._x,this._y,this._width,this._height),t.restore()),t.save(),t.beginPath(),t.rect(this._x,this._y,this._width,this._height),t.clip())},this._onAfterRender=t=>{if(this._enabled&&this._view&&(t.restore(),this._options.drawBorder)){const e=this._view.zoom;t.save(),t.lineWidth=1/e,t.strokeStyle="#cfcfcf",t.strokeRect(this._x,this._y,this._width,this._height),t.restore()}},this._options=Object.assign({rect:{x:0,y:0,width:0,height:0},margins:{},drawBorder:!1,minVisiblePx:30,panClampMode:"minVisible",background:null,shadow:!1},t),(null==t?void 0:t.rect)&&(this._enabled=!0,this._x=t.rect.x,this._y=t.rect.y,this._width=t.rect.width,this._height=t.rect.height),(null==t?void 0:t.margins)&&(this._marginL=null!==(e=t.margins.left)&&void 0!==e?e:0,this._marginR=null!==(i=t.margins.right)&&void 0!==i?i:0,this._marginT=null!==(s=t.margins.top)&&void 0!==s?s:0,this._marginB=null!==(n=t.margins.bottom)&&void 0!==n?n:0),this._panClampMode=this._options.panClampMode,this._background=null!==(h=this._options.background)&&void 0!==h?h:null,!0===this._options.shadow?this._shadow=Object.assign({},p._defaultShadow):this._options.shadow&&"object"==typeof this._options.shadow&&(this._shadow=Object.assign(Object.assign({},p._defaultShadow),this._options.shadow))}install(t){this._view=t,t.onUpdate(this._onUpdate),t.onBeforeRender(this._onBeforeRender),t.onAfterRender(this._onAfterRender)}destroy(){this._view&&(this._view.offUpdate(this._onUpdate),this._view.offBeforeRender(this._onBeforeRender),this._view.offAfterRender(this._onAfterRender),this._view=null)}isEnabled(){return this._enabled}getRect(){return{x:this._x,y:this._y,width:this._width,height:this._height}}setRect(t,e,i,s){this._enabled=!0,this._x=t,this._y=e,this._width=i,this._height=s}clearRect(){this._enabled=!1}setMargins(t){var e,i,s,n;this._marginL=null!==(e=t.left)&&void 0!==e?e:this._marginL,this._marginR=null!==(i=t.right)&&void 0!==i?i:this._marginR,this._marginT=null!==(s=t.top)&&void 0!==s?s:this._marginT,this._marginB=null!==(n=t.bottom)&&void 0!==n?n:this._marginB}getMargins(){return{left:this._marginL,right:this._marginR,top:this._marginT,bottom:this._marginB}}setPanClampMode(t){this._panClampMode=t}getPanClampMode(){return this._panClampMode}cropTo(t){this._doResize("crop",t)}resizeTo(t){this._doResize("resize",t)}zoomToFit(t="contain",e=!0){if(!this._enabled||!this._view)return;const i=this._view.dpr,s=this._view.canvas.width/i,n=this._view.canvas.height/i,h=Math.max(1,s-(this._marginL+this._marginR)),o=Math.max(1,n-(this._marginT+this._marginB));let a;const r=h/this._width,c=o/this._height;a="contain"===t?Math.min(r,c):"cover"===t?Math.max(r,c):"fitWidth"===t?r:c,a=Math.min(this._view.maxZoom,Math.max(this._view.minZoom,a));const _=this._marginL+(h-a*this._width)/2,l=this._marginT+(o-a*this._height)/2,d=_-a*this._x,u=l-a*this._y;e?this._view.setTransformSmooth(a,d,u):this._view.setTransform(a,d,u)}isPointInDocument(t,e){return!this._enabled||t>=this._x&&t<=this._x+this._width&&e>=this._y&&e<=this._y+this._height}get background(){return this._background}setBackground(t){var e;this._background=t,null===(e=this._view)||void 0===e||e.requestRender()}get shadow(){return this._shadow?Object.assign({},this._shadow):null}setShadow(t){var e;this._shadow=!0===t?Object.assign({},p._defaultShadow):t&&"object"==typeof t?Object.assign(Object.assign({},p._defaultShadow),t):null,null===(e=this._view)||void 0===e||e.requestRender()}_clampPan(){if(!this._view)return;const{zoom:t,tx:e,ty:i}=this._view.getTransform(),s=t,n=this._view.dpr,h=this._view.canvas.width/n,o=this._view.canvas.height/n,a=this._x,r=this._y,c=this._x+this._width,_=this._y+this._height;let l=e,d=i;if("margin"===this._panClampMode){const t=this._marginL-s*a,n=h-this._marginR-s*c,u=this._marginT-s*r,g=o-this._marginB-s*_,v=Math.max(1,h-(this._marginL+this._marginR)),p=Math.max(1,o-(this._marginT+this._marginB));l=s*this._width<=v?this._marginL+(v-s*this._width)/2-s*this._x:Math.min(t,Math.max(n,e)),d=s*this._height<=p?this._marginT+(p-s*this._height)/2-s*this._y:Math.min(u,Math.max(g,i))}else if("minVisible"===this._panClampMode){const t=s*this._width,n=s*this._height,u=Math.min(this._options.minVisiblePx,t),g=Math.min(this._options.minVisiblePx,n),v=h-u-s*a,p=u-s*c,m=o-g-s*r,y=g-s*_;l=p<=v?Math.min(v,Math.max(p,e)):(p+v)/2,d=y<=m?Math.min(m,Math.max(y,i)):(y+m)/2}l===e&&d===i||this._view.setPan(l,d)}_doResize(t,e){if(!this._view)return;const i=Math.max(1,Math.floor(e.width)),s=Math.max(1,Math.floor(e.height)),n={width:i,height:s},h=this._view.getLayerManagers();for(const e of h){const i=e.getAllLayers("world");for(const e of i){const i=e;"function"==typeof i.cropTo&&"function"==typeof i.resizeTo&&("crop"===t?i.cropTo(n):i.resizeTo(n))}}this._width=i,this._height=s,this._enabled&&this._clampPan()}}function m(t){return new p(t)}p._defaultShadow={color:"rgba(0, 0, 0, 0.3)",blur:20,offsetX:0,offsetY:4};class y{constructor(t,e,i,s){this.type="snapshot",this._isExecuted=!0,this._target=t,this._beforeData=e,this._afterData=i,this._region=null==s?void 0:s.region}execute(){if(this._isExecuted)return;const t=this._region?{x:this._region.x,y:this._region.y}:void 0;this._target.restoreSnapshot(this._afterData,t),this._isExecuted=!0}undo(){if(!this._isExecuted)return;const t=this._region?{x:this._region.x,y:this._region.y}:void 0;this._target.restoreSnapshot(this._beforeData,t),this._isExecuted=!1}canMerge(){return!1}merge(){return this}}function f(t,e,i,s){return e&&i?new y(t,e,i,s):null}class w{constructor(t){this.type="create-layer",this._layer=t.layer,this._layerManager=t.layerManager,this._insertIndex=t.insertIndex,this._previousSelectedId=t.previousSelectedId,this._getLayerList=t.getLayerList,this._setSelectedId=t.setSelectedId}execute(){this._layerManager.addLayer(this._layer,this._insertIndex);this._getLayerList().splice(this._insertIndex,0,this._layer),this._setSelectedId(this._layer.id)}undo(){this._layerManager.detachLayer(this._layer.id);const t=this._getLayerList(),e=t.findIndex(t=>t.id===this._layer.id);e>-1&&t.splice(e,1),this._setSelectedId(this._previousSelectedId)}}class x{constructor(t){this.type="delete-layer",this._layer=t.layer,this._layerManager=t.layerManager,this._originalIndex=t.originalIndex,this._previousSelectedId=t.previousSelectedId,this._newSelectedId=t.newSelectedId,this._getLayerList=t.getLayerList,this._setSelectedId=t.setSelectedId}execute(){this._layerManager.detachLayer(this._layer.id);const t=this._getLayerList(),e=t.findIndex(t=>t.id===this._layer.id);e>-1&&t.splice(e,1),this._setSelectedId(this._newSelectedId)}undo(){this._layerManager.addLayer(this._layer,this._originalIndex);this._getLayerList().splice(this._originalIndex,0,this._layer),this._setSelectedId(this._previousSelectedId)}}class M{executeCommand(t){t.execute(),this.addCommand(t)}addCommand(t){var e,i;this._redoStack=[];const s=this._undoStack[this._undoStack.length-1];if(s&&(null===(e=s.canMerge)||void 0===e?void 0:e.call(s,t))&&s.merge){const e=null!==(i=s.merge(t))&&void 0!==i?i:s;return void(e!==s&&(this._undoStack[this._undoStack.length-1]=e))}this._undoStack.push(t),this._undoStack.length>this._maxHistorySize&&this._undoStack.shift()}undo(){if(0===this._undoStack.length)return null;const t=this._undoStack.pop();return t.undo(),this._redoStack.push(t),t}redo(){if(0===this._redoStack.length)return null;const t=this._redoStack.pop();return t.execute(),this._undoStack.push(t),t}canUndo(){return this._undoStack.length>0}canRedo(){return this._redoStack.length>0}clear(){this._undoStack=[],this._redoStack=[]}setMaxHistorySize(t){this._maxHistorySize=Math.max(1,t),this._undoStack.length>this._maxHistorySize&&(this._undoStack=this._undoStack.slice(-this._maxHistorySize))}constructor(t){var e,i,s;this._undoStack=[],this._redoStack=[],this._maxHistorySize=null!==(e=null==t?void 0:t.maxHistorySize)&&void 0!==e?e:50,this._undoStack=null!==(i=null==t?void 0:t.undoStack)&&void 0!==i?i:[],this._redoStack=null!==(s=null==t?void 0:t.redoStack)&&void 0!==s?s:[]}}export{_ as BitmapLayer,r as CanvasLayer,d as ContentLayerManager,w as CreateLayerCommand,x as DeleteLayerCommand,p as DocumentPlugin,M as HistoryManager,g as InteractionPlugin,a as LayerBase,l as LayerManagerBase,y as SnapshotCommand,u as TopScreenLayerManager,h as ViewManager,m as createDocumentPlugin,v as createInteractionPlugin,f as createSnapshotCommand};
@@ -21,11 +21,20 @@ declare class LayerManagerBase {
21
21
  */
22
22
  createCanvasLayer(option: ICreateCanvasLayerFromCanvasOption): CanvasLayer;
23
23
  removeLayer(id: string): void;
24
+ /**
25
+ * Detach a layer from the manager without destroying it.
26
+ * Use this when you need to re-add the layer later (e.g., undo/redo).
27
+ */
28
+ detachLayer(id: string): void;
24
29
  /**
25
30
  * Move a layer from one index to another.
26
31
  * Both indices are relative to the layer's space (world or screen).
27
32
  */
28
33
  moveLayer(id: string, toIndex: number): void;
34
+ /**
35
+ * Reorder world layers by providing new order of layer IDs.
36
+ */
37
+ reorderLayers(orderedIds: string[]): void;
29
38
  getLayer(id: string): LayerBase | undefined;
30
39
  getAllLayers(space?: SpaceType): LayerBase[];
31
40
  /**
@@ -26,6 +26,10 @@ declare class ContentLayerManager extends LayerManagerBase {
26
26
  * Override removeLayer to mark cache dirty.
27
27
  */
28
28
  removeLayer(id: string): void;
29
+ /**
30
+ * Override detachLayer to mark cache dirty.
31
+ */
32
+ detachLayer(id: string): void;
29
33
  /**
30
34
  * Remove all layers from the specified space, or all spaces if not specified.
31
35
  * @param space - Optional. 'world' or 'screen'. If not provided, removes all layers.
@@ -1,3 +1,4 @@
1
1
  export * from './types';
2
2
  export * from './snapshot-command';
3
+ export * from './layer-commands';
3
4
  export * from './history-manager';
@@ -0,0 +1,47 @@
1
+ import type { ContentLayerManager } from '../../layer/layer-manager.content';
2
+ import type { CanvasLayer } from '../../layer/layer.canvas';
3
+ import type { ICommand } from './types';
4
+ interface ILayerCommandConfig {
5
+ layer: CanvasLayer;
6
+ layerManager: ContentLayerManager;
7
+ insertIndex: number;
8
+ previousSelectedId: string;
9
+ getLayerList: () => CanvasLayer[];
10
+ setSelectedId: (id: string) => void;
11
+ }
12
+ declare class CreateLayerCommand implements ICommand {
13
+ readonly type = "create-layer";
14
+ private _layer;
15
+ private _layerManager;
16
+ private _insertIndex;
17
+ private _previousSelectedId;
18
+ private _getLayerList;
19
+ private _setSelectedId;
20
+ constructor(config: ILayerCommandConfig);
21
+ execute(): void;
22
+ undo(): void;
23
+ }
24
+ interface IDeleteLayerCommandConfig {
25
+ layer: CanvasLayer;
26
+ layerManager: ContentLayerManager;
27
+ originalIndex: number;
28
+ previousSelectedId: string;
29
+ newSelectedId: string;
30
+ getLayerList: () => CanvasLayer[];
31
+ setSelectedId: (id: string) => void;
32
+ }
33
+ declare class DeleteLayerCommand implements ICommand {
34
+ readonly type = "delete-layer";
35
+ private _layer;
36
+ private _layerManager;
37
+ private _originalIndex;
38
+ private _previousSelectedId;
39
+ private _newSelectedId;
40
+ private _getLayerList;
41
+ private _setSelectedId;
42
+ constructor(config: IDeleteLayerCommandConfig);
43
+ execute(): void;
44
+ undo(): void;
45
+ }
46
+ export { CreateLayerCommand, DeleteLayerCommand };
47
+ export type { ILayerCommandConfig, IDeleteLayerCommandConfig };
@@ -2,7 +2,8 @@ import type { ViewManager } from '../../core/view-manager';
2
2
  import type { Plugin } from '../types';
3
3
  interface InteractionPluginOptions {
4
4
  /**
5
- * Enable pan (drag to move). Default true.
5
+ * Enable pan (drag to move) for mouse/pen. Default true.
6
+ * Note: Touch gestures always have pan enabled.
6
7
  */
7
8
  panEnabled?: boolean;
8
9
  /**
@@ -33,8 +34,9 @@ interface InteractionPluginOptions {
33
34
  /**
34
35
  * Interaction Plugin
35
36
  *
36
- * Handles pan (drag), zoom (wheel), and inertia.
37
- * Can be enabled/disabled at runtime.
37
+ * Handles pan (drag), zoom (wheel), pinch-to-zoom, and inertia.
38
+ * Touch gestures (pan/pinch) always work regardless of panEnabled setting.
39
+ * Mouse/pen pan is controlled by panEnabled.
38
40
  */
39
41
  declare class InteractionPlugin implements Plugin {
40
42
  readonly name = "interaction";
@@ -47,9 +49,20 @@ declare class InteractionPlugin implements Plugin {
47
49
  private _vy;
48
50
  private _lastMoveTs;
49
51
  private _activePointerId;
52
+ private _lastPointerX;
53
+ private _lastPointerY;
54
+ private _touchPointers;
55
+ private _isTouchPanning;
56
+ private _isPinching;
57
+ private _lastPinchDistance;
58
+ private _lastPinchCenterX;
59
+ private _lastPinchCenterY;
60
+ private _lastTouchX;
61
+ private _lastTouchY;
50
62
  private _onDownBound;
51
63
  private _onMoveBound;
52
64
  private _onUpBound;
65
+ private _onCancelBound;
53
66
  private _onWheelBound;
54
67
  constructor(options?: InteractionPluginOptions);
55
68
  install(view: ViewManager): void;
@@ -64,6 +77,14 @@ declare class InteractionPlugin implements Plugin {
64
77
  private _onPointerDown;
65
78
  private _onPointerMove;
66
79
  private _onPointerUp;
80
+ private _onPointerCancel;
81
+ private _onTouchDown;
82
+ private _onTouchMove;
83
+ private _onTouchUp;
84
+ private _onTouchCancel;
85
+ private _startPinch;
86
+ private _handlePinchMove;
87
+ private _endPinch;
67
88
  private _getLineHeightPx;
68
89
  private _normalizeWheelDelta;
69
90
  private _onWheel;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lancercomet/zoom-pan",
3
- "version": "0.1.5",
3
+ "version": "0.2.1",
4
4
  "description": "Yet another web 2D rendering lib.",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.module.js",
@@ -15,11 +15,10 @@
15
15
  },
16
16
  "keywords": [],
17
17
  "author": "",
18
- "license": "ISC",
19
18
  "devDependencies": {
20
19
  "@lancercomet/eslint-config-eslint-rules": "^0.2.0",
21
20
  "@rollup/plugin-terser": "^0.4.4",
22
- "@vitejs/plugin-vue-jsx": "^5.1.1",
21
+ "@vitejs/plugin-vue-jsx": "^5.1.2",
23
22
  "rollup": "^4.53.3",
24
23
  "rollup-plugin-commonjs": "^10.1.0",
25
24
  "rollup-plugin-delete": "^2.0.0",