XRSession: selectstart event - Web APIs 编辑
Secure context
This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
The WebXR event selectstart
is sent to an XRSession
when the user begins a primary action on one of its input sources. Primary actions include things like users pressing triggers or buttons, tapping a touchpad, speaking a command, or performing a recognizable gesture when using a video tracking system or handheld controller with an accelerometer.
Bubbles | Yes |
---|---|
Cancelable | No |
Interface | XRInputSourceEvent |
Event handler property | onselectstart |
For details on how the selectstart
, select
, and selectend
events work, and how you should react to them, see Primary actions in Inputs and input sources.
Examples
The following example uses addEventListener()
to establish handlers for the selection events: selectstart
, selectend
, and select
. This snippet is the core of an event handler to allow the user to grab objects in the scene and move them around.
In this case, a single function is used to handle all three events, allowing them to share certain code that's the same regardless of which of the three events is received. Only after completing those tasks does the onSelectionEvent()
function below dispatch the action out to a specialized function to handle things.
After checking to ensure that the received event is a tracked-pointer
event (the only kind we handle here), the target ray's pose is obtained using getPose()
.
If the target ray pose was fetched successfully, the code then uses the value of Event
property type
to route control to an appropriate function to handle the event which arrived:
- For
selectstart
events, amyBeginTracking()
function is called with the target ray pose'smatrix
. ThemyBeginTracking()
function would presumably start the presentation of the object-dragging process, using the transform to perform a hit test, determining which object to pick up.myBeginTracking()
returns an object representing the object the user has begun to drag. - Upon receiving a
select
event, themyDropObject()
function is called with the target object and the current target ray pose transform as inputs. This places the object into its new position in the world and triggers any effects that may arise from doing so (like scheduling an animation of a splash if dropped in water, etc). - The
selectend
event results in amyStopTracking()
function being called with the object being dragged and the final target ray pose's transform.
xrSession.addEventListener("selectstart", onSelectionEvent);
xrSession.addEventListener("select", onSelectionEvent);
xrSession.addEventListener("selectend", onSelectionEvent);
function onSelectionEvent(event) {
let source = event.inputSource;
let targetObj = null;
if (source.targetRayMode != "tracked-pointer") {
return;
}
let targetRayPose = event.frame.getPose(source.targetRaySpace, myRefSpace);
if (!targetRayPose) {
return;
}
switch(event.type) {
case "selectstart":
targetObj = myBeginTracking(targetRayPose.matrix);
break;
case "select":
myDropObject(targetObj, targetRayPose.matrix);
break;
case "selectend":
myStopTracking(targetObj, targetRayPose.matrix);
break;
}
}
You can of course also set up a handler for selectend
events by setting the XRSession
object's onselectend
event handler property to a function that handles the event:
xrSession.onselectstart = onSelectionEvent;
xrSession.onselect = onSelectionEvent;
xrSession.onselectend = onSelectionEvent;
Specifications
Specification | Status | Comment |
---|---|---|
WebXR Device API The definition of 'selectstart event' in that specification. | Working Draft | Initial definition. |
Browser compatibility
BCD tables only load in the browser
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论