JavaScript 手势解锁

举报
福州司马懿 发表于 2021/11/19 02:19:25 2021/11/19
【摘要】 <!DOCTYPE html> <html> <head> <meta charset="utf8"> <style> * { margi...

在这里插入图片描述

<!DOCTYPE html>
<html>
<head>
<meta charset="utf8">
<style>
* {
	margin: 0;
	padding: 0;
}
#canvas {
	width: 100%;
	height: 100%;
	/*! 要想让元素铺满全屏,要么用absolute的100%,要么用100vh */
	position: absolute;
}
</style>
<script>
class GestureUnlock {
	
	constructor(canvas) {
		//圆圈直径/画布比例
		this.circleSizeRatio = 0.1;
		//圆圈内部镂空直径/圆圈半径
		this.circleHollowRatio = 0.5;
		//圆圈边框线宽/圆圈半径
		this.circleLineWidthRatio = 0.1;
		//画笔线宽/圆圈内部镂空半径
		this.lineWidthRatio = 1;
		//线条颜色
		this.color = "RoyalBlue";
		//行数
		this.numRow = 3;
		//列数
		this.numColumn = 5;
		//圆圈的坐标数组(x,y)
		this.pointPos = [];
		//已经选中的圈编号(i),规则:先横后竖。保存手势的时候只要保存该变量即可
		this.points = [];
		//起始按下是否在某个圆圈的范围
		this.isDownOnPoint = false;
		
		this.doublePI = 2*Math.PI;
		this.canvas = canvas;

		this.initListener();
		this.setData();
		this.draw();
	}
	
	setData() {
		//必须要设置canvas的宽高,而不能用style的宽高,否则默认会按300×150缩放
		//注意:当canvas的大小改变时,ctx就会重置,包括之前设置的样式会全部被重置
		this.canvas.width = this.canvas.clientWidth;
		this.canvas.height = this.canvas.clientHeight;
		const size = Math.min(this.canvas.clientWidth, this.canvas.clientHeight);
		//圆圈的直径
		const circleWidth = size * this.circleSizeRatio;
		this.circleR = circleWidth/2;
		//连接线可填充的孔洞半径
		this.circleHollowR = this.circleR * this.circleHollowRatio;
		//圆圈外径和中间可填充的孔洞中间的半径
		this.circleMiddleR = (this.circleR + this.circleHollowR) / 2;
		//圆圈的边框线条宽度
		this.circleLineWidth = this.circleR * this.circleLineWidthRatio;
		//圆圈填充白色的宽度
		this.circlePadWidth = this.circleR - this.circleHollowR;
		//连接线宽度
		this.lineWidth = this.circleHollowR * this.lineWidthRatio;
		//行的一个间隔长度
		this.rowPad = this.canvas.clientHeight/(this.numRow*2);
		//列的一个间隔长度
		this.columnPad = this.canvas.clientWidth/(this.numColumn*2);
		//console.log(this.rowPad, this.columnPad, this.circleR);
		
		this.ctx = canvas.getContext("2d");
		this.ctx.strokeStyle = "RoyalBlue";
		this.ctx.fillStyle = this.color;
		//线条两端的笔帽(lineCap)支持butt(默认)、round、square,但是默认的拐点会突出
		this.ctx.lineCap = "round";
		//两条线条交汇处的样式(lineJoin)支持miter(默认,尖角)、round、bevel(斜角或缺角)
		this.ctx.lineJoin = "bevel"
	}
	
	drawCircle() {
		this.pointPos = [];
		for(let i=0;i<this.numRow;i++) {	
			for(let j=0;j<this.numColumn;j++) {	
				const x = (2*j+1)*this.columnPad;
				const y = (2*i+1)*this.rowPad;
				this.pointPos.push([x, y]);
				//console.log(x, y)
				this.ctx.beginPath();
				this.ctx.strokeStyle = "white";
				this.ctx.lineWidth = this.circlePadWidth;
				this.ctx.arc(x, y, this.circleMiddleR, 0, this.doublePI);
				this.ctx.stroke();
				
				this.ctx.beginPath();
				this.ctx.strokeStyle = this.color;
				this.ctx.lineWidth = this.circleLineWidth;
				this.ctx.arc(x, y, this.circleR, 0, this.doublePI);
				this.ctx.stroke();			
			}
		}
	}
	
