IT俱乐部 HTML5 html5手写签名的实现示例

html5手写签名的实现示例

前言

业务中需要用户进行签字,如何让用户在手机端进行签字?
示例如下

代码已分享至Gitee: https://gitee.com/lengcz/qianming

H5实现手写签字

创建一个html页面



    
    
    
        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;
        }
    
jSignature inherits colors from parent element. Text = Pen color. Background = Background. (This works even when Flash-based Canvas emulation is used.)

Display Area:

jQuery.noConflict() /* @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); (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 = ['提取签名数据: ','(select export format)'] , name for(var i in export_plugins){ if (export_plugins.hasOwnProperty(i)){ name = export_plugins[i] chops.push('' + name + '') } } chops.push(' or: ') $(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) $('').bind('click', function(e){ $sigdiv.jSignature('reset') }).appendTo($tools) $('