jquery支持手机端手写签名签字保存效果代码

代码语言:html

所属分类:其他

代码描述:jquery支持手机端手写签名签字保存效果代码,可一个字一个字地签。

代码标签: jquery 签名 签字 手机端

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<style>
   html,
body {
	/* 重置默认样式 */
	margin: 0;
	padding: 0;
	background: #eee;
	font-size: 18px;
}

html,
body,
#mycanvas {
	width: 100%;
	height: 100%;
	overflow: hidden;
	background: #FFF;
}

.name-block {
	width: 30px;
	background: #FFF;
	height: 30px;
	display: inline-block;
}

.btn-name {
	width: 50px;
	height: 50px;
}

#test2 {
	width: 100%;
	overflow: hidden;
	margin-top: 10px;
	text-align: center;
}

.btns {
	margin: 0 auto;
	text-align: center;
	margin-top: 20px;
}

.canvas-sign {
	background-color: #2E2E2E;
	position: fixed;
	top: 0;
	left: 0;
	right: 0;
	bottom: 0;
	display: none;
	z-index: 20;
}

* {
	touch-action: pan-y;
}

#canvas {
	margin: 0 auto;
}

#controller {
	position: relative;
	clear: both;
}

#clear_btn {
	/*float: right;
							  					  */
	display: inline-block;
	margin-right: 0;
}

.colorList {
	clear: both;

}

.img-block {
	display: none;
}

#signPeo {
	line-height: 30px;
	height: 30px;
	width: 48px;
	background: #00aaff;
	border-radius: 4px;
	font-size: 16px;
	color: #fff;
	text-align: center;
	margin-right: 3px;
}

.btns button {
	border-radius: 4px !important;
	box-sizing: border-box;
	padding: 10px 20px;
	/* outline: navajowhite; */
	border: 0;
	font-size: 18px;
}

#images {
	background-color: #2E2E2E;
	position: fixed;
	top: 0;
	left: 0;
	right: 0;
	bottom: 0;
	display: none;
	z-index: 30;
}

#images-copy {
	position: absolute;
	top: 30%;
	bottom: 35%;
	left: 10%;
	right: 10%;
	background: #fff;
	border-radius: 6px;
	box-sizing: border-box;
	padding: 10px;
}

.next-imgs {
	display: flex;
	justify-content: center;
}

</style>
</head>

<body>
    <div style="text-align: center;
			display: flex;
			justify-content: center;
			align-items: center; ">
        <p id="signPeo" class="initBlok">签名</p>
        <p style="line-height:30px" class="initBlok">:</p>
        <div id="next-img" style="display: flex;
					justify-content: center;">
            <img class="name-block " id="canvas0"></img>
        </div>
    </div>
    <div class="canvas-sign">
        <div style="text-align: center; ">
            <p style="color:#FFFFFF">请逐字手签:</p>
            <div class="next-img">
                <img class="name-block btn-name" id="ca0"></img>
            </div>
        </div>
        <div id="test2">
            <p style="color:#FFFFFF">签字区域:</p>
            <div id="mycanvas">
                <canvas id="canvas"></canvas>
            </div>
        </div>
        <div class="btns">
            <!-- <button type="button" id="clear" onclick="reset()">重写</button> -->
            <button id="clear_btn" onclick="reset()" class="op_btn">清 除</button>
            <div class="next-t" style="display: inline-block;margin:0 10px">

            </div>
            <button type="button" id="comp">保存</button>
        </div>
    </div>
    <div id="images">
        <div id="images-copy">
            <p>确认以下签名字数是否完整,若不是则点击取消后,补全后再次保存</p>
            <div class="next-imgs">

            </div>
            <div class="btns">
                <button type="button" style="color: #fff;background: #333333;margin-right:5px" id="cancel">取消</button>
                <button type="button" style="color: #fff;background: #00aaff;margin-left:5px" id="submit">确定</button>
            </div>
        </div>
    </div>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/jquery-3.2.1.min.js"></script>
    <script type="text/javascript">
       var canvas = document.getElementById("canvas");
	var canvasWidth = Math.min(500, $(window).width() - 200);
	var cxt = canvas.getContext("2d");
	canvas.width = canvasWidth;
	canvas.height = canvas.width;
	$("#controller").css("width", canvasWidth + "px")
	var isMouseDown = false;
	var lastLoc = {
		x: 0,
		y: 0
	}; //起笔坐标 
	var lastTime = 0; //用于计算时间
	var lastLineWidth = -1;
	var lineColor = "black";
	drawGrid(cxt);

	function beginStroke(point) {
		isMouseDown = true;
		lastLoc = windowToCanvas(point.x, point.y);
		lastTime = new Date().getTime();
	}

	function endStroke() {
		isMouseDown = false;
	}

	function moveStroke(point) {
		var curLoc = windowToCanvas(point.x, point.y);
		var curTime = new Date().getTime();
		var s = calcDistance(curLoc, lastLoc); //获取笔锋经过的距离
		var t = curTime - lastTime; //获取笔锋经过的时间 ,用于计算行笔速度,赋值不同的宽度
		var lineWidth = calcLineWidth(t, s);


		cxt.beginPath();
		cxt.moveTo(lastLoc.x, lastLoc.y);
		cxt.lineTo(curLoc.x, curLoc.y);
		cxt.strokeStyle = lineColor;
		cxt.lineWidth = lineWidth;
		cxt.lineCap = "round";
		cxt.lineJoin = "round";
		cxt.stroke();
		cxt.closePath();

		lastLoc = curLoc;
		lastTime = curTime;
		lastLineWidth = lineWidth;
	}
	canvas.onmousedown = function(e) {
		e.preventDefault();
		beginStroke({
			x: e.clientX,
			y: e.clientY
		});
	}
	canvas.onmouseup = function(e) {
		endStroke();
		e.preventDefault();
	}
	canvas.onmouseout = function(e) {
		endStroke();
		e.preventDefault();
	}
	canvas.onmousemove = function(e) {
		e.preventDefault();
		if (isMouseDown) {
			moveStroke({
				x: e.clientX,
				y: e.clientY
			});
		}
	}
	// 触屏事件
	canvas.addEventListener("touchstart", func.........完整代码请登录后点击上方下载按钮下载查看

网友评论0