Home Web Front-end H5 Tutorial Create screen gesture unlock function with HTML5

Create screen gesture unlock function with HTML5

Nov 20, 2017 pm 02:39 PM
h5 html5 Function

With the development of the times, HTML5 is loved by some people and is also an indispensable programming language in the development process. HTML5 itself is recommended by the W3C. Its development is a technology brewed by hundreds of companies such as Google, Apple, Nokia, and China Mobile. The biggest advantage of this technology is that it is an open technology. In other words, every public standard can be traced back to the W3C database. On the other hand, the HTML5 standard adopted by W3C means that every browser or every platform will implement it. In this section, we will talk about a tutorial on using HTML5 to create a screen gesture unlock function.

Implementation principle Use the HTML5 canvas to draw out the unlocked circles, use touch events to unlock these circles, and look at the code directly.

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;
    }
Copy after login

After the circle in the canvas is drawn, event binding can be performed

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);
    }
Copy after login

Then the most critical step is to draw the unlocking path logic. Through the continuous triggering of the touchmove event, the canvas's The moveTo method and the lineTo method are used to draw the discount, and at the same time determine whether it reaches the circle we drew. LastPoint saves the correct circle path, and restPoint saves the remainder of all circles after removing the correct path. Update method:

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;
            }
        }
 
    }
Copy after login

The last step is the finishing work. Turn the array saved by lastPoint in the path into a password and store it in localstorage. It will then be used to process the unlocking verification logic function storePass(psw) {//After touchend ends Processing of passwords and status

     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;;
        }
 
    }
Copy after login

Unlocking component

Write this HTML5 unlocking into a component and place it at https://github.com/lvming6816077/H5lock

The above is a tutorial on how to use HTML5 to implement the unlocking function. You can practice it.

Related recommendations:

How to use html5 to write a web music player

Example of how to draw a love on html5 canvas

html5 sample code to implement text wheel scrolling

html5 method to achieve snow effect

Prevent html5 How to automatically full-screen the video tag in iPhone

The above is the detailed content of Create screen gesture unlock function with HTML5. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

See all articles