Browse Source

Improves hover appearance

main
Steve Ruiz 4 years ago
parent
commit
39ef1cf317
5 changed files with 69 additions and 28 deletions
  1. 13
    4
      components/canvas/shape.tsx
  2. 7
    3
      components/toolbar.tsx
  3. 12
    10
      lib/shape-utils/draw.tsx
  4. 27
    0
      state/state.ts
  5. 10
    11
      styles/stitches.config.ts

+ 13
- 4
components/canvas/shape.tsx View File

@@ -36,7 +36,13 @@ function Shape({ id, isSelecting }: { id: string; isSelecting: boolean }) {
36 36
       transform={transform}
37 37
       {...events}
38 38
     >
39
-      {isSelecting && <HoverIndicator as="use" href={'#' + id} />}
39
+      {isSelecting && (
40
+        <HoverIndicator
41
+          as="use"
42
+          href={'#' + id}
43
+          strokeWidth={+shape.style.strokeWidth + 8}
44
+        />
45
+      )}
40 46
       <StyledShape id={id} style={shape.style} />
41 47
       {/* 
42 48
       <text
@@ -102,7 +108,8 @@ const StyledGroup = styled('g', {
102 108
         [`& ${HoverIndicator}`]: {
103 109
           opacity: '1',
104 110
           stroke: '$hint',
105
-          zStrokeWidth: [8, 4],
111
+          fill: '$hint',
112
+          // zStrokeWidth: [8, 4],
106 113
         },
107 114
       },
108 115
     },
@@ -113,7 +120,8 @@ const StyledGroup = styled('g', {
113 120
         [`& ${HoverIndicator}`]: {
114 121
           opacity: '1',
115 122
           stroke: '$hint',
116
-          zStrokeWidth: [6, 3],
123
+          fill: '$hint',
124
+          // zStrokeWidth: [6, 3],
117 125
         },
118 126
       },
119 127
     },
@@ -124,7 +132,8 @@ const StyledGroup = styled('g', {
124 132
         [`& ${HoverIndicator}`]: {
125 133
           opacity: '1',
126 134
           stroke: '$hint',
127
-          zStrokeWidth: [8, 4],
135
+          fill: '$hint',
136
+          // zStrokeWidth: [8, 4],
128 137
         },
129 138
       },
130 139
     },

+ 7
- 3
components/toolbar.tsx View File

@@ -1,6 +1,6 @@
1 1
 import state, { useSelector } from 'state'
2 2
 import styled from 'styles'
3
-import { Lock, Menu, Unlock } from 'react-feather'
3
+import { Lock, Menu, RotateCcw, RotateCw, Unlock } from 'react-feather'
4 4
 import { IconButton } from './shared'
5 5
 
6 6
 export default function Toolbar() {
@@ -86,8 +86,12 @@ export default function Toolbar() {
86 86
         <Button onClick={() => state.send('RESET_CAMERA')}>Reset Camera</Button>
87 87
       </Section>
88 88
       <Section>
89
-        <Button onClick={() => state.send('UNDO')}>Undo</Button>
90
-        <Button onClick={() => state.send('REDO')}>Redo</Button>
89
+        <Button title="Undo" onClick={() => state.send('UNDO')}>
90
+          <RotateCcw />
91
+        </Button>
92
+        <Button title="Redo" onClick={() => state.send('REDO')}>
93
+          <RotateCw />
94
+        </Button>
91 95
       </Section>
92 96
     </ToolbarContainer>
93 97
   )

+ 12
- 10
lib/shape-utils/draw.tsx View File

@@ -12,7 +12,7 @@ import {
12 12
   translateBounds,
13 13
 } from 'utils/utils'
14 14
 
15
-const pathCache = new WeakMap<number[][], string>([])
15
+const pathCache = new WeakMap<DrawShape, string>([])
16 16
 
17 17
 const draw = registerShapeUtils<DrawShape>({
18 18
   boundsCache: new WeakMap([]),
@@ -40,27 +40,27 @@ const draw = registerShapeUtils<DrawShape>({
40 40
   },
41 41
 
42 42
   render(shape) {
43
-    const { id, point, points, style } = shape
43
+    const { id, points, style } = shape
44 44
 
45
-    if (!pathCache.has(points)) {
45
+    if (!pathCache.has(shape)) {
46 46
       if (points.length < 2) {
47
-        const left = vec.add(point, [6, 0])
47
+        const left = [+style.strokeWidth, 0]
48 48
         let d: number[][] = []
49 49
         for (let i = 0; i < 10; i++) {
50
-          d.push(vec.rotWith(left, point, i * ((Math.PI * 2) / 8)))
50
+          d.push(vec.rotWith(left, [0, 0], i * ((Math.PI * 2) / 8)))
51 51
         }
52
-        pathCache.set(points, getSvgPathFromStroke(d))
52
+        pathCache.set(shape, getSvgPathFromStroke(d))
53 53
       } else {
54 54
         pathCache.set(
55
-          points,
55
+          shape,
56 56
           getSvgPathFromStroke(
57
-            getStroke(points, { size: Number(style.strokeWidth) * 2 })
57
+            getStroke(points, { size: +style.strokeWidth * 2 })
58 58
           )
59 59
         )
60 60
       }
61 61
     }
62 62
 
63
-    return <path id={id} d={pathCache.get(points)} />
63
+    return <path id={id} d={pathCache.get(shape)} />
64 64
   },
65 65
 
66 66
   applyStyles(shape, style) {
@@ -95,7 +95,9 @@ const draw = registerShapeUtils<DrawShape>({
95 95
 
96 96
     for (let i = 1; i < shape.points.length; i++) {
97 97
       let curr = shape.points[i]
98
-      if (vec.distanceToLineSegment(prev, curr, pt) < 4) {
98
+      if (
99
+        vec.distanceToLineSegment(prev, curr, pt) < +shape.style.strokeWidth
100
+      ) {
99 101
         return true
100 102
       }
101 103
       prev = curr

+ 27
- 0
state/state.ts View File

@@ -274,6 +274,7 @@ const state = createState({
274 274
         },
275 275
         usingTool: {
276 276
           initial: 'draw',
277
+          onEnter: 'clearSelectedIds',
277 278
           states: {
278 279
             draw: {
279 280
               initial: 'creating',
@@ -281,6 +282,11 @@ const state = createState({
281 282
                 creating: {
282 283
                   on: {
283 284
                     CANCELLED: { to: 'selecting' },
285
+                    POINTED_SHAPE: {
286
+                      get: 'newDraw',
287
+                      do: 'createShape',
288
+                      to: 'draw.editing',
289
+                    },
284 290
                     POINTED_CANVAS: {
285 291
                       get: 'newDraw',
286 292
                       do: 'createShape',
@@ -313,6 +319,11 @@ const state = createState({
313 319
                 creating: {
314 320
                   on: {
315 321
                     CANCELLED: { to: 'selecting' },
322
+                    POINTED_SHAPE: {
323
+                      get: 'newDot',
324
+                      do: 'createShape',
325
+                      to: 'dot.editing',
326
+                    },
316 327
                     POINTED_CANVAS: {
317 328
                       get: 'newDot',
318 329
                       do: 'createShape',
@@ -364,6 +375,9 @@ const state = createState({
364 375
                 creating: {
365 376
                   on: {
366 377
                     CANCELLED: { to: 'selecting' },
378
+                    POINTED_SHAPE: {
379
+                      to: 'circle.editing',
380
+                    },
367 381
                     POINTED_CANVAS: {
368 382
                       to: 'circle.editing',
369 383
                     },
@@ -418,6 +432,9 @@ const state = createState({
418 432
                 creating: {
419 433
                   on: {
420 434
                     CANCELLED: { to: 'selecting' },
435
+                    POINTED_SHAPE: {
436
+                      to: 'rectangle.editing',
437
+                    },
421 438
                     POINTED_CANVAS: {
422 439
                       to: 'rectangle.editing',
423 440
                     },
@@ -445,6 +462,11 @@ const state = createState({
445 462
                 creating: {
446 463
                   on: {
447 464
                     CANCELLED: { to: 'selecting' },
465
+                    POINTED_SHAPE: {
466
+                      get: 'newRay',
467
+                      do: 'createShape',
468
+                      to: 'ray.editing',
469
+                    },
448 470
                     POINTED_CANVAS: {
449 471
                       get: 'newRay',
450 472
                       do: 'createShape',
@@ -470,6 +492,11 @@ const state = createState({
470 492
                 creating: {
471 493
                   on: {
472 494
                     CANCELLED: { to: 'selecting' },
495
+                    POINTED_SHAPE: {
496
+                      get: 'newLine',
497
+                      do: 'createShape',
498
+                      to: 'line.editing',
499
+                    },
473 500
                     POINTED_CANVAS: {
474 501
                       get: 'newLine',
475 502
                       do: 'createShape',

+ 10
- 11
styles/stitches.config.ts View File

@@ -45,22 +45,21 @@ const { styled, global, css, theme, getCssString } = createCss({
45 45
   utils: {
46 46
     zStrokeWidth: () => (value: number | number[]) => {
47 47
       if (Array.isArray(value)) {
48
+        // const [val, min, max] = value
49
+        // return {
50
+        //   strokeWidth:
51
+        //     min !== undefined && max !== undefined
52
+        //       ? `clamp(${min}px, (calc(${val}px / var(--camera-zoom))), ${max}px)`
53
+        //       : min !== undefined
54
+        //       ? `min(${min}px, calc(${val}px / var(--camera-zoom)))`
55
+        //       : `calc(${val}px / var(--camera-zoom))`,
56
+        // }
57
+
48 58
         return {
49 59
           strokeWidth: `calc(${value[0]}px / var(--camera-zoom))`,
50 60
         }
51 61
       }
52 62
 
53
-      // const [val, min, max] = value
54
-      // return {
55
-      //   strokeWidth:
56
-      //     min !== undefined && max !== undefined
57
-      //       ? `clamp(${min}, ${val} / var(--camera-zoom), ${max})`
58
-      //       : min !== undefined
59
-      //       ? `min(${min}, ${val} / var(--camera-zoom))`
60
-      //       : `calc(${val} / var(--camera-zoom))`,
61
-      // }
62
-      // }
63
-
64 63
       return {
65 64
         strokeWidth: `calc(${value}px / var(--camera-zoom))`,
66 65
       }

Loading…
Cancel
Save