本文实例为大家分享了jQuery实现移动端笔触canvas电子签名的具体代码,供大家参考,具体内容如下
本文主要是通过jq实现电子签名,其中ios有一个坑,已修复。基于mui+vue框架实现的,如果使用此框架,稍稍改动代码即可。
1.相关代码
1.1引入jq
<script src="/UploadFiles/2021-04-02/jquery-1.11.0.min.js">1.2封装signature.js
(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 #AAAAAA', background: '#FFFFFF', width: 375, 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(); // TO-DO - allow for dynamic canvas resizing // (need to save canvas state before changing width to avoid getting cleared) var timeout = false; $(window).on('resize', $.proxy(function(e) { clearTimeout(timeout); timeout = setTimeout($.proxy(this._resizeCanvas, this), 250); }, this)); } 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(); }, // Get the position of the mouse/touch _getPosition: function(event) { var u = navigator.userAgent; var isIOS = !!u.match(/\(i[^;]+;( U;)"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') { returns = instance[options].apply( instance, Array.prototype.slice.call( args, 1 ) ); } if (options === 'destroy') { $.data(this, 'plugin_' + pluginName, null); } }); return returns !== undefined "htmlcode"><div class="signature-wrapper" v-show="isSignature" :class="isSignature"> <header class="mui-bar mui-bar-nav head-color"> <a class="mui-icon mui-icon-back mui-pull-left" @tap="closeSignature"></a> <h1 class="mui-title">签名</h1> <a id="menu" class="mui-icon mui-pull-right menu-btn" @tap="saveSignature">保存</a> </header> <div class="container"> <div class="row"> <div class="col-xs-12"> <div class="js-signature" data-width="600" data-height="200" data-border="1px solid black" data-line-color="#000000" data-auto-fit="true"></div> <p><button id="clearBtn" class="btn btn-default" @tap="clearCanvas();">重置签名</button> <button id="saveBtn" class="btn btn-default" @tap="previewSignature();" disabled>保存签名</button></p> <div id="signature"> <p><em>当您点击“保存签名”时,您的签名将出现在这里</em></p> </div> <span id="yPos"><p><em>ypos</em></p></span> </div> </div> </div> </div>1.4JS
注:由于此次的$被模型其他框架所替换,因此以jq替代
mounted: function() { this.$nextTick(function() { this.init(); }); }, methods:{ init: function() { jq('.js-signature').eq(0).on('jq.signature.changed', function() { //canvas签名 初始化 jq('#saveBtn').attr('disabled', false); }); } }, clearCanvas:function(){ // 清除重置签名 jq('#signature').html('<p><em>当您点击“保存签名”时,您的签名将出现在这里</em></p>'); jq('.js-signature').eq(0).jqSignature('clearCanvas'); jq('#saveBtn').attr('disabled', true); vm.signatureImg=""; }, previewSignature:function(){ //保存签名 jq('#signature').empty(); var dataUrl = jq('.js-signature').eq(0).jqSignature('getDataURL'); var img = jq('<img>').attr('src', dataUrl); jq('#signature').append(img); console.log(dataUrl) vm.signatureImg=dataUrl; }, saveSignature:function(){ // 保存按钮 逻辑 if(!vm.signatureImg){ $.toast("请先保存签名"); return; } vm.closeSignature(); }, closeSignature:function(){// 关闭签名弹出层 vm.isSignature = false; }, openSignature:function(){ // 打开签名弹出层 vm.isSignature = true; this.$nextTick(function() { if ($('.js-signature').length) { jq('.js-signature').jqSignature(); } }); }2.页面效果图如下
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年11月15日
2024年11月15日
- 黄乙玲1988-无稳定的爱心肝乱糟糟[日本东芝1M版][WAV+CUE]
- 群星《我们的歌第六季 第3期》[320K/MP3][70.68MB]
- 群星《我们的歌第六季 第3期》[FLAC/分轨][369.48MB]
- 群星《燃!沙排少女 影视原声带》[320K/MP3][175.61MB]
- 乱斗海盗瞎6胜卡组推荐一览 深暗领域乱斗海盗瞎卡组分享
- 炉石传说乱斗6胜卡组分享一览 深暗领域乱斗6胜卡组代码推荐
- 炉石传说乱斗本周卡组合集 乱斗模式卡组最新推荐
- 佟妍.2015-七窍玲珑心【万马旦】【WAV+CUE】
- 叶振棠陈晓慧.1986-龙的心·俘虏你(2006复黑限量版)【永恒】【WAV+CUE】
- 陈慧琳.1998-爱我不爱(国)【福茂】【WAV+CUE】
- 咪咕快游豪礼放送,百元京东卡、海量欢乐豆就在咪咕咪粉节!
- 双11百吋大屏焕新“热”,海信AI画质电视成最大赢家
- 海信电视E8N Ultra:真正的百吋,不止是大!
- 曾庆瑜1990-曾庆瑜历年精选[派森][WAV+CUE]
- 叶玉卿1999-深情之选[飞图][WAV+CUE]