Crafty.device
与诸如平板或电话之类的设备相关的方法
属性
方法
Back to top
Crafty.device.deviceMotion()
public Crafty.device.deviceMotion(Function callback)
- callback
只要设备运动发生变化,回调方法就执行一次。
用标准化的设备运动数据做一些事情:
{
acceleration : 'Grab the acceleration including gravity from the results',
rawAcceleration : 'Display the raw acceleration data',
facingUp : 'Z is the acceleration in the Z axis, and if the device is facing up or down',
tiltLR : 'Convert the value from acceleration to degrees. acceleration.x is the acceleration according to gravity, we'll assume we're on Earth and divide by 9.81 (earth gravity) to get a percentage value, and then multiply that by 90 to convert to degrees.',
tiltFB : 'Convert the value from acceleration to degrees.'
}
例子
// Get DeviceMotion event normalized data.
Crafty.device.deviceMotion(function(data){
Crafty.log('data.moAccel : '+data.rawAcceleration+', data.moCalcTiltLR : '+Math.round(data.tiltLR)+', data.moCalcTiltFB : '+Math.round(data.tiltFB)+'');
});
查看浏览器支持情况 http://caniuse.com/#search=motion.
Back to top
Crafty.device.deviceOrientation()
public Crafty.device.deviceOrientation(Function callback)
- callback
一旦设备方向改变,回调方法将执行一次。
用标准化的设备定向数据做一些事情:
{
tiltLR : 'gamma -- the angle in degrees the device is tilted left-to-right.',
tiltFB : 'beta -- the angle in degrees the device is tilted front-to-back',
dir : 'alpha -- the direction the device is facing according to the compass',
motUD : 'The angle's values increase as you tilt the device to the right or towards you.'
}
例子
// Get DeviceOrientation event normalized data.
Crafty.device.deviceOrientation(function(data){
Crafty.log('data.tiltLR : '+Math.round(data.tiltLR)+', data.tiltFB : '+Math.round(data.tiltFB)+', data.dir : '+Math.round(data.dir)+', data.motUD : '+data.motUD+'');
});
查看浏览器支持情况 http://caniuse.com/#search=device
Back to top
Crafty.mobile
检查是否在手持设备上运行。
如果 Crafty.mobile 为 true 引擎会做这些事:
- 设置视口在设备上的最大宽度和高度
- 设置 Crafty.stage.fullscreen 为 true
- 隐藏 window 滚动条