search
HomeWeb Front-endH5 TutorialHTML5实现手势屏幕解锁

  效果展示


  实现原理
  利用HTML5的canvas,将解锁的圈圈划出,利用touch事件解锁这些圈圈,直接看代码。
  1. function createCircle() {// 创建解锁点的坐标,根据canvas的大小来平均分配半径

  2.         var n = chooseType;// 画出n*n的矩阵
  3.         lastPoint = [];
  4.         arr = [];
  5.         restPoint = [];
  6.         r = ctx.canvas.width / (2 + 4 * n);// 公式计算 半径和canvas的大小有关
  7.         for (var i = 0 ; i
  8.             for (var j = 0 ; j
  9.                 arr.push({
  10.                     x: j * 4 * r + 3 * r,
  11.                     y: i * 4 * r + 3 * r
  12.                 });
  13.                 restPoint.push({
  14.                     x: j * 4 * r + 3 * r,
  15.                     y: i * 4 * r + 3 * r
  16.                 });
  17.             }
  18.         }
  19.         //return arr;
  20.     }
复制代码

  canvas里的圆圈画好之后可以进行事件绑定
  1. function bindEvent() {
  2.         can.addEventListener("touchstart", function (e) {
  3.              var po = getPosition(e);
  4.              console.log(po);
  5.              for (var i = 0 ; i
  6.                 if (Math.abs(po.x - arr[i].x)

  7.                     touchFlag = true;
  8.                     drawPoint(arr[i].x,arr[i].y);
  9.                     lastPoint.push(arr[i]);
  10.                     restPoint.splice(i,1);
  11.                     break;
  12.                 }
  13.              }
  14.          }, false);
  15.          can.addEventListener("touchmove", function (e) {
  16.             if (touchFlag) {
  17.                 update(getPosition(e));
  18.             }
  19.          }, false);
  20.          can.addEventListener("touchend", function (e) {
  21.              if (touchFlag) {
  22.                  touchFlag = false;
  23.                  storePass(lastPoint);
  24.                  setTimeout(function(){

  25.                     init();
  26.                 }, 300);
  27.              }


  28.          }, false);
  29.     }
复制代码

  接着到了最关键的步骤绘制解锁路径逻辑,通过touchmove事件的不断触发,调用canvas的moveTo方法和lineTo方法来画出折现,同时判断是否达到我们所画的圈圈里面,其中lastPoint保存正确的圈圈路径,restPoint保存全部圈圈去除正确路径之后剩余的。 Update方法:
  1. function update(po) {// 核心变换方法在touchmove时候调用
  2.         ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);

  3.         for (var i = 0 ; i
  4.             drawCle(arr[i].x, arr[i].y);
  5.         }

  6.         drawPoint(lastPoint);// 每帧花轨迹
  7.         drawLine(po , lastPoint);// 每帧画圆心

  8.         for (var i = 0 ; i
  9.             if (Math.abs(po.x - restPoint[i].x)
  10.                 drawPoint(restPoint[i].x, restPoint[i].y);
  11.                 lastPoint.push(restPoint[i]);
  12.                 restPoint.splice(i, 1);
  13.                 break;
  14.             }
  15.         }

  16.     }
复制代码

  最后就是收尾工作,把路径里面的lastPoint保存的数组变成密码存在localstorage里面,之后就用来处理解锁验证逻辑了。
  1. function storePass(psw) {// touchend结束之后对密码和状态的处理
  2.         if (pswObj.step == 1) {
  3.             if (checkPass(pswObj.fpassword, psw)) {
  4.                 pswObj.step = 2;
  5.                 pswObj.spassword = psw;
  6.                 document.getElementById('title').innerHTML = '密码保存成功';
  7.                 drawStatusPoint('#2CFF26');
  8.                 window.localStorage.setItem('passwordx', JSON.stringify(pswObj.spassword));
  9.                 window.localStorage.setItem('chooseType', chooseType);
  10.             } else {
  11.                 document.getElementById('title').innerHTML = '两次不一致,重新输入';
  12.                 drawStatusPoint('red');
  13.                 delete pswObj.step;
  14.             }
  15.         } else if (pswObj.step == 2) {
  16.             if (checkPass(pswObj.spassword, psw)) {
  17.                 document.getElementById('title').innerHTML = '解锁成功';
  18.                 drawStatusPoint('#2CFF26');
  19.             } else {
  20.                 drawStatusPoint('red');
  21.                 document.getElementById('title').innerHTML = '解锁失败';
  22.             }
  23.         } else {
  24.             pswObj.step = 1;
  25.             pswObj.fpassword = psw;
  26.             document.getElementById('title').innerHTML = '再次输入';
  27.         }

  28.     }
