@inweb/markup 25.9.0 → 25.9.2

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.
@@ -1,5 +1,28 @@
1
1
  import Konva from "konva";
2
2
 
3
+ class WorldTransform {
4
+ screenToWorld(position) {
5
+ return {
6
+ x: position.x,
7
+ y: position.y,
8
+ z: 0
9
+ };
10
+ }
11
+ worldToScreen(position) {
12
+ return {
13
+ x: position.x,
14
+ y: position.y
15
+ };
16
+ }
17
+ getScale() {
18
+ return {
19
+ x: 1,
20
+ y: 1,
21
+ z: 1
22
+ };
23
+ }
24
+ }
25
+
3
26
  class MarkupColor {
4
27
  constructor(r, g, b) {
5
28
  this.setColor(r, g, b);
@@ -29,29 +52,6 @@ class MarkupColor {
29
52
  }
30
53
  }
31
54
 
32
- class WorldTransform {
33
- screenToWorld(position) {
34
- return {
35
- x: position.x,
36
- y: position.y,
37
- z: 0
38
- };
39
- }
40
- worldToScreen(position) {
41
- return {
42
- x: position.x,
43
- y: position.y
44
- };
45
- }
46
- getScale() {
47
- return {
48
- x: 1,
49
- y: 1,
50
- z: 1
51
- };
52
- }
53
- }
54
-
55
55
  const LineTypeSpecs = new Map([ [ "solid", [] ], [ "dot", [ 30, 30, .001, 30 ] ], [ "dash", [ 30, 30 ] ] ]);
56
56
 
57
57
  class KonvaLine {
@@ -1004,16 +1004,45 @@ class KonvaMarkup {
1004
1004
  this._konvaStage.height(height);
1005
1005
  };
1006
1006
  this.pan = event => {
1007
- const dX = event.dX / window.devicePixelRatio;
1008
- const dY = event.dY / window.devicePixelRatio;
1009
- this.getObjects().forEach((obj => obj.ref().move({
1010
- x: dX,
1011
- y: dY
1012
- })));
1007
+ const pointer = this._konvaStage.getPointerPosition();
1008
+ const pointTo = {
1009
+ x: (pointer.x - this._konvaStage.x()) / this._konvaStage.scaleX(),
1010
+ y: (pointer.y - this._konvaStage.y()) / this._konvaStage.scaleX()
1011
+ };
1012
+ const newPos = {
1013
+ x: pointer.x - pointTo.x * this._konvaStage.scale().x + event.dX,
1014
+ y: pointer.y - pointTo.y * this._konvaStage.scale().x + event.dY
1015
+ };
1016
+ this._konvaStage.position(newPos);
1017
+ };
1018
+ this.zoomAt = event => {
1019
+ const oldScale = this._konvaStage.scaleX();
1020
+ const pointer = this._konvaStage.getPointerPosition();
1021
+ const mousePointTo = {
1022
+ x: (pointer.x - this._konvaStage.x()) / oldScale,
1023
+ y: (pointer.y - this._konvaStage.y()) / oldScale
1024
+ };
1025
+ let direction = event.data > 0 ? 1 : -1;
1026
+ const newScale = direction > 0 ? oldScale * event.data : oldScale / event.data;
1027
+ this._konvaStage.scale({
1028
+ x: newScale,
1029
+ y: newScale
1030
+ });
1031
+ const newPos = {
1032
+ x: pointer.x - mousePointTo.x * newScale,
1033
+ y: pointer.y - mousePointTo.y * newScale
1034
+ };
1035
+ this._konvaStage.position(newPos);
1013
1036
  };
1014
1037
  this.redirectToViewer = event => {
1015
1038
  if (this._viewer) this._viewer.emit(event);
1016
1039
  };
1040
+ this.getRelativePointPosition = (point, node) => {
1041
+ const transform = node.getAbsoluteTransform().copy();
1042
+ transform.invert();
1043
+ return transform.point(point);
1044
+ };
1045
+ this.getRelativePointerPosition = node => this.getRelativePointPosition(node.getStage().getPointerPosition(), node);
1017
1046
  }
1018
1047
  initialize(container, containerEvents, viewer, worldTransformer) {
1019
1048
  if (!Konva) throw new Error('Markup error: Konva is not initialized. Forgot to add <script src="https://unpkg.com/konva@9/konva.min.js"><\/script> to your page?');
@@ -1037,11 +1066,13 @@ class KonvaMarkup {
1037
1066
  this._containerEvents.forEach((x => this._markupContainer.addEventListener(x, this.redirectToViewer)));
1038
1067
  this._viewer.addEventListener("changeactivedragger", this.changeActiveDragger);
1039
1068
  this._viewer.addEventListener("pan", this.pan);
1069
+ this._viewer.addEventListener("zoomat", this.zoomAt);
1040
1070
  }
1041
1071
  }
1042
1072
  dispose() {
1043
1073
  var _a, _b;
1044
1074
  if (this._viewer) {
1075
+ this._viewer.removeEventListener("zoomat", this.zoomAt);
1045
1076
  this._viewer.removeEventListener("pan", this.pan);
1046
1077
  this._viewer.removeEventListener("changeactivedragger", this.changeActiveDragger);
1047
1078
  this._containerEvents.forEach((x => this._markupContainer.removeEventListener(x, this.redirectToViewer)));
@@ -1088,6 +1119,14 @@ class KonvaMarkup {
1088
1119
  this.clearSelected();
1089
1120
  this.removeTextInput();
1090
1121
  this.removeImageInput();
1122
+ this._konvaStage.scale({
1123
+ x: 1,
1124
+ y: 1
1125
+ });
1126
+ this._konvaStage.position({
1127
+ x: 0,
1128
+ y: 0
1129
+ });
1091
1130
  const markupColor = ((_a = viewpoint.custom_fields) === null || _a === void 0 ? void 0 : _a.markup_color) || {
1092
1131
  r: 255,
1093
1132
  g: 0,
@@ -1234,7 +1273,7 @@ class KonvaMarkup {
1234
1273
  transformer.nodes([]);
1235
1274
  return;
1236
1275
  }
1237
- const pos = stage.getPointerPosition();
1276
+ const pos = this.getRelativePointerPosition(stage);
1238
1277
  mouseDownPos = pos;
1239
1278
  isPaint = [ "Arrow", "Cloud", "Ellipse", "Line", "Rectangle" ].some((m => m === this._markupMode));
1240
1279
  if (this._markupMode === "Line") {
@@ -1244,7 +1283,7 @@ class KonvaMarkup {
1244
1283
  stage.on("mouseup touchend", (e => {
1245
1284
  if (!this._markupIsActive) return;
1246
1285
  if (isPaint) {
1247
- const pos = stage.getPointerPosition();
1286
+ const pos = this.getRelativePointerPosition(stage);
1248
1287
  const defParams = mouseDownPos && pos.x === mouseDownPos.x && pos.y === mouseDownPos.y;
1249
1288
  const startX = defParams ? mouseDownPos.x : Math.min(mouseDownPos.x, pos.x);
1250
1289
  const startY = defParams ? mouseDownPos.y : Math.min(mouseDownPos.y, pos.y);
@@ -1288,7 +1327,7 @@ class KonvaMarkup {
1288
1327
  if (!isPaint) {
1289
1328
  return;
1290
1329
  }
1291
- const pos = stage.getPointerPosition();
1330
+ const pos = this.getRelativePointerPosition(stage);
1292
1331
  const defParams = mouseDownPos && pos.x === mouseDownPos.x && pos.y === mouseDownPos.y;
1293
1332
  const startX = defParams ? mouseDownPos.x : Math.min(mouseDownPos.x, pos.x);
1294
1333
  const startY = defParams ? mouseDownPos.y : Math.min(mouseDownPos.y, pos.y);
@@ -1344,7 +1383,7 @@ class KonvaMarkup {
1344
1383
  if (e.target === stage) {
1345
1384
  if (this._markupMode === "Text") {
1346
1385
  if (this._textInputRef && this._textInputRef.value) this.addText(this._textInputRef.value, this._textInputPos, this._textInputAngle); else if (transformer.nodes().length === 0) {
1347
- const pos = stage.getPointerPosition();
1386
+ const pos = this.getRelativePointerPosition(stage);
1348
1387
  this.createTextInput(pos, e.evt.pageX, e.evt.pageY, 0, null);
1349
1388
  }
1350
1389
  } else if (this._markupMode === "Image") {
@@ -1352,7 +1391,7 @@ class KonvaMarkup {
1352
1391
  x: this._imageInputPos.x,
1353
1392
  y: this._imageInputPos.y
1354
1393
  }, this._imageInputRef.value, 0, 0, this._imageInputRef.value); else if (transformer.nodes().length === 0) {
1355
- const pos = stage.getPointerPosition();
1394
+ const pos = this.getRelativePointerPosition(stage);
1356
1395
  this.createImageInput(pos);
1357
1396
  }
1358
1397
  }
@@ -1457,11 +1496,13 @@ class KonvaMarkup {
1457
1496
  this.konvaLayerFind("Text").forEach((ref => {
1458
1497
  const textSize = .02;
1459
1498
  const textScale = this._worldTransformer.getScale();
1460
- const position = {
1461
- x: ref.x(),
1462
- y: ref.y()
1463
- };
1464
- const worldPoint = this._worldTransformer.screenToWorld(position);
1499
+ const position = ref.position();
1500
+ const stageAbsoluteTransform = this._konvaStage.getAbsoluteTransform();
1501
+ const atPoint = stageAbsoluteTransform.point({
1502
+ x: position.x,
1503
+ y: position.y
1504
+ });
1505
+ const worldPoint = this._worldTransformer.screenToWorld(atPoint);
1465
1506
  const shape = new KonvaText(null, ref);
1466
1507
  const text = {
1467
1508
  id: shape.id(),
@@ -1470,7 +1511,7 @@ class KonvaMarkup {
1470
1511
  text_size: textSize * textScale.y,
1471
1512
  angle: shape.getRotation(),
1472
1513
  color: shape.getColor(),
1473
- font_size: shape.getFontSize()
1514
+ font_size: shape.getFontSize() * stageAbsoluteTransform.getMatrix()[0]
1474
1515
  };
1475
1516
  texts.push(text);
1476
1517
  }));
@@ -1480,13 +1521,19 @@ class KonvaMarkup {
1480
1521
  const rectangles = [];
1481
1522
  this.konvaLayerFind("Rectangle").forEach((ref => {
1482
1523
  const position = ref.position();
1483
- const worldPoint = this._worldTransformer.screenToWorld(position);
1524
+ const stageAbsoluteTransform = this._konvaStage.getAbsoluteTransform();
1525
+ const atPoint = stageAbsoluteTransform.point({
1526
+ x: position.x,
1527
+ y: position.y
1528
+ });
1529
+ const worldPoint = this._worldTransformer.screenToWorld(atPoint);
1530
+ const scale = stageAbsoluteTransform.getMatrix()[0];
1484
1531
  const shape = new KonvaRectangle(null, ref);
1485
1532
  const rectangle = {
1486
1533
  id: shape.id(),
1487
1534
  position: worldPoint,
1488
- width: shape.getWidth(),
1489
- height: shape.getHeigth(),
1535
+ width: shape.getWidth() * scale,
1536
+ height: shape.getHeigth() * scale,
1490
1537
  line_width: shape.getLineWidth(),
1491
1538
  color: shape.getColor()
1492
1539
  };
@@ -1498,14 +1545,20 @@ class KonvaMarkup {
1498
1545
  const ellipses = [];
1499
1546
  this.konvaLayerFind("Ellipse").forEach((ref => {
1500
1547
  const position = ref.position();
1501
- const worldPoint = this._worldTransformer.screenToWorld(position);
1548
+ const stageAbsoluteTransform = this._konvaStage.getAbsoluteTransform();
1549
+ const atPoint = stageAbsoluteTransform.point({
1550
+ x: position.x,
1551
+ y: position.y
1552
+ });
1553
+ const worldPoint = this._worldTransformer.screenToWorld(atPoint);
1554
+ const scale = stageAbsoluteTransform.getMatrix()[0];
1502
1555
  const shape = new KonvaEllipse(null, ref);
1503
1556
  const ellipse = {
1504
1557
  id: shape.id(),
1505
1558
  position: worldPoint,
1506
1559
  radius: {
1507
- x: ref.getRadiusX(),
1508
- y: ref.getRadiusY()
1560
+ x: ref.getRadiusX() * scale,
1561
+ y: ref.getRadiusY() * scale
1509
1562
  },
1510
1563
  line_width: shape.getLineWidth(),
1511
1564
  color: shape.getColor()
@@ -1543,14 +1596,20 @@ class KonvaMarkup {
1543
1596
  const images = [];
1544
1597
  this.konvaLayerFind("Image").forEach((ref => {
1545
1598
  const position = ref.position();
1546
- const worldPoint = this._worldTransformer.screenToWorld(position);
1599
+ const stageAbsoluteTransform = this._konvaStage.getAbsoluteTransform();
1600
+ const atPoint = stageAbsoluteTransform.point({
1601
+ x: position.x,
1602
+ y: position.y
1603
+ });
1604
+ const worldPoint = this._worldTransformer.screenToWorld(atPoint);
1605
+ const scale = stageAbsoluteTransform.getMatrix()[0];
1547
1606
  const shape = new KonvaImage(null, ref);
1548
1607
  const image = {
1549
1608
  id: shape.id(),
1550
1609
  position: worldPoint,
1551
1610
  src: shape.getSrc(),
1552
- width: shape.getWidth(),
1553
- height: shape.getHeight()
1611
+ width: shape.getWidth() * scale,
1612
+ height: shape.getHeight() * scale
1554
1613
  };
1555
1614
  images.push(image);
1556
1615
  }));
@@ -1560,13 +1619,19 @@ class KonvaMarkup {
1560
1619
  const clouds = [];
1561
1620
  this.konvaLayerFind("Cloud").forEach((ref => {
1562
1621
  const position = ref.position();
1563
- const worldPoint = this._worldTransformer.screenToWorld(position);
1622
+ const stageAbsoluteTransform = this._konvaStage.getAbsoluteTransform();
1623
+ const atPoint = stageAbsoluteTransform.point({
1624
+ x: position.x,
1625
+ y: position.y
1626
+ });
1627
+ const worldPoint = this._worldTransformer.screenToWorld(atPoint);
1628
+ const scale = stageAbsoluteTransform.getMatrix()[0];
1564
1629
  const shape = new KonvaCloud(null, ref);
1565
1630
  const cloud = {
1566
1631
  id: shape.id(),
1567
1632
  position: worldPoint,
1568
- width: shape.getWidth(),
1569
- height: shape.getHeigth(),
1633
+ width: shape.getWidth() * scale,
1634
+ height: shape.getHeigth() * scale,
1570
1635
  line_width: shape.getLineWidth(),
1571
1636
  color: shape.getColor()
1572
1637
  };