Browse Source

Adds zoom helpers, improves drag selection

main
Steve Ruiz 4 years ago
parent
commit
f4e429af0e

+ 2
- 0
components/canvas/misc.tsx View File

@@ -2,6 +2,8 @@ import styled from "styles"
2 2
 
3 3
 const DotCircle = styled("circle", {
4 4
   transform: "scale(var(--scale))",
5
+  strokeWidth: "2",
6
+  fill: "#000",
5 7
 })
6 8
 
7 9
 export { DotCircle }

+ 0
- 1
components/canvas/shape.tsx View File

@@ -95,7 +95,6 @@ const Indicator = styled("path", {
95 95
 const HoverIndicator = styled("path", {
96 96
   fill: "none",
97 97
   stroke: "transparent",
98
-  zStrokeWidth: [8, 4],
99 98
   pointerEvents: "all",
100 99
   strokeLinecap: "round",
101 100
   strokeLinejoin: "round",

+ 21
- 0
hooks/useKeyboardEvents.ts View File

@@ -10,6 +10,27 @@ export default function useKeyboardEvents() {
10 10
       }
11 11
 
12 12
       switch (e.key) {
13
+        case "!": {
14
+          // Shift + 1
15
+          if (e.shiftKey) {
16
+            state.send("ZOOMED_TO_FIT")
17
+          }
18
+          break
19
+        }
20
+        case "@": {
21
+          // Shift + 2
22
+          if (e.shiftKey) {
23
+            state.send("ZOOMED_TO_SELECTION")
24
+          }
25
+          break
26
+        }
27
+        case ")": {
28
+          // Shift + 0
29
+          if (e.shiftKey) {
30
+            state.send("ZOOMED_TO_ACTUAL")
31
+          }
32
+          break
33
+        }
13 34
         case "Escape": {
14 35
           state.send("CANCELLED")
15 36
           break

+ 1
- 2
lib/shape-utils/dot.tsx View File

@@ -4,7 +4,6 @@ import { DotShape, ShapeType } from "types"
4 4
 import { registerShapeUtils } from "./index"
5 5
 import { boundsContained } from "utils/bounds"
6 6
 import { intersectCircleBounds } from "utils/intersections"
7
-import styled from "styles"
8 7
 import { DotCircle } from "components/canvas/misc"
9 8
 import { translateBounds } from "utils/utils"
10 9
 
@@ -23,7 +22,7 @@ const dot = registerShapeUtils<DotShape>({
23 22
       rotation: 0,
24 23
       style: {
25 24
         fill: "#c6cacb",
26
-        stroke: "#000",
25
+        strokeWidth: "0",
27 26
       },
28 27
       ...props,
29 28
     }

+ 1
- 1
lib/shape-utils/line.tsx View File

@@ -36,7 +36,7 @@ const line = registerShapeUtils<LineShape>({
36 36
     return (
37 37
       <g id={id}>
38 38
         <line x1={x1} y1={y1} x2={x2} y2={y2} />
39
-        <DotCircle cx={0} cy={0} r={4} />
39
+        <DotCircle cx={0} cy={0} r={3} />
40 40
       </g>
41 41
     )
42 42
   },

+ 1
- 1
lib/shape-utils/ray.tsx View File

@@ -36,7 +36,7 @@ const ray = registerShapeUtils<RayShape>({
36 36
     return (
37 37
       <g id={id}>
38 38
         <line x1={0} y1={0} x2={x2} y2={y2} />
39
-        <DotCircle cx={0} cy={0} r={4} />
39
+        <DotCircle cx={0} cy={0} r={3} />
40 40
       </g>
41 41
     )
42 42
   },

+ 93
- 12
state/state.ts View File

@@ -1,12 +1,16 @@
1 1
 import { createSelectorHook, createState } from "@state-designer/react"
2 2
 import {
3 3
   clamp,
4
+  getBoundsCenter,
4 5
   getChildren,
5 6
   getCommonBounds,
6 7
   getPage,
8
+  getSelectedBounds,
9
+  getSelectedShapes,
7 10
   getShape,
8 11
   getSiblings,
9 12
   screenToWorld,
13
+  setZoomCSS,
10 14
 } from "utils/utils"
11 15
 import * as vec from "utils/vec"
12 16
 import {
@@ -68,6 +72,13 @@ const state = createState({
68 72
     SELECTED_RECTANGLE_TOOL: { unless: "isReadOnly", to: "rectangle" },
69 73
     TOGGLED_CODE_PANEL_OPEN: "toggleCodePanel",
70 74
     RESET_CAMERA: "resetCamera",
75
+    ZOOMED_TO_FIT: "zoomCameraToFit",
76
+    ZOOMED_TO_SELECTION: { if: "hasSelection", do: "zoomCameraToSelection" },
77
+    ZOOMED_TO_ACTUAL: {
78
+      if: "hasSelection",
79
+      do: "zoomCameraToSelectionActual",
80
+      else: "zoomCameraToActual",
81
+    },
71 82
   },
72 83
   initial: "loading",
73 84
   states: {
@@ -480,7 +491,7 @@ const state = createState({
480 491
       return data.isReadOnly
481 492
     },
482 493
     distanceImpliesDrag(data, payload: PointerInfo) {
483
-      return vec.dist2(payload.origin, payload.point) > 16
494
+      return vec.dist2(payload.origin, payload.point) > 8
484 495
     },
485 496
     isPointedShapeSelected(data) {
486 497
       return data.selectedIds.has(data.pointedId)
@@ -508,6 +519,9 @@ const state = createState({
508 519
     ) {
509 520
       return payload.target === "rotate"
510 521
     },
522
+    hasSelection(data) {
523
+      return data.selectedIds.size > 0
524
+    },
511 525
   },
512 526
   actions: {
513 527
     /* --------------------- Shapes --------------------- */
@@ -565,7 +579,7 @@ const state = createState({
565 579
     startTranslateSession(data, payload: PointerInfo) {
566 580
       session = new Sessions.TranslateSession(
567 581
         data,
568
-        screenToWorld(payload.point, data),
582
+        screenToWorld(inputs.pointer.origin, data),
569 583
         payload.altKey
570 584
       )
571 585
     },
@@ -697,29 +711,96 @@ const state = createState({
697 711
 
698 712
       document.documentElement.style.setProperty("--camera-zoom", "1")
699 713
     },
700
-    centerCamera(data) {
714
+    zoomCameraToSelection(data) {
715
+      const { camera } = data
716
+
717
+      const bounds = getSelectedBounds(data)
718
+
719
+      const zoom =
720
+        bounds.width > bounds.height
721
+          ? (window.innerWidth - 128) / bounds.width
722
+          : (window.innerHeight - 128) / bounds.height
723
+
724
+      const mx = window.innerWidth - bounds.width * zoom
725
+      const my = window.innerHeight - bounds.height * zoom
726
+
727
+      camera.zoom = zoom
728
+
729
+      camera.point = vec.add(
730
+        [-bounds.minX, -bounds.minY],
731
+        [mx / 2 / zoom, my / 2 / zoom]
732
+      )
733
+
734
+      setZoomCSS(camera.zoom)
735
+    },
736
+    zoomCameraToSelectionActual(data) {
737
+      const { camera } = data
738
+
739
+      const bounds = getSelectedBounds(data)
740
+
741
+      const zoom = 1
742
+
743
+      const mx = window.innerWidth - 128 - bounds.width * zoom
744
+      const my = window.innerHeight - 128 - bounds.height * zoom
745
+
746
+      camera.zoom = zoom
747
+      camera.point = vec.add(
748
+        [-bounds.minX, -bounds.minY],
749
+        [mx / 2 / zoom, my / 2 / zoom]
750
+      )
751
+
752
+      setZoomCSS(camera.zoom)
753
+    },
754
+    zoomCameraToActual(data) {
755
+      const { camera } = data
756
+
757
+      const center = [window.innerWidth / 2, window.innerHeight / 2]
758
+
759
+      const p0 = screenToWorld(center, data)
760
+      camera.zoom = 1
761
+      const p1 = screenToWorld(center, data)
762
+      camera.point = vec.add(camera.point, vec.sub(p1, p0))
763
+
764
+      setZoomCSS(camera.zoom)
765
+    },
766
+    zoomCameraToFit(data) {
767
+      const { camera } = data
701 768
       const { shapes } = getPage(data)
702
-      getCommonBounds()
703
-      data.camera.zoom = 1
704
-      data.camera.point = [window.innerWidth / 2, window.innerHeight / 2]
705 769
 
706
-      document.documentElement.style.setProperty("--camera-zoom", "1")
770
+      const bounds = getCommonBounds(
771
+        ...Object.values(shapes).map((shape) =>
772
+          getShapeUtils(shape).getBounds(shape)
773
+        )
774
+      )
775
+
776
+      const zoom =
777
+        bounds.width > bounds.height
778
+          ? (window.innerWidth - 104) / bounds.width
779
+          : (window.innerHeight - 104) / bounds.height
780
+
781
+      const mx = window.innerWidth - bounds.width * zoom
782
+      const my = window.innerHeight - bounds.height * zoom
783
+
784
+      camera.zoom = zoom
785
+      camera.point = vec.add(
786
+        [-bounds.minX, -bounds.minY],
787
+        [mx / 2 / zoom, my / 2 / zoom]
788
+      )
789
+
790
+      setZoomCSS(camera.zoom)
707 791
     },
708 792
     zoomCamera(data, payload: { delta: number; point: number[] }) {
709 793
       const { camera } = data
710 794
       const p0 = screenToWorld(payload.point, data)
711 795
       camera.zoom = clamp(
712 796
         camera.zoom - (payload.delta / 100) * camera.zoom,
713
-        0.5,
797
+        0.1,
714 798
         3
715 799
       )
716 800
       const p1 = screenToWorld(payload.point, data)
717 801
       camera.point = vec.add(camera.point, vec.sub(p1, p0))
718 802
 
719
-      document.documentElement.style.setProperty(
720
-        "--camera-zoom",
721
-        camera.zoom.toString()
722
-      )
803
+      setZoomCSS(camera.zoom)
723 804
     },
724 805
     panCamera(data, payload: { delta: number[]; point: number[] }) {
725 806
       const { camera } = data

+ 12
- 7
styles/stitches.config.ts View File

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

+ 11
- 0
utils/utils.ts View File

@@ -1355,6 +1355,14 @@ export function getSelectedShapes(data: Data, pageId = data.currentPageId) {
1355 1355
   return ids.map((id) => page.shapes[id])
1356 1356
 }
1357 1357
 
1358
+export function getSelectedBounds(data: Data) {
1359
+  return getCommonBounds(
1360
+    ...getSelectedShapes(data).map((shape) =>
1361
+      getShapeUtils(shape).getBounds(shape)
1362
+    )
1363
+  )
1364
+}
1365
+
1358 1366
 export function isMobile() {
1359 1367
   return _isMobile()
1360 1368
 }
@@ -1474,3 +1482,6 @@ export function forceIntegerChildIndices(shapes: Shape[]) {
1474 1482
     shapes[i].childIndex = i + 1
1475 1483
   }
1476 1484
 }
1485
+export function setZoomCSS(zoom: number) {
1486
+  document.documentElement.style.setProperty("--camera-zoom", zoom.toString())
1487
+}

Loading…
Cancel
Save