-
Notifications
You must be signed in to change notification settings - Fork 4
/
gesture_js.go
186 lines (164 loc) · 3.6 KB
/
gesture_js.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"context"
"math"
"syscall/js"
"time"
webgl "github.com/seqsense/webgl-go"
)
const (
doubleTapInterval = 250 * time.Millisecond
tapMaxEndDuration = 150 * time.Millisecond
)
type gestureMode int
const (
gestureNone gestureMode = iota
gestureRotate
gestureWheel
gestureDrag
gestureMove
)
type gesture struct {
canvas webgl.Canvas
pointer0 webgl.TouchEvent
onClick func(webgl.MouseEvent)
onMouseUp func(webgl.MouseEvent)
onMouseDrag func(webgl.MouseEvent)
onMouseDown func(webgl.MouseEvent)
onWheel func(e webgl.WheelEvent)
mode gestureMode
distance0 float64
lastStart time.Time
tapCnt int
clickCancel func()
}
func (g *gesture) fromLastEnd(now time.Time) time.Duration {
return now.Sub(g.lastStart)
}
func (g *gesture) touchEnd(e webgl.TouchEvent) {
e.PreventDefault()
e.StopPropagation()
now := time.Now()
switch g.mode {
case gestureNone:
if g.fromLastEnd(now) < tapMaxEndDuration {
if g.clickCancel != nil {
g.clickCancel()
}
ctx, cancel := context.WithCancel(context.Background())
g.clickCancel = cancel
tapCnt, pointer0 := g.tapCnt, g.pointer0
go func() {
defer cancel()
select {
case <-time.After(doubleTapInterval):
switch tapCnt {
case 1:
pointer0.ShiftKey = true
case 2:
pointer0.AltKey = true
}
g.onClick(g.touchToMouse(pointer0, 0))
case <-ctx.Done():
}
}()
}
case gestureRotate:
g.onMouseUp(g.touchToMouse(g.pointer0, 0))
case gestureDrag:
g.onMouseUp(g.touchToMouse(g.pointer0, 1))
}
g.mode = gestureNone
}
func (g *gesture) touchMove(e webgl.TouchEvent) {
e.PreventDefault()
e.StopPropagation()
now := time.Now()
if g.mode == gestureNone {
n := len(e.Touches)
if g.tapCnt == 1 {
n = 3
}
switch n {
case 1:
if g.fromLastEnd(now) > doubleTapInterval {
g.onMouseDown(g.touchToMouse(g.pointer0, 0))
g.mode = gestureRotate
}
case 2:
g.mode = gestureWheel
case 3:
g.onMouseDown(g.touchToMouse(g.pointer0, 1))
g.mode = gestureDrag
}
}
switch g.mode {
case gestureRotate:
g.onMouseDrag(g.touchToMouse(e, 0))
case gestureWheel:
if len(e.Touches) != 2 {
break
}
d := math.Hypot(
float64(e.Touches[0].ClientX-e.Touches[1].ClientX),
float64(e.Touches[0].ClientY-e.Touches[1].ClientY),
)
we := webgl.WheelEvent{
MouseEvent: webgl.MouseEvent{
UIEvent: webgl.UIEvent{
Event: webgl.NewEvent("WheelEvent"),
},
AltKey: e.AltKey,
CtrlKey: e.CtrlKey,
ShiftKey: e.ShiftKey,
},
DeltaY: (g.distance0 - d) / 10,
}
g.onWheel(we)
g.distance0 = d
case gestureDrag:
g.onMouseDrag(g.touchToMouse(e, 1))
}
if len(e.Touches) > 0 {
g.pointer0 = e
}
}
func (g *gesture) touchStart(e webgl.TouchEvent) {
e.PreventDefault()
e.StopPropagation()
now := time.Now()
if g.clickCancel != nil {
g.clickCancel()
g.clickCancel = nil
}
switch len(e.Touches) {
case 1:
if g.fromLastEnd(now) < doubleTapInterval {
g.tapCnt++
} else {
g.tapCnt = 0
}
g.lastStart = now
case 2:
g.distance0 = math.Hypot(
float64(e.Touches[0].ClientX-e.Touches[1].ClientX),
float64(e.Touches[0].ClientY-e.Touches[1].ClientY),
)
case 3:
}
g.pointer0 = e
}
func (g *gesture) touchToMouse(e webgl.TouchEvent, button webgl.MouseButton) webgl.MouseEvent {
bcr := js.Value(g.canvas).Call("getBoundingClientRect")
x, y := e.Touches[0].ClientX, e.Touches[0].ClientY
cx, cy := bcr.Get("x").Int(), bcr.Get("y").Int()
return webgl.MouseEvent{
UIEvent: e.UIEvent,
OffsetX: x - cx,
OffsetY: y - cy,
Button: button,
AltKey: e.AltKey,
CtrlKey: e.CtrlKey,
ShiftKey: e.ShiftKey,
}
}