ソースを参照

Fixes rotation, translation

main
Steve Ruiz 4年前
コミット
6da9e5f019

+ 4
- 3
lib/shape-utils/circle.tsx ファイルの表示

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
     return this
86
     return this
87
   },
87
   },
88
 
88
 
89
-  rotate(shape) {
89
+  translateTo(shape, point) {
90
+    shape.point = point
90
     return this
91
     return this
91
   },
92
   },
92
 
93
 

+ 3
- 3
lib/shape-utils/dot.tsx ファイルの表示

69
     )
69
     )
70
   },
70
   },
71
 
71
 
72
-  rotate(shape) {
72
+  rotateTo(shape) {
73
     return this
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
     return this
78
     return this
79
   },
79
   },
80
 
80
 

+ 4
- 3
lib/shape-utils/ellipse.tsx ファイルの表示

99
     )
99
     )
100
   },
100
   },
101
 
101
 
102
-  rotate(shape) {
102
+  rotateTo(shape, rotation) {
103
+    shape.rotation = rotation
103
     return this
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
     return this
109
     return this
109
   },
110
   },
110
 
111
 

+ 4
- 4
lib/shape-utils/index.tsx ファイルの表示

40
   // Create a new shape.
40
   // Create a new shape.
41
   create(props: Partial<K>): K
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
   // Transform to fit a new bounding box when more than one shape is selected.
49
   // Transform to fit a new bounding box when more than one shape is selected.
50
   transform(
50
   transform(

+ 3
- 3
lib/shape-utils/line.tsx ファイルの表示

78
     )
78
     )
79
   },
79
   },
80
 
80
 
81
-  rotate(shape) {
81
+  rotateTo(shape) {
82
     return this
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
     return this
87
     return this
88
   },
88
   },
89
 
89
 

+ 4
- 3
lib/shape-utils/polyline.tsx ファイルの表示

86
     )
86
     )
87
   },
87
   },
88
 
88
 
89
-  rotate(shape) {
89
+  rotateTo(shape, rotation) {
90
+    shape.rotation = rotation
90
     return this
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
     return this
96
     return this
96
   },
97
   },
97
 
98
 

+ 3
- 3
lib/shape-utils/ray.tsx ファイルの表示

78
     )
78
     )
79
   },
79
   },
80
 
80
 
81
-  rotate(shape) {
81
+  rotateTo(shape) {
82
     return this
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
     return this
87
     return this
88
   },
88
   },
89
 
89
 

+ 4
- 3
lib/shape-utils/rectangle.tsx ファイルの表示

95
     )
95
     )
96
   },
96
   },
97
 
97
 
98
-  rotate(shape) {
98
+  rotateTo(shape, rotation) {
99
+    shape.rotation = rotation
99
     return this
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
     return this
105
     return this
105
   },
106
   },
106
 
107
 

+ 2
- 4
state/commands/rotate.ts ファイルの表示

