HTML5 gesture screen unlocking-

Source: Internet
Author: User
Use HTML5 canvas to draw out unlocked circles, use touch events to unlock these circles, and directly view the code .,. Effect display


   Implementation Principle
Use HTML5 canvas to draw unlocked circles and use touch events to unlock these circles and view the Code directly.

  1. Function createCircle () {// create the coordinates of the unlock point, and evenly allocate the radius based on the size of the canvas.

  2. Var n = chooseType; // draws the n * n matrix.
  3. LastPoint = [];
  4. Arr = [];
  5. RestPoint = [];
  6. R = ctx. canvas. width/(2 + 4 * n); // The formula calculates the radius and the size of the canvas.
  7. For (var I = 0; I <n; I ++ ){
  8. For (var j = 0; j <n; 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. }


Events can be bound after the circles in the canvas are drawn.

  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. 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. }


Next we went to the most critical step to draw the logic of the unlock path. Through the constant triggering of the touchmove event, we called the moveTo method and lineTo method of canvas to draw the discount, and determined whether the discount reached the circle we drew, the lastPoint stores the correct circle path, and the restPoint stores all circles to remove the remaining paths. Update method:

  1. Function update (po) {// The core transformation method called during touchmove
  2. Ctx. clearRect (0, 0, ctx. canvas. width, ctx. canvas. height );

  3. For (var I = 0; I <arr. length; I ++) {// draw the Panel first at each frame.
  4. DrawCle (arr [I]. x, arr [I]. y );
  5. }

  6. DrawPoint (lastPoint); // the path of each frame
  7. DrawLine (po, lastPoint); // draw the center of each frame

  8. For (var I = 0; I <restPoint. length; I ++ ){
  9. If (Math. abs (po. x-restPoint [I]. x) <r & Math. abs (po. y-restPoint [I]. y) <r ){
  10. DrawPoint (restPoint [I]. x, restPoint [I]. y );
  11. LastPoint. push (restPoint [I]);
  12. RestPoint. splice (I, 1 );
  13. Break;
  14. }
  15. }

  16. }


The final step is to close the work, and change the array stored by the lastPoint in the path into a password in localstorage, which is then used to process the unlock verification logic.

  1. Function storePass (psw) {// process the password and status after 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 = 'password saved successfully ';
  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 = 'inconsistent twice, re-input ';
  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 = 'unlocked successfully ';
  18. DrawStatusPoint ('#2cff26 ');
  19. } Else {
  20. DrawStatusPoint ('red ');
  21. Document. getElementById ('title'). innerHTML = 'unlock failed ';
  22. }
  23. } Else {
  24. PswObj. step = 1;
  25. PswObj. fpassword = psw;
  26. Document. getElementById ('title'). innerHTML = 'input again ';
  27. }

  28. }


   Unlock Components
The HTML5 unlock is written as a component, placed in the https://github.com/lvming6816077/H5lock

Reproduced from AlloyTeam: http://www.alloyteam.com/2015/07... u-shou-shi-jie-suo/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.