🧑🎓 个人主页:《爱蹦跶的大A阿》
🔥当前正在更新专栏:《VUE》 、《JavaScript保姆级教程》、《krpano》、《Krpano中文文档》
这个插件使用陀螺仪和移动或平板设备的加速度传感器来控制krpano的观看/观看方向。
注意-在许多较新的浏览器版本中,使用HTTPS是从浏览器获得陀螺支持的核心要求。
主题
- 语法
- API集成
- 插件状态属性
- 插件的事件
- 插件操作
- iFrame的笔记
![](https://i-blog.csdnimg.cn/blog_migrate/4332e2d4d5238232afeed23c50ec5154.png)
![陀螺仪测试](https://i-blog.csdnimg.cn/blog_migrate/a6f477eb604c377ea2042991b045855b.png)
![](https://i-blog.csdnimg.cn/blog_migrate/a7fa8819caa172eabe6da619233edd61.png)
语法
<plugin name="gyro" keep="true"
url="gyro2.js"
enabled="false"
camroll="true"
absolute="false"
north="0"
friction="0.0"
touch_mode="full"
sensor="1"
sensor_mode="1"
softstart="0.5"
autocalibration="false"
desktopsupport="false"
onavailable=""
onunavailable=""
onenable=""
ondisable=""
ondenied=""
/>
动态或只读层/热点变量:
- isavailable
- isenabled
动作/功能:
- resetsensor (hlookat vlookat *)
Krpano API 集成
- 加载后,插件对象也将作为全局陀螺仪变量可用。
插件属性
|
属性名称 | 类型 | 默认值 |
---|---|---|
camroll | Boolean | true |
|
属性名称 | 类型 | 默认值 |
---|---|---|
absolute | Boolean | false |
|
属性名称 | 类型 | 默认值 |
---|---|---|
north | Number | 0.0 |
|
属性名称 | 类型 | 默认值 |
---|---|---|
friction | String / Number | 0.0 |
|
属性名称 | 类型 | 默认值 |
---|---|---|
touch_mode | String | "full" |
|
属性名称 | 类型 | 默认值 |
---|---|---|
sensor | int | 1 |
|
属性名称 | 类型 | 默认值 |
---|---|---|
sensor_mode | int | 1 |
|
插件状态属性(只读)
插件的事件
除了直接的插件事件,插件还会调度这些krpano事件:
<events gyro_onavailable=""
gyro_onunavailable=""
gyro_onenable=""
gyro_ondisable=""
gyro_ondenied=""
/>
插件操作
resetsensor(hlookat, vlookat*)
- 重置传感器跟踪以观察给定的视线(水平观察)方向。
- 当使用touch_mode=full时,还可以选择设置vlook(垂直观看)方向。
✨ 结语
当在一个“跨源”iframe中使用陀螺仪(这意味着外部页面和内部iframe页面在不同的源/域上)时,陀螺仪的使用可能会被浏览器阻止。
为了解决这个限制,可以在外部页面中“捕获”devicemotion事件,并通过postMessage API将其发送到内部iframe。
下面是外部(包含iframe的)网页的Javascript代码示例:
<script>
var pano_iframe_name = "enter_here_the_id_of_your_iframe_element";
window.addEventListener("devicemotion", function(e){ var iframe = document.getElementById(pano_iframe_name); if (iframe) iframe.contentWindow.postMessage({ type:"devicemotion", deviceMotionEvent:{ acceleration:{ x:e.acceleration.x, y:e.acceleration.y, z:e.acceleration.z }, accelerationIncludingGravity:{ x:e.accelerationIncludingGravity.x, y:e.accelerationIncludingGravity.y, z:e.accelerationIncludingGravity.z }, rotationRate:{ alpha:e.rotationRate.alpha, beta:e.rotationRate.beta, gamma:e.rotationRate.gamma }, interval:e.interval, timeStamp:e.timeStamp } }, "*"); });
</script>