您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

详解HTML5中canvas支持触摸屏的签名面板的示例代码

2024/3/19 16:48:09发布65次查看
1.前言       最近实在是太忙了,从国庆之后的辞职,在慢慢的找工作,到今天在现在的这家公司上班大半个月了,太多的心酸泪无以言表,面试过程中,见到的坑货公司是一家又一家,好几家公司自己都只是上一天班就走了,其中有第一天上班就加班到10点的,有一家公司在体育西路那边,太远,第一天回家挤公交也是太累,以前上班都是走路上班的,自己确实不适合挤公交,还有的公司面试的时候和你说什么大数据,性能优化什么的,进公司一看,他们就是用的最简单的三层,没有什么设计模式,总之太多心酸,幸运的是现在这家公司还不错,找工作就是要宁缺毋滥。
2.canvcas标签canvas基础教程:5ba626b379994d53f7acf72a64f9b697 标签定义图形,比如图表和其他图像。html5 的 canvas 元素使用 javascript 在网页上绘制图像。甚至可以在 canvas 上创建并操作动画,这不是使用画笔和油彩所能够实现的。跨所有 web 浏览器的完整 html5 支持还没有完成,但在新兴的支持中,canvas 已经可以在几乎所有现代浏览器上良好运行。canvas 拥有多种绘制路径、矩形、圆形、字符以及添加图像的方法。来公司一个月了主要也是学习为主,我是做后台开发,以前也没有用过oracle数据库,这一个月也主要是学习h5的一些新特新,还有就是css3.0,在就是oracle在服务器上面的安装部署,一些数据导入导出,数据备份啥的,前端的东西都比较差,现在也是一个学习的机会,就当好好学习了。
3.手写签名面板公司做的是自动化办公oa系统,一些审核的地方需要加入一些手写签名的功能,刚开始做这个也是没有思路,在网上也找了一下资料,后来发现h5有这个canvcas新标签,感到格外是欣喜。于是拿过来试一下,还真可以。
4.页面代码@{ layout = null; }<!doctype html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>testpage</title> <script src="~/assets/jquery-2.1.1.js"></script> <script src="~/assets/bootstrap/bootstrap.js"></script> <link href="~/assets/bootstrap/bootstrap.css" rel="stylesheet" /> <script src="~/scripts/writingpad.js"></script> <script src="~/assets/jq-signature.js"></script> </head> <body style="background-color:#b6ff00"> <button class="btn btn-primary" type="button" style="position:relative">点击我</button> </body> </html> <script> $(function () { $(".btn,.btn-primary").click(function () { var wp = new writingpad(); //wp.init(); }); });</script>
5.脚本代码一/** * 功能:签名canvas面板初始化,为writingpad.js手写面板js服务。 * 作者:黄金锋 (549387177@qq.com) * 日期:2015-11-15 15:51:01 * 版本:version 1.0 */ (function (window, document, $) { 'use strict'; // get a regular interval for drawing to the screen window.requestanimframe = (function (callback) { return window.requestanimationframe || window.webkitrequestanimationframe || window.mozrequestanimationframe || window.orequestanimationframe || window.msrequestanimaitonframe || function (callback) { window.settimeout(callback, 1000/60); }; })(); /* * plugin constructor */ var pluginname = 'jqsignature', defaults = { linecolor: '#222222', linewidth: 1, border: '1px dashed #ccff99', background: '#ffffff', width: 500, height: 200, autofit: false }, canvasfixture = '<canvas></canvas>'; function signature(element, options) { // dom elements/objects this.element = element; this.$element = $(this.element); this.canvas = false; this.$canvas = false; this.ctx = false; // drawing state this.drawing = false; this.currentpos = { x: 0, y: 0 }; this.lastpos = this.currentpos; // determine plugin settings this._data = this.$element.data(); this.settings = $.extend({}, defaults, options, this._data); // initialize the plugin this.init(); } signature.prototype = { // initialize the signature canvas init: function() { // set up the canvas this.$canvas = $(canvasfixture).appendto(this.$element); this.$canvas.attr({ width: this.settings.width, height: this.settings.height }); this.$canvas.css({ boxsizing: 'border-box', width: this.settings.width + 'px', height: this.settings.height + 'px', border: this.settings.border, background: this.settings.background, cursor: 'crosshair' }); // fit canvas to width of parent if (this.settings.autofit === true) { this._resizecanvas(); } this.canvas = this.$canvas[0]; this._resetcanvas(); // set up mouse events this.$canvas.on('mousedown touchstart', $.proxy(function(e) { this.drawing = true; this.lastpos = this.currentpos = this._getposition(e); }, this)); this.$canvas.on('mousemove touchmove', $.proxy(function(e) { this.currentpos = this._getposition(e); }, this)); this.$canvas.on('mouseup touchend', $.proxy(function(e) { this.drawing = false; // trigger a change event var changedevent = $.event('jq.signature.changed'); this.$element.trigger(changedevent); }, this)); // prevent document scrolling when touching canvas $(document).on('touchstart touchmove touchend', $.proxy(function(e) { if (e.target === this.canvas) { e.preventdefault(); } }, this)); // start drawing var that = this; (function drawloop() { window.requestanimframe(drawloop); that._rendercanvas(); })(); }, // clear the canvas clearcanvas: function() { this.canvas.width = this.canvas.width; this._resetcanvas(); }, // get the content of the canvas as a base64 data url getdataurl: function() { return this.canvas.todataurl(); }, reloaddata: function () { this.$canvas.remove(); this._data = this.$element.data(); //for (var i in this.settings) { // alert(i+":"+this.settings[i]); //} //this.settings = $.extend({}, defaults, this._data); this.init(); }, // get the position of the mouse/touch _getposition: function(event) { var xpos, ypos, rect; rect = this.canvas.getboundingclientrect(); event = event.originalevent; // touch event if (event.type.indexof('touch') !== -1) { // event.constructor === touchevent xpos = event.touches[0].clientx - rect.left; ypos = event.touches[0].clienty - rect.top; } // mouse event else { xpos = event.clientx - rect.left; ypos = event.clienty - rect.top; } return { x: xpos, y: ypos }; }, // render the signature to the canvas _rendercanvas: function() { if (this.drawing) { this.ctx.moveto(this.lastpos.x, this.lastpos.y); this.ctx.lineto(this.currentpos.x, this.currentpos.y); this.ctx.stroke(); this.lastpos = this.currentpos; } }, // reset the canvas context _resetcanvas: function() { this.ctx = this.canvas.getcontext("2d"); this.ctx.strokestyle = this.settings.linecolor; this.ctx.linewidth = this.settings.linewidth; }, // resize the canvas element _resizecanvas: function() { var width = this.$element.outerwidth(); this.$canvas.attr('width', width); this.$canvas.css('width', width + 'px'); } }; /* * plugin wrapper and initialization */ $.fn[pluginname] = function ( options ) { var args = arguments; if (options === undefined || typeof options === 'object') { return this.each(function () { if (!$.data(this, 'plugin_' + pluginname)) { $.data(this, 'plugin_' + pluginname, new signature( this, options )); } }); } else if (typeof options === 'string' && options[0] !== '_' && options !== 'init') { var returns; this.each(function () { var instance = $.data(this, 'plugin_' + pluginname); if (instance instanceof signature && typeof instance[options] === 'function') { var myarr=array.prototype.slice.call( args, 1 ); returns = instance[options].apply(instance, myarr); } if (options === 'destroy') { $.data(this, 'plugin_' + pluginname, null); } //if (options === 'reloaddata') { // //this.$canvas.remove(); // $.data(this, 'plugin_' + pluginname, null); // this._data = this.$element.data(); // this.settings = $.extend({}, defaults, options, this._data); // this.init(); //} }); return returns !== undefined ? returns : this; } }; })(window, document, jquery);
6.脚本代码二/** * 功能:使用该jquery插件来制作在线签名或涂鸦板,用户绘制的东西可以用图片的形式保存下来。 * 作者:黄金锋 (549387177@qq.com) * 日期:2015-11-16 13:51:01 * 版本:version 1.0 */ var writingpad = function () { var current = null; $(function () { inithtml(); inittable(); initsignature(); if ($(".modal")) { $(".modal").modal("toggle"); } else { alert("没用手写面板"); } $(document).on("click", "#myclose,.close", null, function () { $('#mymodal').modal('hide'); $("#mymodal").remove(); }); $(document).on("click", "#mysave", null, function () { var myimg = $('#myimg').empty(); var dataurl = $('.js-signature').jqsignature('getdataurl'); var img = $('<img>').attr('src', dataurl); $(myimg).append($('<p>').text("图片保存在这里")); $(myimg).append(img); }); $(document).on("click", "#myempty", null, function () { $('.js-signature').jqsignature('clearcanvas'); }); $(document).on("click", "#mybackcolor", null, function () { $('#colorpanel').css('left', '95px').css('top', '45px').css("display", "block").fadein(); //$("canvas").css("background", "#eeeeee"); $("#btnsave").data("sender", "#mybackcolor"); }); $(document).on("click", "#mycolor", null, function () { $('#colorpanel').css('left', '205px').css('top', '45px').css("display", "block").fadein(); $("#btnsave").data("sender", "#mycolor"); }); $(document).on("mouseover", "#mytable", null, function () { if ((event.srcelement.tagname == "td") && (current != event.srcelement)) { if (current != null) { current.style.backgroundcolor = current._background } event.srcelement._background = event.srcelement.style.backgroundcolor; //$("input[name=discolor]").css("background-color", event.srcelement.style.backgroundcolor); //var color = event.srcelement.style.backgroundcolor; //var strarr = color.substring(4, color.length - 1).split(','); //var num = showrgb(strarr); //$("input[name=hexcolor]").val(num); current = event.srcelement; } }); $(document).on("mouseout", "#mytable", null, function () { if (current != null) current.style.backgroundcolor = current._background }); $(document).on("click", "#mytable", null, function () { if (event.srcelement.tagname == "td") { var color = event.srcelement._background; if (color) { $("input[name=discolor]").css("background-color", color); var strarr = color.substring(4, color.length - 1).split(','); var num = showrgb(strarr); $("input[name=hexcolor]").val(num); } } }); $(document).on("click", "#btnsave", null, function () { $('#colorpanel').css("display", "none"); var typedata = $("#btnsave").data("sender"); var hexcolor = $("input[name=hexcolor]").val(); var data = $(".js-signature").data(); if (typedata == "#mycolor") { data["plugin_jqsignature"]["settings"]["linecolor"] = hexcolor; $('.js-signature').jqsignature('reloaddata'); } if (typedata == "#mybackcolor") { data["plugin_jqsignature"]["settings"]["background"] = hexcolor; $('.js-signature').jqsignature('reloaddata'); } }); $("#mymodal").on('hide.bs.modal', function () { $("#colorpanel").remove(); $("#mymodal").remove(); $("#mytable").remove(); }); }); function inithtml() { var html = '<div class="modal" id="mymodal">' + '<div class="modal-dialog">' + '<div class="modal-content">' + '<div class="modal-header">' + '<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span> <span class="sr-only">close</span></button>' + '<h4 class="modal-title">手写面板</h4>' + '</div>' + '<div class="modal-body">' + '<div class="js-signature" id="mysignature">' + '</div>' + '<div>' + '<button type="button" class="btn btn-default" id="myempty">清空面板</button>' + '<button type="button" class="btn btn-default" id="mybackcolor">设置背景颜色</button>' + //'<div style="position:absolute;relative">' + '<button type="button" class="btn btn-default" id="mycolor">设置字体颜色</button>' + '<div id="colorpanel" style="position:absolute;z-index:99;display:none"></div>' + //'</div>'+ '</div>' + '</div>' + '<div class="modal-footer">' + '<button type="button" class="btn btn-default" id="myclose">关闭</button>' + '<button type="button" class="btn btn-primary" id="mysave">保存</button>' + '<div id="myimg">' + '<div>' + '</div>' + '</div>' + '</div>' + '</div>'; $('body').append(html); } function inittable() { var colortable = ""; var colorhex = new array('00', '33', '66', '99', 'cc', 'ff'); var spcolorhex = new array('ff0000', '00ff00', '0000ff', 'ffff00', '00ffff', 'ff00ff'); for (var i = 0; i < 2; i++) { for (var j = 0; j < 6; j++) { colortable = colortable + '<tr height=12>'; colortable = colortable + '<td width=11 style="background-color:#000000"></td>'; if (i == 0) { colortable = colortable + '<td width=11 style="background-color:#' + colorhex[j] + colorhex[j] + colorhex[j] + '"></td>'; } else { colortable = colortable + '<td width=11 style="background-color:#' + spcolorhex[j] + '"></td>'; } //colortable = colortable + '<td width=11 style="background-color:#000000"></td>'; for (var k = 0; k < 3; k++) { for (l = 0; l < 6; l++) { colortable = colortable + '<td width=11 style="background-color:#' + colorhex[k + i * 3] + colorhex[l] + colorhex[j] + '"></td>'; } } colortable = colortable + '</tr>'; } } colortable = '<table border="1" id="mytable" cellspacing="0" cellpadding="0" style="border-collapse: collapse;cursor:pointer;" bordercolor="000000">' + colortable + '</table>' + '<table width=225 border="0" cellspacing="0" cellpadding="0" style="border:1px #000000 solid;border-collapse: collapse;background-color:#000000">' + '<tr style="height:30px">' + '<td colspan=21 bgcolor=#cccccc>' + '<table cellpadding="0" cellspacing="1" border="0" style="border-collapse: collapse">' + '<tr>' + '<td width="3"><input type="text" name="discolor" size="6" disabled style="border:solid 1px #000000;background-color:#ffff00"></td>' + '<td width="3"><input type="text" name="hexcolor" size="7" style="border:inset 1px;font-family:arial;" value="#000000"></td>' + '<td width="3"><button type="button" class="btn btn-primary btn-sm" id="btnsave">确认</button></td>' + '</tr>' + '</table>' + '</td>' + '</tr>' + '</table>'; $("#colorpanel").append(colortable); } function initsignature() { if (window.requestanimframe) { var signature = $("#mysignature"); signature.jqsignature({ width: 500, height: 200, border: '1px solid red', background: '#16a085', linecolor: '#abcdef', linewidth: 2, autofit: false }); //{ width: 600, height: 200, border: '1px solid red', background: '#16a085', linecolor: '#abcdef', linewidth: 2, autofit: true } } else { alert("请加载jq-signature.js"); return; } } function showrgb(arr) { hexcode = "#"; for (x = 0; x < 3; x++) { var n = arr[x]; if (n == "") n = "0"; if (parseint(n) != n) return alert("rgb颜色值不是数字!"); if (n > 255) return alert("rgb颜色数字必须在0-255之间!"); var c = "0123456789abcdef", b = "", a = n % 16; b = c.substr(a, 1); a = (n - a) / 16; hexcode += c.substr(a, 1) + b } return hexcode; } function init() { } return { init: function () { init(); } }; }
以上就是详解html5中canvas支持触摸屏的签名面板的示例代码的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product