永利402游戏网站

当前位置:永利402游戏网站-永利402com官方网站 > 永利402游戏网站 > HTML5兑现显示器手势解锁

HTML5兑现显示器手势解锁

来源:http://www.xtcsyb.com 作者:永利402游戏网站-永利402com官方网站 时间:2019-09-20 02:13

HTML5贯彻显示屏手势解锁

2015/07/18 · HTML5 · 1 评论 · 手势解锁

原稿出处: AlloyTeam   

成效展现

图片 1

落到实处原理 利用HTML5的canvas,将解锁的框框划出,利用touch事件解锁这几个层面,直接看代码。

JavaScript

function createCircle() {// 创立解锁点的坐标,依照canvas的分寸来平均分配半径 var n = chooseType;// 画出n*n的矩阵 lastPoint = []; arr = []; restPoint = []; r = ctx.canvas.width / (2 + 4 * n);// 公式总括 半径和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; }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
function createCircle() {// 创建解锁点的坐标,根据canvas的大小来平均分配半径
 
        var n = chooseType;// 画出n*n的矩阵
        lastPoint = [];
        arr = [];
        restPoint = [];
        r = ctx.canvas.width / (2 + 4 * n);// 公式计算 半径和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;
    }

canvas里的圈子画好之后方可开展事件绑定

JavaScript

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) { // 用来决断开始点是还是不是在规模内部 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); }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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) { // 用来判断起始点是否在圈圈内部
 
                    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);
    }

继而到了最要紧的步调绘制解锁路线逻辑,通过touchmove事件的接连不断触发,调用canvas的moveTo方法和lineTo方法来画出折现,同一时等候法庭判果断是还是不是达到大家所画的框框里面,当中lastPoint保存不易的层面路线,restPoint保存全体范围去除准确路径之后剩余的。 Update方法:

JavaScript

function update(po) {// 宗旨转移格局在touchmove时候调用 ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height); for (var i = 0 ; i < arr.length ; i++) { // 每帧先把面板画出来 drawCle(arr[i].x, arr[i].y); } drawPoint(lastPoint);// 每帧花轨迹 drawLine(po , lastPoint);// 每帧画圆心 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; } } }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function update(po) {// 核心变换方法在touchmove时候调用
        ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
 
        for (var i = 0 ; i < arr.length ; i++) { // 每帧先把面板画出来
            drawCle(arr[i].x, arr[i].y);
        }
 
        drawPoint(lastPoint);// 每帧花轨迹
        drawLine(po , lastPoint);// 每帧画圆心
 
        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;
            }
        }
 
    }

最终正是截至职业,把门路里面包车型大巴lastPoint保存的数组产生密码存在localstorage里面,之后就用来处掌握锁验证逻辑了

JavaScript

