(
function
(){
window.canvasLock=
function
(obj){
this
.width=obj.width;
this
.height=obj.height;
this
.circleNum=obj.circleNum;
}
canvasLock.prototype.initDom=
function
(){
var
div=document.createElement(
"div"
);
var
h4=
"<h4 id='title' class='title'>绘制解锁图案</h4>"
;
div.innerHTML=h4;
div.setAttribute(
"style"
,
"position:absolute;top:0;left:0;right:0;bottom:0;"
);
var
canvas=document.createElement(
"canvas"
);
canvas.setAttribute(
"id"
,
"canvas"
);
canvas.style.cssText=
"background:pink;display:inine-block;margin-top:15px;"
;
div.appendChild(canvas);
document.body.appendChild(div);
var
width=
this
.width||300;
var
height=
this
.height||300;
canvas.style.width=width+
"px"
;
canvas.style.height=height+
"px"
;
canvas.width=width;
canvas.height=height;
}
canvasLock.prototype.drawCircle=
function
(x,y){
this
.ctx.strokeStyle=
"#abcdef"
;
this
.ctx.lineWidth=2;
this
.ctx.beginPath();
this
.ctx.arc(x,y,
this
.r,0,2*Math.PI,
true
);
this
.ctx.closePath();
this
.ctx.stroke();
}
canvasLock.prototype.createCircle=
function
(){
var
n=
this
.circleNum;
var
count=0;
this
.r=
this
.canvas.width/(4*n+2);
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:(4*j+3)*r,
y:(4*i+3)*r,
index:count
};
this
.arr.push(obj);
this
.restPoint.push(obj);
}
}
this
.ctx.clearRect(0,0,
this
.canvas.width,
this
.canvas.height);
for
(
var
i=0;i<
this
.arr.length;i++){
this
.drawCircle(
this
.arr[i].x,
this
.arr[i].y);
}
}
canvasLock.prototype.bindEvent=
function
(){
var
self=
this
;
this
.canvas.addEventListener(
"touchstart"
,
function
(e){
var
po=self.getPosition(e);
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.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.storePass(self.lastPoint);
setTimeout(
function
(){
self.reset();
},300);
}
},
false
);
}
canvasLock.prototype.storePass=
function
(){
if
(
this
.checkPass()){
document.getElementById(
"title"
).innerHTML=
"解锁成功"
;
this
.drawStatusPoint(
"lightgreen"
);
}
else
{
document.getElementById(
"title"
).innerHTML=
"解锁失败"
;
this
.drawStatusPoint(
"orange"
);
}
}
canvasLock.prototype.checkPass=
function
(){
var
p1=
"123"
,
p2=
""
;
for
(
var
i=0;i<
this
.lastPoint.length;i++){
p2+=
this
.lastPoint[i].index;
}
return
p1===p2;
}
canvasLock.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,2*Math.PI,
true
);
this
.ctx.closePath();
this
.ctx.stroke();
}
}
canvasLock.prototype.reset=
function
(){
this
.createCircle();
}
canvasLock.prototype.getPosition=
function
(e){
var
rect=e.currentTarget.getBoundingClientRect();
var
po={
x:(e.touches[0].clientX-rect.left),
y:(e.touches[0].clientY-rect.top)
};
return
po;
}
canvasLock.prototype.update=
function
(po){
this
.ctx.clearRect(0,0,
this
.canvas.width,
this
.canvas.height);
for
(
var
i=0;i<
this
.arr.length;i++){
this
.drawCircle(
this
.arr[i].x,
this
.arr[i].y);
}
this
.drawPoint();
this
.drawLine(po);
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
.lastPoint.push(
this
.restPoint[i]);
this
.restPoint.splice(i,1);
break
;
}
}
}
canvasLock.prototype.drawPoint=
function
(){
for
(
var
i=0;i<
this
.lastPoint.length;i++){
this
.ctx.fillStyle=
"#abcdef"
;
this
.ctx.beginPath();
this
.ctx.arc(
this
.lastPoint[i].x,
this
.lastPoint[i].y,
this
.r/2,0,2*Math.PI,
true
);
this
.ctx.closePath();
this
.ctx.fill();
}
}
canvasLock.prototype.drawLine=
function
(po){
this
.ctx.beginPath();
this
.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();
}
canvasLock.prototype.init=
function
(){
this
.initDom();
this
.canvas=document.getElementById(
"canvas"
);
this
.ctx=
this
.canvas.getContext(
"2d"
);
this
.touchFlag=
false
;
this
.createCircle();
this
.bindEvent();
}
})();