瀏覽代碼

Creates standard shape fallback for dashed / dotted rects, ellipses

main
Steve Ruiz 4 年之前
父節點
當前提交
03603b5190
共有 3 個文件被更改,包括 79 次插入57 次删除
  1. 39
    27
      state/shape-utils/ellipse.tsx
  2. 0
    10
      state/shape-utils/notes.md
  3. 40
    20
      state/shape-utils/rectangle.tsx

+ 39
- 27
state/shape-utils/ellipse.tsx 查看文件

1
 import { uniqueId } from 'utils/utils'
1
 import { uniqueId } from 'utils/utils'
2
 import vec from 'utils/vec'
2
 import vec from 'utils/vec'
3
-import { EllipseShape, ShapeType } from 'types'
3
+import { DashStyle, EllipseShape, ShapeType } from 'types'
4
 import { getShapeUtils } from './index'
4
 import { getShapeUtils } from './index'
5
 import { boundsContained, getRotatedEllipseBounds } from 'utils/bounds'
5
 import { boundsContained, getRotatedEllipseBounds } from 'utils/bounds'
6
 import { intersectEllipseBounds } from 'utils/intersections'
6
 import { intersectEllipseBounds } from 'utils/intersections'
7
 import { pointInEllipse } from 'utils/hitTests'
7
 import { pointInEllipse } from 'utils/hitTests'
8
 import { ease, getSvgPathFromStroke, rng, translateBounds } from 'utils/utils'
8
 import { ease, getSvgPathFromStroke, rng, translateBounds } from 'utils/utils'
9
-import { defaultStyle, getShapeStyle } from 'state/shape-styles'
9
+import {
10
+  defaultStyle,
11
+  getShapeStyle,
12
+  getStrokeDashArray,
13
+} from 'state/shape-styles'
10
 import getStroke from 'perfect-freehand'
14
 import getStroke from 'perfect-freehand'
11
 import { registerShapeUtils } from './register'
15
 import { registerShapeUtils } from './register'
12
 
16
 
40
     const { id, radiusX, radiusY, style } = shape
44
     const { id, radiusX, radiusY, style } = shape
41
     const styles = getShapeStyle(style)
45
     const styles = getShapeStyle(style)
42
 
46
 
43
-    if (!pathCache.has(shape)) {
44
-      renderPath(shape)
45
-    }
47
+    if (style.dash === DashStyle.Solid) {
48
+      if (!pathCache.has(shape)) {
49
+        renderPath(shape)
50
+      }
46
 
51
 
47
-    const path = pathCache.get(shape)
52
+      const path = pathCache.get(shape)
53
+
54
+      return (
55
+        <g id={id}>
56
+          <ellipse
57
+            id={id}
58
+            cx={radiusX}
59
+            cy={radiusY}
60
+            rx={Math.max(0, radiusX - +styles.strokeWidth / 2)}
61
+            ry={Math.max(0, radiusY - +styles.strokeWidth / 2)}
62
+            stroke="none"
63
+          />
64
+          <path d={path} fill={styles.stroke} />
65
+        </g>
66
+      )
67
+    }
48
 
68
 
49
     return (
69
     return (
50
-      <g id={id}>
51
-        <ellipse
52
-          id={id}
53
-          cx={radiusX}
54
-          cy={radiusY}
55
-          rx={Math.max(0, radiusX - Number(styles.strokeWidth) / 2)}
56
-          ry={Math.max(0, radiusY - Number(styles.strokeWidth) / 2)}
57
-          stroke="none"
58
-        />
59
-        <path d={path} fill={styles.stroke} />
60
-      </g>
70
+      <ellipse
71
+        id={id}
72
+        cx={radiusX}
73
+        cy={radiusY}
74
+        rx={Math.max(0, radiusX - +styles.strokeWidth / 2)}
75
+        ry={Math.max(0, radiusY - +styles.strokeWidth / 2)}
76
+        fill={styles.fill}
77
+        stroke={styles.stroke}
78
+        strokeDasharray={getStrokeDashArray(
79
+          style.dash,
80
+          +styles.strokeWidth
81
+        ).join(' ')}
82
+      />
61
     )
83
     )