function storePass(psw) {// touchend甘休之后对密码和气象的拍卖 if (pswObj.step == 1) { if (checkPass(pswObj.fpassword, psw)) { pswObj.step = 2; pswObj.spassword = psw; document.getElementById('title').innerHTML = '密码保存成功'; drawStatusPoint('#2CFF26'); window.localStorage.setItem('passwordx', JSON.stringify(pswObj.spassword)); window.localStorage.setItem('chooseType', chooseType); } else { document.getElementById('title').innerHTML = '三遍不等同,重新输入'; drawStatusPoint('red'); delete pswObj.step; } } else if (pswObj.step == 2) { if (checkPass(pswObj.spassword, psw)) { document.getElementById('title').innerHTML = '解锁成功'; drawStatusPoint('#2CFF26'); } else { drawStatusPoint('red'); document.getElementById('title').innerHTML = '解锁退步'; } } else { pswObj.step = 1; pswObj.fpassword = psw; document.getElementById('title').innerHTML = '再度输入'; } }

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
function storePass(psw) {// touchend结束之后对密码和状态的处理
        if (pswObj.step == 1) {
            if (checkPass(pswObj.fpassword, psw)) {
                pswObj.step = 2;
                pswObj.spassword = psw;
                document.getElementById('title').innerHTML = '密码保存成功';
                drawStatusPoint('#2CFF26');
                window.localStorage.setItem('passwordx', JSON.stringify(pswObj.spassword));
                window.localStorage.setItem('chooseType', chooseType);
            } else {
                document.getElementById('title').innerHTML = '两次不一致,重新输入';
                drawStatusPoint('red');
                delete pswObj.step;
            }
        } else if (pswObj.step == 2) {
            if (checkPass(pswObj.spassword, psw)) {
                document.getElementById('title').innerHTML = '解锁成功';
                drawStatusPoint('#2CFF26');
            } else {
                drawStatusPoint('red');
                document.getElementById('title').innerHTML = '解锁失败';
            }
        } else {
            pswObj.step = 1;
            pswObj.fpassword = psw;
            document.getElementById('title').innerHTML = '再次输入';
        }
 
    }

解锁组件

将那几个HTML5解锁写成了二个零部件,放在

二维码体验: 图片 2

 

参照他事他说加以考察资料:

1 赞 4 收藏 1 评论

图片 3

图片 4

福衢寿车原理选用HTML5的canvas,将解锁的规模划出,利用touch事件解锁那一个层面,直接看代码。

functioncreateCircle(){// 创制解锁点的坐标,根据canvas的分寸来平均分配半径

varn = chooseType;// 画出n*n的矩阵

lastPoint = [];

arr = [];

restPoint = [];

r = ctx.canvas.width / (2+4* n);// 公式计算 半径和canvas的轻重缓急有关

for(vari =0; i < n ; i++) {

for(varj =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;

}

canvas里的圈子画好现在方可扩充事件绑定

functionbindEvent(){

can.addEventListener("touchstart",function(e){

varpo = getPosition(e);

console.log(po);

for(vari =0; i < arr.length ; i++) {

if(Math.abs(po.x - arr[i].x) < r &&Math.abs(po.y - arr[i].y) < r) {// 用来决断开端点是还是不是在规模内部

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);

}

紧接着到了最要紧的步调绘制解锁路径逻辑,通过touchmove事件的随处触发,调用canvas的moveTo方法和lineTo方法来画出折现,同不经常间剖断是或不是到达大家所画的层面里面,在那之中lastPoint保存不易的范畴路线,restPoint保存全体规模去除准确路径之后剩余的。 Update方法:

functionupdate(po){// 大旨转移格局在touchmove时候调用

ctx.clearRect(0,0, ctx.canvas.width, ctx.canvas.height);

for(vari =0; i < arr.length ; i++) {// 每帧先把面板画出来

drawCle(arr[i].x, arr[i].y);

}

drawPoint(lastPoint);// 每帧花轨迹

drawLine(po , lastPoint);// 每帧画圆心

for(vari =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;

}

}

}

终极正是结束职业,把路子里面包车型客车lastPoint保存的数组变成密码存在localstorage里面,之后就用来处精晓锁验证逻辑了

functionstorePass(psw){// touchend甘休今后对密码和景色的处理

if(pswObj.step ==1) {

if(checkPass(pswObj.fpassword, psw)) {

pswObj.step =2;

pswObj.spassword = psw;

document.getElementById('title').innerHTML ='密码保存成功';

drawStatusPoint('#2CFF26');

window.localStorage.setItem('passwordx',JSON.stringify(pswObj.spassword));

window.localStorage.setItem('chooseType', chooseType);

}else{

document.getElementById('title').innerHTML ='四遍不等同,重新输入';

drawStatusPoint('red');

deletepswObj.step;

}

}elseif(pswObj.step ==2) {

if(checkPass(pswObj.spassword, psw)) {

document.getElementById('title').innerHTML ='解锁成功';

drawStatusPoint('#2CFF26');

}else{

drawStatusPoint('red');

document.getElementById('title').innerHTML ='解锁退步';

}

}else{

pswObj.step =1;

pswObj.fpassword = psw;

document.getElementById('title').innerHTML ='再一次输入';

}

}

本文由永利402游戏网站-永利402com官方网站发布于永利402游戏网站,转载请注明出处:HTML5兑现显示器手势解锁

关键词:

上一篇:一个前端开发工程师的Vim跟IDE一样

下一篇:没有了