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.
- Function createCircle () {// create the coordinates of the unlock point, and evenly allocate the radius based on the size of the canvas.
-
- Var n = chooseType; // draws the n * n matrix.
- LastPoint = [];
- Arr = [];
- RestPoint = [];
- R = ctx. canvas. width/(2 + 4 * n); // The formula calculates the radius and the size of the canvas.
- For (var I = 0; I <n; I ++ ){
- For (var j = 0; j <n; j ++ ){
- Arr. push ({
- X: j * 4 * r + 3 * r,
- Y: I * 4 * r + 3 * r
- });
- RestPoint. push ({
- X: j * 4 * r + 3 * r,
- Y: I * 4 * r + 3 * r
- });
- }
- }
- // Return arr;
- }
Events can be bound after the circles in the canvas are drawn.
- Function bindEvent (){
- Can. addEventListener ("touchstart", function (e ){
- Var po = getPosition (e );
- Console. log (po );
- For (var I = 0; I <arr. length; I ++ ){
- 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
-
- TouchFlag = true;
- DrawPoint (arr [I]. x, arr [I]. y );
- LastPoint. push (arr [I]);
- RestPoint. splice (I, 1 );
- Break;
- }
- }
- }, False );
- Can. addEventListener ("touchmove", function (e ){
- If (touchFlag ){
- Update (getPosition (e ));
- }
- }, False );
- Can. addEventListener ("touchend", function (e ){
- If (touchFlag ){
- TouchFlag = false;
- StorePass (lastPoint );
- SetTimeout (function (){
-
- Init ();
- },300 );
- }
-
-
- }, False );
- }
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:
- Function update (po) {// The core transformation method called during touchmove
- Ctx. clearRect (0, 0, ctx. canvas. width, ctx. canvas. height );
-
- For (var I = 0; I <arr. length; I ++) {// draw the Panel first at each frame.
- DrawCle (arr [I]. x, arr [I]. y );
- }
-
- DrawPoint (lastPoint); // the path of each frame
- DrawLine (po, lastPoint); // draw the center of each frame
-
- For (var I = 0; I <restPoint. length; I ++ ){
- If (Math. abs (po. x-restPoint [I]. x) <r & Math. abs (po. y-restPoint [I]. y) <r ){
- DrawPoint (restPoint [I]. x, restPoint [I]. y );
- LastPoint. push (restPoint [I]);
- RestPoint. splice (I, 1 );
- Break;
- }
- }
-
- }
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.
- Function storePass (psw) {// process the password and status after touchend
- If (pswObj. step = 1 ){
- If (checkPass (pswObj. fpassword, psw )){
- PswObj. step = 2;
- PswObj. spassword = psw;
- Document. getElementById ('title'). innerHTML = 'password saved successfully ';
- DrawStatusPoint ('#2cff26 ');
- Window. localStorage. setItem ('passwordx', JSON. stringify (pswObj. spassword ));
- Window. localStorage. setItem ('choosetype ', chooseType );
- } Else {
- Document. getElementById ('title'). innerHTML = 'inconsistent twice, re-input ';
- DrawStatusPoint ('red ');
- Delete pswObj. step;
- }
- } Else if (pswObj. step = 2 ){
- If (checkPass (pswObj. spassword, psw )){
- Document. getElementById ('title'). innerHTML = 'unlocked successfully ';
- DrawStatusPoint ('#2cff26 ');
- } Else {
- DrawStatusPoint ('red ');
- Document. getElementById ('title'). innerHTML = 'unlock failed ';
- }
- } Else {
- PswObj. step = 1;
- PswObj. fpassword = psw;
- Document. getElementById ('title'). innerHTML = 'input again ';
- }
-
- }
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/