WeChat applets implement the gesture pattern lock function and gesture lock screen function

Source: Internet
Author: User

The applet implements the gesture pattern lock function and gesture lock screen function.

Examples of this article share the code for the screen lock of the gesture pattern of the applet for your reference. The details are as follows:

Reference

H5lock

WXML

<View class = "container"> <view class = "reset" bindtap = "resetPwd"> reset Password </view> <view class = "title" >{{ title }} </view> <canvas-id = "canvas" class = "canvas" bindtouchend = "onTouchEnd" bindtouchstart = "onTouchStart" bindtouchmove = "onTouchMove"> </canvas> </ view>

JS

Var Locker = class {constructor (page, opt) {var obj = opt ||{}; this. page = page; this. width = obj. width | 300; this. height = obj. height | 300; this. canvasId = obj. id | 'canvas '; this. cleColor = obj. cleColor | '# CFE6FF'; this. cleCenterColor = obj. cleCenterColor | '# CFE6FF'; var chooseType = obj. chooseType | 3; // determine whether the cache contains chooseType. If yes, use the cache. If no, use the passed value this. chooseType = Number (wx. getStorageSync ('ch OoseType ') | chooseType; this. init () ;}init () {this. pswObj = wx. getStorageSync ('passwordxx ')? {Step: 2, spassword: JSON. parse (wx. getStorageSync ('passwordxx') }:{}; this. makeState (); // create the canvas drawing context (specify canvasId) this. ctx = wx. createCanvasContext (this. canvasId, this); this. touchFlag = false; this. lastPoint = []; // draw the circle this. createCircle (); // canvas binding event this. bindEvent ();} makeState () {if (this. pswObj. step = 2) {this. page. setData ({title: 'unlock '});} else if (this. pswObj. step = 1 ){ // Pass} else {// pass} // draw circle method drawCle (x, y) {// set the border color. This. ctx. setStrokeStyle (this. cleColor); // use set // to set the line width. This. ctx. setLineWidth (2); // note that to create a path with set //, you must call fill or stroke to fill or stroke the path. This. ctx. beginPath (); // draw an arc. This. ctx. arc (x, y, this. r, 0, Math. PI * 2, true); // close a path this. ctx. closePath (); // draw the border of the current path. The default color is black. This. ctx. stroke (); // draws the descriptions (paths, variants, and styles) in the drawing context to the canvas. This. ctx. draw (true);} // Method for Calculating the distance between two points getDis (a, B) {return Math. sqrt (Math. pow (. x-B. x, 2) + Math. pow (. y-B. y, 2);} // create the coordinates of the unlock point. The radius is evenly allocated to createCircle () {var n = this based on the canvas size (PX by default. chooseType; var count = 0; // calculate the circle radius this. r = this. width/(2 + 4 * n); this. arr = []; this. restPoint = []; var r = this. r; // obtain the coordinates of the center and the number of the current circle for (var I = 0; I <n; I ++) {for (var j = 0; j <n; j ++) {cou Nt ++; var obj = {x: j * 4 * r + 3 * r, y: I * 4 * r + 3 * r, index: count}; this. arr. push (obj); this. restPoint. push (obj) ;}// clear the canvas this. ctx. clearRect (0, 0, this. width, this. height); // draw all circles this. arr. forEach (current => {this. drawCle (current. x, current. y) ;}) ;}// set the password to draw getPosition (e) {// obtain the coordinates of the touch point relative to the canvas var po = {x: e. touches [0]. x, y: e. touches [0]. y}; return po;} precisePosition (po) {Var arr = this. restPoint. filter (current => Math. abs (po. x-current. x) <this. r & Math. abs (po. y-current. y) <this. r); return arr [0];} drawPoint (obj) {// initialize the center for (var I = 0; I <this. lastPoint. length; I ++) {this. ctx. setFillStyle (this. cleCenterColor); // use the set method this. ctx. beginPath (); this. ctx. arc (this. lastPoint [I]. x, this. lastPoint [I]. y, this. r/2, 0, Math. PI * 2, true); this. ctx. closePa Th (); this. ctx. fill (); this. ctx. draw (true) ;}} drawLine (po) {// unlock track this. ctx. beginPath (); this. ctx. lineWidth = 3; this. ctx. moveTo (this. lastPoint [0]. x, this. lastPoint [0]. y); for (var I = 1; I <this. lastPoint. length; I ++) {this. ctx. lineTo (this. lastPoint [I]. x, this. lastPoint [I]. y);} this. ctx. lineTo (po. x, po. y); this. ctx. stroke (); this. ctx. closePath (); this. ctx. draw (true);} pickPoints (fromPt, toP T) {var lineLength = this. getDis (fromPt, toPt); var dir = toPt. index> fromPt. index? 1:-1; var len = this. restPoint. length; var I = dir = 1? 0: (len-1); var limit = dir = 1? Len:-1; while (I! = Limit) {var pt = this. restPoint [I]; if (this. getDis (pt, fromPt) + this. getDis (pt, toPt) === lineLength) {this. drawPoint (pt. x, pt. y); this. lastPoint. push (pt); this. restPoint. splice (I, 1); if (limit> 0) {I --; limit -- ;}} I + = dir ;}} update (po) {// The core transformation method calls this when touchmove. ctx. clearRect (0, 0, this. width, this. height); for (var I = 0; I <this. arr. length; I ++) {// draw the panel from each frame first this. drawCle (this. arr [I]. x, this. arr [I]. y);} this. drawPoint (this. lastPoint); // this. drawLine (po, this. lastPoint); // draw the center of each frame for (var I = 0; I <this. restPoint. length; I ++) {var pt = this. restPoint [I]; if (Math. abs (po. x-pt. x) <this. r & Math. abs (po. y-pt. y) <this. r) {this. drawPoint (pt. x, pt. y); this. pickPoints (this. lastPoint [this. lastPoint. length-1], pt); break ;}} checkPass (psw1, psw2) {// check password var p1 = '', p2 = ''; for (var I = 0; I <psw1.length; I ++) {p1 + = psw1 [I]. index + psw1 [I]. index;} for (var I = 0; I <psw2.length; I ++) {p2 + = psw2 [I]. index + psw2 [I]. index;} return p1 === p2;} storePass (psw) {// process the password and status after touchend if (this. pswObj. step = 1) {if (this. checkPass (this. pswObj. fpassword, psw) {this. pswObj. step = 2; this. pswObj. spassword = psw; this. page. setData ({title: 'password saved successfully'}); this. drawStatusPoint ('# 2cff26'); wx. setStorageSync ('passwordxx', JSON. stringify (this. pswObj. spassword); wx. setStorageSync ('choosetype ', this. chooseType);} else {this. page. setData ({title: 'inconsistent twice, re-enter '}); this. drawStatusPoint ('red'); delete this. pswObj. step;} else if (this. pswObj. step = 2) {if (this. checkPass (this. pswObj. spassword, psw) {this. page. setData ({title: 'unlocked successfully'}); this. drawStatusPoint ('# 2cff26');} else {this. drawStatusPoint ('red'); this. page. setData ({title: 'unlock failed'}) ;}} else {this. pswObj. step = 1; this. pswObj. fpassword = psw; this. page. setData ({title: 'input again '}) ;}} drawStatusPoint (type) {// initialization status line for (var I = 0; I <this. lastPoint. length; I ++) {this. ctx. strokeStyle = type; this. ctx. beginPath (); this. ctx. arc (this. lastPoint [I]. x, this. lastPoint [I]. y, this. r, 0, Math. PI * 2, true); this. ctx. closePath (); this. ctx. stroke (); this. ctx. draw (true) ;}} updatePassword () {wx. removeStorageSync ('passwordxx'); wx. removeStorageSync ('choosetype '); this. pswObj ={}; this. page. setData ({title: 'unlock pattern drawn '}); this. reset ();} reset () {this. makeState (); this. createCircle ();} bindEvent () {var self = this; this. page. onTouchStart = function (e) {var po = self. getPosition (e); self. lastPoint = []; for (var I = 0; I <self. arr. length; I ++) {if (Math. abs (po. x-self. arr [I]. x) <self. r & Math. abs (po. y-self. arr [I]. y) <self. r) {self. touchFlag = true; self. drawPoint (self. arr [I]. x, self. arr [I]. y); self. lastPoint. push (self. arr [I]); self. restPoint. splice (I, 1); break ;}} this. page. onTouchMove = function (e) {if (self. touchFlag) {self. update (self. getPosition (e) ;}} this. page. onTouchEnd = function (e) {if (self. touchFlag) {self. touchFlag = false; self. storePass (self. lastPoint); setTimeout (function () {self. reset () ;}, 300) ;}} module. exports = Locker;

Download DEMO

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

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.