HTML5 implements gesture screen unlocking_html/css_WEB-ITnose

WBOY
Release: 2016-06-24 11:39:51
Original
1046 people have browsed it

For details, please click

Effect display


 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.

  1. function createCircle() {// Create the coordinates of the unlock point and evenly distribute the radius according to the size of the canvas
  2. var n = chooseType;// draw n*n matrix
  3. lastPoint = [];
  4. arr = [];
  5. restPoint = [] ;
  6. r = ctx.canvas.width / (2 4 * n);//The formula to calculate the radius is related to the size of the canvas
  7. for (var i = 0; i < n; i ) {
  8.                                                                                                 r.                    x: j * 4 * r 3 * r,
  9.                     y: i * 4 * r 3 * r
  10.                              }
  11. //return arr;
  12. }
  13. Copy code
  14. After the circle in the canvas is drawn, you can proceed Event binding
    1. function bindEvent() {
    2. can.addEventListener("touchstart", function (e) {
    3. var po = getPosition(e);
    4. console.log(po);
    5. for (var i = 0 ; i < arr.length ; i ) {
    6. if (Math.abs(po. x - arr[i].x) < r && Math.abs(po.y - arr[i].y) < r) { // Used to determine whether the starting point is inside the circle
    7.                                                                                                                  lastPoint.push(arr[i]);
    8.                                                                                      }
    9.            }, false);
    10. can.addEventListener("touchmove", function (e) {
    11. if (touchFlag) {
    12. update(getPosition(e));
    13. }
    14. }, false);
    15. can.addEventListener("touchend", function (e) {
    16. if (touchFlag) {
    17. touchFlag = false;
    18.                       storePass(lastPoint);
    19.                 }, 300);
    20. }
    21. }, false);
    22. }
    23. Copy Code
    24. Then comes the most critical step of drawing the unlocking path logic. Through the continuous triggering of touchmove events, the moveTo method and lineTo method of canvas are called to draw the discount and judge at the same time. Whether it reaches the circle we drew, lastPoint saves the correct circle path, and restPoint saves the rest of all circles after removing the correct path. Update method:
    25. function update(po) {// The core transformation method is called when touchmove
    ctx.clearRect(0 , 0, ctx.canvas.width, ctx.canvas.height);

    for (var i = 0 ; i < arr.length ; i ) { // Each frame starts with Draw the panel


    drawCle(arr[i].x, arr[i].y);

    }

    drawPoint(lastPoint) ;//Flower trajectory in each frame

    1. drawLine(po, lastPoint);// Draw circle center in each frame
    2. for (var i = 0; i < restPoint. length ; i ) {
    3. if (Math.abs(po.x - restPoint[i].x) < r && Math.abs(po.y - restPoint[i].y) < r ) {
    4.                                                                                                                  (i, 1);
    5.                                                                   
    6. Copy code


    7. The last step is the finishing work, turning the array saved by lastPoint in the path into a password and storing it in localstorage, which will then be used to process the unlocking verification logic.

      1. function storePass(psw) {// Processing of password and status after touchend ends
      2. if (pswObj.step = = 1) {
      3.                                                                                                               .spassword = psw;
      4.             document.getElementById('title').innerHTML = 'Password saved successfully';                                                              setItem('passwordx', JSON .stringify (pswObj.spassword)); elementById('title' ).innerHTML = 'Inconsistent twice, re-enter'; 🎜> } else if (pswObj.step == 2) {
      5.             if (checkPass(pswObj.spassword, psw)) {
      6.                 document.getElementById('title').innerHTML = '解锁成功';
      7. drawStatusPoint('#2CFF26');
      8. } else {
      9. drawStatusPoint('red'); document.getElementById('title ').innerHTML = 'Unlock failed'; bj.fpassword = psw;
      10.                            document.getElementById('title').innerHTML = 'Enter again'; >
      11. Copy code
      12.  
      13. Unlock component
      14. Write this HTML5 unlock as a component and place it at https:// github.com/lvming6816077/H5lock
      15. Reprinted from AlloyTeam: http://www.alloyteam.com/2015/07 ... u-shou-shi-jie-suo/
      16. For more html5 content, please click
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template