前言
业务中需要用户进行签字,如何让用户在手机端进行签字?
示例如下
代码已分享至gitee: https://gitee.com/lengcz/qianming
h5实现手写签字
创建一个html页面
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <!-- always force latest ie rendering engine (even in intranet) & chrome frame remove this if you use the .htaccess --> <meta http-equiv="x-ua-compatible" content="ie=edge,chrome=1"> <meta name="viewport" content="initial-scale=1.0, target-densitydpi=device-dpi" /><!-- this is for mobile (android) chrome --> <meta name="viewport" content="initial-scale=1.0, width=device-height"><!-- mobile safari, firefox, opera mobile --> <script src="/js/modernizr.js"></script> <!--[if lt ie 9]> <script type="text/javascript" src="libs/flashcanvas.js"></script> <![endif]--> <style type="text/css"> div { margin-top:1em; margin-bottom:1em; } input { padding: .5em; margin: .5em; } select { padding: .5em; margin: .5em; } #signatureparent { color:darkblue; background-color:darkgrey; /*max-width:600px;*/ padding:20px; } /*this is the div within which the signature canvas is fitted*/ #signature { border: 2px dotted black; background-color:lightgrey; } /* drawing the 'gripper' for touch-enabled devices */ html.touch #content { float:left; width:92%; } html.touch #scrollgrabber { float:right; width:4%; margin-right:2%; background-image:url(data:image/png;base64,ivborw0kggoaaaansuheugaaaaeaaaafcaaaaach79ldaaaaaxnsr0iars4c6qaaabjjrefucb1jmmmqxjct4t/dfwaplgoxlrt3iwaaaabjru5erkjggg==) } html.borderradius #scrollgrabber { border-radius: 1em; } </style> </head> <body> <div> <div id="content"> <div id="signatureparent"> <div>jsignature inherits colors from parent element. text = pen color. background = background. (this works even when flash-based canvas emulation is used.)</div> <div id="signature"></div></div> <div id="tools"></div> <div><p>display area:</p><div id="displayarea"></div></div> </div> <div id="scrollgrabber"></div> </div> <script src="/js/jquery-3.2.1.min.js"></script> <script type="text/javascript"> jquery.noconflict() </script> <script> /* @preserve jquery pub/sub plugin by peter higgins (dante@dojotoolkit.org) loosely based on dojo publish/subscribe api, limited in scope. rewritten blindly. original is (c) dojo foundation 2004-2010. released under either afl or new bsd, see: http://dojofoundation.org/license for more information. */ (function($) { var topics = {}; $.publish = function(topic, args) { if (topics[topic]) { var currenttopic = topics[topic], args = args || {}; for (var i = 0, j = currenttopic.length; i < j; i++) { currenttopic[i].call($, args); } } }; $.subscribe = function(topic, callback) { if (!topics[topic]) { topics[topic] = []; } topics[topic].push(callback); return { "topic": topic, "callback": callback }; }; $.unsubscribe = function(handle) { var topic = handle.topic; if (topics[topic]) { var currenttopic = topics[topic]; for (var i = 0, j = currenttopic.length; i < j; i++) { if (currenttopic[i] === handle.callback) { currenttopic.splice(i, 1); } } } }; })(jquery); </script> <script src="/js/jsignature.min.noconflict.js"></script> <script> (function($){ $(document).ready(function() { // this is the part where jsignature is initialized. var $sigdiv = $("#signature").jsignature({'undobutton':true}) // all the code below is just code driving the demo. , $tools = $('#tools') , $extraarea = $('#displayarea') , pubsubprefix = 'jsignature.demo.' var export_plugins = $sigdiv.jsignature('listplugins','export') , chops = ['<span><b>提取签名数据: </b></span><select>','<option value="">(select export format)</option>'] , name for(var i in export_plugins){ if (export_plugins.hasownproperty(i)){ name = export_plugins[i] chops.push('<option value="' + name + '">' + name + '</option>') } } chops.push('</select><span><b> or: </b></span>') $(chops.join('')).bind('change', function(e){ if (e.target.value !== ''){ var data = $sigdiv.jsignature('getdata', e.target.value) $.publish(pubsubprefix + 'formatchanged') if (typeof data === 'string'){ $('textarea', $tools).val(data) } else if($.isarray(data) && data.length === 2){ $('textarea', $tools).val(data.join(',')) $.publish(pubsubprefix + data[0], data); } else { try { $('textarea', $tools).val(json.stringify(data)) } catch (ex) { $('textarea', $tools).val('not sure how to stringify this, likely binary, format.') } } } }).appendto($tools) $('<input type="button" value="reset">').bind('click', function(e){ $sigdiv.jsignature('reset') }).appendto($tools) $('<div><textarea style="width:100%;height:7em;"></textarea></div>').appendto($tools) $.subscribe(pubsubprefix + 'formatchanged', function(){ $extraarea.html('') }) $.subscribe(pubsubprefix + 'image/svg+xml', function(data) { try{ var i = new image() i.src = 'data:' + data[0] + ';base64,' + btoa( data[1] ) $(i).appendto($extraarea) } catch (ex) { } var message = [ "if you don't see an image immediately above, it means your browser is unable to display in-line (data-url-formatted) svg." , "this is not an issue with jsignature, as we can export proper svg document regardless of browser's ability to display it." , "try this page in a modern browser to see the svg on the page, or export data as plain svg, save to disk as text file and view in any svg-capabale viewer." ] $( "<div>" + message.join("<br/>") + "</div>" ).appendto( $extraarea ) }); $.subscribe(pubsubprefix + 'image/svg+xml;base64', function(data) { var i = new image() i.src = 'data:' + data[0] + ',' + data[1] $(i).appendto($extraarea) var message = [ "if you don't see an image immediately above, it means your browser is unable to display in-line (data-url-formatted) svg." , "this is not an issue with jsignature, as we can export proper svg document regardless of browser's ability to display it." , "try this page in a modern browser to see the svg on the page, or export data as plain svg, save to disk as text file and view in any svg-capabale viewer." ] $( "<div>" + message.join("<br/>") + "</div>" ).appendto( $extraarea ) }); $.subscribe(pubsubprefix + 'image/png;base64', function(data) { var i = new image() i.src = 'data:' + data[0] + ',' + data[1] $('<span><b>as you can see, one of the problems of "image" extraction (besides not working on some old androids, elsewhere) is that it extracts a lot of data and includes all the decoration that is not part of the signature.</b></span>').appendto($extraarea) $(i).appendto($extraarea) }); $.subscribe(pubsubprefix + 'image/jsignature;base30', function(data) { $('<span><b>this is a vector format not natively render-able by browsers. format is a compressed "movement coordinates arrays" structure tuned for use server-side. the bonus of this format is its tiny storage footprint and ease of deriving rendering instructions in programmatic, iterative manner.</b></span>').appendto($extraarea) }); if (modernizr.touch){ $('#scrollgrabber').height($('#content').height()) } }) })(jquery) </script> </body> </html>
modernizr.js
/* modernizr 2.5.2 (custom build) | mit & bsd * build: http://www.modernizr.com/download/#-borderradius-csscolumns-canvas-touch-mq-cssclasses-addtest-teststyles-testprop-testallprops-prefixes-domprefixes-fullscreen_api */ ;window.modernizr = function(a, b, c) { function a(a) { j.csstext = a } function b(a, b) { return a(m.join(a + ";") + (b || "")) } function c(a, b) { return typeof a === b } function d(a, b) { return !!~("" + a).indexof(b) } function e(a, b) { for (var d in a) if (j[a[d]] !== c) return b == "pfx" ? a[d] : !0; return !1 } function f(a, b, d) { for (var e in a) { var f = b[a[e]]; if (f !== c) return d === !1 ? a[e] : c(f, "function") ? f.bind(d || b) : f } return !1 } function g(a, b, c) { var d = a.charat(0).touppercase() + a.substr(1) , e = (a + " " + o.join(d + " ") + d).split(" "); return c(b, "string") || c(b, "undefined") ? e(e, b) : (e = (a + " " + p.join(d + " ") + d).split(" "), f(e, b, c)) } var d = "2.5.2", e = {}, f = !0, g = b.documentelement, h = "modernizr", i = b.createelement(h), j = i.style, k, l = {}.tostring, m = " -webkit- -moz- -o- -ms- ".split(" "), n = "webkit moz o ms", o = n.split(" "), p = n.tolowercase().split(" "), q = {}, r = {}, s = {}, t = [], u = t.slice, v, w = function(a, c, d, e) { var f, i, j, k = b.createelement("div"), l = b.body, m = l ? l : b.createelement("body"); if (parseint(d, 10)) while (d--) j = b.createelement("div"), j.id = e ? e[d] : h + (d + 1), k.appendchild(j); return f = ["­", "<style>", a, "</style>"].join(""), k.id = h, m.innerhtml += f, m.appendchild(k), l || g.appendchild(m), i = c(k, a), l ? k.parentnode.removechild(k) : m.parentnode.removechild(m), !!i }, x = function(b) { var c = a.matchmedia || a.msmatchmedia; if (c) return c(b).matches; var d; return w("@media " + b + " { #" + h + " { position: absolute; } }", function(b) { d = (a.getcomputedstyle ? getcomputedstyle(b, null) : b.currentstyle)["position"] == "absolute" }), d }, y = {}.hasownproperty, z; !c(y, "undefined") && !c(y.call, "undefined") ? z = function(a, b) { return y.call(a, b) } : z = function(a, b) { return b in a && c(a.constructor.prototype[b], "undefined") } , function.prototype.bind || (function.prototype.bind = function(b) { var c = this; if (typeof c != "function") throw new typeerror; var d = u.call(arguments, 1) , e = function() { if (this instanceof e) { var a = function() {}; a.prototype = c.prototype; var f = new a , g = c.apply(f, d.concat(u.call(arguments))); return object(g) === g ? g : f } return c.apply(b, d.concat(u.call(arguments))) }; return e } ); var h = function(c, d) { var f = c.join("") , g = d.length; w(f, function(c, d) { var f = b.stylesheets[b.stylesheets.length - 1] , h = f ? f.cssrules && f.cssrules[0] ? f.cssrules[0].csstext : f.csstext || "" : "" , i = c.childnodes , j = {}; while (g--) j[i[g].id] = i[g]; e.touch = "ontouchstart"in a || a.documenttouch && b instanceof documenttouch || (j.touch && j.touch.offsettop) === 9 }, g, d) }([, ["@media (", m.join("touch-enabled),("), h, ")", "{#touch{top:9px;position:absolute}}"].join("")], [, "touch"]); q.canvas = function() { var a = b.createelement("canvas"); return !!a.getcontext && !!a.getcontext("2d") } , q.touch = function() { return e.touch } , q.borderradius = function() { return g("borderradius") } , q.csscolumns = function() { return g("columncount") } ; for (var i in q) z(q, i) && (v = i.tolowercase(), e[v] = q[i](), t.push((e[v] ? "" : "no-") + v)); return e.addtest = function(a, b) { if (typeof a == "object") for (var d in a) z(a, d) && e.addtest(d, a[d]); else { a = a.tolowercase(); if (e[a] !== c) return e; b = typeof b == "function" ? b() : b, g.classname += " " + (b ? "" : "no-") + a, e[a] = b } return e } , a(""), i = k = null, e._version = d, e._prefixes = m, e._domprefixes = p, e._cssomprefixes = o, e.mq = x, e.testprop = function(a) { return e([a]) } , e.testallprops = g, e.teststyles = w, g.classname = g.classname.replace(/(^|\s)no-js(\s|$)/, "$1$2") + (f ? " js " + t.join(" ") : ""), e }(this, this.document), modernizr.addtest("fullscreen", function() { for (var a = 0; a < modernizr._domprefixes.length; a++) if (document[modernizr._domprefixes[a].tolowercase() + "cancelfullscreen"]) return !0; return !!document.cancelfullscreen || !1 });
运行测试用例
到此这篇关于html5手写签名的实现示例的文章就介绍到这了,更多相关html5手写签名内容请搜索代码网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持代码网!
发表评论