快速入门:使用加速计响应用户移动 (HTML)

[ 本文适用于编写 Windows 运行时应用的 Windows 8.x 和 Windows Phone 8.x 开发人员。如果你要针对 Windows 10 进行开发,请参阅 最新文档 ]

可以使用加速计和用 JavaScript 编写的应用响应用户移动。基于加速计的应用通常仅使用一个或两个轴进行输入。但是,它也可以使用振动事件作为另一个输入源。

目标: 完成此快速入门后,你将了解如何使用加速计检测运动中的变化。

先决条件

你应当熟悉 HTML、JavaScript 和事件。

你使用的设备或仿真器必须支持加速计。

完成所需时间: 15 分钟.

说明

1. 打开 Microsoft Visual Studio

打开 Visual Studio 的一个实例。

2. 创建新项目

创建新项目,从“JavaScript/应用商店应用”项目类型中选择“空白应用程序”****。

3. 替换 JavaScript 代码

打开你项目的 default.js 文件,并使用下列内容替换现有的代码。

// For an introduction to the Blank template, see the following documentation:
// https://go.microsoft.com/fwlink/p/?linkid=232509
(function () {
    "use strict";
    var accelerometer;

    var app = WinJS.Application;

    // This function responds to all app activations.
    app.onactivated = function (eventObject) {
        if (eventObject.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.launch) {
            if (accelerometer == null) {
                accelerometer = Windows.Devices.Sensors.Accelerometer.getDefault();

                // Establish the report interval
                var minimumReportInterval = accelerometer.minimumReportInterval;
                var reportInterval = minimumReportInterval > 16 ? minimumReportInterval : 16;
                accelerometer.reportInterval = reportInterval;

                // Establish the event handler
                accelerometer.addEventListener("readingchanged", onDataChanged);
            }
            WinJS.UI.processAll();
        }
    };

    // This function is called each time an accelerometer event
    // is fired by the driver.
    function onDataChanged(e) {
        var reading = e.reading;
        var accelX = reading.accelerationX;
        var accelY = reading.accelerationY;
        var accelZ = reading.accelerationZ;

        id('eventOutputX').innerHTML = accelX.toFixed(2);
        id('eventOutputY').innerHTML = accelY.toFixed(2);
        id('eventOutputZ').innerHTML = accelZ.toFixed(2);
    }

    // This function is invoked within onDataChanged to
    // retrieve the given identifier from the HTML document.
    function id(elementId) {
        return document.getElementById(elementId);
    }

    app.start();
})();

4. 为应用添加 HTML

打开 Windows 和 Windows Phone 项目的 default.html 文件,并将以下 HTML 复制到该文件的 BODY 标记内。


    <div class="item" id="scenarioOutput">
    X: <a id="eventOutputX">no data</a>
    <br />
    Y: <a id="eventOutputY">no data</a>
    <br />
    Z: <a id="eventOutputZ">no data</a>
    <br />
    </div>

5. 生成、部署并运行应用

按 F5 或选择“调试”>“启动调试”****来构建、部署并运行应用。

应用运行后,你可以通过迁移设备或使用仿真器工具更改加速计的值。

6. 停止应用

  1. 按 ALT+Tab 切换到 Visual Studio。
  2. 按 Shift+F5 或选择“调试”>“停止调试”****来停止应用。

摘要和后续步骤

前面的示例演示了,只需要写入极少的代码即可将加速计输入集成到你的应用。

该应用在 onactivated 函数中建立了与默认加速计的连接。这通过下面一行实现。

accelerometer = Windows.Devices.Sensors.Accelerometer.getDefault();

onDataChanged 函数中捕获新加速计数据。每次传感器驱动程序从传感器接收到新数据时,它都将使用此函数(或事件处理程序)将此值传递到你的应用中。应用在下行中注册此事件处理程序。

accelerometer.addEventListener("readingchanged", onDataChanged);

通过对下面显示的 DOM 元素进行更新,将这些新值写到屏幕上。

    <div class="item" id="scenarioOutput">
    X: <a id="eventOutputX">no data</a>
    <br />
    Y: <a id="eventOutputY">no data</a>
    <br />
    Z: <a id="eventOutputZ">no data</a>
    <br />
    </div>

该应用在 onactivated 函数中建立了报告间隔。此代码检索设备支持的最短间隔,并将它与所请求的间隔 16 毫秒(大约 60-Hz 刷新率)进行比较。如果支持的最短间隔大于所请求的间隔,则此代码会将报告间隔设置为所支持的最短间隔。否则,它会将报告间隔设置为所请求的间隔。

var minimumReportInterval = accelerometer.minimumReportInterval;
var reportInterval = minimumReportInterval > 16 ? minimumReportInterval : 16;
accelerometer.reportInterval = reportInterval;

如果你在编写简单应用,则后续步骤通常涉及到将加速计输入与图形输出相集成。

例如,你可以使用显示用户的移动的图形显示创建步程计。

相关主题

Accelerometer class

加速计示例