62
-
63
-    // return (
64
-    //   <ellipse
65
-    //     id={id}
66
-    //     cx={radiusX}
67
-    //     cy={radiusY}
68
-    //     rx={Math.max(0, radiusX - Number(styles.strokeWidth) / 2)}
69
-    //     ry={Math.max(0, radiusY - Number(styles.strokeWidth) / 2)}
70
-    //   />
71
-    // )
72
   },
84
   },
73
 
85
 
74
   getBounds(shape) {
86
   getBounds(shape) {

+ 0
- 10
state/shape-utils/notes.md 查看文件

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

+ 40
- 20
state/shape-utils/rectangle.tsx 查看文件

1
 import { uniqueId } from 'utils/utils'
1
 import { uniqueId } from 'utils/utils'
2
 import vec from 'utils/vec'
2
 import vec from 'utils/vec'
3
-import { RectangleShape, ShapeType } from 'types'
3
+import { DashStyle, RectangleShape, ShapeType } from 'types'
4
 import {
4
 import {
5
   getSvgPathFromStroke,
5
   getSvgPathFromStroke,
6
   translateBounds,
6
   translateBounds,
8
   shuffleArr,
8
   shuffleArr,
9
   pointsBetween,
9
   pointsBetween,
10
 } from 'utils/utils'
10
 } from 'utils/utils'
11
-import { defaultStyle, getShapeStyle } from 'state/shape-styles'
11
+import {
12
+  defaultStyle,
13
+  getShapeStyle,
14
+  getStrokeDashArray,
15
+} from 'state/shape-styles'
12
 import getStroke from 'perfect-freehand'
16
 import getStroke from 'perfect-freehand'
13
 import { registerShapeUtils } from './register'
17
 import { registerShapeUtils } from './register'
14
 
18
 
40
 
44
 
41
   render(shape) {
45
   render(shape) {
42
     const { id, size, radius, style } = shape
46
     const { id, size, radius, style } = shape
47
+
43
     const styles = getShapeStyle(style)
48
     const styles = getShapeStyle(style)
44
 
49
 
45
-    if (!pathCache.has(shape.size)) {
46
-      renderPath(shape)
47
-    }
50
+    if (style.dash === DashStyle.Solid) {
51
+      if (!pathCache.has(shape.size)) {
52
+        renderPath(shape)
53
+      }
48
 
54
 
49
-    const path = pathCache.get(shape.size)
55
+      const path = pathCache.get(shape.size)
56
+
57
+      return (
58
+        <g id={id}>
59
+          <rect
60
+            rx={radius}
61
+            ry={radius}
62
+            x={+styles.strokeWidth / 2}
63
+            y={+styles.strokeWidth / 2}
64
+            width={Math.max(0, size[0] + -styles.strokeWidth)}
65
+            height={Math.max(0, size[1] + -styles.strokeWidth)}
66
+            strokeWidth={0}
67
+            fill={styles.fill}
68
+          />
69
+          <path d={path} fill={styles.stroke} />
70
+        </g>
71
+      )
72
+    }
50
 
73
 
51
     return (
74
     return (
52
-      <g id={id}>
53
-        <rect
54
-          className="hi"
55
-          rx={radius}
56
-          ry={radius}
57
-          x={+styles.strokeWidth / 2}
58
-          y={+styles.strokeWidth / 2}
59
-          width={Math.max(0, size[0] + -styles.strokeWidth)}
60
-          height={Math.max(0, size[1] + -styles.strokeWidth)}
61
-          strokeWidth={0}
62
-          fill={styles.fill}
63
-        />
64
-        <path d={path} fill={styles.stroke} />
65
-      </g>
75
+      <rect
76
+        id={id}
77
+        width={size[0]}
78
+        height={size[1]}
79
+        fill={styles.fill}
80
+        stroke={styles.stroke}
81
+        strokeDasharray={getStrokeDashArray(
82
+          style.dash,
83
+          +styles.strokeWidth
84
+        ).join(' ')}
85
+      />
66
     )
86
     )
67
   },
87
   },
68
 
88
 

Loading…
取消
儲存