Implementation principle
Use HTML5 's canvas to draw out the unlocked circles and use the touch event to unlock the circles and see the code directly.
- function createcircle () {//creates the coordinates of the unlocking point, distributes the radius evenly according to the size of the canvas
- var n = choosetype;//to draw a 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 * 3 * R,
- Y:I * 3 * r
- });
- Restpoint.push ({
- X:J * 3 * R,
- Y:I * 3 * r
- });
- }
- }
- return arr;
- }
Copy Code
Event binding can be done after the circle in the canvas is 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) {//To determine if the starting point is inside the loop
- 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);
- }
Copy Code
Then to the most critical steps to draw the unlock path logic, through the constant triggering of the Touchmove event, call the canvas MoveTo method and LineTo method to draw a discount, while judging whether to reach the circle we draw, where lastpoint save the correct circle path, Restpoint Save all circles to remove the correct path after the remaining. Update method:
- Function Update (PO) {//Core transform method called at touchmove time
- Ctx.clearrect (0, 0, ctx.canvas.width, ctx.canvas.height);
- for (var i = 0; i < arr.length; i++) {//each frame draw the panel first
- Drawcle (arr[i].x, ARR[I].Y);
- }
- Drawpoint (lastpoint);//flower trajectory per frame
- DrawLine (PO, lastpoint);//Draw 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
- }
- }
- }
Copy Code
Finally, the closing work, the lastpoint stored in the path of the array into the password exists localstorage inside, then used to understand the lock verification logic.
- function Storepass (PSW) {//Touchend after the end of the processing of the password and status
- 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 = ' two times inconsistent, re-enter ';
- 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 = ' re-enter ';
- }
- }
Phone screen unlock