登录  /  注册
首页 > web前端 > H5教程 > 正文

HTML5实现手势屏幕解锁

php中文网
发布: 2016-05-17 09:07:31
原创
1150人浏览过
  效果展示


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

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号