首页 > 其他分享 >html canvas签名

html canvas签名

时间:2025-01-23 16:44:48浏览次数:1  
标签:canvas self height html 签名 signature var document

<!DOCTYPE html>
<html>
<head>
<title>手写签名demo</title>
<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 {
padding:0;
margin:0;
width: 100%;
height: 100%;
}
.text-center {
text-align: center;
}
#signature-button {
display: block;
margin: 0 auto;
}
#signature-img {
width: 200px;
height: 100px;
margin: 10px auto;
display: none;
}
#signature-pop {
position: fixed;
background-color: #fff;
top:0;
left:0;
bottom:0;
right:0;
width: 100%;
height: 100%;
display: none;
}
#signature-container {
width: 100%;
height: 100%;
}
.demo-top-info {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 40px;
background: #cccccc;
box-sizing: border-box;
text-align: center;
}
.demo-top-info .return {
padding-left: 5px;
height: 40px;
line-height: 40px;
position: absolute;
left: 10px;
display: inline-block;
}

.signature-title {
height: 40px;
line-height: 40px;
display: inline-block;
}

.canvas-container {
width: 100%;
height: 100%;
background-color: #ffffff;
box-sizing: border-box;
padding: 40px 0 40px 0;
}

.canvas-parent {
width: 100%;
height: 100%;
}

.demo-bottom-info {
position: absolute;
bottom:0;
left:0;
width: 100%;
height: 40px;
background: #cccccc;
padding: 0 2%;
box-sizing: border-box;
overflow: hidden;
}

#clean_canvas {
float: left;
font-size: 18px;
margin-top: 2px;
background: #f8f8f8;
border: 1px solid #ddd;
padding: 5px 10px;
border-radius: 4px;
}

#sure_canvas {
float: right;
padding: 3px 0;
background: #00005F;
padding: 5px 10px;
border: 1px solid #00005F;
border-radius: 4px;
color: #fff;
margin-top: 3px;
}

#canvas2 {
position: absolute;
top: 0;
left:0;
z-index: 999;
margin-left: 40px;
}

.signature-result-img{
border: 1px solid #000000;
width: 99vw;
}

.buttonDiv{
margin-right: 5vw;
width: 20vw;
height: 5vh;
background-color: #ed0d0d;
border: 1px solid #eee;
}
</style>
</head>
<body>
<div class="app-container" style="padding-top: 10vh;">

<div class="signature-result-img">
<img src="" id="signature-img" alt="签名预览"/>
</div>
<div style="padding-top: 5vh; display: flex; justify-content: center; align-items: center;">
<button class="buttonDiv" onclick="anewSignature()">重新签名</button>
<button class="buttonDiv" onclick="saveSignature()">提交</button>

</div>
</div>
<div id="signature-pop">
<div id="signature-container">
<div class="demo-top-info" id="rotate-container">
<div class="return" id="return" onclick="history.back()">返回</div>
<div class="signature-title text-center">手写签名</div>
</div>
<div class="canvas-container">
<div class="canvas-parent">
<canvas id="canvas"></canvas>
</div>
</div>
<div class="demo-bottom-info">
<div id="clean_canvas">清空</div>
<div id="sure_canvas">确认</div>
</div>
</div>
<canvas id="canvas2"></canvas>
</div>

