温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

HTML5中device access设备访问的示例分析

发布时间:2021-06-10 09:34:30 来源:亿速云 阅读:128 作者:小新 栏目:web开发

这篇文章主要介绍HTML5中device access设备访问的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

camera api (含图片预览)

主要为利用input type=file, accept="image/*" 进行处理

图片预览方式(两种)

const file = e.target.files[0]
// 方式1 
const url1 = window.URL.createObjectURL(file);
let url2

// 方式2
const reader = new FileReader();
reader.onload = (e) => {
    url2 = e.target.result;
};
reader.readAsDataURL(file);

touch events (触屏事件)

  1. touchstart

  2. touchen

  3. touchcancel 电话的接入或者弹出信息等比较高级的事件触发,一般做保存操作

  4. touchmove

  5. geolocation

注意谷歌浏览器要https才能提供定位服务

if (navigator.geolocation){
        navigator.geolocation.getCurrentPosition((position) => {
            this.geolocation = `latitude:${position.coords.latitude},longitude:${position.coords.longitude}`
        }, (err) => {
            console.log(err);
        }, {
                enableHighAccuracy: true, 
                maximumAge        : 30000,  // buffer memory timre
                timeout           : 27000   // waiting time 
        })
    } else {
        alert('geolocation not supported!')
    }

device orientation and motion

window.addEventListener('deviceorientation',(doe) => {
        this.absolute = doe.absolute //false 表示方向数据由设备本身坐标系提供
        this.alpha = doe.alpha // 绕Z轴0-360 进入时手机水平正对的方向为0或360
        this.beta = doe.beta // 绕X轴-180~180 描述由前向后旋转
        this.gamma = doe.gamma // 绕Y轴-90~90 描述由左向右旋转
    }, true)

    // chrome v65 只支持accelerationIncludingGravity和interval(应该因为一些限制没有找到),其它浏览器最新版基本都支持
    window.addEventListener('devicemotion', (dme) => {
        this.acceleration = dme.acceleration
        this.accelerationIncludingGravity = dme.accelerationIncludingGravity
        this.rotationRate = dme.rotationRate
        this.interval  = dme.interval 
    }, false)

Pointer Lock(鼠标锁定)

<button onclick="lockPointer();">锁住它!</button>
    <div id="pointer-lock-element" style="width:500px;height:500px;background-color: red"></div>
// 简单示例,将鼠标锁定在 pointer-lock-element 元素内
    let = document.getElementById("pointer-lock-element");
    
    document.addEventListener("mousemove", function(e) {
        var movementX = e.movementX 
            movementY = e.movementY

        // 打印鼠标移动的增量值。
        console.log("X=" + movementX, "Y=" + movementY);
    }, false);

    function lockPointer() {
        elem = document.getElementById("pointer-lock-element");
        elem.requestPointerLock = elem.requestPointerLock    ||
                            elem.mozRequestPointerLock ||
                            elem.webkitRequestPointerLock;
        elem.requestPointerLock();
    }

以上是“HTML5中device access设备访问的示例分析”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI