首頁  >  文章  >  web前端  >  用HTML5製作螢幕手勢解鎖功能

用HTML5製作螢幕手勢解鎖功能

小云云
小云云原創
2017-11-20 14:39:562327瀏覽

隨著時代的發展,HTML5深受一些人喜愛,在開發過程中也是必不可少的一種程式語言。 HTML5本身是由W3C推薦出來的,它的開發是透過Google、蘋果,諾基亞、中國移動等幾百家公司一起醞釀的技術,這個技術最大的好處在於它是一個公開的技術。換句話說,每一個公開的標準都可以根據W3C的資料庫找出根源。另一方面,W3C通過的HTML5標準也意味著每個瀏覽器或每個平台都會去實現。本節內容我們就講講用HTML5製作螢幕手勢解鎖功能教學。

實現原理 利用HTML5的canvas,將解鎖的圈圈劃出,並利用touch事件解鎖這些圈圈,直接看代碼。

function createCircle() {// 创建解锁点的坐标,根据canvas的大小来平均分配半径
 
        var n = chooseType;// 画出n*n的矩阵 
        lastPoint = [];
        arr = [];
        restPoint = [];
        r = ctx.canvas.width / (2 + 4 * n);// 公式计算 半径和canvas的大小有关
        for (var i = 0 ; i < n ; i++) {
            for (var j = 0 ; j < n ; j++) {
                arr.push({
                    x: j * 4 * r + 3 * r,
                    y: i * 4 * r + 3 * r
                });
                restPoint.push({
                    x: j * 4 * r + 3 * r,
                    y: i * 4 * r + 3 * r
                });
            }
        }
        //return arr;
    }

canvas裡的圓圈畫好之後可以進行事件綁定

function bindEvent() {
        can.addEventListener("touchstart", function (e) {
             var po = getPosition(e);
             console.log(po);
             for (var i = 0 ; i < arr.length ; i++) {
                if (Math.abs(po.x - arr[i].x) < r && Math.abs(po.y - arr[i].y) < r) { // 用来判断起始点是否在圈圈内部
 
                    touchFlag = true;
                    drawPoint(arr[i].x,arr[i].y);
                    lastPoint.push(arr[i]);
                    restPoint.splice(i,1);
                    break;
                }
             }
         }, false);
         can.addEventListener("touchmove", function (e) {
            if (touchFlag) {
                update(getPosition(e));
            }
         }, false);
         can.addEventListener("touchend", function (e) {
             if (touchFlag) {
                 touchFlag = false;
                 storePass(lastPoint);
                 setTimeout(function(){
 
                    init();
                }, 300);
             }
 
         }, false);
    }

接著到了最關鍵的步驟繪製解鎖路徑邏輯,透過touchmove事件的不斷觸發,調用canvas的moveTo方法和lineTo方法來畫出折現,同時判斷是否達到我們所畫的圈圈裡面,其中lastPoint保存正確的圈圈路徑,restPoint保存全部圈圈去除正確路徑之後剩餘的。 Update方法:

function update(po) {// 核心变换方法在touchmove时候调用
        ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
 
        for (var i = 0 ; i < arr.length ; i++) { // 每帧先把面板画出来
            drawCle(arr[i].x, arr[i].y);
        }
 
        drawPoint(lastPoint);// 每帧花轨迹
        drawLine(po , lastPoint);// 每帧画圆心
 
        for (var i = 0 ; i < restPoint.length ; i++) {
            if (Math.abs(po.x - restPoint[i].x) < r && Math.abs(po.y - restPoint[i].y) < r) {
                drawPoint(restPoint[i].x, restPoint[i].y);
                lastPoint.push(restPoint[i]);
                restPoint.splice(i, 1);
                break;
            }
        }
 
    }

最後就是收尾工作,把路徑裡面的lastPoint保存的陣列變成密碼存在localstorage裡面,之後就用來處理解鎖驗證邏輯了function storePass(psw) {// touchend結束之後密碼與狀態的處理
 

     if (pswObj.step == 1) {
            if (checkPass(pswObj.fpassword, psw)) {
                pswObj.step = 2;
                pswObj.spassword = psw;
                document.getElementById(&#39;title&#39;).innerHTML = &#39;密码保存成功&#39;;
                drawStatusPoint(&#39;#2CFF26&#39;);
                window.localStorage.setItem(&#39;passwordx&#39;, JSON.stringify(pswObj.spassword));
                window.localStorage.setItem(&#39;chooseType&#39;, chooseType);
            } else {
                document.getElementById(&#39;title&#39;).innerHTML = &#39;两次不一致,重新输入&#39;;
                drawStatusPoint(&#39;red&#39;);
                delete pswObj.step;
            }
        } else if (pswObj.step == 2) {
            if (checkPass(pswObj.spassword, psw)) {
                document.getElementById(&#39;title&#39;).innerHTML = &#39;解锁成功&#39;;
                drawStatusPoint(&#39;#2CFF26&#39;);
            } else {
                drawStatusPoint(&#39;red&#39;);
                document.getElementById(&#39;title&#39;).innerHTML = &#39;解锁失败&#39;;
            }
        } else {
            pswObj.step = 1;
            pswObj.fpassword = psw;
            document.getElementById(&#39;title&#39;).innerHTML = &#39;再次输入&#39;;
        }
 
    }

解鎖元件

將這個HTML5解鎖寫成了一個元件,放在https://github.com/lvming6816077/H5lock

#以上就是如何用HTML5來實現解鎖功能教程,大家可以動手實作一下。

相關推薦:

如何用html5 寫出網頁音樂播放器

html5 canvas繪製愛心的方法範例

html5實作文字輪滾的範例程式碼

html5實作下雪效果的方法

防止html5的video標籤在iphone中自動全螢幕的方法

以上是用HTML5製作螢幕手勢解鎖功能的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn