Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 970951 of VRPose

  • Revision slug: Web/API/VRPositionState
  • Revision title: VRPositionState
  • Revision id: 970951
  • Created:
  • Creator: chrisdavidmills
  • Is current revision? No
  • Comment

Revision Content

{{APIRef("WebVR API")}}{{SeeCompatTable}}

The VRPositionState interface of the WebVR API represents the position state at a given timestamp (which includes orientation, position, velocity, and acceleration information.)

This interface is accessible through the {{domxref("PositionSensorVRDevice.getState()")}} method.

Properties

{{domxref("VRPositionState.timeStamp")}} {{readonlyInline}}
Returns the current time stamp of the system — a monotonically increasing value useful for determining if position data has been updated, and what order updates have occured in.
{{domxref("VRPositionState.hasPosition")}} {{readonlyInline}}
A boolean indicating whether the {{domxref("VRPositionState.position")}} property is valid (i.e. if the hardware is currently registering a valid position). If it is false, the position property will return null.
{{domxref("VRPositionState.position")}} {{readonlyInline}}
Returns the current position of the sensor relative to the head mounted display, as a 3D vector ({{domxref("DOMPoint")}} value.)
{{domxref("VRPositionState.linearVelocity")}} {{readonlyInline}}
Returns the current linear velocity of the sensor.
{{domxref("VRPositionState.linearAcceleration")}} {{readonlyInline}}
Returns the current linear acceleration of the sensor.
{{domxref("VRPositionState.hasOrientation")}} {{readonlyInline}}
A boolean indicating whether the {{domxref("VRPositionState.orientation")}} property is valid (i.e. if the hardware is currently registering a valid orientation). If it is false, the orientation property will return null.
{{domxref("VRPositionState.orientation")}} {{readonlyInline}}
Returns the current orientation of the sensor relative to the head mounted display, as a quarternion value (currently represented by a {{domxref("DOMPoint")}} value, but will be updated when DOMQuarternion is implemented.)
{{domxref("VRPositionState.angularVelocity")}} {{readonlyInline}}
Returns the current angular velocity of the sensor.
{{domxref("VRPositionState.angularAcceleration")}} {{readonlyInline}}
Returns the current angular acceleration of the sensor.

Examples

The following example is taken from our positionsensorvrdevice demo, which uses the WebVR API to update the view of a simple {{domxref("CanvasRenderingContext2D","2D canvas")}} scene on each frame of a {{domxref("window.requestAnimationFrame()","requestAnimationFrame")}} loop.

function setView() {
  var posState = gPositionSensor.getState();
  if(posState.hasPosition) {
    posPara.textContent = 'Position: x' + roundToTwo(posState.position.x) + " y"
                                + roundToTwo(posState.position.y) + " z"
                                + roundToTwo(posState.position.z);
    xPos = -posState.position.x * WIDTH * 2;
    yPos = posState.position.y * HEIGHT * 2;
    if(-posState.position.z > 0.01) {
      zPos = -posState.position.z;
    } else {
      zPos = 0.01;
    }
  }

  if(posState.hasOrientation) {
    orientPara.textContent = 'Orientation: x' + roundToTwo(posState.orientation.x) + " y"
                                + roundToTwo(posState.orientation.y) + " z"
                                + roundToTwo(posState.orientation.z);
    xOrient = posState.orientation.x * WIDTH;
    yOrient = -posState.orientation.y * HEIGHT * 2;
    zOrient = posState.orientation.z * 180;

  }
}

Here we are grabbing a {{domxref("VRPositionState")}} object using {{domxref("PositionSensorVRDevice.getState()")}} and storing it in posState. We then check to make sure that position and orientation info is present in the current frame using {{domxref("VRPositionState.hasPosition")}} and {{domxref("VRPositionState.hasOrientation")}} (these return null if, for example the head mounted display is turned off or not pointing at the position sensor, which would cause an error.)

We then output the x, y and z position and orientation values for informational purposes, and use those values to update the xPos, yPos, and zPos variables, which are used to update the scene rendering on each frame.

