溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

HTML5中device access設備訪問的示例分析

發(fā)布時間:2021-06-10 09:34:30 來源:億速云 閱讀:130 作者:小新 欄目:web開發(fā)

這篇文章主要介紹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 電話的接入或者彈出信息等比較高級的事件觸發(fā),一般做保存操作

  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 表示方向數(shù)據(jù)由設備本身坐標系提供
        this.alpha = doe.alpha // 繞Z軸0-360 進入時手機水平正對的方向為0或360
        this.beta = doe.beta // 繞X軸-180~180 描述由前向后旋轉(zhuǎn)
        this.gamma = doe.gamma // 繞Y軸-90~90 描述由左向右旋轉(zhuǎn)
    }, 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 元素內(nèi)
    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設備訪問的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關知識,歡迎關注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI