ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

在 Vue 3 应用中用 VueUse useDeviceOrientation 响应式接入设备方向传感器

在 Vue 3 应用中用 VueUse useDeviceOrientation 响应式接入设备方向传感器 在 Vue 3 应用中用 VueUse useDeviceOrientation 响应式接入设备方向传感器【免费下载链接】airi Self hosted, you-owned Grok Companion, a container of souls of waifu, cyber livings to bring them into our worlds, wishing to achieve Neuro-samas altitude. Capable of realtime voice chat, Minecraft, Factorio playing. Web / macOS / Windows supported.项目地址: https://gitcode.com/GitHub_Trending/ai/airi导读useDeviceOrientation是 VueUse 在 Sensors传感器分类下提供的一个组合式函数它把浏览器的原生 DeviceOrientationEvent 与 apps/stage-web补充移动端传感器交互。读完本文你将掌握useDeviceOrientation的完整 API、状态语义、组件用法以及如何在 Vue 3 / Nuxt 3 项目中落地移动端朝向相关的实战方案。前置条件与依赖本文所述技巧适用于 Vue 3及以上或 Nuxt 3及以上项目。useDeviceOrientation属于 VueUse 核心库直接安装即可使用无需额外扩展包。在本仓库中各前端应用均通过 pnpm workspace 的 catalog 统一声明依赖例如 apps/stage-pocket/package.json、apps/stage-tamagotchi/package.json、apps/stage-web/package.json 中都存在vueuse/core: catalog:对应的版本目录定义在 pnpm-workspace.yamlvueuse/core: ^14.4.0 vueuse/shared: ^14.4.0也就是说当前仓库前端端到端使用的 VueUse 版本基线为 14.x仓库内所有应用包括 apps/component-calling 与 apps/ui-server-auth共享同一份 catalog 版本避免多应用间版本漂移。基本用法在script setup中直接导入并解构import { useDeviceOrientation } from vueuse/core const { isAbsolute, alpha, beta, gamma, } useDeviceOrientation()调用后即可获得四个响应式状态它们会在设备触发DeviceOrientationEvent时自动更新无需手动注册或移除事件监听也无需手动清理副作用——useDeviceOrientation会在组件卸载时自动注销底层事件监听。响应式状态语义状态类型说明isAbsoluteboolean表示设备是否提供绝对方向数据即相对于地球坐标系而非相对参考方向。alphanumber设备绕 z 轴竖直轴的旋转角度单位为度取值范围 0 到 360。betanumber设备绕 x 轴水平轴即前后俯仰的旋转角度单位为度取值范围 -180 到 180。gammanumber设备绕 y 轴水平轴即左右翻转/横滚的旋转角度单位为度取值范围 -90 到 90。关于状态字段更详细的定义可查阅 MDN 上 DeviceOrientationEvent 实例属性 的说明。从类型声明见下文可以看出这四个字段并非普通数字isAbsolute是ShallowRefboolean浅层响应式布尔直接对应原生事件的isAbsolute字段alpha、beta、gamma均为Refnumber | null因为在设备尚未提供方向数据或浏览器不支持时值为null使用前需要做空值判断。组件式用法VueUse 为所有函数式组合式 API 提供了对应的组件包装useDeviceOrientation也不例外。当你不希望在script中维护状态、而想直接在模板中通过作用域插槽消费数据时可以使用UseDeviceOrientation组件template UseDeviceOrientation v-slot{ alpha, beta, gamma } Alpha: {{ alpha }} Beta: {{ beta }} Gamma: {{ gamma }} /UseDeviceOrientation /template组件式用法对函数式 API 的Composable习惯做了模板化封装适合快速原型、无逻辑纯展示场景或在 JSX / 模板驱动的低代码场景中直接复用。类型声明详解useDeviceOrientation的完整类型声明如下export interface UseDeviceOrientationOptions extends ConfigurableWindow {} export interface UseDeviceOrientationReturn extends Supportable { isAbsolute: ShallowRefboolean, boolean alpha: Refnumber | null, number | null beta: Refnumber | null, number | null gamma: Refnumber | null, number | null } /** * Reactive DeviceOrientationEvent. * * see https://vueuse.org/useDeviceOrientation * param options * * __NO_SIDE_EFFECTS__ */ export declare function useDeviceOrientation( options?: UseDeviceOrientationOptions, ): UseDeviceOrientationReturn几个值得注意的细节UseDeviceOrientationOptions extends ConfigurableWindow意味着它可以接收{ window?: Window }配置项用于在非默认窗口环境如 iframe、Electron 渲染进程、服务端测试环境下指定目标window对象。其余配置项为空接口即该函数没有额外行为开关。UseDeviceOrientationReturn extends Supportable返回对象继承了isSupported布尔字段在 VueUse 中由useSupported推导用于判断当前环境是否支持DeviceOrientationEvent。在桌面浏览器或不支持该 API 的 WebView 中isSupported为false此时应避免直接消费传感器数据。__NO_SIDE_EFFECTS__注解表明该函数在顶层调用不会产生副作用配合打包器的 sideEffects 标记可被安全 tree-shake未使用时可从产物中剔除。源码级原理解读VueUse 的useDeviceOrientation内部实现思路属于第三方库源码仓库内仅以依赖形式引用可以归纳为通过useSupported检测window.DeviceOrientationEvent是否存在得到isSupported在tryOnMounted阶段将四个ref初始化为null/false用useEventListener挂载window上的deviceorientation事件事件回调中分别把event.alpha、event.beta、event.gamma、event.absolute写入对应的 ref组件卸载时自动移除监听。由于 VueUse 14.x 统一基于vue-demi适配 Vue 2 / Vue 3这套响应式封装在 Vue 3 的ref体系中表现最为自然在本仓库中vueuse/core与vue、vue-demi一起通过 catalog 管理见 apps/stage-pocket/package.json可直接与组件内其他响应式逻辑协作。权限与浏览器兼容性说明与同为 Sensors 分类的 useDeviceMotion 不同useDeviceOrientation的旧版 API 通常不要求显式权限申请但需要注意iOS Safari对传感器类 API 存在严格的用户手势与 HTTPS 要求实际接入时必须使用 HTTPS或 localhost环境并在用户交互后请求授权Android / 桌面 Chrome对deviceorientation的授权策略随版本演进建议在调用前用返回的isSupported判断环境支持度避免在桌面端误展示传感器 UI在 Capacitor 封装的移动端 WebView本仓库 apps/stage-pocket 提供dev:ios/dev:android脚本中请确认 WebView 配置允许传感器事件透传。仓库中的实际前端入口也体现了对“移动端视口与朝向变化”这一问题的重视例如 packages/stage-layouts/src/browser/adaptive-input.ts 中通过监听window的orientationchange事件在朝向变化后重新测量布局尺寸onOrientationChange并在对应测试 packages/stage-layouts/src/browser/adaptive-input.geometry.test.ts 中验证了“朝向变化后缓存测量结果失效”的行为。这说明在移动端场景中朝向数据与布局响应是相辅相成的useDeviceOrientation负责提供连续的方向角度而orientationchange负责感知 90°/180° 的屏幕旋转并触发重新布局。实战组合成可复用的朝向工具基于 VueUse 的组合式函数习惯参见 .agents/skills/vueuse-functions/SKILL.md 中“优先使用 VueUse composables 而非自定义代码”的原则可以把useDeviceOrientation与computed组合成业务可复用的朝向状态例如import { useDeviceOrientation } from vueuse/core import { computed } from vue const { isAbsolute, alpha, beta, gamma, isSupported } useDeviceOrientation() // 判断是否处于横屏/竖屏的大致姿态 const isLandscape computed(() { if (!isSupported.value) return false if (alpha.value null || beta.value null || gamma.value null) return false // 以 beta 的俯仰角为主判据|beta| 45° 视为接近竖屏否则视为接近横屏 return Math.abs(beta.value) 45 }) // 仅在支持且数据可用时渲染 const ready computed(() isSupported.value alpha.value ! null)在此基础上你还可以使用useDebouncedFn/useThrottleFn或仓库中已使用的refDebounced/refThrottled参考 apps/stage-pocket/src/pages/devtools/gesture-circle.vue 对轨迹点的节流/防抖处理来降低高频传感器事件的渲染频率结合useDeviceMotion的加速度数据实现晃动检测、防抖步数等更丰富的传感器交互把alpha用于指南针式 UI、把beta/gamma用于倾角校正或横屏游戏控制。总结useDeviceOrientation以极小的 API 面一个函数、四个 ref完整封装了原生 DeviceOrientationEvent将“监听事件 → 手动更新状态 → 手动清理”的原生流程收敛为一行调用。结合 VueUse 14.x 在仓库中的 catalog 统一管理pnpm-workspace.yaml、Supportable环境检测与ConfigurableWindow的可配置窗口能力它非常适合在 ai/airi 这类同时面向 Web、Capacitor 移动端与 Electron 桌面端的多端前端工程中作为移动端朝向交互的标准化基础能力。在实际落地时请务必结合 HTTPS 环境、iOS 授权策略与orientationchange布局联动才能把传感器数据安全、稳定地接入产品功能。【免费下载链接】airi Self hosted, you-owned Grok Companion, a container of souls of waifu, cyber livings to bring them into our worlds, wishing to achieve Neuro-samas altitude. Capable of realtime voice chat, Minecraft, Factorio playing. Web / macOS / Windows supported.项目地址: https://gitcode.com/GitHub_Trending/ai/airi创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表