参考☞: 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 实现手机图案解锁的更多相关文章

  1. 用calc()绘制手机图案解锁的九宫格样式

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. Python&Appium实现安卓手机图形解锁

    首先,在解锁状态下,建立一个Session,打开APP.然后,调用press_keycode()方法传入整型数值"26",锁定屏幕.通过implicitly_wait()方法等待两 ...

  3. 长沙理工大学第十二届ACM大赛-重现赛C 安卓图案解锁 (模拟)

    链接:https://ac.nowcoder.com/acm/contest/1/C来源:牛客网 安卓图案解锁 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 131072K,其他语言 ...

  4. Android SimpleAdapter ListView (锁定手机,解锁手机的列表)

    SimpleAdapter是扩展性最好的适配器,可以定义各种你想要的布局. 构造方法: SimpleAdapter(Context context, List<? extends Map< ...

  5. canvas在手机qq浏览器显示错乱

    做大转盘的时候,使用html5 canvas 生成转盘,但在手机qq浏览器中显示错乱. 原本想在后台生成大转盘图片,后来想一想既然用图片来实现, 还不如直接由canvas 导出 toDataURL 在 ...

  6. 关于华为P9手机的解锁、刷Recovery、获取Root、安装Busybox,以及升级降级的全过程(和一些错误的解决方法)

    我有一部华为P9手机,型号EVA-TL00,属于移动定制机.用了半年多了,想给手机添加一些功能,发现有些功能必须Root之后才能用代码实现,所以动了Root的打算. 一.手机解锁.(不解锁则无法对手机 ...

  7. canvas一周一练 -- canvas绘制马尾图案 (5)

    运行效果: <!DOCTYPE html> <html> <head> </head> <body> <canvas id=" ...

  8. 小米手机BL解锁连接不上手机

    解锁工具下载页面:http://www.miui.com/unlock/download.html 线刷工具下载页面:http://www.miui.com/shuaji-393.html 额外注意说 ...

  9. App自动化(2)--Python&Appium实现安卓手机九宫格解锁

    九宫格作为常见的手势密码,我们在使用的时候,是从起点开始,按住不放,然后滑动手指,直到最后一个点松开手指,如果与设置的手势密码匹配,则解锁成功. 现在大多数九宫格作为一个元素存在,很难定位到每一个点. ...

随机推荐

  1. Typescript 01 安装与使用

    ---恢复内容开始--- 一. 介绍 1. TypeScript 是由微软开发的一款开源的编程语言. 2. TypeScript 是 Javascript 的超级,遵循最新的 ES6.Es5 规范.T ...

  2. ArrayBuffer转base64详解

    先贴代码: const base64String = window.btoa(String.fromCharCode(... new Uint8Array(buffer))) 看起来非常的简洁,优美. ...

  3. 20170809-从URL输入到页面展现

    从URL输入到页面展现 1.输入URL URL:统一资源定位符,是对可以从互联网上得到的资源的位置和访问方法的一种简洁的表示. URL包含以下几部分:协议.服务器名称(或IP地址).路径.参数和查询. ...

  4. go结构体继承组合和匿名字段

    1.结构体方法 go不是纯粹的面向对象的,在go里面函数是一等公民,但是go也有结构体实现类似java一样类的功能来提供抽象.结构体的方法分为值方法和指针方法,前者在方法中做的改变不会改变调用的实例对 ...

  5. WEB渗透 - XSS

    听说这个时间点是人类这种生物很重要的一个节点 cross-site scripting 跨站脚本漏洞 类型 存储型(持久) 反射型(非持久) DOM型 利用 先检测,看我们输入的内容是否有返回以及有无 ...

  6. IntelliJ IDEA 2018.3 x64的破解和安装

    IntelliJ IDEA 2018.3 x64的破解和安装 前言 IntelliJ IDEA 作为一个优秀的Java开发环境,深受许多开发者喜爱,但是它的价格却贵得让人无法接受,这篇文章将介绍永久激 ...

  7. Go语言转义字符

    \a 匹配响铃符 (相当于 \x07) 注意:正则表达式中不能使用 \b 匹配退格符,因为 \b 被用来匹配单词边界, 可以使用 \x08 表示退格符. \f 匹配换页符 (相当于 \x0C) \t ...

  8. ggplot2(8) 精雕细琢

    8.1 主题 主题系统控制着图形中的非数据元素外观,它不会影响几何对象和标度等数据元素.这题不能改变图形的感官性质,但它可以使图形变得更具美感,满足整体一致性的要求.主题的控制包括标题.坐标轴标签.图 ...

  9. 手撸MyBatis从配置文件到读出数据库的模拟实现

    手动模拟MyBatis入门案例的底层实现: 需要了解的关键技术: java反射.动态代理(comming soon) 一.Mybatis入门案例 点击此处跳过入门案例 首先看一下MyBatis最基础的 ...

  10. jQuery实现鼠标移入切换图片

    初始效果: 鼠标移入效果: 首先添加jQuery库,我这边直接引用百度CDN地址 <script src="https://apps.bdimg.com/libs/jquery/2.1 ...