如何使DeviceOrientationEvent和DeviceMotionEvent在Safari上运行? [英] How do I get DeviceOrientationEvent and DeviceMotionEvent to work on Safari?

查看:720
本文介绍了如何使DeviceOrientationEvent和DeviceMotionEvent在Safari上运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的网站上实现DeviceOrientationEvent和DeviceMotionEvent以获得3D效果.但是,控制台不会记录任何信息,并且显然iOS 13需要用户设置权限才能开始执行此操作.我似乎无法弄清楚如何正确设置它.

我已经进行了一些研究,这是我发现的: https://github.com/w3c/deviceorientation/issues/57#issuecomment-498417027

不幸的是,在线提供的所有其他方法不再可用.

window.addEventListener('deviceorientation', function(event) {
    console.log(event.alpha + ' : ' + event.beta + ' : ' + event.gamma);
});

我收到以下错误消息:

[警告]在请求并授予许可之前,不会触发任何设备运动或方向事件.

解决方案

从iOS 13 beta 2开始,您需要调用DeviceOrientationEvent.requestPermission()来访问陀螺仪或加速度计.这将显示一个权限对话框,提示用户允许该站点的运动和方向访问.

请注意,如果您尝试在页面加载时自动调用它,则此方法将无效.用户需要采取一些措施(例如点击按钮)才能显示对话框.

此外,当前的实施方式似乎要求该网站启用https.

有关更多信息,请参见此页面

I'm trying to implement DeviceOrientationEvent and DeviceMotionEvent on my website for a 3D effect. However, the console doesn't log any info and apparently iOS 13 requires a user set permission to start doing this. I can't seem to figure out how to set it up properly.

I've done some research and this is what I found: https://github.com/w3c/deviceorientation/issues/57#issuecomment-498417027

All other methods provided online are not usable anymore sadly.

window.addEventListener('deviceorientation', function(event) {
    console.log(event.alpha + ' : ' + event.beta + ' : ' + event.gamma);
});

I get the following error message:

[Warning] No device motion or orientation events will be fired until permission has been requested and granted.

解决方案

As of iOS 13 beta 2, you need to call DeviceOrientationEvent.requestPermission() to access to gyroscope or accelerometer. This will present a permission dialog prompting the user to allow motion and orientation access for this site.

Note that this will not work if you try to call it automatically when the page loads. The user needs to take some action (like tapping a button) to be able to display the dialog.

Also, the current implementation seems to require that the site have https enabled.

For more information, see this page

这篇关于如何使DeviceOrientationEvent和DeviceMotionEvent在Safari上运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