Home Web Front-end H5 Tutorial HTML5实现手势屏幕解锁

HTML5实现手势屏幕解锁

May 17, 2016 am 09:07 AM

  效果展示


  实现原理
  利用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 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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1248
24
What Does H5 Refer To? Exploring the Context What Does H5 Refer To? Exploring the Context Apr 12, 2025 am 12:03 AM

H5referstoHTML5,apivotaltechnologyinwebdevelopment.1)HTML5introducesnewelementsandAPIsforrich,dynamicwebapplications.2)Itsupportsmultimediawithoutplugins,enhancinguserexperienceacrossdevices.3)SemanticelementsimprovecontentstructureandSEO.4)H5'srespo

H5: The Evolution of Web Standards and Technologies H5: The Evolution of Web Standards and Technologies Apr 15, 2025 am 12:12 AM

Web standards and technologies have evolved from HTML4, CSS2 and simple JavaScript to date and have undergone significant developments. 1) HTML5 introduces APIs such as Canvas and WebStorage, which enhances the complexity and interactivity of web applications. 2) CSS3 adds animation and transition functions to make the page more effective. 3) JavaScript improves development efficiency and code readability through modern syntax of Node.js and ES6, such as arrow functions and classes. These changes have promoted the development of performance optimization and best practices of web applications.

H5 Code: Best Practices for Web Developers H5 Code: Best Practices for Web Developers Apr 16, 2025 am 12:14 AM

Best practices for H5 code include: 1. Use correct DOCTYPE declarations and character encoding; 2. Use semantic tags; 3. Reduce HTTP requests; 4. Use asynchronous loading; 5. Optimize images. These practices can improve the efficiency, maintainability and user experience of web pages.

Is H5 a Shorthand for HTML5? Exploring the Details Is H5 a Shorthand for HTML5? Exploring the Details Apr 14, 2025 am 12:05 AM

H5 is not just the abbreviation of HTML5, it represents a wider modern web development technology ecosystem: 1. H5 includes HTML5, CSS3, JavaScript and related APIs and technologies; 2. It provides a richer, interactive and smooth user experience, and can run seamlessly on multiple devices; 3. Using the H5 technology stack, you can create responsive web pages and complex interactive functions.

H5 and HTML5: Commonly Used Terms in Web Development H5 and HTML5: Commonly Used Terms in Web Development Apr 13, 2025 am 12:01 AM

H5 and HTML5 refer to the same thing, namely HTML5. HTML5 is the fifth version of HTML, bringing new features such as semantic tags, multimedia support, canvas and graphics, offline storage and local storage, improving the expressiveness and interactivity of web pages.

H5: Tools, Frameworks, and Best Practices H5: Tools, Frameworks, and Best Practices Apr 11, 2025 am 12:11 AM

The tools and frameworks that need to be mastered in H5 development include Vue.js, React and Webpack. 1.Vue.js is suitable for building user interfaces and supports component development. 2.React optimizes page rendering through virtual DOM, suitable for complex applications. 3.Webpack is used for module packaging and optimize resource loading.

Understanding H5 Code: The Fundamentals of HTML5 Understanding H5 Code: The Fundamentals of HTML5 Apr 17, 2025 am 12:08 AM

HTML5 is a key technology for building modern web pages, providing many new elements and features. 1. HTML5 introduces semantic elements such as, , etc., which enhances web page structure and SEO. 2. Support multimedia elements and embed media without plug-ins. 3. Forms enhance new input types and verification properties, simplifying the verification process. 4. Offer offline and local storage functions to improve web page performance and user experience.

Deconstructing H5 Code: Tags, Elements, and Attributes Deconstructing H5 Code: Tags, Elements, and Attributes Apr 18, 2025 am 12:06 AM

HTML5 code consists of tags, elements and attributes: 1. The tag defines the content type and is surrounded by angle brackets, such as. 2. Elements are composed of start tags, contents and end tags, such as contents. 3. Attributes define key-value pairs in the start tag, enhance functions, such as. These are the basic units for building web structure.

See all articles