ソースを参照

adds pressure sensitivity

main
Steve Ruiz 4年前
コミット
8623958e74

+ 1
- 1
components/canvas/defs.tsx ファイルの表示

28
 }
28
 }
29
 
29
 
30
 const Def = memo(function Def({ id }: { id: string }) {
30
 const Def = memo(function Def({ id }: { id: string }) {
31
-  const shape = useSelector(({ data }) => getPage(data).shapes[id])
31
+  const shape = useSelector((s) => getPage(s.data).shapes[id])
32
   if (!shape) return null
32
   if (!shape) return null
33
   return getShapeUtils(shape).render(shape)
33
   return getShapeUtils(shape).render(shape)
34
 })
34
 })

+ 2
- 4
components/canvas/shape.tsx ファイルの表示

15
 }
15
 }
16
 
16
 
17
 function Shape({ id, isSelecting, parentPoint }: ShapeProps) {
17
 function Shape({ id, isSelecting, parentPoint }: ShapeProps) {
18
-  const shape = useSelector(({ data }) => getPage(data).shapes[id])
18
+  const shape = useSelector((s) => getPage(s.data).shapes[id])
19
 
19
 
20
   const rGroup = useRef<SVGGElement>(null)
20
   const rGroup = useRef<SVGGElement>(null)
21
 
21
 
25
   // may sometimes run before the hook in the Page component, which means
25
   // may sometimes run before the hook in the Page component, which means
26
   // a deleted shape will still be pulled here before the page component
26
   // a deleted shape will still be pulled here before the page component
27
   // detects the change and pulls this component.
27
   // detects the change and pulls this component.
28
-  if (!shape) {
29
-    return null
30
-  }
28
+  if (!shape) return null
31
 
29
 
32
   const isGroup = shape.type === ShapeType.Group
30
   const isGroup = shape.type === ShapeType.Group
33
 
31
 

+ 28
- 11
lib/shape-utils/draw.tsx ファイルの表示

157
 
157
 
158
 export default draw
158
 export default draw
159
 
159
 
160
+const simulatePressureSettings = {
161
+  simulatePressure: true,
162
+}
163
+
164
+const realPressureSettings = {
165
+  easing: (t: number) => t * t,
166
+  simulatePressure: false,
167
+  // start: { taper: 1 },
168
+  // end: { taper: 1 },
169
+}
170
+
160
 function renderPath(shape: DrawShape, style: ShapeStyles) {
171
 function renderPath(shape: DrawShape, style: ShapeStyles) {
161
   const styles = getShapeStyle(style)
172
   const styles = getShapeStyle(style)
162
 
173
 
163
-  pathCache.set(
164
-    shape.points,
165
-    getSvgPathFromStroke(
166
-      getStroke(shape.points, {
167
-        size: 1 + +styles.strokeWidth * 2,
168
-        thinning: 0.85,
169
-        end: { taper: +styles.strokeWidth * 20 },
170
-        start: { taper: +styles.strokeWidth * 20 },
171
-      })
172
-    )
173
-  )
174
+  if (shape.points.length < 2) {
175
+    pathCache.set(shape.points, '')
176
+    return
177
+  }
178
+
179
+  const options =
180
+    shape.points[1][2] === 0.5 ? simulatePressureSettings : realPressureSettings
181
+
182
+  const stroke = getStroke(shape.points, {
183
+    size: 1 + +styles.strokeWidth * 2,
184
+    thinning: 0.85,
185
+    end: { taper: +styles.strokeWidth * 20 },
186
+    start: { taper: +styles.strokeWidth * 20 },
187
+    ...options,
188
+  })
189
+
190
+  pathCache.set(shape.points, getSvgPathFromStroke(stroke))
174
 }
191
 }

+ 1
- 0
lib/shape-utils/index.tsx ファイルの表示

8
   ShapeBinding,
8
   ShapeBinding,
9
   Mutable,
9
   Mutable,
10
   ShapeByType,
10
   ShapeByType,
11
+  Data,
11
 } from 'types'
12
 } from 'types'
12
 import * as vec from 'utils/vec'
13
 import * as vec from 'utils/vec'