	drawLine(x, y) {
		const that = this;
		function getPointPos(i) {
			return that.pointPos[that.points[i]];
		}
	
		if(this.points.length == 0) return;
		for(let i=0;i<this.points.length;i++) {
			//ES6的自动拆包
			this.ctx.beginPath();
			this.ctx.arc(...getPointPos(i), this.circleHollowR, 0, this.doublePI);
			this.ctx.fill();
		}
		
		this.ctx.beginPath();
		this.ctx.lineWidth = this.lineWidth;
		//ES6的自动拆包
		this.ctx.moveTo(...getPointPos(0));
		for(let i=1;i<this.points.length;i++) {
			this.ctx.lineTo(...getPointPos(i));
		}
		
		if(x != undefined && y != undefined) {
			this.ctx.lineTo(x, y);
		}
		this.ctx.stroke();
	}
	
	draw(x, y) {
		this.ctx.clearRect(0,0,this.canvas.width, this.canvas.height);
		this.drawLine(x, y);
		this.drawCircle();
	}
	
	indexOfPoint(x,y) {
		if(this.pointPos.length == 0) throw new Error("未找到圆圈坐标数组");
		//为了减少计算量,将每个圆当作正方形来看
		for(let i=0;i<this.pointPos.length;i++) {
			if((Math.abs(x - this.pointPos[i][0]) < this.circleR) && 
				(Math.abs(y - this.pointPos[i][1]) < this.circleR)) {
				return i;
			}
		}
		return -1;
	}
	
	pushToPoints(index) {
		if(index == -1 || this.points.includes(index)) return false;
		this.points.push(index);
		return true;
	}
	
	initListener() {
		//内嵌箭头函数(即匿名函数)是为了继续使用this
		this.canvas.addEventListener("mousedown", (e)=>{this.touchStart(e);});
		this.canvas.addEventListener("mousemove", (e)=>{this.touchMove(e);});
		this.canvas.addEventListener("mouseup", (e)=>{this.touchEnd(e);});
		this.canvas.addEventListener("touchstart", (e)=>{this.touchStart(e);});
		this.canvas.addEventListener("touchmove", (e)=>{this.touchMove(e);});
		this.canvas.addEventListener("touchend", (e)=>{this.touchEnd(e);});
		
		window.addEventListener("resize", ()=>{	this.setData(); this.draw(); });
	}
	
	//兼容PC端和手机端的获取单击点坐标
	getEventPos(event) {
		const x = event.clientX || event.touches[0].clientX;
		const y = event.clientY || event.touches[0].clientY;
		return [x,y];
	}
	
	touchStart(e) {
		const [x,y] = this.getEventPos(e);
		this.points = [];
		const index = this.indexOfPoint(x, y);
		this.isDownOnPoint = this.pushToPoints(index);
	}
	
	touchMove(e) {
		if(!this.isDownOnPoint) return;
		const [x,y] = this.getEventPos(e);
		const index = this.indexOfPoint(x, y);
		this.pushToPoints(index);
		this.draw(x, y);
	}
	
	touchEnd(e) {
		if(!this.isDownOnPoint) return;
		this.isDownOnPoint = false;
		this.draw();
		if(this.points.length > 1) {
			/*
			浏览器中弹出的对话框都是模态对话框(Modal Dialogue Box,又叫做模式对话框)
			是指在用户想要对对话框以外的应用程序进行操作时,必须首先对该对话框进行响应。
			*/
			//conform 遵守;confirm 确认
			//const ret = confirm("点击的顺序是:" + this.points.join(" -> "));
			alert("点击的顺序是:" + this.points.join(" -> "));
			this.points = [];
			this.draw();
		}
	}
}
window.()=>{
	const canvas = document.getElementById("canvas");
	const gestureUnlock = new GestureUnlock(canvas);
	
}

</script>
</head>
<body>
<canvas id="canvas"></canvas>
</body>
</html>

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221

文章来源: blog.csdn.net,作者:福州-司马懿,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/chy555chy/article/details/86552453

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。