@tsdraw/core 0.8.4 → 0.8.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts CHANGED
@@ -169,7 +169,13 @@ declare function setViewport(viewport: Viewport, updater: {
169
169
  zoom?: number;
170
170
  }): Viewport;
171
171
  declare function panViewport(viewport: Viewport, dx: number, dy: number): Viewport;
172
- declare function zoomViewport(viewport: Viewport, factor: number, centerX?: number, centerY?: number): Viewport;
172
+ interface ZoomRange {
173
+ min: number;
174
+ max: number;
175
+ }
176
+ declare const DEFAULT_ZOOM_RANGE: ZoomRange;
177
+ declare function clampZoom(zoom: number, range?: ZoomRange): number;
178
+ declare function zoomViewport(viewport: Viewport, factor: number, centerX?: number, centerY?: number, zoomRange?: ZoomRange): Viewport;
173
179
 
174
180
  interface IEditor {
175
181
  getZoomLevel(): number;
@@ -264,7 +270,10 @@ declare function moveCameraPan(session: CameraPanSession, currentScreenX: number
264
270
  interface CameraSlideAnimation {
265
271
  stop: () => void;
266
272
  }
267
- declare function startCameraSlide(session: CameraPanSession, applyPan: (dx: number, dy: number) => void, onFrame: () => void): CameraSlideAnimation | null;
273
+ interface CameraSlideOptions {
274
+ friction?: number;
275
+ }
276
+ declare function startCameraSlide(session: CameraPanSession, applyPan: (dx: number, dy: number) => void, onFrame: () => void, slideOptions?: CameraSlideOptions): CameraSlideAnimation | null;
268
277
 
269
278
  type TsdrawRenderTheme = 'light' | 'dark';
270
279
  declare function resolveThemeColor(colorStyle: string, theme: TsdrawRenderTheme): string;
@@ -313,6 +322,7 @@ interface EditorOptions {
313
322
  dragDistanceSquared?: number;
314
323
  toolDefinitions?: ToolDefinition[];
315
324
  initialToolId?: ToolId;
325
+ zoomRange?: ZoomRange;
316
326
  }
317
327
  type EditorListener = () => void;
318
328
  declare class Editor {
@@ -323,6 +333,7 @@ declare class Editor {
323
333
  viewport: Viewport;
324
334
  readonly options: {
325
335
  dragDistanceSquared: number;
336
+ zoomRange?: ZoomRange;
326
337
  };
327
338
  private drawStyle;
328
339
  private readonly toolStateContext;
@@ -656,4 +667,4 @@ declare function decodePathToPoints(segments: {
656
667
  y: number;
657
668
  }[];
658
669
 
659
- export { type Bounds, type CameraPanSession, type CameraSlideAnimation, CanvasRenderer, CircleDrawingState, CircleIdleState, type ColorStyle, DEFAULT_COLORS, DRAG_DISTANCE_SQUARED, type DashStyle, type DefaultToolId, DocumentStore, type DocumentStoreSnapshot, type DrawSegment, type DrawShape, ERASER_MARGIN, Editor, type EditorOptions, EraserErasingState, EraserIdleState, EraserPointingState, type FillStyle, HandDraggingState, HandIdleState, type ICanvasRenderer, type IEditor, InputManager, MAX_POINTS_PER_SHAPE, type PageState, PenDrawingState, PenIdleState, type PointerInput, type ResizeHandle, STROKE_WIDTHS, type SegmentType, SelectIdleState, type SelectionBounds, type Shape, type ShapeId, type SizeStyle, SquareDrawingState, SquareIdleState, StateNode, type StateNodeConstructor, type ToolDefinition, type ToolId, type ToolKeyInfo, ToolManager, type ToolPointerDownInfo, type ToolPointerMoveInfo, type ToolStateContext, type ToolStateTransitionInfo, type TransformSnapshot, type TsdrawDocumentSnapshot, type TsdrawEditorSnapshot, type TsdrawHistorySnapshot, type TsdrawPageRecord, type TsdrawPersistedRecord, type TsdrawRenderTheme, type TsdrawSessionStateSnapshot, type TsdrawShapeRecord, type Vec3, type Viewport, applyMove, applyResize, applyRotation, beginCameraPan, boundsContainPoint, boundsIntersect, boundsOf, buildStartPositions, buildTransformSnapshots, closestOnSegment, createViewport, decodeFirstPoint, decodeLastPoint, decodePathToPoints, decodePoints, distance, documentSnapshotToRecords, encodePoints, getSelectionBoundsPage, getShapeBounds, getShapesInBounds, getTopShapeAtPoint, isSelectTool, minDistanceToPolyline, moveCameraPan, normalizeSelectionBounds, padBounds, pageToScreen, panViewport, pointHitsShape, recordsToDocumentSnapshot, resolveThemeColor, rotatePoint, screenToPage, segmentHitsShape, segmentTouchesPolyline, setViewport, shapePagePoints, sqDistance, startCameraSlide, zoomViewport };
670
+ export { type Bounds, type CameraPanSession, type CameraSlideAnimation, type CameraSlideOptions, CanvasRenderer, CircleDrawingState, CircleIdleState, type ColorStyle, DEFAULT_COLORS, DEFAULT_ZOOM_RANGE, DRAG_DISTANCE_SQUARED, type DashStyle, type DefaultToolId, DocumentStore, type DocumentStoreSnapshot, type DrawSegment, type DrawShape, ERASER_MARGIN, Editor, type EditorOptions, EraserErasingState, EraserIdleState, EraserPointingState, type FillStyle, HandDraggingState, HandIdleState, type ICanvasRenderer, type IEditor, InputManager, MAX_POINTS_PER_SHAPE, type PageState, PenDrawingState, PenIdleState, type PointerInput, type ResizeHandle, STROKE_WIDTHS, type SegmentType, SelectIdleState, type SelectionBounds, type Shape, type ShapeId, type SizeStyle, SquareDrawingState, SquareIdleState, StateNode, type StateNodeConstructor, type ToolDefinition, type ToolId, type ToolKeyInfo, ToolManager, type ToolPointerDownInfo, type ToolPointerMoveInfo, type ToolStateContext, type ToolStateTransitionInfo, type TransformSnapshot, type TsdrawDocumentSnapshot, type TsdrawEditorSnapshot, type TsdrawHistorySnapshot, type TsdrawPageRecord, type TsdrawPersistedRecord, type TsdrawRenderTheme, type TsdrawSessionStateSnapshot, type TsdrawShapeRecord, type Vec3, type Viewport, type ZoomRange, applyMove, applyResize, applyRotation, beginCameraPan, boundsContainPoint, boundsIntersect, boundsOf, buildStartPositions, buildTransformSnapshots, clampZoom, closestOnSegment, createViewport, decodeFirstPoint, decodeLastPoint, decodePathToPoints, decodePoints, distance, documentSnapshotToRecords, encodePoints, getSelectionBoundsPage, getShapeBounds, getShapesInBounds, getTopShapeAtPoint, isSelectTool, minDistanceToPolyline, moveCameraPan, normalizeSelectionBounds, padBounds, pageToScreen, panViewport, pointHitsShape, recordsToDocumentSnapshot, resolveThemeColor, rotatePoint, screenToPage, segmentHitsShape, segmentTouchesPolyline, setViewport, shapePagePoints, sqDistance, startCameraSlide, zoomViewport };
package/dist/index.d.ts CHANGED
@@ -169,7 +169,13 @@ declare function setViewport(viewport: Viewport, updater: {
169
169
  zoom?: number;
170
170
  }): Viewport;
171
171
  declare function panViewport(viewport: Viewport, dx: number, dy: number): Viewport;
172
- declare function zoomViewport(viewport: Viewport, factor: number, centerX?: number, centerY?: number): Viewport;
172
+ interface ZoomRange {
173
+ min: number;
174
+ max: number;
175
+ }
176
+ declare const DEFAULT_ZOOM_RANGE: ZoomRange;
177
+ declare function clampZoom(zoom: number, range?: ZoomRange): number;
178
+ declare function zoomViewport(viewport: Viewport, factor: number, centerX?: number, centerY?: number, zoomRange?: ZoomRange): Viewport;
173
179
 
174
180
  interface IEditor {
175
181
  getZoomLevel(): number;
@@ -264,7 +270,10 @@ declare function moveCameraPan(session: CameraPanSession, currentScreenX: number
264
270
  interface CameraSlideAnimation {
265
271
  stop: () => void;
266
272
  }
267
- declare function startCameraSlide(session: CameraPanSession, applyPan: (dx: number, dy: number) => void, onFrame: () => void): CameraSlideAnimation | null;
273
+ interface CameraSlideOptions {
274
+ friction?: number;
275
+ }
276
+ declare function startCameraSlide(session: CameraPanSession, applyPan: (dx: number, dy: number) => void, onFrame: () => void, slideOptions?: CameraSlideOptions): CameraSlideAnimation | null;
268
277
 
269
278
  type TsdrawRenderTheme = 'light' | 'dark';
270
279
  declare function resolveThemeColor(colorStyle: string, theme: TsdrawRenderTheme): string;
@@ -313,6 +322,7 @@ interface EditorOptions {
313
322
  dragDistanceSquared?: number;
314
323
  toolDefinitions?: ToolDefinition[];
315
324
  initialToolId?: ToolId;
325
+ zoomRange?: ZoomRange;
316
326
  }
317
327
  type EditorListener = () => void;
318
328
  declare class Editor {
@@ -323,6 +333,7 @@ declare class Editor {
323
333
  viewport: Viewport;
324
334
  readonly options: {
325
335
  dragDistanceSquared: number;
336
+ zoomRange?: ZoomRange;
326
337
  };
327
338
  private drawStyle;
328
339
  private readonly toolStateContext;
@@ -656,4 +667,4 @@ declare function decodePathToPoints(segments: {
656
667
  y: number;
657
668
  }[];
658
669
 
659
- export { type Bounds, type CameraPanSession, type CameraSlideAnimation, CanvasRenderer, CircleDrawingState, CircleIdleState, type ColorStyle, DEFAULT_COLORS, DRAG_DISTANCE_SQUARED, type DashStyle, type DefaultToolId, DocumentStore, type DocumentStoreSnapshot, type DrawSegment, type DrawShape, ERASER_MARGIN, Editor, type EditorOptions, EraserErasingState, EraserIdleState, EraserPointingState, type FillStyle, HandDraggingState, HandIdleState, type ICanvasRenderer, type IEditor, InputManager, MAX_POINTS_PER_SHAPE, type PageState, PenDrawingState, PenIdleState, type PointerInput, type ResizeHandle, STROKE_WIDTHS, type SegmentType, SelectIdleState, type SelectionBounds, type Shape, type ShapeId, type SizeStyle, SquareDrawingState, SquareIdleState, StateNode, type StateNodeConstructor, type ToolDefinition, type ToolId, type ToolKeyInfo, ToolManager, type ToolPointerDownInfo, type ToolPointerMoveInfo, type ToolStateContext, type ToolStateTransitionInfo, type TransformSnapshot, type TsdrawDocumentSnapshot, type TsdrawEditorSnapshot, type TsdrawHistorySnapshot, type TsdrawPageRecord, type TsdrawPersistedRecord, type TsdrawRenderTheme, type TsdrawSessionStateSnapshot, type TsdrawShapeRecord, type Vec3, type Viewport, applyMove, applyResize, applyRotation, beginCameraPan, boundsContainPoint, boundsIntersect, boundsOf, buildStartPositions, buildTransformSnapshots, closestOnSegment, createViewport, decodeFirstPoint, decodeLastPoint, decodePathToPoints, decodePoints, distance, documentSnapshotToRecords, encodePoints, getSelectionBoundsPage, getShapeBounds, getShapesInBounds, getTopShapeAtPoint, isSelectTool, minDistanceToPolyline, moveCameraPan, normalizeSelectionBounds, padBounds, pageToScreen, panViewport, pointHitsShape, recordsToDocumentSnapshot, resolveThemeColor, rotatePoint, screenToPage, segmentHitsShape, segmentTouchesPolyline, setViewport, shapePagePoints, sqDistance, startCameraSlide, zoomViewport };
670
+ export { type Bounds, type CameraPanSession, type CameraSlideAnimation, type CameraSlideOptions, CanvasRenderer, CircleDrawingState, CircleIdleState, type ColorStyle, DEFAULT_COLORS, DEFAULT_ZOOM_RANGE, DRAG_DISTANCE_SQUARED, type DashStyle, type DefaultToolId, DocumentStore, type DocumentStoreSnapshot, type DrawSegment, type DrawShape, ERASER_MARGIN, Editor, type EditorOptions, EraserErasingState, EraserIdleState, EraserPointingState, type FillStyle, HandDraggingState, HandIdleState, type ICanvasRenderer, type IEditor, InputManager, MAX_POINTS_PER_SHAPE, type PageState, PenDrawingState, PenIdleState, type PointerInput, type ResizeHandle, STROKE_WIDTHS, type SegmentType, SelectIdleState, type SelectionBounds, type Shape, type ShapeId, type SizeStyle, SquareDrawingState, SquareIdleState, StateNode, type StateNodeConstructor, type ToolDefinition, type ToolId, type ToolKeyInfo, ToolManager, type ToolPointerDownInfo, type ToolPointerMoveInfo, type ToolStateContext, type ToolStateTransitionInfo, type TransformSnapshot, type TsdrawDocumentSnapshot, type TsdrawEditorSnapshot, type TsdrawHistorySnapshot, type TsdrawPageRecord, type TsdrawPersistedRecord, type TsdrawRenderTheme, type TsdrawSessionStateSnapshot, type TsdrawShapeRecord, type Vec3, type Viewport, type ZoomRange, applyMove, applyResize, applyRotation, beginCameraPan, boundsContainPoint, boundsIntersect, boundsOf, buildStartPositions, buildTransformSnapshots, clampZoom, closestOnSegment, createViewport, decodeFirstPoint, decodeLastPoint, decodePathToPoints, decodePoints, distance, documentSnapshotToRecords, encodePoints, getSelectionBoundsPage, getShapeBounds, getShapesInBounds, getTopShapeAtPoint, isSelectTool, minDistanceToPolyline, moveCameraPan, normalizeSelectionBounds, padBounds, pageToScreen, panViewport, pointHitsShape, recordsToDocumentSnapshot, resolveThemeColor, rotatePoint, screenToPage, segmentHitsShape, segmentTouchesPolyline, setViewport, shapePagePoints, sqDistance, startCameraSlide, zoomViewport };
package/dist/index.js CHANGED
@@ -247,8 +247,13 @@ function setViewport(viewport, updater) {
247
247
  function panViewport(viewport, dx, dy) {
248
248
  return { ...viewport, x: viewport.x + dx, y: viewport.y + dy };
249
249
  }
250
- function zoomViewport(viewport, factor, centerX, centerY) {
251
- const zoom = Math.max(0.1, Math.min(4, viewport.zoom * factor));
250
+ var DEFAULT_ZOOM_RANGE = { min: 0.1, max: 4 };
251
+ function clampZoom(zoom, range) {
252
+ const { min, max } = range ?? DEFAULT_ZOOM_RANGE;
253
+ return Math.max(min, Math.min(max, zoom));
254
+ }
255
+ function zoomViewport(viewport, factor, centerX, centerY, zoomRange) {
256
+ const zoom = clampZoom(viewport.zoom * factor, zoomRange);
252
257
  if (centerX == null || centerY == null) {
253
258
  return { ...viewport, zoom };
254
259
  }
@@ -303,7 +308,8 @@ var SLIDE_FRICTION = 0.92;
303
308
  var SLIDE_MIN_SPEED = 0.01;
304
309
  var SLIDE_MAX_SPEED = 2;
305
310
  var SLIDE_MIN_VELOCITY_TO_START = 0.1;
306
- function startCameraSlide(session, applyPan, onFrame) {
311
+ function startCameraSlide(session, applyPan, onFrame, slideOptions) {
312
+ const friction = slideOptions?.friction ?? SLIDE_FRICTION;
307
313
  const timeSinceLastMove = performance.now() - session.lastMoveTime;
308
314
  const FRAME_DURATION = 16;
309
315
  const decayFactor = Math.pow(1 - VELOCITY_LERP_FACTOR, timeSinceLastMove / FRAME_DURATION);
@@ -323,7 +329,7 @@ function startCameraSlide(session, applyPan, onFrame) {
323
329
  lastTime = now;
324
330
  applyPan(dirX * currentSpeed * elapsed, dirY * currentSpeed * elapsed);
325
331
  onFrame();
326
- currentSpeed *= SLIDE_FRICTION;
332
+ currentSpeed *= friction;
327
333
  if (currentSpeed < SLIDE_MIN_SPEED) {
328
334
  rafId = 0;
329
335
  return;
@@ -1709,7 +1715,7 @@ var Editor = class {
1709
1715
  historyBatchChanged = false;
1710
1716
  // Creates a new editor instance with the given options (with defaults if not provided)
1711
1717
  constructor(opts = {}) {
1712
- this.options = { dragDistanceSquared: opts.dragDistanceSquared ?? DRAG_DISTANCE_SQUARED };
1718
+ this.options = { dragDistanceSquared: opts.dragDistanceSquared ?? DRAG_DISTANCE_SQUARED, zoomRange: opts.zoomRange };
1713
1719
  this.lastDocumentSnapshot = this.getDocumentSnapshot();
1714
1720
  this.store.listen(() => {
1715
1721
  this.captureDocumentHistory();
@@ -1823,7 +1829,7 @@ var Editor = class {
1823
1829
  this.viewport = {
1824
1830
  x: partial.x ?? this.viewport.x,
1825
1831
  y: partial.y ?? this.viewport.y,
1826
- zoom: Math.max(0.1, Math.min(4, rawZoom))
1832
+ zoom: clampZoom(rawZoom, this.options.zoomRange)
1827
1833
  };
1828
1834
  this.emitChange();
1829
1835
  }
@@ -1834,7 +1840,7 @@ var Editor = class {
1834
1840
  });
1835
1841
  }
1836
1842
  zoomAt(factor, screenX, screenY) {
1837
- this.viewport = zoomViewport(this.viewport, factor, screenX, screenY);
1843
+ this.viewport = zoomViewport(this.viewport, factor, screenX, screenY, this.options.zoomRange);
1838
1844
  this.emitChange();
1839
1845
  }
1840
1846
  deleteShapes(ids) {
@@ -2254,6 +2260,6 @@ function applyResize(editor, handle, startBounds, startShapes, pointer, lockAspe
2254
2260
  }
2255
2261
  }
2256
2262
 
2257
- export { CanvasRenderer, CircleDrawingState, CircleIdleState, DEFAULT_COLORS, DRAG_DISTANCE_SQUARED, DocumentStore, ERASER_MARGIN, Editor, EraserErasingState, EraserIdleState, EraserPointingState, HandDraggingState, HandIdleState, InputManager, MAX_POINTS_PER_SHAPE, PenDrawingState, PenIdleState, STROKE_WIDTHS, SelectIdleState, SquareDrawingState, SquareIdleState, StateNode, ToolManager, applyMove, applyResize, applyRotation, beginCameraPan, boundsContainPoint, boundsIntersect, boundsOf, buildStartPositions, buildTransformSnapshots, closestOnSegment, createViewport, decodeFirstPoint, decodeLastPoint, decodePathToPoints, decodePoints, distance, documentSnapshotToRecords, encodePoints, getSelectionBoundsPage, getShapeBounds2 as getShapeBounds, getShapesInBounds, getTopShapeAtPoint, isSelectTool, minDistanceToPolyline, moveCameraPan, normalizeSelectionBounds, padBounds, pageToScreen, panViewport, pointHitsShape, recordsToDocumentSnapshot, resolveThemeColor, rotatePoint, screenToPage, segmentHitsShape, segmentTouchesPolyline, setViewport, shapePagePoints, sqDistance, startCameraSlide, zoomViewport };
2263
+ export { CanvasRenderer, CircleDrawingState, CircleIdleState, DEFAULT_COLORS, DEFAULT_ZOOM_RANGE, DRAG_DISTANCE_SQUARED, DocumentStore, ERASER_MARGIN, Editor, EraserErasingState, EraserIdleState, EraserPointingState, HandDraggingState, HandIdleState, InputManager, MAX_POINTS_PER_SHAPE, PenDrawingState, PenIdleState, STROKE_WIDTHS, SelectIdleState, SquareDrawingState, SquareIdleState, StateNode, ToolManager, applyMove, applyResize, applyRotation, beginCameraPan, boundsContainPoint, boundsIntersect, boundsOf, buildStartPositions, buildTransformSnapshots, clampZoom, closestOnSegment, createViewport, decodeFirstPoint, decodeLastPoint, decodePathToPoints, decodePoints, distance, documentSnapshotToRecords, encodePoints, getSelectionBoundsPage, getShapeBounds2 as getShapeBounds, getShapesInBounds, getTopShapeAtPoint, isSelectTool, minDistanceToPolyline, moveCameraPan, normalizeSelectionBounds, padBounds, pageToScreen, panViewport, pointHitsShape, recordsToDocumentSnapshot, resolveThemeColor, rotatePoint, screenToPage, segmentHitsShape, segmentTouchesPolyline, setViewport, shapePagePoints, sqDistance, startCameraSlide, zoomViewport };
2258
2264
  //# sourceMappingURL=index.js.map
2259
2265
  //# sourceMappingURL=index.js.map