</body>
<script type="text/javascript">
var confirm = false
// 手绘签名类
var SignatureClass = (function(){

//获取当前位置(返回px)
function getStyle(obj, attr) {
if (obj.currentStyle) {
return obj.currentStyle[attr];
} else {
return getComputedStyle(obj, false)[attr];
}
}

function Signatrue(options) {
this.cavnas = null;
this.ctx = null;
this.maxparams = {};
this.minparams = {};
this.entershuping = true; //是否是竖屏 true=是
this.firstTouch = true; // 第一次点击或touch
this.allowSubmit = false; // 是否手写了内容 是=才能提交 否=不能提交
if(options === void 0) {
options = {};
}
this.options = options;
}

// 初始化
Signatrue.prototype.init = function() {
var self = this;
console.log(getStyle(document.getElementById("rotate-container"), "height")); // 不知道为什么加,否则有些Apple出问题
this.hengshuping();
this.clear();
this.submit();

window.addEventListener('resize',function() {
self.hengshuping();
},false);
}

// 判断横竖屏
Signatrue.prototype.hengshuping = function() {
if (window.orientation == 90 || window.orientation == -90) {
this.entershuping = false; // 横屏
document.getElementById("canvas2").style.display = "none";
document.getElementById("canvas").style.display = "block";
this.canvas = document.getElementById("canvas");
this.ctx = this.canvas.getContext("2d");
this.ctx.lineWidth = 1;
this.ctx.strokeStyle = "#000000";
this.drawEvent();
document.querySelector("#signature-container").style.width = window.innerWidth + "px";
document.querySelector("#signature-container").style.height = window.innerHeight + "px";
this.canvas.height = parseInt(getStyle(document.getElementById("canvas").parentNode, 'height'));
this.canvas.width = parseInt(getStyle(document.getElementById("canvas").parentNode, 'width'));
document.querySelector("#signature-container").style.transform = "rotate(0deg)";
document.querySelector("#signature-container").style.transformOrigin = "0 0";
document.querySelector("#signature-container").style.marginLeft = "0px";
} else { // 竖屏
this.entershuping = true;
document.getElementById("canvas").style.display = "none";
document.getElementById("canvas2").style.display = "block";
this.canvas = document.getElementById("canvas2");
this.ctx = this.canvas.getContext("2d");
this.canvas.height = window.innerHeight;
this.canvas.width = window.innerWidth - 40 - 40;
this.ctx.lineWidth = 1;
this.ctx.strokeStyle = "#000000";
this.drawEvent();
document.querySelector("#signature-container").style.width = window.innerHeight + "px";
document.querySelector("#signature-container").style.height = window.innerWidth + "px";
document.querySelector("#signature-container").style.transform = "rotate(90deg)";
document.querySelector("#signature-container").style.transformOrigin = "0 0";
document.querySelector("#signature-container").style.marginLeft = window.innerWidth + "px";
}
}

// 绘画事件
Signatrue.prototype.drawEvent = function() {
var self = this;
this.canvas.addEventListener("touchstart", function(evt) {
var oEvent = evt || event;
oEvent.preventDefault();
var position = self.pos(oEvent);
self.ctx.beginPath();
self.ctx.moveTo(position.x, position.y);
if (self.firstTouch) {
self.minparams = {
x: position.x,
y: position.y
};
self.maxparams = {
x: position.x,
y: position.y
};
self.firstTouch = false;
} else {
self.judgeSize(position);
}
self.canvas.addEventListener("touchmove", touchmove, false)

function touchmove(evt) {
var oEvent = evt || event;
oEvent.preventDefault();
var position = self.pos(oEvent);
self.ctx.lineTo(position.x, position.y);
self.judgeSize(position);
self.ctx.stroke();
}
document.addEventListener("touchend", touchend, false)

function touchend(evt) {
var oEvent = evt || event;
oEvent.preventDefault();
self.allowSubmit = true;
self.canvas.removeEventListener("touchmove", touchmove);
document.removeEventListener("touchend", touchend);
}
})
}

// 清空画笔
Signatrue.prototype.clear = function() {
var self = this;
document.getElementById("clean_canvas").addEventListener("click", function() {
self.ctx.clearRect(0, 0, self.canvas.width, self.canvas.height);
self.allowSubmit = false;
})
}

// 提交画笔内容
Signatrue.prototype.submit = function() {
var self = this;
document.getElementById("sure_canvas").addEventListener("click", function() {
if (self.allowSubmit) {
var imgSRC = self.canvas.toDataURL('image/png', 1);
var img = new Image();
img.src = imgSRC;
self.ctx.clearRect(0, 0, self.canvas.width, self.canvas.height);
img.onload = function() {
var width = self.maxparams.x - self.minparams.x;
var height = self.maxparams.y - self.minparams.y;
var targetWidth = 200;
var targetHeight = 100;
var clipCanvas = document.createElement("canvas");
clipCanvas.width = targetWidth;
clipCanvas.height = targetHeight;
var ctx2 = clipCanvas.getContext("2d");
if (self.entershuping) {
ctx2.translate(0, targetHeight);
ctx2.rotate(-90 * Math.PI / 180);
ctx2.drawImage(img, self.minparams.x, self.minparams.y, width, height, 0, 0, targetHeight, targetWidth);
} else {
ctx2.drawImage(img, self.minparams.x, self.minparams.y, width, height, 0, 0, targetWidth, targetHeight);
}
var imgSRC2 = clipCanvas.toDataURL('image/png', 1);
if(self.options && self.options.callback && typeof self.options.callback == 'function') {
self.options.callback(imgSRC2);
}
self.canvas = null;
self.ctx = null;
ctx2 = null;
clipCanvas = null;

}
} else {
alert("没有手写签名,不能提交!")
}
})
}


// 获取当前位置
Signatrue.prototype.pos = function(event) {
var x = event.touches[0].pageX - event.target.offsetLeft;
var y = event.touches[0].pageY - event.target.offsetTop;
return {
x: x,
y: y
};
}

// 优化画笔界限,去掉空白区域
Signatrue.prototype.judgeSize = function(value) {
if (this.minparams.x > value.x) {
this.minparams.x = value.x;
}
if (this.maxparams.x < value.x) {
this.maxparams.x = value.x;
}
if (this.minparams.y > value.y) {
this.minparams.y = value.y;
}
if (this.maxparams.y < value.y) {
this.maxparams.y = value.y;
}
}
return Signatrue;
})();

