소스 검색

Improve comments on draw

main
Steve Ruiz 4 년 전
부모
커밋
fe833b6e3b
1개의 변경된 파일28개의 추가작업 그리고 11개의 파일을 삭제
  1. 28
    11
      state/sessions/draw-session.ts

+ 28
- 11
state/sessions/draw-session.ts 파일 보기

@@ -22,11 +22,15 @@ export default class BrushSession extends BaseSession {
22 22
     this.origin = point
23 23
     this.previous = point
24 24
     this.last = point
25
-    this.points = [[0, 0]]
26 25
     this.snapshot = getDrawSnapshot(data, id)
27 26
 
28
-    const page = getPage(data)
29
-    const shape = page.shapes[id] as DrawShape
27
+    // Add a first point but don't update the shape yet. We'll update
28
+    // when the draw session ends; if the user hasn't added additional
29
+    // points, this single point will be interpreted as a "dot" shape.
30
+    this.points = [[0, 0]]
31
+
32
+    const shape = getPage(data).shapes[id]
33
+
30 34
     getShapeUtils(shape).translateTo(shape, point)
31 35
 
32 36
     updateParents(data, [shape.id])
@@ -42,12 +46,18 @@ export default class BrushSession extends BaseSession {
42 46
 
43 47
     const delta = vec.vec(this.origin, point)
44 48
 
49
+    // Drawing while holding shift will "lock" the pen to either the
50
+    // x or y axis, depending on which direction has the greater
51
+    // delta. Pressing shift will also add more points to "return"
52
+    // the pen to the axis.
45 53
     if (isLocked) {
46 54
       if (!this.isLocked && this.points.length > 1) {
47 55
         this.isLocked = true
48 56
         const returning = [...this.previous]
49 57
 
50
-        if (Math.abs(delta[0]) < Math.abs(delta[1])) {
58
+        const isVertical = Math.abs(delta[0]) < Math.abs(delta[1])
59
+
60
+        if (isVertical) {
51 61
           this.lockedDirection = 'vertical'
52 62
           returning[0] = this.origin[0]
53 63
         } else {
@@ -58,10 +68,8 @@ export default class BrushSession extends BaseSession {
58 68
         this.previous = returning
59 69
         this.points.push(vec.sub(returning, this.origin))
60 70
       }
61
-    } else {
62
-      if (this.isLocked) {
63
-        this.isLocked = false
64
-      }
71
+    } else if (this.isLocked) {
72
+      this.isLocked = false
65 73
     }
66 74
 
67 75
     if (this.isLocked) {
@@ -72,18 +80,27 @@ export default class BrushSession extends BaseSession {
72 80
       }
73 81
     }
74 82
 
83
+    // Low pass the current input point against the previous one
75 84
     point = vec.med(this.previous, point)
76 85
 
86
+    this.previous = point
87
+
88
+    // Round the new point (helps keep our data tidy)
77 89
     const next = vec.round([...vec.sub(point, this.origin), pressure])
78 90
 
79
-    // Don't add duplicate points
91
+    // Don't add duplicate points. It's important to test against the
92
+    // adjusted (low-passed) point rather than the input point.
80 93
     if (vec.isEqual(this.last, next)) return
81 94
 
95
+    this.last = next
96
+
82 97
     this.points.push(next)
83 98
 
84
-    this.last = next
85
-    this.previous = point
99
+    // We draw a dot when the number of points is 1 or 2, so this guard
100
+    // prevents a "flash" of a dot when a user begins drawing a line.
101
+    if (this.points.length <= 2) return
86 102
 
103
+    // ...otherwise, update the points and update the shape's parents.
87 104
     const shape = getShape(data, snapshot.id) as DrawShape
88 105
     getShapeUtils(shape).setProperty(shape, 'points', [...this.points])
89 106
     updateParents(data, [shape.id])

Loading…
취소
저장