Skip to content

Commit 6da835b

Browse files
authored
Implement TouchEvent internals; add Touch
This change adds support for all TouchEvent fields, including those of TouchList type. It also comes with support for Touch (element of TouchList). References: - https://developer.mozilla.org/en-US/docs/Web/API/TouchEvent - https://developer.mozilla.org/en-US/docs/Web/API/TouchList - https://developer.mozilla.org/en-US/docs/Web/API/Touch GitHub-Pull-Request: #55
1 parent 662b7b8 commit 6da835b

File tree

1 file changed

+76
-2
lines changed

1 file changed

+76
-2
lines changed

events.go

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func wrapEvent(o *js.Object) Event {
9696
case js.Global.Get("TimeEvent"):
9797
return &TimeEvent{ev}
9898
case js.Global.Get("TouchEvent"):
99-
return &TouchEvent{ev}
99+
return &TouchEvent{BasicEvent: ev}
100100
case js.Global.Get("TrackEvent"):
101101
return &TrackEvent{ev}
102102
case js.Global.Get("TransitionEvent"):
@@ -308,7 +308,81 @@ type StorageEvent struct{ *BasicEvent }
308308
type SVGEvent struct{ *BasicEvent }
309309
type SVGZoomEvent struct{ *BasicEvent }
310310
type TimeEvent struct{ *BasicEvent }
311-
type TouchEvent struct{ *BasicEvent }
311+
312+
// TouchEvent represents an event sent when the state of contacts with a touch-sensitive
313+
// surface changes. This surface can be a touch screen or trackpad, for example. The event
314+
// can describe one or more points of contact with the screen and includes support for
315+
// detecting movement, addition and removal of contact points, and so forth.
316+
//
317+
// Reference: https://developer.mozilla.org/en-US/docs/Web/API/TouchEvent.
318+
type TouchEvent struct {
319+
*BasicEvent
320+
AltKey bool `js:"altKey"`
321+
CtrlKey bool `js:"ctrlKey"`
322+
MetaKey bool `js:"metaKey"`
323+
ShiftKey bool `js:"shiftKey"`
324+
}
325+
326+
// ChangedTouches lists all individual points of contact whose states changed between
327+
// the previous touch event and this one.
328+
//
329+
// Reference: https://developer.mozilla.org/en-US/docs/Web/API/TouchEvent/changedTouches.
330+
func (ev *TouchEvent) ChangedTouches() []*Touch {
331+
return touchListToTouches(ev.Get("changedTouches"))
332+
}
333+
334+
// TargetTouches lists all points of contact that are both currently in contact with the
335+
// touch surface and were also started on the same element that is the target of the event.
336+
//
337+
// Reference: https://developer.mozilla.org/en-US/docs/Web/API/TouchEvent/targetTouches.
338+
func (ev *TouchEvent) TargetTouches() []*Touch {
339+
return touchListToTouches(ev.Get("targetTouches"))
340+
}
341+
342+
// Touches lists all current points of contact with the surface, regardless of target
343+
// or changed status.
344+
//
345+
// Reference: https://developer.mozilla.org/en-US/docs/Web/API/TouchEvent/touches.
346+
func (ev *TouchEvent) Touches() []*Touch {
347+
return touchListToTouches(ev.Get("touches"))
348+
}
349+
350+
func touchListToTouches(tl *js.Object) []*Touch {
351+
out := make([]*Touch, tl.Length())
352+
for i := range out {
353+
out[i] = &Touch{Object: tl.Index(i)}
354+
}
355+
return out
356+
}
357+
358+
// Touch represents a single contact point on a touch-sensitive device. The contact point
359+
// is commonly a finger or stylus and the device may be a touchscreen or trackpad.
360+
//
361+
// Reference: https://developer.mozilla.org/en-US/docs/Web/API/Touch.
362+
type Touch struct {
363+
*js.Object
364+
Identifier int `js:"identifier"`
365+
ScreenX float64 `js:"screenX"`
366+
ScreenY float64 `js:"screenY"`
367+
ClientX float64 `js:"clientX"`
368+
ClientY float64 `js:"clientY"`
369+
PageX float64 `js:"pageX"`
370+
PageY float64 `js:"pageY"`
371+
RadiusX float64 `js:"radiusX"`
372+
RadiusY float64 `js:"radiusY"`
373+
RotationAngle float64 `js:"rotationAngle"`
374+
Force float64 `js:"force"`
375+
}
376+
377+
// Target returns the Element on which the touch point started when it was first placed
378+
// on the surface, even if the touch point has since moved outside the interactive area
379+
// of that element or even been removed from the document.
380+
//
381+
// Reference: https://developer.mozilla.org/en-US/docs/Web/API/Touch/target.
382+
func (t *Touch) Target() Element {
383+
return wrapElement(t.Get("target"))
384+
}
385+
312386
type TrackEvent struct{ *BasicEvent }
313387
type TransitionEvent struct{ *BasicEvent }
314388
type UIEvent struct{ *BasicEvent }

0 commit comments

Comments
 (0)