HTML5实现屏幕手势解锁(转载)

https://github.com/lvming6816077/H5lock
How to use?

<script type="text/javascript" src="src/H5lock.publish.js">
var opt = {
chooseType: 3, // 3 , 4 , 5,
width: 300, // lock wrap width
height: 300, // lock wrap height
container: 'element', // the id attribute of element
inputEnd: function(psw){} // when draw end param is password string
}
var lock = new H5lock(opt);
lock.init();

lock.drawStatusPoint('notright') // draw the last notright circle

lock.drawStatusPoint('right') // draw the last right circle

lock.reset() // reset the lock

http://threejs.org/examples/
http://www.inf.usi.ch/phd/wettel/codecity-download.html (JSCity:把源码可视化成建筑物的 JS 库)
http://www.alloyteam.com/2015/07/html5-shi-xian-ping-mu-shou-shi-jie-suo/ (Web前端 腾讯AlloyTeam Blog )
http://top.jobbole.com/22960/(JSCity:把源码可视化成建筑物的 JS 库)

JS:

(function(){
window.H5lock = function(obj){
this.height = obj.height;
this.width = obj.width;
this.chooseType = Number(window.localStorage.getItem('chooseType')) || obj.chooseType;
}; H5lock.prototype.drawCle = function(x, y) { // 初始化解锁密码面板
this.ctx.strokeStyle = '#CFE6FF';
this.ctx.lineWidth = 2;
this.ctx.beginPath();
this.ctx.arc(x, y, this.r, 0, Math.PI * 2, true);
this.ctx.closePath();
this.ctx.stroke();
}
H5lock.prototype.drawPoint = function() { // 初始化圆心
for (var i = 0 ; i < this.lastPoint.length ; i++) {
this.ctx.fillStyle = '#CFE6FF';
this.ctx.beginPath();
this.ctx.arc(this.lastPoint[i].x, this.lastPoint[i].y, this.r / 2, 0, Math.PI * 2, true);
this.ctx.closePath();
this.ctx.fill();
}
}
H5lock.prototype.drawStatusPoint = function(type) { // 初始化状态线条
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();
}
}
H5lock.prototype.drawLine = function(po, lastPoint) {// 解锁轨迹
this.ctx.beginPath();
this.ctx.lineWidth = 3;
this.ctx.moveTo(this.lastPoint[0].x, this.lastPoint[0].y);
console.log(this.lastPoint.length);
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(); }
H5lock.prototype.createCircle = function() {// 创建解锁点的坐标,根据canvas的大小来平均分配半径 var n = this.chooseType;
var count = 0;
this.r = this.ctx.canvas.width / (2 + 4 * n);// 公式计算
this.lastPoint = [];
this.arr = [];
this.restPoint = [];
var r = this.r;
for (var i = 0 ; i < n ; i++) {
for (var j = 0 ; j < n ; j++) {
count++;
var obj = {
x: j * 4 * r + 3 * r,
y: i * 4 * r + 3 * r,
index: count
};
this.arr.push(obj);
this.restPoint.push(obj);
}
}
this.ctx.clearRect(0, 0, this.ctx.canvas.width, this.ctx.canvas.height);
for (var i = 0 ; i < this.arr.length ; i++) {
this.drawCle(this.arr[i].x, this.arr[i].y);
}
//return arr;
}
H5lock.prototype.getPosition = function(e) {// 获取touch点相对于canvas的坐标
var rect = e.currentTarget.getBoundingClientRect();
var po = {
x: e.touches[0].clientX - rect.left,
y: e.touches[0].clientY - rect.top
};
return po;
}
H5lock.prototype.update = function(po) {// 核心变换方法在touchmove时候调用
this.ctx.clearRect(0, 0, this.ctx.canvas.width, this.ctx.canvas.height); for (var i = 0 ; i < this.arr.length ; i++) { // 每帧先把面板画出来
this.drawCle(this.arr[i].x, this.arr[i].y);
} this.drawPoint(this.lastPoint);// 每帧花轨迹
this.drawLine(po , this.lastPoint);// 每帧画圆心 for (var i = 0 ; i < this.restPoint.length ; i++) {
if (Math.abs(po.x - this.restPoint[i].x) < this.r && Math.abs(po.y - this.restPoint[i].y) < this.r) {
this.drawPoint(this.restPoint[i].x, this.restPoint[i].y);
this.lastPoint.push(this.restPoint[i]);
this.restPoint.splice(i, 1);
break;
}
} }
H5lock.prototype.checkPass = function(psw1, psw2) {// 检测密码
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;
}
H5lock.prototype.storePass = function(psw) {// touchend结束之后对密码和状态的处理
if (this.pswObj.step == 1) {
if (this.checkPass(this.pswObj.fpassword, psw)) {
this.pswObj.step = 2;
this.pswObj.spassword = psw;
document.getElementById('title').innerHTML = '密码保存成功';
this.drawStatusPoint('#2CFF26');
window.localStorage.setItem('passwordxx', JSON.stringify(this.pswObj.spassword));
window.localStorage.setItem('chooseType', this.chooseType);
} else {
document.getElementById('title').innerHTML = '两次不一致,重新输入';
this.drawStatusPoint('red');
delete this.pswObj.step;
}
} else if (this.pswObj.step == 2) {
if (this.checkPass(this.pswObj.spassword, psw)) {
document.getElementById('title').innerHTML = '解锁成功';
this.drawStatusPoint('#2CFF26');
} else {
this.drawStatusPoint('red');
document.getElementById('title').innerHTML = '解锁失败';
}
} else {
this.pswObj.step = 1;
this.pswObj.fpassword = psw;
document.getElementById('title').innerHTML = '再次输入';
} }
H5lock.prototype.makeState = function() {
if (this.pswObj.step == 2) {
document.getElementById('updatePassword').style.display = 'block';
//document.getElementById('chooseType').style.display = 'none';
document.getElementById('title').innerHTML = '请解锁';
} else if (this.pswObj.step == 1) {
//document.getElementById('chooseType').style.display = 'none';
document.getElementById('updatePassword').style.display = 'none';
} else {
document.getElementById('updatePassword').style.display = 'none';
//document.getElementById('chooseType').style.display = 'block';
}
}
H5lock.prototype.setChooseType = function(type){
chooseType = type;
init();
}
H5lock.prototype.updatePassword = function(){
window.localStorage.removeItem('passwordxx');
window.localStorage.removeItem('chooseType');
this.pswObj = {};
document.getElementById('title').innerHTML = '绘制解锁图案';
this.reset();
}
H5lock.prototype.initDom = function(){
var wrap = document.createElement('div');
var str = '<h4 id="title" class="title">绘制解锁图案</h4>'+
'<a id="updatePassword" style="position: absolute;right: 5px;top: 5px;color:#fff;font-size: 10px;display:none;">重置密码</a>'+
'<canvas id="canvas" width="300" height="300" style="display: inline-block;margin-top: 15px;"></canvas>';
wrap.setAttribute('style','position: absolute;top:0;left:0;right:0;bottom:0;');
wrap.innerHTML = str;
document.body.appendChild(wrap);
}
H5lock.prototype.init = function() {
this.initDom();
this.pswObj = window.localStorage.getItem('passwordxx') ? {
step: 2,
spassword: JSON.parse(window.localStorage.getItem('passwordxx'))
} : {};
this.lastPoint = [];
this.makeState();
this.touchFlag = false;
this.canvas = document.getElementById('canvas');
this.ctx = this.canvas.getContext('2d');
this.createCircle();
this.bindEvent();
}
H5lock.prototype.reset = function() {
this.makeState();
this.createCircle();
}
H5lock.prototype.bindEvent = function() {
var self = this;
this.canvas.addEventListener("touchstart", function (e) {
e.preventDefault();// 某些android 的 touchmove不宜触发 所以增加此行代码
var po = self.getPosition(e);
console.log(po);
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;
}
}
}, false);
this.canvas.addEventListener("touchmove", function (e) {
if (self.touchFlag) {
self.update(self.getPosition(e));
}
}, false);
this.canvas.addEventListener("touchend", function (e) {
if (self.touchFlag) {
self.touchFlag = false;
self.storePass(self.lastPoint);
setTimeout(function(){ self.reset();
}, 300);
} }, false);
document.addEventListener('touchmove', function(e){
e.preventDefault();
},false);
document.getElementById('updatePassword').addEventListener('click', function(){
self.updatePassword();
});
}
})();

  HTML5:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<title>H5lock</title>