Specifications

Specification Status Comment
{{SpecName('WebVR', '#vrpositionstate', 'VRPositionState')}} {{Spec2('WebVR')}} Initial definition

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support {{CompatVersionUnknown}}[1] {{CompatGeckoDesktop(39)}}[2] {{CompatNo}} {{CompatNo}} {{CompatNo}}
Feature Android Firefox Mobile (Gecko) Firefox OS (Gecko) IE Phone Opera Mobile Safari Mobile Chrome for Android
Basic support {{CompatNo}} {{CompatGeckoMobile(39)}}[2]
{{CompatGeckoMobile(44)}}[3]
{{CompatNo}} {{CompatNo}} {{CompatNo}} {{CompatNo}} {{CompatNo}}
  • [1] The support in Chrome is currently experimental. To find information on Chrome's WebVR implementation status including supporting builds, check out Bringing VR to Chrome by Brandon Jones.
  • [2] The support for this feature is currently disabled by default in Firefox. To enable WebVR support in Firefox Nightly/Developer Edition, you can go to about:config and enable the dom.vr* prefs. A better option however is to install the WebVR Enabler Add-on, which does this for you and sets up other necessary parts of the environment.
  • [3] The dom.vr* prefs are enabled by default at this point, in Nightly/Aurora editions.

See also

Revision Source

<div>{{APIRef("WebVR API")}}{{SeeCompatTable}}</div>

<p>The <strong><code>VRPositionState</code></strong> interface of the <a href="/en-US/docs/Web/API/WebVR_API">WebVR API</a> represents the position state at a given timestamp (which includes orientation, position, velocity, and acceleration information.)</p>

<p>This interface is accessible through the {{domxref("PositionSensorVRDevice.getState()")}} method.</p>

<h2 id="Properties">Properties</h2>

<dl>
 <dt>{{domxref("VRPositionState.timeStamp")}} {{readonlyInline}}</dt>
 <dd>Returns the current time stamp of the system — a monotonically increasing value useful for determining if position data has been updated, and what order updates have occured in.</dd>
 <dt>{{domxref("VRPositionState.hasPosition")}} {{readonlyInline}}</dt>
 <dd>A boolean indicating whether the {{domxref("VRPositionState.position")}} property is valid (i.e. if the hardware is currently registering a valid position). If it is <code>false</code>, the position property will return <code>null</code>.</dd>
 <dt>{{domxref("VRPositionState.position")}} {{readonlyInline}}</dt>
 <dd>Returns the current position of the sensor relative to the head mounted display, as a 3D vector ({{domxref("DOMPoint")}} value.)</dd>
 <dt>{{domxref("VRPositionState.linearVelocity")}} {{readonlyInline}}</dt>
 <dd>Returns the current linear velocity of the sensor.</dd>
 <dt>{{domxref("VRPositionState.linearAcceleration")}} {{readonlyInline}}</dt>
 <dd>Returns the current linear acceleration of the sensor.</dd>
 <dt>{{domxref("VRPositionState.hasOrientation")}} {{readonlyInline}}</dt>
 <dd>A boolean indicating whether the {{domxref("VRPositionState.orientation")}} property is valid (i.e. if the hardware is currently registering a valid orientation). If it is <code>false</code>, the orientation property will return <code>null</code>.</dd>
 <dt>{{domxref("VRPositionState.orientation")}} {{readonlyInline}}</dt>
 <dd>Returns the current orientation of the sensor relative to the head mounted display, as a quarternion value (currently represented by a {{domxref("DOMPoint")}} value, but will be updated when <code>DOMQuarternion</code> is implemented.)</dd>
 <dt>{{domxref("VRPositionState.angularVelocity")}} {{readonlyInline}}</dt>
 <dd>Returns the current angular velocity of the sensor.</dd>
 <dt>{{domxref("VRPositionState.angularAcceleration")}} {{readonlyInline}}</dt>
 <dd>Returns the current angular acceleration of the sensor.</dd>
</dl>

