@tsdraw/core 0.8.1 → 0.8.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -225,34 +225,25 @@ var StateNode = class {
225
225
 
226
226
  // src/canvas/viewport.ts
227
227
  function createViewport() {
228
- return { x: 0, y: 0, zoom: 1, rotation: 0 };
228
+ return { x: 0, y: 0, zoom: 1 };
229
229
  }
230
230
  function screenToPage(viewport, screenX, screenY) {
231
- const tx = screenX - viewport.x;
232
- const ty = screenY - viewport.y;
233
- const cos = Math.cos(viewport.rotation);
234
- const sin = Math.sin(viewport.rotation);
235
231
  return {
236
- x: (tx * cos + ty * sin) / viewport.zoom,
237
- y: (-tx * sin + ty * cos) / viewport.zoom
232
+ x: (screenX - viewport.x) / viewport.zoom,
233
+ y: (screenY - viewport.y) / viewport.zoom
238
234
  };
239
235
  }
240
236
  function pageToScreen(viewport, pageX, pageY) {
241
- const scaledX = pageX * viewport.zoom;
242
- const scaledY = pageY * viewport.zoom;
243
- const cos = Math.cos(viewport.rotation);
244
- const sin = Math.sin(viewport.rotation);
245
237
  return {
246
- x: scaledX * cos - scaledY * sin + viewport.x,
247
- y: scaledX * sin + scaledY * cos + viewport.y
238
+ x: pageX * viewport.zoom + viewport.x,
239
+ y: pageY * viewport.zoom + viewport.y
248
240
  };
249
241
  }
250
242
  function setViewport(viewport, updater) {
251
243
  return {
252
244
  x: updater.x ?? viewport.x,
253
245
  y: updater.y ?? viewport.y,
254
- zoom: updater.zoom ?? viewport.zoom,
255
- rotation: updater.rotation ?? viewport.rotation
246
+ zoom: updater.zoom ?? viewport.zoom
256
247
  };
257
248
  }
258
249
  function panViewport(viewport, dx, dy) {
@@ -264,23 +255,9 @@ function zoomViewport(viewport, factor, centerX, centerY) {
264
255
  return { ...viewport, zoom };
265
256
  }
266
257
  const pageBefore = screenToPage(viewport, centerX, centerY);
267
- const cos = Math.cos(viewport.rotation);
268
- const sin = Math.sin(viewport.rotation);
269
- const x = centerX - (pageBefore.x * zoom * cos - pageBefore.y * zoom * sin);
270
- const y = centerY - (pageBefore.x * zoom * sin + pageBefore.y * zoom * cos);
271
- return { x, y, zoom, rotation: viewport.rotation };
272
- }
273
- function rotateViewport(viewport, delta, centerX, centerY) {
274
- const rotation = viewport.rotation + delta;
275
- if (centerX == null || centerY == null) {
276
- return { ...viewport, rotation };
277
- }
278
- const pageBefore = screenToPage(viewport, centerX, centerY);
279
- const cos = Math.cos(rotation);
280
- const sin = Math.sin(rotation);
281
- const x = centerX - (pageBefore.x * viewport.zoom * cos - pageBefore.y * viewport.zoom * sin);
282
- const y = centerY - (pageBefore.x * viewport.zoom * sin + pageBefore.y * viewport.zoom * cos);
283
- return { x, y, zoom: viewport.zoom, rotation };
258
+ const x = centerX - pageBefore.x * zoom;
259
+ const y = centerY - pageBefore.y * zoom;
260
+ return { x, y, zoom };
284
261
  }
285
262
 
286
263
  // src/utils/colors.ts
@@ -313,7 +290,6 @@ var CanvasRenderer = class {
313
290
  render(ctx, viewport, shapes) {
314
291
  ctx.save();
315
292
  ctx.translate(viewport.x, viewport.y);
316
- ctx.rotate(viewport.rotation);
317
293
  ctx.scale(viewport.zoom, viewport.zoom);
318
294
  for (const shape of shapes) {
319
295
  if (shape.type === "draw") {
@@ -806,7 +782,7 @@ var PenDrawingState = class extends StateNode {
806
782
  const z = this._startInfo?.point?.z ?? 0.5;
807
783
  this._isPenDevice = penActive;
808
784
  this._hasPressure = penActive || z !== 0.5;
809
- const pressure = this._hasPressure ? toFixed(z) : 0.5;
785
+ const pressure = this._hasPressure ? toFixed(z * 1.25) : 0.5;
810
786
  this._phase = inputs.getShiftKey() ? "straight" : "free";
811
787
  this._extending = false;
812
788
  this._lastSample = { ...origin };
@@ -903,13 +879,12 @@ var PenDrawingState = class extends StateNode {
903
879
  const curPt = inputs.getCurrentPagePoint();
904
880
  if (!this._hasPressure) {
905
881
  const liveZ = curPt.z ?? 0.5;
906
- if (liveZ !== 0.5 || inputs.getIsPen()) {
882
+ if (liveZ > 0 && liveZ !== 0.5 || inputs.getIsPen()) {
907
883
  this._hasPressure = true;
908
- this.editor.updateShapes([{ id, type: "draw", props: { isPen: true } }]);
909
884
  }
910
885
  }
911
886
  const local = this.editor.getPointInShapeSpace(shape, curPt);
912
- const pressure = this._hasPressure ? toFixed(curPt.z ?? 0.5) : 0.5;
887
+ const pressure = this._hasPressure ? toFixed((curPt.z ?? 0.5) * 1.25) : 0.5;
913
888
  const pt = { x: toFixed(local.x), y: toFixed(local.y), z: pressure };
914
889
  switch (this._phase) {
915
890
  case "starting_straight": {
@@ -1055,7 +1030,7 @@ var PenDrawingState = class extends StateNode {
1055
1030
  const firstPt = {
1056
1031
  x: 0,
1057
1032
  y: 0,
1058
- z: this._hasPressure ? toFixed(curPage.z ?? 0.5) : 0.5
1033
+ z: this._hasPressure ? toFixed((curPage.z ?? 0.5) * 1.25) : 0.5
1059
1034
  };
1060
1035
  this._activePts = [firstPt];
1061
1036
  this.editor.createShape({
@@ -1087,7 +1062,7 @@ var PenDrawingState = class extends StateNode {
1087
1062
  endStroke() {
1088
1063
  if (!this._target) return;
1089
1064
  this.editor.updateShapes([
1090
- { id: this._target.id, type: "draw", props: { isComplete: true } }
1065
+ { id: this._target.id, type: "draw", props: { isComplete: true, isPen: this._hasPressure } }
1091
1066
  ]);
1092
1067
  this.ctx.transition("pen_idle");
1093
1068
  }
@@ -1166,6 +1141,7 @@ var GeometricDrawingState = class extends StateNode {
1166
1141
  }
1167
1142
  // If user dragged, use the drag extents for the final shape
1168
1143
  // If they just clicked without dragging, use default-sized shape
1144
+ // If they dragged just a bit (most likely a click), remove the shape and go back to idle
1169
1145
  completeShape() {
1170
1146
  const activeShape = this.getActiveShape();
1171
1147
  const config = this.getConfig();
@@ -1175,7 +1151,15 @@ var GeometricDrawingState = class extends StateNode {
1175
1151
  }
1176
1152
  const originPoint = this.editor.input.getOriginPagePoint();
1177
1153
  const cursorPoint = this.editor.input.getCurrentPagePoint();
1178
- const finalizedBounds = this.editor.input.getIsDragging() ? this.editor.input.getShiftKey() ? config.buildConstrainedBounds(originPoint.x, originPoint.y, cursorPoint.x, cursorPoint.y) : config.buildUnconstrainedBounds(originPoint.x, originPoint.y, cursorPoint.x, cursorPoint.y) : config.buildDefaultBounds(originPoint.x, originPoint.y);
1154
+ const dx = cursorPoint.x - originPoint.x;
1155
+ const dy = cursorPoint.y - originPoint.y;
1156
+ const draggedFarEnough = dx * dx + dy * dy > this.editor.options.dragDistanceSquared;
1157
+ if (!draggedFarEnough) {
1158
+ this.removeCurrentShape();
1159
+ this.ctx.transition(config.idleStateId, this.startedAt);
1160
+ return;
1161
+ }
1162
+ const finalizedBounds = this.editor.input.getShiftKey() ? config.buildConstrainedBounds(originPoint.x, originPoint.y, cursorPoint.x, cursorPoint.y) : config.buildUnconstrainedBounds(originPoint.x, originPoint.y, cursorPoint.x, cursorPoint.y);
1179
1163
  this.editor.store.updateShape(activeShape.id, {
1180
1164
  x: finalizedBounds.x,
1181
1165
  y: finalizedBounds.y,
@@ -1205,10 +1189,6 @@ var GeometricDrawingState = class extends StateNode {
1205
1189
 
1206
1190
  // src/tools/geometric/geometricShapeHelpers.ts
1207
1191
  var MIN_SIDE_LENGTH = 1;
1208
- var DEFAULT_RECTANGLE_WIDTH = 180;
1209
- var DEFAULT_RECTANGLE_HEIGHT = 120;
1210
- var DEFAULT_ELLIPSE_WIDTH = 180;
1211
- var DEFAULT_ELLIPSE_HEIGHT = 120;
1212
1192
  function toSizedBounds(anchorX, anchorY, cursorX, cursorY, forceEqualSides) {
1213
1193
  const rawDeltaX = cursorX - anchorX;
1214
1194
  const rawDeltaY = cursorY - anchorY;
@@ -1233,16 +1213,6 @@ function buildSquareBounds(anchorX, anchorY, cursorX, cursorY) {
1233
1213
  function buildRectangleBounds(anchorX, anchorY, cursorX, cursorY) {
1234
1214
  return toSizedBounds(anchorX, anchorY, cursorX, cursorY, false);
1235
1215
  }
1236
- function buildDefaultCenteredRectangleBounds(centerX, centerY) {
1237
- const halfWidth = DEFAULT_RECTANGLE_WIDTH / 2;
1238
- const halfHeight = DEFAULT_RECTANGLE_HEIGHT / 2;
1239
- return {
1240
- x: centerX - halfWidth,
1241
- y: centerY - halfHeight,
1242
- width: DEFAULT_RECTANGLE_WIDTH,
1243
- height: DEFAULT_RECTANGLE_HEIGHT
1244
- };
1245
- }
1246
1216
  function buildRectangleSegments(width, height) {
1247
1217
  const topLeft = { x: 0, y: 0, z: 0.5 };
1248
1218
  const topRight = { x: width, y: 0, z: 0.5 };
@@ -1261,16 +1231,6 @@ function buildCircleBounds(anchorX, anchorY, cursorX, cursorY) {
1261
1231
  function buildEllipseBounds(anchorX, anchorY, cursorX, cursorY) {
1262
1232
  return toSizedBounds(anchorX, anchorY, cursorX, cursorY, false);
1263
1233
  }
1264
- function buildDefaultCenteredEllipseBounds(centerX, centerY) {
1265
- const halfWidth = DEFAULT_ELLIPSE_WIDTH / 2;
1266
- const halfHeight = DEFAULT_ELLIPSE_HEIGHT / 2;
1267
- return {
1268
- x: centerX - halfWidth,
1269
- y: centerY - halfHeight,
1270
- width: DEFAULT_ELLIPSE_WIDTH,
1271
- height: DEFAULT_ELLIPSE_HEIGHT
1272
- };
1273
- }
1274
1234
  function buildEllipseSegments(width, height) {
1275
1235
  const centerX = width / 2;
1276
1236
  const centerY = height / 2;
@@ -1298,7 +1258,6 @@ var SquareDrawingState = class extends GeometricDrawingState {
1298
1258
  idleStateId: "square_idle",
1299
1259
  buildConstrainedBounds: buildSquareBounds,
1300
1260
  buildUnconstrainedBounds: buildRectangleBounds,
1301
- buildDefaultBounds: buildDefaultCenteredRectangleBounds,
1302
1261
  buildSegments: buildRectangleSegments
1303
1262
  };
1304
1263
  }
@@ -1320,7 +1279,6 @@ var CircleDrawingState = class extends GeometricDrawingState {
1320
1279
  idleStateId: "circle_idle",
1321
1280
  buildConstrainedBounds: buildCircleBounds,
1322
1281
  buildUnconstrainedBounds: buildEllipseBounds,
1323
- buildDefaultBounds: buildDefaultCenteredEllipseBounds,
1324
1282
  buildSegments: buildEllipseSegments
1325
1283
  };
1326
1284
  }
@@ -1771,8 +1729,7 @@ var Editor = class {
1771
1729
  this.viewport = {
1772
1730
  x: partial.x ?? this.viewport.x,
1773
1731
  y: partial.y ?? this.viewport.y,
1774
- zoom: Math.max(0.1, Math.min(4, rawZoom)),
1775
- rotation: partial.rotation ?? this.viewport.rotation
1732
+ zoom: Math.max(0.1, Math.min(4, rawZoom))
1776
1733
  };
1777
1734
  this.emitChange();
1778
1735
  }
@@ -1786,10 +1743,6 @@ var Editor = class {
1786
1743
  this.viewport = zoomViewport(this.viewport, factor, screenX, screenY);
1787
1744
  this.emitChange();
1788
1745
  }
1789
- rotateAt(delta, screenX, screenY) {
1790
- this.viewport = rotateViewport(this.viewport, delta, screenX, screenY);
1791
- this.emitChange();
1792
- }
1793
1746
  deleteShapes(ids) {
1794
1747
  if (ids.length === 0) return;
1795
1748
  this.store.deleteShapes(ids);
@@ -1812,8 +1765,7 @@ var Editor = class {
1812
1765
  viewport: {
1813
1766
  x: this.viewport.x,
1814
1767
  y: this.viewport.y,
1815
- zoom: this.viewport.zoom,
1816
- rotation: this.viewport.rotation
1768
+ zoom: this.viewport.zoom
1817
1769
  },
1818
1770
  currentToolId: this.getCurrentToolId(),
1819
1771
  drawStyle: this.getCurrentDrawStyle(),
@@ -1821,10 +1773,7 @@ var Editor = class {
1821
1773
  };
1822
1774
  }
1823
1775
  loadSessionStateSnapshot(snapshot) {
1824
- this.setViewport({
1825
- ...snapshot.viewport,
1826
- rotation: snapshot.viewport.rotation ?? 0
1827
- });
1776
+ this.setViewport(snapshot.viewport);
1828
1777
  this.setCurrentDrawStyle({
1829
1778
  color: snapshot.drawStyle.color,
1830
1779
  dash: snapshot.drawStyle.dash,
@@ -2265,7 +2214,6 @@ exports.pointHitsShape = pointHitsShape;
2265
2214
  exports.recordsToDocumentSnapshot = recordsToDocumentSnapshot;
2266
2215
  exports.resolveThemeColor = resolveThemeColor;
2267
2216
  exports.rotatePoint = rotatePoint;
2268
- exports.rotateViewport = rotateViewport;
2269
2217
  exports.screenToPage = screenToPage;
2270
2218
  exports.segmentHitsShape = segmentHitsShape;
2271
2219
  exports.segmentTouchesPolyline = segmentTouchesPolyline;