21
         for (let { id, point, rotation } of after.shapes) {
21
         for (let { id, point, rotation } of after.shapes) {
22
           const shape = shapes[id]
22
           const shape = shapes[id]
23
           const utils = getShapeUtils(shape)
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
         data.boundsRotation = after.boundsRotation
27
         data.boundsRotation = after.boundsRotation
33
         for (let { id, point, rotation } of before.shapes) {
32
         for (let { id, point, rotation } of before.shapes) {
34
           const shape = shapes[id]
33
           const shape = shapes[id]
35
           const utils = getShapeUtils(shape)
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
         data.boundsRotation = before.boundsRotation
38
         data.boundsRotation = before.boundsRotation

+ 2
- 2
state/commands/translate.ts ファイルの表示

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

+ 3
- 3
state/sessions/rotate-session.ts ファイルの表示

45
       const shape = page.shapes[id]
45
       const shape = page.shapes[id]
46
 
46
 
47
       getShapeUtils(shape)
47
       getShapeUtils(shape)
48
-        .rotate(shape, (PI2 + (rotation + rot)) % PI2)
49
-        .translate(
48
+        .rotateTo(shape, (PI2 + (rotation + rot)) % PI2)
49
+        .translateTo(
50
           shape,
50
           shape,
51
           vec.sub(vec.rotWith(center, boundsCenter, rot % PI2), offset)
51
           vec.sub(vec.rotWith(center, boundsCenter, rot % PI2), offset)
52
         )
52
         )
58
 
58
 
59
     for (let { id, point, rotation } of this.snapshot.shapes) {
59
     for (let { id, point, rotation } of this.snapshot.shapes) {
60
       const shape = page.shapes[id]
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 ファイルの表示

40
 
40
 
41
         for (const { id, point } of initialShapes) {
41
         for (const { id, point } of initialShapes) {
42
           const shape = shapes[id]
42
           const shape = shapes[id]
43
-          getShapeUtils(shape).translate(shape, point)
43
+          getShapeUtils(shape).translateTo(shape, point)
44
         }
44
         }
45
 
45
 
46
         for (const clone of clones) {
46
         for (const clone of clones) {
51
 
51
 
52
       for (const { id, point } of clones) {
52
       for (const { id, point } of clones) {
53
         const shape = shapes[id]
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
     } else {
56
     } else {
57
       if (this.isCloning) {
57
       if (this.isCloning) {
69
 
69
 
70
       for (const { id, point } of initialShapes) {
70
       for (const { id, point } of initialShapes) {
71
         const shape = shapes[id]
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
 
80
 
81
     for (const { id, point } of initialShapes) {
81
     for (const { id, point } of initialShapes) {
82
       const shape = shapes[id]
82
       const shape = shapes[id]
83
-      getShapeUtils(shape).translate(shape, point)
83
+      getShapeUtils(shape).translateTo(shape, point)
84
     }
84
     }
85
 
85
 
86
     for (const { id } of clones) {
86
     for (const { id } of clones) {

+ 19
- 26
state/state.ts ファイルの表示

72
     SELECTED_RECTANGLE_TOOL: { unless: "isReadOnly", to: "rectangle" },
72
     SELECTED_RECTANGLE_TOOL: { unless: "isReadOnly", to: "rectangle" },
73
     TOGGLED_CODE_PANEL_OPEN: "toggleCodePanel",
73
     TOGGLED_CODE_PANEL_OPEN: "toggleCodePanel",
74
     RESET_CAMERA: "resetCamera",
74
     RESET_CAMERA: "resetCamera",
75
-    ZOOMED_TO_FIT: {
76
-      if: "hasSelection",
77
-      do: "zoomCameraToFit",
78
-      else: "resetCamera",
79
-    },
75
+    ZOOMED_TO_FIT: "zoomCameraToFit",
80
     ZOOMED_TO_SELECTION: {
76
     ZOOMED_TO_SELECTION: {
81
       if: "hasSelection",
77
       if: "hasSelection",
82
       do: "zoomCameraToSelection",
78
       do: "zoomCameraToSelection",
83
-      else: "resetCamera",
84
     },
79
     },
85
     ZOOMED_TO_ACTUAL: {
80
     ZOOMED_TO_ACTUAL: {
86
       if: "hasSelection",
81
       if: "hasSelection",
721
       const bounds = getSelectedBounds(data)
716
       const bounds = getSelectedBounds(data)
722
 
717
 
723
       const zoom =
718
       const zoom =
724
-        bounds.width > bounds.height
719
+        bounds.width < bounds.height
725
           ? (window.innerWidth - 128) / bounds.width
720
           ? (window.innerWidth - 128) / bounds.width
726
           : (window.innerHeight - 128) / bounds.height
721
           : (window.innerHeight - 128) / bounds.height
727
 
722
 
742
 
737
 
743
       const bounds = getSelectedBounds(data)
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
       setZoomCSS(camera.zoom)
746
       setZoomCSS(camera.zoom)
757
     },
747
     },
758
     zoomCameraToActual(data) {
748
     zoomCameraToActual(data) {
769
     },
759
     },
770
     zoomCameraToFit(data) {
760
     zoomCameraToFit(data) {
771
       const { camera } = data
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
       const bounds = getCommonBounds(
770
       const bounds = getCommonBounds(
775
         ...Object.values(shapes).map((shape) =>
771
         ...Object.values(shapes).map((shape) =>
778
       )
774
       )
779
 
775
 
780
       const zoom =
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
       camera.zoom = zoom
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
       setZoomCSS(camera.zoom)
787
       setZoomCSS(camera.zoom)
795
     },
788
     },

読み込み中…
キャンセル
保存