<h2 id="Examples">Examples</h2>

<p>The following example is taken from our <a href="https://mdn.github.io/webvr-tests/positionsensorvrdevice/">positionsensorvrdevice</a> demo, which uses the WebVR API to update the view of a simple {{domxref("CanvasRenderingContext2D","2D canvas")}} scene on each frame of a {{domxref("window.requestAnimationFrame()","requestAnimationFrame")}} loop.</p>

<pre class="brush: js">
function setView() {
  var posState = gPositionSensor.getState();
  if(posState.hasPosition) {
    posPara.textContent = 'Position: x' + roundToTwo(posState.position.x) + " y"
                                + roundToTwo(posState.position.y) + " z"
                                + roundToTwo(posState.position.z);
    xPos = -posState.position.x * WIDTH * 2;
    yPos = posState.position.y * HEIGHT * 2;
    if(-posState.position.z &gt; 0.01) {
      zPos = -posState.position.z;
    } else {
      zPos = 0.01;
    }
  }

  if(posState.hasOrientation) {
    orientPara.textContent = 'Orientation: x' + roundToTwo(posState.orientation.x) + " y"
                                + roundToTwo(posState.orientation.y) + " z"
                                + roundToTwo(posState.orientation.z);
    xOrient = posState.orientation.x * WIDTH;
    yOrient = -posState.orientation.y * HEIGHT * 2;
    zOrient = posState.orientation.z * 180;

  }
}</pre>

<p>Here we are grabbing a {{domxref("VRPositionState")}} object using {{domxref("PositionSensorVRDevice.getState()")}} and storing it in <code>posState</code>. We then check to make sure that position and orientation info is present in the current frame using {{domxref("VRPositionState.hasPosition")}} and {{domxref("VRPositionState.hasOrientation")}} (these return <code>null</code> if, for example the head mounted display is turned off or not pointing at the position sensor, which would cause an error.)</p>

<p>We then output the x, y and z position and orientation values for informational purposes, and use those values to update the <code>xPos</code>, <code>yPos</code>, and <code>zPos</code> variables, which are used to update the scene rendering on each frame.</p>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('WebVR', '#vrpositionstate', 'VRPositionState')}}</td>
   <td>{{Spec2('WebVR')}}</td>
   <td>Initial definition</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari (WebKit)</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatVersionUnknown}}<sup>[1]</sup></td>
   <td>{{CompatGeckoDesktop(39)}}<sup>[2]</sup></td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>Firefox OS (Gecko)</th>
   <th>IE Phone</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
   <th>Chrome for Android</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatGeckoMobile(39)}}<sup>[2]</sup><br />
    {{CompatGeckoMobile(44)}}<sup>[3]</sup></td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
 </tbody>
</table>
</div>

<ul>
 <li>[1] The support in Chrome is currently experimental. To find information on Chrome's WebVR implementation status including supporting builds, check out <a href="https://blog.tojicode.com/2014/07/bringing-vr-to-chrome.html">Bringing VR to Chrome</a> by Brandon Jones.</li>
 <li>[2] The support for this feature is currently disabled by default in Firefox. To enable WebVR support in Firefox Nightly/Developer Edition, you can go to <code>about:config</code> and enable the <code>dom.vr*</code> prefs. A better option however is to install the <a href="https://www.mozvr.com/downloads/webvr-addon-0.1.0.xpi">WebVR Enabler Add-on</a>, which does this for you and sets up other necessary parts of the <a href="/en-US/docs/Web/API/WebVR_API/WebVR_environment_setup">environment</a>.</li>
 <li>[3] The <code>dom.vr*</code> prefs are enabled by default at this point, in Nightly/Aurora editions.</li>
</ul>

<h2 id="See_also">See also</h2>

<ul>
 <li><a href="/en-US/docs/Web/API/WebVR_API">WebVR API homepage</a>.</li>
 <li><a href="https://mozvr.com/">MozVr.com</a> — demos, downloads, and other resources from the Mozilla VR team.</li>
</ul>
Revert to this revision