参考☞: 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>

最新文章

  1. 【blade04】用面向对象的方法写javascript坦克大战
  2. C#实现队列
  3. 通过Class获取标签,兼容的几种思路
  4. spring4 security 4 +websocket 实现单点登录
  5. UrlConnection连接和Socket连接的区别
  6. libtiff4.04
  7. 夺命雷公狗ThinkPHP项目之----企业网站12之文章添加的实现
  8. cocos2dx win打包apk
  9. android开发艺术探索
  10. poj1014 dp 多重背包
  11. iOS中二维码的生成与使用(入门篇)
  12. RFC2889转发性能測试用例设计和自己主动化脚本实现
  13. 在OpenStack虚拟机实例中创建swap分区的一种方法
  14. 专注VR/AR广告 ,内容感知广告公司Uru获80万美元投资
  15. Eclipse知识
  16. Linux OpenSSH后门的添加与防范
  17. 搭建ssm项目框架
  18. 清理sql2012数据库日志
  19. Java编程:悲观锁、乐观锁的区别及使用场景
  20. struts2在配置文件与JSP中用OGNL获取Action属性

热门文章

  1. LeetCode 81.Search in Rotated Sorted Array II(M)
  2. 把.net Core 项目迁移到VS2019 for MAC
  3. 观察者模式(Observer)和发布-订阅者模式(Publish/Subscribe)区别
  4. Vue Snackbar 消息条队列显示,依次动画消失的实现
  5. centos7下pymysql安装
  6. 简单易用的图像解码库介绍 —— stb_image
  7. 【python系统学习10】布尔值
  8. C语言程序设计(五) 选择控制结构
  9. 今天建了一个Python学习交流的QQ群,求喜欢python的一起来交流。
  10. WPF 启动缓慢问题