复制代码

  解锁组件
  将这个HTML5解锁写成了一个组件,放在https://github.com/lvming6816077/H5lock

  转载自AlloyTeam:http://www.alloyteam.com/2015/07 ... u-shou-shi-jie-suo/

Statement
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
What is the H5 tag in HTML?What is the H5 tag in HTML?May 09, 2025 am 12:11 AM

The H5 tag in HTML is a fifth-level title that is used to tag smaller titles or sub-titles. 1) The H5 tag helps refine content hierarchy and improve readability and SEO. 2) Combined with CSS, you can customize the style to enhance the visual effect. 3) Use H5 tags reasonably to avoid abuse and ensure the logical content structure.

H5 Code: A Beginner's Guide to Web StructureH5 Code: A Beginner's Guide to Web StructureMay 08, 2025 am 12:15 AM

The methods of building a website in HTML5 include: 1. Use semantic tags to define the web page structure, such as, , etc.; 2. Embed multimedia content, use and tags; 3. Apply advanced functions such as form verification and local storage. Through these steps, you can create a modern web page with clear structure and rich features.

H5 Code Structure: Organizing Content for ReadabilityH5 Code Structure: Organizing Content for ReadabilityMay 07, 2025 am 12:06 AM

A reasonable H5 code structure allows the page to stand out among a lot of content. 1) Use semantic labels such as, etc. to organize content to make the structure clear. 2) Control the rendering effect of pages on different devices through CSS layout such as Flexbox or Grid. 3) Implement responsive design to ensure that the page adapts to different screen sizes.

H5 vs. Older HTML Versions: A ComparisonH5 vs. Older HTML Versions: A ComparisonMay 06, 2025 am 12:09 AM

The main differences between HTML5 (H5) and older versions of HTML include: 1) H5 introduces semantic tags, 2) supports multimedia content, and 3) provides offline storage functions. H5 enhances the functionality and expressiveness of web pages through new tags and APIs, such as and tags, improving user experience and SEO effects, but need to pay attention to compatibility issues.

H5 vs. HTML5: Clarifying the Terminology and RelationshipH5 vs. HTML5: Clarifying the Terminology and RelationshipMay 05, 2025 am 12:02 AM

The difference between H5 and HTML5 is: 1) HTML5 is a web page standard that defines structure and content; 2) H5 is a mobile web application based on HTML5, suitable for rapid development and marketing.

HTML5 Features: The Core of H5HTML5 Features: The Core of H5May 04, 2025 am 12:05 AM

The core features of HTML5 include semantic tags, multimedia support, form enhancement, offline storage and local storage. 1. Semantic tags such as, improve code readability and SEO effect. 2. Multimedia support simplifies the process of embedding media content through and tags. 3. Form Enhancement introduces new input types and verification properties, simplifying form development. 4. Offline storage and local storage improve web page performance and user experience through ApplicationCache and localStorage.

H5: Exploring the Latest Version of HTMLH5: Exploring the Latest Version of HTMLMay 03, 2025 am 12:14 AM

HTML5isamajorrevisionoftheHTMLstandardthatrevolutionizeswebdevelopmentbyintroducingnewsemanticelementsandcapabilities.1)ItenhancescodereadabilityandSEOwithelementslike,,,and.2)HTML5enablesricher,interactiveexperienceswithoutplugins,allowingdirectembe

Beyond Basics: Advanced Techniques in H5 CodeBeyond Basics: Advanced Techniques in H5 CodeMay 02, 2025 am 12:03 AM

Advanced tips for H5 include: 1. Use complex graphics to draw, 2. Use WebWorkers to improve performance, 3. Enhance user experience through WebStorage, 4. Implement responsive design, 5. Use WebRTC to achieve real-time communication, 6. Perform performance optimization and best practices. These tips help developers build more dynamic, interactive and efficient web applications.

See all articles

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

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!