Skip to content

Latest commit

 

History

History
64 lines (47 loc) · 2.61 KB

File metadata and controls

64 lines (47 loc) · 2.61 KB

DeviceMotionEvent

Namespace: SpawnDev.SpawnJS.JSObjects
Inherits: Event
Source: JSObjects/DeviceMotionEvent.cs
MDN Reference: DeviceMotionEvent on MDN

The DeviceMotionEvent interface provides web developers with information about the speed of changes for the device's position and orientation. https://developer.mozilla.org/en-US/docs/Web/API/DeviceMotionEvent

Constructors

Signature Description
DeviceMotionEvent(SpawnJSObjectReference _ref) Default deserialize constructor

Properties

Property Type Access Description
Acceleration DeviceMotionXYZ get An object giving the acceleration of the device on the three axis X, Y and Z. Acceleration is expressed in m/s².
AccelerationIncludingGravity DeviceMotionXYZ get An object giving the acceleration of the device on the three axis X, Y and Z with the effect of gravity. Acceleration is expressed in m/s².
RotationRate DeviceMotionAlphaBetaGamma get An object giving the rate of change of the device's orientation on the three orientation axis alpha, beta and gamma. Rotation rate is expressed in degrees per seconds.
Interval double get A number representing the interval of time, in milliseconds, at which data is obtained from the device.

Examples

SpawnDev.SpawnJS Mapping Note: The JavaScript examples below show the standard Web API usage. In SpawnDev.SpawnJS, the same API is available with C# conventions:

  • Properties and methods use PascalCase (e.g., readyState becomes ReadyState)
  • Events use ActionEvent with +=/-= (e.g., addEventListener("click", fn) becomes OnClick += handler)
  • Async methods return Task<T> instead of Promise<T>
  • Objects should be disposed with using statements
  • Access via JS.Get<DeviceMotionEvent>(...) or constructor new DeviceMotionEvent(...)
window.addEventListener("devicemotion", (event) => {
  console.log(`${event.acceleration.x} m/s2`);
});

See full example on MDN

Examples

JavaScript (MDN):

window.addEventListener("devicemotion", (event) => {
  console.log(`${event.acceleration.x} m/s2`);
});

C# (SpawnDev.SpawnJS):

// Requires: builder.Services.AddSpawnJSRuntime();
// Inject SpawnJSRuntime in your component or service:
// [Inject] SpawnJSRuntime JS { get; set; }

window.addEventListener("devicemotion", (event) => {
Console.WriteLine($"{event.Acceleration.x} m/s2");
});