Explorar el Código

Fixes rotation, translation

main
Steve Ruiz hace 4 años
padre
commit
6da9e5f019

+ 4
- 3
lib/shape-utils/circle.tsx Ver fichero

@@ -81,12 +81,13 @@ const circle = registerShapeUtils<CircleShape>({
81 81
     )
82 82
   },
83 83
 
84
-  translate(shape, delta) {
85
-    shape.point = vec.add(shape.point, delta)
84
+  rotateTo(shape, rotation) {
85
+    shape.rotation = rotation
86 86
     return this
87 87
   },
88 88
 
89
-  rotate(shape) {
89
+  translateTo(shape, point) {
90
+    shape.point = point
90 91
     return this
91 92
   },
92 93
 

+ 3
- 3
lib/shape-utils/dot.tsx Ver fichero

@@ -69,12 +69,12 @@ const dot = registerShapeUtils<DotShape>({
69 69
     )
70 70
   },
71 71
 
72
-  rotate(shape) {
72
+  rotateTo(shape) {
73 73
     return this
74 74
   },
75 75
 
76
-  translate(shape, delta) {
77
-    shape.point = vec.add(shape.point, delta)
76
+  translateTo(shape, point) {
77
+    shape.point = point
78 78
     return this
79 79
   },
80 80
 

+ 4
- 3
lib/shape-utils/ellipse.tsx Ver fichero

@@ -99,12 +99,13 @@ const ellipse = registerShapeUtils<EllipseShape>({
99 99
     )
100 100
   },
101 101
 
102
-  rotate(shape) {
102
+  rotateTo(shape, rotation) {
103
+    shape.rotation = rotation
103 104
     return this
104 105
   },
105 106
 
106
-  translate(shape, delta) {
107
-    shape.point = vec.add(shape.point, delta)
107
+  translateTo(shape, point) {
108
+    shape.point = point
108 109
     return this
109 110
   },
110 111
 

+ 4
- 4
lib/shape-utils/index.tsx Ver fichero

@@ -40,11 +40,11 @@ export interface ShapeUtility<K extends Readonly<Shape>> {
40 40
   // Create a new shape.
41 41
   create(props: Partial<K>): K
42 42
 
43
-  // Apply a translation to a shape.
44
-  translate(this: ShapeUtility<K>, shape: K, delta: number[]): ShapeUtility<K>
43
+  // Set the shape's point.
44
+  translateTo(this: ShapeUtility<K>, shape: K, delta: number[]): ShapeUtility<K>
45 45
 
46
-  // Apply a rotation to a shape.
47
-  rotate(this: ShapeUtility<K>, shape: K, rotation: number): ShapeUtility<K>
46
+  // Set the shape's rotation.
47
+  rotateTo(this: ShapeUtility<K>, shape: K, rotation: number): ShapeUtility<K>
48 48
 
49 49
   // Transform to fit a new bounding box when more than one shape is selected.
50 50
   transform(

+ 3
- 3
lib/shape-utils/line.tsx Ver fichero

@@ -78,12 +78,12 @@ const line = registerShapeUtils<LineShape>({
78 78
     )
79 79
   },
80 80
 
81
-  rotate(shape) {
81
+  rotateTo(shape) {
82 82
     return this
83 83
   },
84 84
 
85
-  translate(shape, delta) {
86
-    shape.point = vec.add(shape.point, delta)
85
+  translateTo(shape, point) {
86
+    shape.point = point
87 87
     return this
88 88
   },
89 89
 

+ 4
- 3
lib/shape-utils/polyline.tsx Ver fichero

@@ -86,12 +86,13 @@ const polyline = registerShapeUtils<PolylineShape>({
86 86
     )
87 87
   },
88 88
 
89
-  rotate(shape) {
89
+  rotateTo(shape, rotation) {
90
+    shape.rotation = rotation
90 91
     return this
91 92
   },
92 93
 
93
-  translate(shape, delta) {
94
-    shape.point = vec.add(shape.point, delta)
94
+  translateTo(shape, point) {
95
+    shape.point = point
95 96
     return this
96 97
   },
97 98
 

+ 3
- 3
lib/shape-utils/ray.tsx Ver fichero

@@ -78,12 +78,12 @@ const ray = registerShapeUtils<RayShape>({
78 78
     )
79 79
   },
80 80
 
81
-  rotate(shape) {
81
+  rotateTo(shape) {
82 82
     return this
83 83
   },
84 84
 
85
-  translate(shape, delta) {
86
-    shape.point = vec.add(shape.point, delta)
85
+  translateTo(shape, point) {
86
+    shape.point = point
87 87
     return this
88 88
   },
89 89
 

+ 4
- 3
lib/shape-utils/rectangle.tsx Ver fichero

@@ -95,12 +95,13 @@ const rectangle = registerShapeUtils<RectangleShape>({
95 95
     )
96 96
   },
97 97
 
98
-  rotate(shape) {
98
+  rotateTo(shape, rotation) {
99
+    shape.rotation = rotation
99 100
     return this
100 101
   },
101 102
 
102
-  translate(shape, delta) {
103
-    shape.point = vec.add(shape.point, delta)
103
+  translateTo(shape, point) {
104
+    shape.point = point
104 105
     return this
105 106
   },
106 107
 

+ 2
- 4
state/commands/rotate.ts Ver fichero

@@ -21,8 +21,7 @@ export default function rotateCommand(
21 21
         for (let { id, point, rotation } of after.shapes) {
22 22
           const shape = shapes[id]
23 23
           const utils = getShapeUtils(shape)
24
-          utils.rotate(shape, rotation)
25
-          utils.translate(shape, point)
24
+          utils.rotateTo(shape, rotation).translateTo(shape, point)
26 25
         }
27 26
 
28 27
         data.boundsRotation = after.boundsRotation
@@ -33,8 +32,7 @@ export default function rotateCommand(
33 32
         for (let { id, point, rotation } of before.shapes) {
34 33
           const shape = shapes[id]
35 34
           const utils = getShapeUtils(shape)
36
-          utils.rotate(shape, rotation)
37
-          utils.translate(shape, point)
35
+          utils.rotateTo(shape, rotation).translateTo(shape, point)
38 36
         }
39 37
 
40 38
         data.boundsRotation = before.boundsRotation

+ 2
- 2
state/commands/translate.ts Ver fichero

@@ -34,7 +34,7 @@ export default function translateCommand(
34 34
 
35 35
         for (const { id, point } of initialShapes) {
36 36
           const shape = shapes[id]
37
-          getShapeUtils(shape).translate(shape, point)
37
+          getShapeUtils(shape).translateTo(shape, point)
38 38
           data.selectedIds.add(id)
39 39
         }
40 40
       },
@@ -52,7 +52,7 @@ export default function translateCommand(
52 52
 
53 53
         for (const { id, point } of initialShapes) {
54 54
           const shape = shapes[id]
55
-          getShapeUtils(shape).translate(shape, point)
55
+          getShapeUtils(shape).translateTo(shape, point)
56 56
           data.selectedIds.add(id)
57 57
         }
58 58
       },

+ 3
- 3
state/sessions/rotate-session.ts Ver fichero

@@ -45,8 +45,8 @@ export default class RotateSession extends BaseSession {
45 45
       const shape = page.shapes[id]
46 46
 
47 47
       getShapeUtils(shape)
48
-        .rotate(shape, (PI2 + (rotation + rot)) % PI2)
49
-        .translate(
48
+        .rotateTo(shape, (PI2 + (rotation + rot)) % PI2)
49
+        .translateTo(
50 50
           shape,
51 51
           vec.sub(vec.rotWith(center, boundsCenter, rot % PI2), offset)
52 52
         )
@@ -58,7 +58,7 @@ export default class RotateSession extends BaseSession {
58 58
 
59 59
     for (let { id, point, rotation } of this.snapshot.shapes) {
60 60
       const shape = page.shapes[id]
61
-      getShapeUtils(shape).rotate(shape, rotation).translate(shape, point)
61
+      getShapeUtils(shape).rotateTo(shape, rotation).translateTo(shape, point)
62 62
     }
63 63
   }
64 64
 

+ 4
- 4
state/sessions/translate-session.ts Ver fichero

@@ -40,7 +40,7 @@ export default class TranslateSession extends BaseSession {
40 40
 
41 41
         for (const { id, point } of initialShapes) {
42 42
           const shape = shapes[id]
43
-          getShapeUtils(shape).translate(shape, point)
43
+          getShapeUtils(shape).translateTo(shape, point)
44 44
         }
45 45
 
46 46
         for (const clone of clones) {
@@ -51,7 +51,7 @@ export default class TranslateSession extends BaseSession {
51 51
 
52 52
       for (const { id, point } of clones) {
53 53
         const shape = shapes[id]
54
-        getShapeUtils(shape).translate(shape, vec.add(point, delta))
54
+        getShapeUtils(shape).translateTo(shape, vec.add(point, delta))
55 55
       }
56 56
     } else {
57 57
       if (this.isCloning) {
@@ -69,7 +69,7 @@ export default class TranslateSession extends BaseSession {
69 69
 
70 70
       for (const { id, point } of initialShapes) {
71 71
         const shape = shapes[id]
72
-        getShapeUtils(shape).translate(shape, vec.add(point, delta))
72
+        getShapeUtils(shape).translateTo(shape, vec.add(point, delta))
73 73
       }
74 74
     }
75 75
   }
@@ -80,7 +80,7 @@ export default class TranslateSession extends BaseSession {
80 80
 
81 81
     for (const { id, point } of initialShapes) {
82 82
       const shape = shapes[id]
83
-      getShapeUtils(shape).translate(shape, point)
83
+      getShapeUtils(shape).translateTo(shape, point)
84 84
     }
85 85
 
86 86
     for (const { id } of clones) {

+ 19
- 26
state/state.ts Ver fichero

@@ -72,15 +72,10 @@ const state = createState({
72 72
     SELECTED_RECTANGLE_TOOL: { unless: "isReadOnly", to: "rectangle" },
73 73
     TOGGLED_CODE_PANEL_OPEN: "toggleCodePanel",
74 74
     RESET_CAMERA: "resetCamera",
75
-    ZOOMED_TO_FIT: {
76
-      if: "hasSelection",
77
-      do: "zoomCameraToFit",
78
-      else: "resetCamera",
79
-    },
75
+    ZOOMED_TO_FIT: "zoomCameraToFit",
80 76
     ZOOMED_TO_SELECTION: {
81 77
       if: "hasSelection",
82 78
       do: "zoomCameraToSelection",
83
-      else: "resetCamera",
84 79
     },
85 80
     ZOOMED_TO_ACTUAL: {
86 81
       if: "hasSelection",
@@ -721,7 +716,7 @@ const state = createState({
721 716
       const bounds = getSelectedBounds(data)
722 717
 
723 718
       const zoom =
724
-        bounds.width > bounds.height
719
+        bounds.width < bounds.height
725 720
           ? (window.innerWidth - 128) / bounds.width
726 721
           : (window.innerHeight - 128) / bounds.height
727 722
 
@@ -742,17 +737,12 @@ const state = createState({
742 737
 
743 738
       const bounds = getSelectedBounds(data)
744 739
 
745
-      const zoom = 1
746
-
747
-      const mx = window.innerWidth - 128 - bounds.width * zoom
748
-      const my = window.innerHeight - 128 - bounds.height * zoom
740
+      const mx = window.innerWidth - bounds.width
741
+      const my = window.innerHeight - bounds.height
749 742
 
750
-      camera.zoom = zoom
751
-      camera.point = vec.add(
752
-        [-bounds.minX, -bounds.minY],
753
-        [mx / 2 / zoom, my / 2 / zoom]
754
-      )
743
+      camera.zoom = 1
755 744
 
745
+      camera.point = vec.add([-bounds.minX, -bounds.minY], [mx / 2, my / 2])
756 746
       setZoomCSS(camera.zoom)
757 747
     },
758 748
     zoomCameraToActual(data) {
@@ -769,7 +759,13 @@ const state = createState({
769 759
     },
770 760
     zoomCameraToFit(data) {
771 761
       const { camera } = data
772
-      const { shapes } = getPage(data)
762
+      const page = getPage(data)
763
+
764
+      const shapes = Object.values(page.shapes)
765
+
766
+      if (shapes.length === 0) {
767
+        return
768
+      }
773 769
 
774 770
       const bounds = getCommonBounds(
775 771
         ...Object.values(shapes).map((shape) =>
@@ -778,18 +774,15 @@ const state = createState({
778 774
       )
779 775
 
780 776
       const zoom =
781
-        bounds.width > bounds.height
782
-          ? (window.innerWidth - 104) / bounds.width
783
-          : (window.innerHeight - 104) / bounds.height
777
+        bounds.width < bounds.height
778
+          ? (window.innerWidth - 128) / bounds.width
779
+          : (window.innerHeight - 128) / bounds.height
784 780
 
785
-      const mx = window.innerWidth - bounds.width * zoom
786
-      const my = window.innerHeight - bounds.height * zoom
781
+      const mx = (window.innerWidth - bounds.width * zoom) / 2 / zoom
782
+      const my = (window.innerHeight - bounds.height * zoom) / 2 / zoom
787 783
 
788 784
       camera.zoom = zoom
789
-      camera.point = vec.add(
790
-        [-bounds.minX, -bounds.minY],
791
-        [mx / 2 / zoom, my / 2 / zoom]
792
-      )
785
+      camera.point = vec.add([-bounds.minX, -bounds.minY], [mx, my])
793 786
 
794 787
       setZoomCSS(camera.zoom)
795 788
     },

Loading…
Cancelar
Guardar