Browse Source

Simplify

customisations
alemart 9 months ago
parent
commit
0580637bfc
1 changed files with 10 additions and 11 deletions
  1. 10
    11
      src/core/viewport.ts

+ 10
- 11
src/core/viewport.ts View File

997
     convertToPixels(position: Vector2, space: "normalized" | "adjusted" = 'normalized'): Vector2
997
     convertToPixels(position: Vector2, space: "normalized" | "adjusted" = 'normalized'): Vector2
998
     {
998
     {
999
         const canvas = this.canvas;
999
         const canvas = this.canvas;
1000
-        let mx = 1, my = 1;
1000
+        let px = position.x, py = position.y;
1001
 
1001
 
1002
         if(space == 'adjusted') {
1002
         if(space == 'adjusted') {
1003
             // convert from adjusted to normalized space
1003
             // convert from adjusted to normalized space
1004
             const a = canvas.width / canvas.height;
1004
             const a = canvas.width / canvas.height;
1005
 
1005
 
1006
             if(a >= 1)
1006
             if(a >= 1)
1007
-                my *= a;
1007
+                py *= a;
1008
             else
1008
             else
1009
-                mx /= a;
1009
+                px /= a;
1010
         }
1010
         }
1011
         else if(space != 'normalized')
1011
         else if(space != 'normalized')
1012
             throw new IllegalArgumentError(`Invalid space: "${space}"`);
1012
             throw new IllegalArgumentError(`Invalid space: "${space}"`);
1013
 
1013
 
1014
         // convert from normalized to canvas space
1014
         // convert from normalized to canvas space
1015
-        const x = 0.5 * (1 + position.x * mx) * canvas.width;
1016
-        const y = 0.5 * (1 - position.y * my) * canvas.height;
1015
+        const x = 0.5 * (1 + px) * canvas.width;
1016
+        const y = 0.5 * (1 - py) * canvas.height;
1017
 
1017
 
1018
         // done!
1018
         // done!
1019
         return new Vector2(x, y);
1019
         return new Vector2(x, y);
1029
     convertFromPixels(position: Vector2, space: "normalized" | "adjusted" = 'normalized'): Vector2
1029
     convertFromPixels(position: Vector2, space: "normalized" | "adjusted" = 'normalized'): Vector2
1030
     {
1030
     {
1031
         const canvas = this.canvas;
1031
         const canvas = this.canvas;
1032
-        let mx = 1, my = 1;
1033
 
1032
 
1034
         // convert from canvas to normalized space
1033
         // convert from canvas to normalized space
1035
-        const x = 2 * position.x / canvas.width - 1;
1036
-        const y = -2 * position.y / canvas.height + 1;
1034
+        let x = 2 * position.x / canvas.width - 1;
1035
+        let y = -2 * position.y / canvas.height + 1;
1037
 
1036
 
1038
         if(space == 'adjusted') {
1037
         if(space == 'adjusted') {
1039
             // convert from normalized to adjusted space
1038
             // convert from normalized to adjusted space
1040
             const a = canvas.width / canvas.height;
1039
             const a = canvas.width / canvas.height;
1041
 
1040
 
1042
             if(a >= 1)
1041
             if(a >= 1)
1043
-                my /= a;
1042
+                y /= a;
1044
             else
1043
             else
1045
-                mx *= a;
1044
+                x *= a;
1046
         }
1045
         }
1047
         else if(space != 'normalized')
1046
         else if(space != 'normalized')
1048
             throw new IllegalArgumentError(`Invalid space: "${space}"`);
1047
             throw new IllegalArgumentError(`Invalid space: "${space}"`);
1049
 
1048
 
1050
         // done!
1049
         // done!
1051
-        return new Vector2(x * mx, y * my);
1050
+        return new Vector2(x, y);
1052
     }
1051
     }
1053
 
1052
 
1054
     /**
1053
     /**

Loading…
Cancel
Save