13
 import {
14
 import {

+ 11
- 5
state/inputs.tsx ファイルの表示

19
       pointerId: touch.identifier,
19
       pointerId: touch.identifier,
20
       origin: [touch.clientX, touch.clientY],
20
       origin: [touch.clientX, touch.clientY],
21
       point: [touch.clientX, touch.clientY],
21
       point: [touch.clientX, touch.clientY],
22
+      pressure: 0.5,
22
       shiftKey,
23
       shiftKey,
23
       ctrlKey,
24
       ctrlKey,
24
       metaKey: isDarwin() ? metaKey : ctrlKey,
25
       metaKey: isDarwin() ? metaKey : ctrlKey,
42
       ...prev,
43
       ...prev,
43
       pointerId: touch.identifier,
44
       pointerId: touch.identifier,
44
       point: [touch.clientX, touch.clientY],
45
       point: [touch.clientX, touch.clientY],
46
+      pressure: 0.5,
45
       shiftKey,
47
       shiftKey,
46
       ctrlKey,
48
       ctrlKey,
47
       metaKey: isDarwin() ? metaKey : ctrlKey,
49
       metaKey: isDarwin() ? metaKey : ctrlKey,
61
     const info = {
63
     const info = {
62
       target,
64
       target,
63
       pointerId: e.pointerId,
65
       pointerId: e.pointerId,
64
-      origin: [e.clientX, e.clientY, e.pressure],
65
-      point: [e.clientX, e.clientY, e.pressure],
66
+      origin: [e.clientX, e.clientY],
67
+      point: [e.clientX, e.clientY],
68
+      pressure: e.pressure || 0.5,
66
       shiftKey,
69
       shiftKey,
67
       ctrlKey,
70
       ctrlKey,
68
       metaKey: isDarwin() ? metaKey : ctrlKey,
71
       metaKey: isDarwin() ? metaKey : ctrlKey,
81
     const info = {
84
     const info = {
82
       target,
85
       target,
83
       pointerId: e.pointerId,
86
       pointerId: e.pointerId,
84
-      origin: [e.clientX, e.clientY, e.pressure],
85
-      point: [e.clientX, e.clientY, e.pressure],
87
+      origin: [e.clientX, e.clientY],
88
+      point: [e.clientX, e.clientY],
89
+      pressure: e.pressure || 0.5,
86
       shiftKey,
90
       shiftKey,
87
       ctrlKey,
91
       ctrlKey,
88
       metaKey: isDarwin() ? metaKey : ctrlKey,
92
       metaKey: isDarwin() ? metaKey : ctrlKey,
100
     const info = {
104
     const info = {
101
       ...prev,
105
       ...prev,
102
       pointerId: e.pointerId,
106
       pointerId: e.pointerId,
103
-      point: [e.clientX, e.clientY, e.pressure],
107
+      point: [e.clientX, e.clientY],
108
+      pressure: e.pressure || 0.5,
104
       shiftKey,
109
       shiftKey,
105
       ctrlKey,
110
       ctrlKey,
106
       metaKey: isDarwin() ? metaKey : ctrlKey,
111
       metaKey: isDarwin() ? metaKey : ctrlKey,
123
       ...prev,
128
       ...prev,
124
       origin: prev?.origin || [e.clientX, e.clientY],
129
       origin: prev?.origin || [e.clientX, e.clientY],
125
       point: [e.clientX, e.clientY],
130
       point: [e.clientX, e.clientY],
131
+      pressure: e.pressure || 0.5,
126
       shiftKey,
132
       shiftKey,
127
       ctrlKey,
133
       ctrlKey,
128
       metaKey: isDarwin() ? metaKey : ctrlKey,
134
       metaKey: isDarwin() ? metaKey : ctrlKey,

+ 15
- 6
state/sessions/draw-session.ts ファイルの表示

11
 export default class BrushSession extends BaseSession {
11
 export default class BrushSession extends BaseSession {
12
   origin: number[]
12
   origin: number[]
13
   previous: number[]
13
   previous: number[]
14
+  last: number[]
14
   points: number[][]
15
   points: number[][]
15
   snapshot: DrawSnapshot
16
   snapshot: DrawSnapshot
16
   isLocked: boolean
17
   isLocked: boolean
20
     super(data)
21
     super(data)
21
     this.origin = point
22
     this.origin = point
22
     this.previous = point
23
     this.previous = point
24
+    this.last = point
23
     this.points = []
25
     this.points = []
24
     this.snapshot = getDrawSnapshot(data, id)
26
     this.snapshot = getDrawSnapshot(data, id)
25
 
27
 
28
     getShapeUtils(shape).translateTo(shape, point)
30
     getShapeUtils(shape).translateTo(shape, point)
29
   }
31
   }
30
 
32
 
31
-  update = (data: Data, point: number[], isLocked = false) => {
33
+  update = (
34
+    data: Data,
35
+    point: number[],
36
+    pressure: number,
37
+    isLocked = false
38
+  ) => {
32
     const { snapshot } = this
39
     const { snapshot } = this
33
 
40
 
34
     const delta = vec.vec(this.origin, point)
41
     const delta = vec.vec(this.origin, point)
63
       }
70
       }
64
     }
71
     }
65
 
72
 
66
-    if (this.previous) {
67
-      point = vec.med(this.previous, point)
68
-    }
73
+    point = vec.med(this.previous, point)
74
+
75
+    const next = [...vec.sub(point, this.origin), pressure]
69
 
76
 
70
-    prevEndPoint = [...point]
71
-    const next = vec.sub(point, this.origin)
77
+    // Don't add duplicate points
78
+    if (vec.isEqual(this.last, next)) return
72
 
79
 
73
     this.points.push(next)
80
     this.points.push(next)
81
+
82
+    this.last = next
74
     this.previous = point
83
     this.previous = point
75
 
84
 
76
     const shape = getShape(data, snapshot.id) as DrawShape
85
     const shape = getShape(data, snapshot.id) as DrawShape

+ 7
- 1
state/state.ts ファイルの表示

981
       session.update(
981
       session.update(
982
         data,
982
         data,
983
         screenToWorld(inputs.pointer.point, data),
983
         screenToWorld(inputs.pointer.point, data),
984
+        payload.pressure,
984
         payload.shiftKey
985
         payload.shiftKey
985
       )
986
       )
986
     },
987
     },
987
     updateDrawSession(data, payload: PointerInfo) {
988
     updateDrawSession(data, payload: PointerInfo) {
988
-      session.update(data, screenToWorld(payload.point, data), payload.shiftKey)
989
+      session.update(
990
+        data,
991
+        screenToWorld(payload.point, data),
992
+        payload.pressure,
993
+        payload.shiftKey
994
+      )
989
     },
995
     },
990
 
996
 
991
     // Arrow
997
     // Arrow

+ 1
- 0
types.ts ファイルの表示

248
   pointerId: number
248
   pointerId: number
249
   origin: number[]
249
   origin: number[]
250
   point: number[]
250
   point: number[]
251
+  pressure: number
251
   shiftKey: boolean
252
   shiftKey: boolean
252
   ctrlKey: boolean
253
   ctrlKey: boolean
253
   metaKey: boolean
254
   metaKey: boolean

読み込み中…
キャンセル
保存