Selaa lähdekoodia

Fixes bug with arrowhead, fits ellipse and rectangle sizes within bounding box

main
Steve Ruiz 4 vuotta sitten
vanhempi
commit
4fb7096f2b
4 muutettua tiedostoa jossa 35 lisäystä ja 32 poistoa
  1. 19
    23
      state/shape-utils/arrow.tsx
  2. 3
    2
      state/shape-utils/ellipse.tsx
  3. 11
    5
      state/shape-utils/rectangle.tsx
  4. 2
    2
      utils/dashes.ts

+ 19
- 23
state/shape-utils/arrow.tsx Näytä tiedosto

@@ -102,6 +102,8 @@ const arrow = registerShapeUtils<ArrowShape>({
102 102
 
103 103
     const strokeWidth = +styles.strokeWidth
104 104
 
105
+    const sw = strokeWidth * (style.dash === DashStyle.Solid ? 1 : 1.618)
106
+
105 107
     const arrowDist = vec.dist(start.point, end.point)
106 108
 
107 109
     let shaftPath: JSX.Element
@@ -126,7 +128,7 @@ const arrow = registerShapeUtils<ArrowShape>({
126 128
             }
127 129
           : getPerfectDashProps(
128 130
               arrowDist,
129
-              strokeWidth * 1.618,
131
+              sw,
130 132
               shape.style.dash === DashStyle.Dotted ? 'dotted' : 'dashed',
131 133
               2
132 134
             )
@@ -149,9 +151,7 @@ const arrow = registerShapeUtils<ArrowShape>({
149 151
           <path
150 152
             d={path}
151 153
             fill="none"
152
-            strokeWidth={
153
-              strokeWidth * (style.dash === DashStyle.Solid ? 1 : 1.618)
154
-            }
154
+            strokeWidth={sw}
155 155
             strokeDasharray={strokeDasharray}
156 156
             strokeDashoffset={strokeDashoffset}
157 157
             strokeLinecap="round"
@@ -176,7 +176,7 @@ const arrow = registerShapeUtils<ArrowShape>({
176 176
                 start.point,
177 177
                 end.point
178 178
               ) - 1,
179
-              strokeWidth * 1.618,
179
+              sw,
180 180
               shape.style.dash === DashStyle.Dotted ? 'dotted' : 'dashed',
181 181
               2
182 182
             )
@@ -205,7 +205,7 @@ const arrow = registerShapeUtils<ArrowShape>({
205 205
           <path
206 206
             d={path}
207 207
             fill="none"
208
-            strokeWidth={strokeWidth * 2}
208
+            strokeWidth={sw}
209 209
             strokeDasharray={strokeDasharray}
210 210
             strokeDashoffset={strokeDashoffset}
211 211
             strokeLinecap="round"
@@ -395,35 +395,31 @@ const arrow = registerShapeUtils<ArrowShape>({
395 395
       const handle = handles[id]
396 396
 
397 397
       shape.handles[handle.id] = handle
398
+    }
398 399
 
400
+    if ('bend' in handles) {
399 401
       const { start, end, bend } = shape.handles
400 402
 
401 403
       const dist = vec.dist(start.point, end.point)
402 404
 
403
-      if (handle.id === 'bend') {
404
-        const midPoint = vec.med(start.point, end.point)
405
-        const u = vec.uni(vec.vec(start.point, end.point))
406
-        const ap = vec.add(midPoint, vec.mul(vec.per(u), dist / 2))
407
-        const bp = vec.sub(midPoint, vec.mul(vec.per(u), dist / 2))
405
+      const midPoint = vec.med(start.point, end.point)
406
+      const u = vec.uni(vec.vec(start.point, end.point))
407
+      const ap = vec.add(midPoint, vec.mul(vec.per(u), dist / 2))
408
+      const bp = vec.sub(midPoint, vec.mul(vec.per(u), dist / 2))
408 409
 
409
-        bend.point = vec.nearestPointOnLineSegment(ap, bp, bend.point, true)
410
-        shape.bend = vec.dist(bend.point, midPoint) / (dist / 2)
410
+      bend.point = vec.nearestPointOnLineSegment(ap, bp, bend.point, true)
411
+      shape.bend = vec.dist(bend.point, midPoint) / (dist / 2)
411 412
 
412
-        const sa = vec.angle(end.point, start.point)
413
-        const la = sa - Math.PI / 2
414
-        if (isAngleBetween(sa, la, vec.angle(end.point, bend.point))) {
415
-          shape.bend *= -1
416
-        }
413
+      const sa = vec.angle(end.point, start.point)
414
+      const la = sa - Math.PI / 2
415
+
416
+      if (isAngleBetween(sa, la, vec.angle(end.point, bend.point))) {
417
+        shape.bend *= -1
417 418
       }
418 419
     }
419 420
 
420 421
     shape.handles.bend.point = getBendPoint(shape)
421 422
 
422
-    // const newBounds = this.getRotatedBounds(shape)
423
-    // const newCenter = getBoundsCenter(newBounds)
424
-
425
-    // shape.point = vec.add(shape.point, vec.neg(vec.sub(newCenter, prevCenter)))
426
-
427 423
     return this
428 424
   },
429 425
 

+ 3
- 2
state/shape-utils/ellipse.tsx Näytä tiedosto

@@ -73,6 +73,7 @@ const ellipse = registerShapeUtils<EllipseShape>({
73 73
     }
74 74
 
75 75
     const h = Math.pow(rx - ry, 2) / Math.pow(rx + ry, 2)
76
+
76 77
     const perimeter =
77 78
       Math.PI * (rx + ry) * (1 + (3 * h) / (10 + Math.sqrt(4 - 3 * h)))
78 79
 
@@ -190,8 +191,8 @@ function renderPath(shape: EllipseShape) {
190 191
 
191 192
   const strokeWidth = +getShapeStyle(style).strokeWidth
192 193
 
193
-  const rx = radiusX + getRandom() * strokeWidth
194
-  const ry = radiusY + getRandom() * strokeWidth
194
+  const rx = radiusX + getRandom() * strokeWidth - strokeWidth / 2
195
+  const ry = radiusY + getRandom() * strokeWidth - strokeWidth / 2
195 196
 
196 197
   const points: number[][] = []
197 198
   const start = Math.PI + Math.PI * getRandom()

+ 11
- 5
state/shape-utils/rectangle.tsx Näytä tiedosto

@@ -182,18 +182,24 @@ function renderPath(shape: RectangleShape) {
182 182
 
183 183
   const getRandom = rng(shape.id)
184 184
 
185
-  const baseOffset = +styles.strokeWidth / 2
185
+  const strokeWidth = +styles.strokeWidth
186
+
187
+  const baseOffset = strokeWidth / 2
186 188
 
187 189
   const offsets = Array.from(Array(4)).map(() => [
188 190
     getRandom() * baseOffset,
189 191
     getRandom() * baseOffset,
190 192
   ])
191 193
 
192
-  const [w, h] = shape.size
193
-  const tl = vec.add([0, 0], offsets[0])
194
-  const tr = vec.add([w, 0], offsets[1])
194
+  const sw = strokeWidth
195
+
196
+  const w = Math.max(0, shape.size[0] - sw / 2)
197
+  const h = Math.max(0, shape.size[1] - sw / 2)
198
+
199
+  const tl = vec.add([sw / 2, sw / 2], offsets[0])
200
+  const tr = vec.add([w, sw / 2], offsets[1])
195 201
   const br = vec.add([w, h], offsets[2])
196
-  const bl = vec.add([0, h], offsets[3])
202
+  const bl = vec.add([sw / 2, h], offsets[3])
197 203
 
198 204
   const lines = shuffleArr(
199 205
     [

+ 2
- 2
utils/dashes.ts Näytä tiedosto

@@ -23,8 +23,8 @@ export function getPerfectDashProps(
23 23
     ratio = 1
24 24
     strokeDashoffset = (dashLength / 2).toString()
25 25
   } else {
26
-    dashLength = strokeWidth / 4
27
-    ratio = 4
26
+    dashLength = strokeWidth / 100
27
+    ratio = 100
28 28
     strokeDashoffset = '0'
29 29
   }
30 30
 

Loading…
Peruuta
Tallenna