瀏覽代碼

fixes rotation on drawn shapes

main
Steve Ruiz 4 年之前
父節點
當前提交
45fd645885
共有 4 個文件被更改,包括 21 次插入19 次删除
  1. 3
    5
      components/canvas/selected.tsx
  2. 5
    3
      components/canvas/shape.tsx
  3. 4
    11
      lib/shape-utils/draw.tsx
  4. 9
    0
      lib/shape-utils/notes.md

+ 3
- 5
components/canvas/selected.tsx 查看文件

1
 import styled from 'styles'
1
 import styled from 'styles'
2
 import { useSelector } from 'state'
2
 import { useSelector } from 'state'
3
-import { deepCompareArrays, getPage } from 'utils/utils'
3
+import { deepCompareArrays, getBoundsCenter, getPage } from 'utils/utils'
4
 import { getShapeUtils } from 'lib/shape-utils'
4
 import { getShapeUtils } from 'lib/shape-utils'
5
 import useShapeEvents from 'hooks/useShapeEvents'
5
 import useShapeEvents from 'hooks/useShapeEvents'
6
 import { memo, useRef } from 'react'
6
 import { memo, useRef } from 'react'
38
   // to handle parent rotation.
38
   // to handle parent rotation.
39
 
39
 
40
   const center = getShapeUtils(shape).getCenter(shape)
40
   const center = getShapeUtils(shape).getCenter(shape)
41
-  const bounds = getShapeUtils(shape).getBounds(shape)
42
 
41
 
43
   const transform = `
42
   const transform = `
44
-  rotate(${shape.rotation * (180 / Math.PI)}, 
45
-  ${center})
46
-  translate(${bounds.minX},${bounds.minY})
43
+  rotate(${shape.rotation * (180 / Math.PI)}, ${center})
44
+  translate(${shape.point})
47
   `
45
   `
48
 
46
 
49
   return (
47
   return (

+ 5
- 3
components/canvas/shape.tsx 查看文件

2
 import { useSelector } from 'state'
2
 import { useSelector } from 'state'
3
 import styled from 'styles'
3
 import styled from 'styles'
4
 import { getShapeUtils } from 'lib/shape-utils'
4
 import { getShapeUtils } from 'lib/shape-utils'
5
-import { getPage } from 'utils/utils'
5
+import { getBoundsCenter, getPage } from 'utils/utils'
6
 import { ShapeStyles, ShapeType } from 'types'
6
 import { ShapeStyles, ShapeType } from 'types'
7
 import useShapeEvents from 'hooks/useShapeEvents'
7
 import useShapeEvents from 'hooks/useShapeEvents'
8
 import * as vec from 'utils/vec'
8
 import * as vec from 'utils/vec'
30
   const isGroup = shape.type === ShapeType.Group
30
   const isGroup = shape.type === ShapeType.Group
31
 
31
 
32
   const center = getShapeUtils(shape).getCenter(shape)
32
   const center = getShapeUtils(shape).getCenter(shape)
33
+  const rotation = shape.rotation * (180 / Math.PI)
33
 
34
 
34
   const transform = `
35
   const transform = `
35
-  rotate(${shape.rotation * (180 / Math.PI)}, ${vec.sub(center, parentPoint)})
36
-  translate(${vec.sub(shape.point, parentPoint)})
36
+  translate(${vec.neg(parentPoint)})
37
+  rotate(${rotation}, ${center})
38
+  translate(${shape.point})
37
   `
39
   `
38
 
40
 
39
   const style = getShapeStyle(shape.style)
41
   const style = getShapeStyle(shape.style)

+ 4
- 11
lib/shape-utils/draw.tsx 查看文件

72
   },
72
   },
73
 
73
 
74
   getRotatedBounds(shape) {
74
   getRotatedBounds(shape) {
75
-    const rBounds = translateBounds(
75
+    return translateBounds(
76
       getBoundsFromPoints(shape.points, shape.rotation),
76
       getBoundsFromPoints(shape.points, shape.rotation),
77
       shape.point
77
       shape.point
78
     )
78
     )
79
-
80
-    const bounds = this.getBounds(shape)
81
-
82
-    const delta = vec.sub(getBoundsCenter(bounds), getBoundsCenter(rBounds))
83
-
84
-    return translateBounds(rBounds, delta)
85
   },
79
   },
86
 
80
 
87
   getCenter(shape) {
81
   getCenter(shape) {
88
-    const bounds = this.getRotatedBounds(shape)
89
-    return [bounds.minX + bounds.width / 2, bounds.minY + bounds.height / 2]
82
+    return getBoundsCenter(this.getBounds(shape))
90
   },
83
   },
91
 
84
 
92
   hitTest(shape, point) {
85
   hitTest(shape, point) {
131
     shape.points = initialShape.points.map(([x, y]) => {
124
     shape.points = initialShape.points.map(([x, y]) => {
132
       return [
125
       return [
133
         bounds.width *
126
         bounds.width *
134
-          (scaleX < 0
127
+          (scaleX < 0 // * sin?
135
             ? 1 - x / initialShapeBounds.width
128
             ? 1 - x / initialShapeBounds.width
136
             : x / initialShapeBounds.width),
129
             : x / initialShapeBounds.width),
137
         bounds.height *
130
         bounds.height *
138
-          (scaleY < 0
131
+          (scaleY < 0 // * cos?
139
             ? 1 - y / initialShapeBounds.height
132
             ? 1 - y / initialShapeBounds.height
140
             : y / initialShapeBounds.height),
133
             : y / initialShapeBounds.height),
141
       ]
134
       ]

+ 9
- 0
lib/shape-utils/notes.md 查看文件

1
 # Shape Utils
1
 # Shape Utils
2
+
3
+## Text
4
+
5
+- Click to create a shape, type to add its content
6
+- Click to select
7
+- Double click (long press?) to begin editing
8
+- Deselect to confirm changes
9
+- Escape to revert changes
10
+- If confirmed changes and text is empty, delete shape

Loading…
取消
儲存