Przeglądaj źródła

Creates standard shape fallback for dashed / dotted rects, ellipses

main
Steve Ruiz 4 lat temu
rodzic
commit
03603b5190

+ 39
- 27
state/shape-utils/ellipse.tsx Wyświetl plik

@@ -1,12 +1,16 @@
1 1
 import { uniqueId } from 'utils/utils'
2 2
 import vec from 'utils/vec'
3
-import { EllipseShape, ShapeType } from 'types'
3
+import { DashStyle, EllipseShape, ShapeType } from 'types'
4 4
 import { getShapeUtils } from './index'
5 5
 import { boundsContained, getRotatedEllipseBounds } from 'utils/bounds'
6 6
 import { intersectEllipseBounds } from 'utils/intersections'
7 7
 import { pointInEllipse } from 'utils/hitTests'
8 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 14
 import getStroke from 'perfect-freehand'
11 15
 import { registerShapeUtils } from './register'
12 16
 
@@ -40,35 +44,43 @@ const ellipse = registerShapeUtils<EllipseShape>({
40 44
     const { id, radiusX, radiusY, style } = shape
41 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 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 86
   getBounds(shape) {

+ 0
- 10
state/shape-utils/notes.md Wyświetl plik

@@ -1,10 +0,0 @@
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 Wyświetl plik

@@ -1,6 +1,6 @@
1 1
 import { uniqueId } from 'utils/utils'
2 2
 import vec from 'utils/vec'
3
-import { RectangleShape, ShapeType } from 'types'
3
+import { DashStyle, RectangleShape, ShapeType } from 'types'
4 4
 import {
5 5
   getSvgPathFromStroke,
6 6
   translateBounds,
@@ -8,7 +8,11 @@ import {
8 8
   shuffleArr,
9 9
   pointsBetween,
10 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 16
 import getStroke from 'perfect-freehand'
13 17
 import { registerShapeUtils } from './register'
14 18
 
@@ -40,29 +44,45 @@ const rectangle = registerShapeUtils<RectangleShape>({
40 44
 
41 45
   render(shape) {
42 46
     const { id, size, radius, style } = shape
47
+
43 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 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
 

Ładowanie…
Anuluj
Zapisz