@cornerstonejs/core 1.70.15 → 1.71.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cornerstonejs/core",
3
- "version": "1.70.15",
3
+ "version": "1.71.1",
4
4
  "description": "",
5
5
  "main": "src/index.ts",
6
6
  "types": "dist/types/index.d.ts",
@@ -47,5 +47,5 @@
47
47
  "type": "individual",
48
48
  "url": "https://ohif.org/donate"
49
49
  },
50
- "gitHead": "34f07424f7863e9883ed87e58d287249f8de8b30"
50
+ "gitHead": "434959300faeb4eb32dfc2dc19a3aa5aaf1f2a51"
51
51
  }
@@ -635,7 +635,7 @@ class VideoViewport extends Viewport implements IVideoViewport {
635
635
 
636
636
  // NOTE: the parallel scale should be done first
637
637
  // because it affects the focal point later
638
- if (camera.parallelScale !== undefined) {
638
+ if (parallelScale) {
639
639
  this.videoCamera.parallelScale =
640
640
  this.element.clientHeight / 2 / parallelScale;
641
641
  }
@@ -885,7 +885,7 @@ class VideoViewport extends Viewport implements IVideoViewport {
885
885
  };
886
886
 
887
887
  /**
888
- * Converts and [x,y] video coordinate to a Cornerstone3D VideoViewport.
888
+ * Converts `[x, y, 0]` world video coordinate to canvas CSS coordinates.
889
889
  *
890
890
  * @param worldPos - world coord to convert to canvas
891
891
  * @returns Canvas position
@@ -894,47 +894,53 @@ class VideoViewport extends Viewport implements IVideoViewport {
894
894
  const pan: Point2 = this.videoCamera.panWorld;
895
895
  const worldToCanvasRatio: number = this.getWorldToCanvasRatio();
896
896
 
897
- const subCanvasPos: Point2 = [
897
+ const canvasPos: Point2 = [
898
898
  (worldPos[0] + pan[0]) * worldToCanvasRatio,
899
899
  (worldPos[1] + pan[1]) * worldToCanvasRatio,
900
900
  ];
901
901
 
902
- const canvasPos: Point2 = [subCanvasPos[0], subCanvasPos[1]];
903
-
904
902
  return canvasPos;
905
903
  };
906
904
 
907
905
  public getPan(): Point2 {
908
- const worldPan = this.videoCamera.panWorld;
909
- return [worldPan[0], worldPan[1]];
906
+ const panWorld = this.videoCamera.panWorld;
907
+ return [panWorld[0], panWorld[1]];
910
908
  }
911
909
 
912
910
  public getRotation = () => 0;
913
911
 
912
+ /**
913
+ * Uses the transform to convert canvas coordinates into index coordinates.
914
+ */
914
915
  protected canvasToIndex = (canvasPos: Point2): Point2 => {
915
916
  const transform = this.getTransform();
916
917
  transform.invert();
917
918
 
918
- return transform.transformPoint(canvasPos);
919
+ return transform.transformPoint(
920
+ <Point2>canvasPos.map((it) => it * devicePixelRatio)
921
+ );
919
922
  };
920
923
 
921
924
  protected indexToCanvas = (indexPos: Point2): Point2 => {
922
925
  const transform = this.getTransform();
923
- return transform.transformPoint(indexPos);
926
+ return <Point2>(
927
+ transform.transformPoint(indexPos).map((it) => it / devicePixelRatio)
928
+ );
924
929
  };
925
930
 
931
+ /**
932
+ * Sets initial video camera to center the image area. The values
933
+ * are set in canvas CSS pixel units and NOT in canvas index units.
934
+ */
926
935
  private refreshRenderValues() {
927
936
  // this means that each unit (pixel) in the world (video) would be
928
- // represented by n pixels in the canvas.
929
- let worldToCanvasRatio = this.canvas.width / this.videoWidth;
937
+ // represented by n pixels in the canvas, measured in css pixels
938
+ let worldToCanvasRatio = this.canvas.offsetWidth / this.videoWidth;
930
939
 
931
940
  if (this.videoHeight * worldToCanvasRatio > this.canvas.height) {
932
941
  // If by fitting the width, we exceed the height of the viewport, then we need to decrease the
933
942
  // size of the viewport further by considering its verticality.
934
- const secondWorldToCanvasRatio =
935
- this.canvas.height / (this.videoHeight * worldToCanvasRatio);
936
-
937
- worldToCanvasRatio *= secondWorldToCanvasRatio;
943
+ worldToCanvasRatio = this.canvas.offsetHeight / this.videoHeight;
938
944
  }
939
945
 
940
946
  // Set the width as big as possible, this is the portion of the canvas
@@ -943,8 +949,8 @@ class VideoViewport extends Viewport implements IVideoViewport {
943
949
  const drawHeight = Math.floor(this.videoHeight * worldToCanvasRatio);
944
950
 
945
951
  // calculate x and y offset in order to center the image
946
- const xOffsetCanvas = this.canvas.width / 2 - drawWidth / 2;
947
- const yOffsetCanvas = this.canvas.height / 2 - drawHeight / 2;
952
+ const xOffsetCanvas = (this.canvas.offsetWidth - drawWidth) / 2;
953
+ const yOffsetCanvas = (this.canvas.offsetHeight - drawHeight) / 2;
948
954
 
949
955
  const xOffsetWorld = xOffsetCanvas / worldToCanvasRatio;
950
956
  const yOffsetWorld = yOffsetCanvas / worldToCanvasRatio;
@@ -965,17 +971,27 @@ class VideoViewport extends Viewport implements IVideoViewport {
965
971
  this.renderFrame();
966
972
  };
967
973
 
974
+ /**
975
+ * Creates a transform from video index coordinates to canvas coordinates.
976
+ */
968
977
  protected getTransform() {
969
978
  const panWorld: Point2 = this.videoCamera.panWorld;
979
+ const devicePixelRatio = window.devicePixelRatio || 1;
970
980
  const worldToCanvasRatio: number = this.getWorldToCanvasRatio();
971
981
  const canvasToWorldRatio: number = this.getCanvasToWorldRatio();
972
- const halfCanvas = [this.canvas.width / 2, this.canvas.height / 2];
982
+ const halfCanvas = [
983
+ this.canvas.offsetWidth / 2,
984
+ this.canvas.offsetHeight / 2,
985
+ ];
973
986
  const halfCanvasWorldCoordinates = [
974
987
  halfCanvas[0] * canvasToWorldRatio,
975
988
  halfCanvas[1] * canvasToWorldRatio,
976
989
  ];
977
990
  const transform = new Transform();
978
991
 
992
+ // Start by converting into canvas index coordinates FROM canvas css pixel coordinates
993
+ transform.scale(devicePixelRatio, devicePixelRatio);
994
+
979
995
  // Translate to the center of the canvas (move origin of the transform
980
996
  // to the center of the canvas)
981
997
  transform.translate(halfCanvas[0], halfCanvas[1]);
@@ -1021,11 +1037,19 @@ class VideoViewport extends Viewport implements IVideoViewport {
1021
1037
  return new CanvasActor(this, image);
1022
1038
  }
1023
1039
 
1040
+ /**
1041
+ * Renders the video frame to the viewport.
1042
+ */
1024
1043
  private renderFrame = () => {
1025
1044
  const transform = this.getTransform();
1026
1045
  const transformationMatrix: number[] = transform.getMatrix();
1027
1046
 
1028
- this.canvasContext.transform(
1047
+ const ctx = this.canvasContext;
1048
+
1049
+ ctx.resetTransform();
1050
+
1051
+ // Need to correct the transform for device pixel ratio scaling.
1052
+ ctx.transform(
1029
1053
  transformationMatrix[0],
1030
1054
  transformationMatrix[1],
1031
1055
  transformationMatrix[2],
@@ -1034,13 +1058,7 @@ class VideoViewport extends Viewport implements IVideoViewport {
1034
1058
  transformationMatrix[5]
1035
1059
  );
1036
1060
 
1037
- this.canvasContext.drawImage(
1038
- this.videoElement,
1039
- 0,
1040
- 0,
1041
- this.videoWidth,
1042
- this.videoHeight
1043
- );
1061
+ ctx.drawImage(this.videoElement, 0, 0, this.videoWidth, this.videoHeight);
1044
1062
 
1045
1063
  for (const actor of this.getActors()) {
1046
1064
  (actor.actor as ICanvasActor).render(this, this.canvasContext);