<style type="text/css">
body {
text-align: center;
background-color: #305066;
}
.title {
color: #22C3AA;
}
</style> </head>
<body>
<script type="text/javascript" src="javascripts/H5lock.js"></script>
<script type="text/javascript">
//http://www.nihaoshijie.com.cn/index.php/archives/537http://www.nihaoshijie.com.cn/index.php/archives/537
/*
http://top.jobbole.com/22960/
http://threejs.org/examples/
http://www.inf.usi.ch/phd/wettel/codecity-download.html
https://github.com/lvming6816077/H5lock
http://www.alloyteam.com/2015/07/html5-shi-xian-ping-mu-shou-shi-jie-suo/
*/
new H5lock({
chooseType: 3
}).init();
</script>
</body>
</html>

HTML5实现屏幕手势解锁的更多相关文章

  1. HTML5实现屏幕手势解锁(转载)

    来源:https://github.com/lvming6816077/H5lockhttp://threejs.org/examples/http://www.inf.usi.ch/phd/wett ...

  2. OC屏幕手势解锁

    感觉屏幕解锁好像很牛的样子,所以试着写了一个,代码很简单,手势用到的也是原生的,如果该代码帮助了你,记得点赞,如果该代码有任何问题,也可以随时和我联系.改代码用到的两张图片,是我随便找的两张,可以自行 ...

  3. iOS-高仿支付宝手势解锁(九宫格)

    概述 高仿支付宝手势解锁, 通过手势枚举去实现手势密码相对应操作. 详细 代码下载:http://www.demodashi.com/demo/10706.html 基上篇[TouchID 指纹解锁] ...

  4. SJGestureUnlock快速集成手势解锁

    前言:如果页面显示不完整或图片看不了还请移步:简书 SJGestureUnlock.h 常用自定义属性 @interface SJGestureUnlock : UIView @property (n ...

  5. Quartz2D复习(二) --- 手势解锁

    这次支付宝手机客户端升级,把手势解锁那个功能去掉了,引起很多人的抱怨,觉得少了手势解锁的保护,个人信息容易泄漏了... 那么手势解锁功能是怎么是实现的呢,这里使用Quart2D来简单模拟一下, 先看下 ...

  6. iOS--开发之手势解锁

    本文主要介绍通过手势识别实现手势解锁功能,这个方法被广泛用于手机解锁,密码验证,快捷支付等功能实现.事例效果如下所示. 首先,我们先分析功能的实现过程,首先我们需要先看大致的实现过程: 1.加载九宫格 ...

  7. 2016-1-10 手势解锁demo的实现

    一:实现自定义view,在.h,.m文件中代码如下: #import <UIKit/UIKit.h> @class ZLLockView; @protocol ZLLockViewDele ...

  8. iOS绘制手势解锁密码

    手势解锁这个功能其实已经用的越来越少了.但是郁闷不知道我公司为什么每次做一个app都要把手势解锁加上.....于是就自己研究了一下手势解锁页面的实现.. 要想实现这个页面,先说说需要掌握哪些: UIP ...

  9. [iOS UI进阶 - 5.0] 手势解锁Demo

    A.需求 1.九宫格手势解锁 2.使用了绘图和手势事件   code source: https://github.com/hellovoidworld/GestureUnlockDemo     B ...

随机推荐

  1. 【Unity3D游戏开发】之游戏目录结构之最佳实践和优化 (十一)

    游戏目录结构之最佳实践 前置条件 1.多人协作开发,git管理 2.游戏不大,所有Scene合并到一起Scene中,eg.RoleScene.MapScene.StoreScene 3.Master一 ...

  2. PCL点云库:Kd树

    Kd树按空间划分生成叶子节点,各个叶子节点里存放点数据,其可以按半径搜索或邻区搜索.PCL中的Kd tree的基础数据结构使用了FLANN以便可以快速的进行邻区搜索.FLANN is a librar ...

  3. VB6中的引用传递 与 VB.NET中的引用传递的区别

    首先注意一点,在VB6中缺省参数传递的方式是:引用传递,而在VB.NET中缺省参数传递的方式是:值传递. 然后我们看下面VB6中的引用传递与VB.NET中的引用传递的对比. VB6中的引用传递 Pri ...

  4. collectionView布局原理及瀑布流布局方式

    一直以来都想研究瀑布流的具体实现方法(起因是因为一则男女程序员应聘的笑话,做程序的朋友应该都知道).最近学习到了瀑布流的实现方法,瀑布流的实现方式有多种,这里应用collectionView来重写其U ...

  5. centos7 安装mysql5.7.16

    1 下载地址http://dev.mysql.com/downloads/mysql/#downloads 2. 解压 -linux-glibc2.-x86_64.tar.gz 3.移动解压出来的文件 ...

  6. laravel 中 与前端的一些事3 之使用Gulp编译less

    更多关于less的使用方法,参考上一篇scss的使用,两者大同小异

  7. int转string

    #include<iostream> #include<sstream> #include<string> using namespace std; int mai ...

  8. hdu 1532(最大流)

    Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  9. Project2010简易操作指南[转]

    Microsoft Office Project 2010 操作手册 英文界面版 一.启动阶段 1. 前期准备 (1)新建项目文件 选择 File — NewNew 菜单, 选择项目模版 打开项目文件 ...

  10. iOS开发UI篇—xib的简单使用

    iOS开发UI篇—xib的简单使用 一.简单介绍 xib和storyboard的比较,一个轻量级一个重量级. 共同点: 都用来描述软件界面 都用Interface Builder工具来编辑 不同点: ...