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.
- function createCircle() {// Create the coordinates of the unlock point and evenly distribute the radius according to the size of the canvas
-
- var n = chooseType;// draw n*n matrix
- lastPoint = [];
- arr = [];
- restPoint = [] ;
- r = ctx.canvas.width / (2 4 * n);//The formula to calculate the radius is related to the size of the canvas
- for (var i = 0; i < n; i ) {
- r. x: j * 4 * r 3 * r,
- y: i * 4 * r 3 * r
}-
//return arr;-
}-
-
Copy code-
-
- After the circle in the canvas is drawn, you can proceed Event binding
-
- 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
-
- lastPoint.push(arr[i]);
- }
- }, false);
- can.addEventListener("touchmove", function (e) {
- if (touchFlag) {
- update(getPosition(e));
- }
- }, false);
- can.addEventListener("touchend", function (e) {
- if (touchFlag) {
- touchFlag = false;
- storePass(lastPoint);
- }, 300);
- }
-
-
- }, false);
- }
-
- Copy Code
-
-
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: -
-
-
- 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
- drawLine(po, lastPoint);// Draw circle center in 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 ) {
- (i, 1);
-
- Copy code
-
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.
- function storePass(psw) {// Processing of password and status after touchend ends
- if (pswObj.step = = 1) {
- .spassword = psw;
- 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) {
- if (checkPass(pswObj.spassword, psw)) {
- document.getElementById('title').innerHTML = '解锁成功';
- drawStatusPoint('#2CFF26');
- } else {
- drawStatusPoint('red'); document.getElementById('title ').innerHTML = 'Unlock failed'; bj.fpassword = psw;
- document.getElementById('title').innerHTML = 'Enter again'; >
Copy code-
-
-
Unlock component-
- Write this HTML5 unlock as a component and place it at https:// github.com/lvming6816077/H5lock
- Reprinted from AlloyTeam: http://www.alloyteam.com/2015/07 ... u-shou-shi-jie-suo/
-
-
For more html5 content, please click -
-