document.getElementById('signature-pop').style.display = 'block';
var signatrueDraw = new SignatureClass({
callback: function(base64) {
document.getElementById('signature-img').src = base64;
document.getElementById('signature-pop').style.display = 'none';
document.getElementById('signature-img').style.display = 'block';
}
});
signatrueDraw.init();

// 重新签名
function anewSignature() {
document.getElementById('signature-pop').style.display = 'block';
var signatrueDraw = new SignatureClass({
callback: function(base64) {
document.getElementById('signature-img').src = base64;
document.getElementById('signature-pop').style.display = 'none';
document.getElementById('signature-img').style.display = 'block';
}
});
signatrueDraw.init();
}

// 保存签名
function saveSignature() {

}

// 退出
function goToReturn(){

}
</script>
</html>

标签:canvas,self,height,html,签名,signature,var,document
From: https://www.cnblogs.com/mlx819/p/18688106

相关文章

  • 【Html.js——功能实现】为图片添加景深效果(蓝桥杯真题-2161)【合集】
    目录......
  • 请问如何修改HTML网站模板?
    要修改HTML网站模板,您需要具备一定的HTML和CSS知识。以下是一些基本的步骤:下载模板:从合法的来源下载HTML网站模板,并解压缩到本地计算机。打开模板文件:使用文本编辑器(如Notepad++、SublimeText等)打开模板文件。通常,您需要编辑的文件包括HTML文件(如index.html)和CSS文件(如style.c......
  • SM9 - 数字签名算法
    符号A,B:使用标识密码系统的两个用户。\(cf\):椭圆曲线阶相对于\(N\)的余因子。\(cid\):用一个字节表示的曲线的标识符,其中\(\mbox{0x10}\)表示\(F_p\)(素数\(P>2^{191}\))上常曲线(即非超奇异曲线),\(\mbox{0x11}\)表示\(F_p\)表示超奇异曲线,\(\mbox{0x12}\)表示\(F_p\)上常曲线及其扭......
  • 抽取网易云音乐热门评论:html+css+python+js 音乐与灵魂的碰撞
    抽取网易云音乐热门评论:html+css+python+js音乐与灵魂的碰撞代码链接不说废话,上代码!!!get.py#get.pyimporturllib.requestimporturllib.errorimporturllib.parseimportjson#抓取网易云音乐指定url的热评defget_hotComments(id):url='https://music.163.......
  • 抽取网易云音乐热门评论(附代码):html+css+python+js 音乐与灵魂的碰撞
    抽取网易云音乐热门评论:html+css+python+js音乐与灵魂的碰撞代码链接不说废话,上代码!!!get.py#get.pyimporturllib.requestimporturllib.errorimporturllib.parseimportjson#抓取网易云音乐指定url的热评defget_hotComments(id):url='https://musi......
  • HTML表单相关知识
    表单的基本结构标签名标签语义常用属性单/双标签form表单action:用于指定表单的提交地址(需与后端人员沟通确定)method:用于控制表单的提交方式target:用于控制表单如何打开页面,常用值如下:_self:在本页签打开页面       _blank:在新页签打开页面双input输入框ty......
  • html css网页制作成品——HTML+CSS+js书亦烧仙草的茶网页设计(6页)附源码
    目录     一、......
  • html5怎样和Native App进行交互?
    HTML5与NativeApp的交互主要发生在HybridApp(混合应用)中,这种应用结合了原生应用的性能和Web应用的跨平台优势。在HybridApp中,HTML5页面通常通过WebView组件嵌入,并与原生代码进行交互。以下是一些实现HTML5与NativeApp交互的常见方法:1.JavaScript与原生代码交互原生代码调用......
  • canvas画出来的图是位图还是矢量图?
    在前端开发中,canvas画出来的图是位图。以下是对此问题的详细解释:位图与矢量图的区别:位图(Bitmap):由像素点组成,每个像素点都有自己的颜色和位置。放大位图时,可以看到构成图像的像素点,图像可能会变得模糊。矢量图(VectorGraphics):由数学公式定义的几何形状组成,如线条、多边形和曲......
  • 请说说canvas的closePath()与lineTo()的区别是什么?
    在前端开发中,Canvas是一个非常有用的工具,它允许开发者在网页上绘制图形。closePath()和lineTo()是CanvasRenderingContext2D接口中的两个方法,它们在绘制路径时有着不同的作用。lineTo(x,y)lineTo(x,y)方法用于在画布上创建一条从当前点到指定(x,y)坐标的直线。当......