当前位置:   article > 正文

Html5实现手机九宫格密码解锁功能_vue实现手机九宫格解锁

vue实现手机九宫格解锁

HTML5真的是很强大,前端时间看到一个canvas实现九宫格的密码解锁。今天抽出时间模仿了一个,特定分享一下!

效果截图如下:


效果看起来还不错吧!

源码如下:

  1. <!DOCTYPE html>
  2. <html>
  3. <head lang="zh-CN">
  4. <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport"/>
  5. <meta charset="UTF-8">
  6. <title>html5实现网页解锁功能</title>
  7. <style type="text/css">
  8. html, body {
  9. margin: 0;
  10. padding: 0;
  11. width: 100%;
  12. height: 100%;
  13. }
  14. </style>
  15. <script type="text/javascript">
  16. /**
  17. * 半径,画布宽度,画布高度,画布x内边距,画布y内边距
  18. */
  19. var R = 26, canvasWidth = 400, canvasHeight = 320, OffsetX = 30, OffsetY = 30;
  20. var circleArr = [];
  21. function createCirclePoint(diffX, diffY) {
  22. for (var row = 0; row < 3; row++) {
  23. for (var col = 0; col < 3; col++) {
  24. // 计算圆心坐标
  25. var Point = {
  26. X: (OffsetX + col * diffX + ( col * 2 + 1) * R),
  27. Y: (OffsetY + row * diffY + (row * 2 + 1) * R)
  28. };
  29. circleArr.push(Point);
  30. }
  31. }
  32. }
  33. window.onload = function () {
  34. var canvas = document.getElementById("lockCanvas");
  35. canvasWidth = document.body.offsetWidth;//网页可见区域宽
  36. canvas.width = canvasWidth;
  37. canvas.height = canvasHeight;
  38. var cxt = canvas.getContext("2d");
  39. /**
  40. * 每行3个圆
  41. * OffsetX为canvas x方向内边距
  42. * */
  43. var X = (canvasWidth - 2 * OffsetX - R * 2 * 3) / 2;
  44. var Y = (canvasHeight - 2 * OffsetY - R * 2 * 3) / 2;
  45. createCirclePoint(X, Y);
  46. bindEvent(canvas, cxt);
  47. //CW=2*offsetX+R*2*3+2*X
  48. Draw(cxt, circleArr, [],null);
  49. }
  50. function Draw(cxt, circleArr, pwdArr,touchPoint) {
  51. if (pwdArr.length > 0) {
  52. cxt.beginPath();
  53. for (var i = 0; i < pwdArr.length; i++) {
  54. var pointIndex = pwdArr[i];
  55. cxt.lineTo(circleArr[pointIndex].X, circleArr[pointIndex].Y);
  56. }
  57. cxt.lineWidth = 10;
  58. cxt.strokeStyle = "#627eed";
  59. cxt.stroke();
  60. cxt.closePath();
  61. if(touchPoint!=null){
  62. var lastPointIndex=pwdArr[pwdArr.length-1];
  63. var lastPoint=circleArr[lastPointIndex];
  64. cxt.beginPath();
  65. cxt.moveTo(lastPoint.X,lastPoint.Y);
  66. cxt.lineTo(touchPoint.X,touchPoint.Y);
  67. cxt.stroke();
  68. cxt.closePath();
  69. }
  70. }
  71. for (var i = 0; i < circleArr.length; i++) {
  72. var Point = circleArr[i];
  73. cxt.fillStyle = "#627eed";
  74. cxt.beginPath();
  75. cxt.arc(Point.X, Point.Y, R, 0, Math.PI * 2, true);
  76. cxt.closePath();
  77. cxt.fill();
  78. cxt.fillStyle = "#ffffff";
  79. cxt.beginPath();
  80. cxt.arc(Point.X, Point.Y, R - 3, 0, Math.PI * 2, true);
  81. cxt.closePath();
  82. cxt.fill();
  83. if(pwdArr.indexOf(i)>=0){
  84. cxt.fillStyle = "#627eed";
  85. cxt.beginPath();
  86. cxt.arc(Point.X, Point.Y, R -16, 0, Math.PI * 2, true);
  87. cxt.closePath();
  88. cxt.fill();
  89. }
  90. }
  91. }
  92. /**
  93. * 计算选中的密码
  94. */
  95. function getSelectPwd(touches,pwdArr){
  96. for (var i = 0; i < circleArr.length; i++) {
  97. var currentPoint = circleArr[i];
  98. var xdiff = Math.abs(currentPoint.X - touches.pageX);
  99. var ydiff = Math.abs(currentPoint.Y - touches.pageY);
  100. var dir = Math.pow((xdiff * xdiff + ydiff * ydiff), 0.5);
  101. if(dir > R || pwdArr.indexOf(i) >= 0)
  102. continue;
  103. pwdArr.push(i);
  104. break;
  105. }
  106. }
  107. /**
  108. * 给画布绑定事件
  109. */
  110. function bindEvent(canvas, cxt) {
  111. var pwdArr = [];
  112. canvas.addEventListener("touchstart", function (e) {
  113. getSelectPwd(e.touches[0],pwdArr);
  114. }, false);
  115. canvas.addEventListener("touchmove", function (e) {
  116. e.preventDefault();
  117. var touches = e.touches[0];
  118. getSelectPwd(touches,pwdArr);
  119. cxt.clearRect(0,0,canvasWidth,canvasHeight);
  120. Draw(cxt,circleArr,pwdArr,{X:touches.pageX,Y:touches.pageY});
  121. }, false);
  122. canvas.addEventListener("touchend", function (e) {
  123. cxt.clearRect(0,0,canvasWidth,canvasHeight);
  124. Draw(cxt,circleArr,pwdArr,null);
  125. alert("密码结果是:"+pwdArr.join("->"));
  126. pwdArr=[];
  127. }, false);
  128. }
  129. </script>
  130. </head>
  131. <body>
  132. <canvas id="lockCanvas"></canvas>
  133. </body>
  134. </html>
好了下班了,就写这么多了!

欢迎大家关注我的博客,如有疑问,请加qq群:454796847、135430763 共同进步!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/672289
推荐阅读
相关标签
  

闽ICP备14008679号