You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Meteor应用中访问加速度计?求同类插件及使用示例

Hey there! Since you're already using mdg:camera for your Meteor project's camera functionality, I’ve got a great option for accessing the accelerometer: the Cordova Device Motion plugin. It’s the most widely used solution for device motion (including accelerometer data) in the Meteor ecosystem, and it integrates smoothly with your existing setup.

Installation Steps

First, head to your Meteor project’s root directory and run this command to install the plugin:

meteor add cordova:cordova-plugin-device-motion@2.0.1

(Note: You can adjust the version number to match the latest stable release if needed—this is a current reliable version.)

Usage Examples

1. Get One-Time Acceleration Reading

In your client-side code (like client/main.js), you can fetch a single snapshot of the device’s acceleration data like this:

// Check if the accelerometer is supported on the device
if (typeof navigator.accelerometer !== 'undefined') {
  navigator.accelerometer.getCurrentAcceleration(
    // Success callback
    function(acceleration) {
      console.log(`X-axis acceleration: ${acceleration.x}`);
      console.log(`Y-axis acceleration: ${acceleration.y}`);
      console.log(`Z-axis acceleration: ${acceleration.z}`);
      console.log(`Timestamp: ${acceleration.timestamp}`);
      // You can render this data to your UI or use it for business logic here
    },
    // Error callback
    function(error) {
      console.error('Failed to get acceleration data:', error.message);
    }
  );
} else {
  console.warn('Accelerometer is not supported on this device');
}

2. Continuously Monitor Acceleration

If you need real-time updates of the device’s motion, use the watchAcceleration method to listen for ongoing changes:

let accelerationWatchId = null;

// Start monitoring acceleration
function startAccelerationWatch() {
  if (typeof navigator.accelerometer !== 'undefined') {
    // Configure update interval (in milliseconds) – here we update every 1 second
    const watchOptions = { frequency: 1000 };
    
    accelerationWatchId = navigator.accelerometer.watchAcceleration(
      function(acceleration) {
        console.log(`Real-time acceleration: X=${acceleration.x}, Y=${acceleration.y}, Z=${acceleration.z}`);
        // Update UI with the latest data
        document.getElementById('accel-display').textContent = 
          `X: ${acceleration.x.toFixed(2)} | Y: ${acceleration.y.toFixed(2)} | Z: ${acceleration.z.toFixed(2)}`;
      },
      function(error) {
        console.error('Failed to monitor acceleration:', error.message);
      },
      watchOptions
    );
  } else {
    console.warn('Accelerometer not supported on this device');
  }
}

// Stop monitoring acceleration
function stopAccelerationWatch() {
  if (accelerationWatchId !== null && typeof navigator.accelerometer !== 'undefined') {
    navigator.accelerometer.clearWatch(accelerationWatchId);
    accelerationWatchId = null;
    console.log('Acceleration monitoring stopped');
  }
}

// Start watching when the app loads, stop after 5 seconds as an example
Meteor.startup(() => {
  startAccelerationWatch();
  setTimeout(stopAccelerationWatch, 5000);
});
Important Notes
  • This plugin relies on Cordova, so it will only work on mobile devices (iOS/Android) or Cordova-compatible simulators. It won’t function in a standard web browser (you’ll see the "not supported" warning).
  • Make sure your Meteor project is set up for mobile platforms first—run meteor add-platform ios or meteor add-platform android if you haven’t already.

内容的提问来源于stack exchange,提问作者Phrosen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:46:55