canvas 实现手机图案解锁
参考☞: https://www.cnblogs.com/chenyingying0/
先上效果图:

我是在 vue 里面实现js 文件 ,所以如果需要在vue 里面使用 可以将以下内容import '@/component/unlock', 这样直接引入就好,上代码
//component/unlock.js
(function() {
// canvasLock是全局对象
window.canvasLock = function(obj) {
this.width = obj.width
this.height = obj.height
this.circleNum = obj.circleNum
;(this.isOk = true), (this.errrorSum = '0')
}
//动态生成DOM
canvasLock.prototype.initDom = function() {
//创建一个div
var div = document.createElement('div')
let app = document.getElementById('app')
var h4 = "<h4 id='title' class='title'>绘制解锁图案</h4>"
div.innerHTML = h4
div.id = 'unlock'
div.setAttribute(
'style',
'position:absolute;top:0;left:0;right:0;bottom:0;'
) //创建canvas
var canvas = document.createElement('canvas')
canvas.setAttribute('id', 'canvas')
//cssText 的本质就是设置 HTML 元素的 style 属性值
canvas.style.cssText =
'background:#2d3a4b;display:inine-block;margin-top:15px;' div.appendChild(canvas)
app.appendChild(div) //设置canvas默认宽高
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 = '解锁成功'
window.location.href = 'http://localhost:8080/home'
this.drawStatusPoint('lightgreen')
} else {
this.errrorSum++
if (this.errrorSum === 5) {
let num = 30
this.isOk = false
let time = setInterval(() => {
num--
document.getElementById('title').innerHTML = `${num}秒后方可继续解锁`
if (num === 0) {
document.getElementById('title').innerHTML = `绘制图案解锁`
}
}, 1000)
setTimeout(() => {
this.isOk = true
this.errrorSum = 0
clearInterval(time)
}, 30000)
} else if (this.errrorSum <) {
document.getElementById('title').innerHTML = '解锁失败'
this.drawStatusPoint('orange')
}
}
} //判断输入的密码
canvasLock.prototype.checkPass = function() {
var p1 = '72943816', //成功的密码
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()
} //获取鼠标点击处离canvas的距离
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) {
if (this.isOk) {
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] ? this.lastPoint[0].x : '',
this.lastPoint[0] ? 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()
}
})() new canvasLock({ circleNum: 3 }).init()
使用的vue 文件
<template></template>
<script>
import '@/component/unlock'
export default {
created() {
document.getElementById('unlock').style.display = 'block'
},
beforeDestroy() {
document.getElementById('unlock').style.display = 'none'
}
}
</script>
<style>
body {
background: #2d3a4b;
text-align: center;
}
</style>
canvas 实现手机图案解锁的更多相关文章
- 用calc()绘制手机图案解锁的九宫格样式
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Python&Appium实现安卓手机图形解锁
首先,在解锁状态下,建立一个Session,打开APP.然后,调用press_keycode()方法传入整型数值"26",锁定屏幕.通过implicitly_wait()方法等待两 ...
- 长沙理工大学第十二届ACM大赛-重现赛C 安卓图案解锁 (模拟)
链接:https://ac.nowcoder.com/acm/contest/1/C来源:牛客网 安卓图案解锁 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 131072K,其他语言 ...
- Android SimpleAdapter ListView (锁定手机,解锁手机的列表)
SimpleAdapter是扩展性最好的适配器,可以定义各种你想要的布局. 构造方法: SimpleAdapter(Context context, List<? extends Map< ...
- canvas在手机qq浏览器显示错乱
做大转盘的时候,使用html5 canvas 生成转盘,但在手机qq浏览器中显示错乱. 原本想在后台生成大转盘图片,后来想一想既然用图片来实现, 还不如直接由canvas 导出 toDataURL 在 ...
- 关于华为P9手机的解锁、刷Recovery、获取Root、安装Busybox,以及升级降级的全过程(和一些错误的解决方法)
我有一部华为P9手机,型号EVA-TL00,属于移动定制机.用了半年多了,想给手机添加一些功能,发现有些功能必须Root之后才能用代码实现,所以动了Root的打算. 一.手机解锁.(不解锁则无法对手机 ...
- canvas一周一练 -- canvas绘制马尾图案 (5)
运行效果: <!DOCTYPE html> <html> <head> </head> <body> <canvas id=" ...
- 小米手机BL解锁连接不上手机
解锁工具下载页面:http://www.miui.com/unlock/download.html 线刷工具下载页面:http://www.miui.com/shuaji-393.html 额外注意说 ...
- App自动化(2)--Python&Appium实现安卓手机九宫格解锁
九宫格作为常见的手势密码,我们在使用的时候,是从起点开始,按住不放,然后滑动手指,直到最后一个点松开手指,如果与设置的手势密码匹配,则解锁成功. 现在大多数九宫格作为一个元素存在,很难定位到每一个点. ...
随机推荐
- 组件化思路:以selection为例子,使用prop-types实现组件化控制,重用
需求 书接上文,UI 积累之select section 这里又来两个需求了. 当我点击选择了option后,我应该看到的是我选择的option的内容 多例重用,即同样是个selection,我只是需 ...
- FCC 成都社区·前端周刊 第 7 期
01. ES2016, 2017, 2018 中的新特性 文章介绍了 18 个 ECMAScript 2016,2017 和 2018 中新增加的特性,这些特性已被加入到 TC39 提案中.包括Arr ...
- JZOJ 1775. 合并果子2 (Standard IO)
1775. 合并果子2 (Standard IO) Time Limits: 1000 ms Memory Limits: 65536 KB Description 在一个果园里,多多已经将所有的果子 ...
- 广告行业中那些趣事系列6:BERT线上化ALBERT优化原理及项目实践(附github)
摘要:BERT因为效果好和适用范围广两大优点,所以在NLP领域具有里程碑意义.实际项目中主要使用BERT来做文本分类任务,其实就是给文本打标签.因为原生态BERT预训练模型动辄几百兆甚至上千兆的大小, ...
- 数据结构之HashMap
前言 在我们开发中,HashMap是我们非常常用的数据结构,接下来我将进一步去了解HashMap的原理.结构. 1.HashMap的实现原理 HashMap底层是基于Hash表(也称“散列”)的数据结 ...
- R调用C++示例
sourceCpp {Rcpp}:Source C++ Code from a File or String sourceCpp(file = "", code = NULL, e ...
- 用shell脚本实现定时备份数据库
1.备份数据库的方法 可以使用命令查看 ls /usr/local/mysql/bin 这个mysqldump就是系统内置的用来备份数据库的工具. 2.实现方法 ①先随便进入一个位置创建一个目录 ② ...
- git回滚到任意一个版本
1.首先查找提交的记录(-3表示显示最近的3条) git log -3 2.强制回滚到制定版本 git reset --hard 制定版本commitId 如:git reset --hard 4ba ...
- Scikit-learn之特征抽取
一.安装包 pip install Scikit-learn 二.字典特征抽取 1.字典特征抽取 #!/usr/bin/env python # -*- coding: utf-8 -*- #auth ...
- 【分布式锁】03-使用Redisson实现RedLock原理
前言 前面已经学习了Redission可重入锁以及公平锁的原理,接着看看Redission是如何来实现RedLock的. RedLock原理 RedLock是基于redis实现的分布式锁,它能够保证以 ...