jQuery實(shí)現(xiàn)移動(dòng)端筆觸canvas電子簽名
本文實(shí)例為大家分享了jQuery實(shí)現(xiàn)移動(dòng)端筆觸canvas電子簽名的具體代碼,供大家參考,具體內(nèi)容如下
本文主要是通過(guò)jq實(shí)現(xiàn)電子簽名,其中ios有一個(gè)坑,已修復(fù)?;趍ui+vue框架實(shí)現(xiàn)的,如果使用此框架,稍稍改動(dòng)代碼即可。
1.相關(guān)代碼
1.1引入jq
<script src="jquery-1.11.0.min.js" type="text/javascript"></script>
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;)? CPU.+Mac OS X/); //ios終端 var xPos, yPos, rect, getRect; 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 - 50; $('#yPos').html('<p><em>當(dāng)您點(diǎn)擊“保存簽名”時(shí),W您的簽名將出現(xiàn)在這里'+yPos+','+rect.top+','+rect.bottom+'</em></p>'); } // Mouse event else { xPos = event.clientX - rect.left; yPos = event.clientY - rect.top; $('#yPos').html(yPos); } return { x: xPos, y: yPos // y: isIOS?1.7*yPos: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') { returns = instance[options].apply( instance, Array.prototype.slice.call( args, 1 ) ); } if (options === 'destroy') { $.data(this, 'plugin_' + pluginName, null); } }); return returns !== undefined ? returns : this; } }; })(window, document, jQuery);
1.3DOM頁(yè)面index.html
<div class="signature-wrapper" v-show="isSignature" :class="isSignature?'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>當(dāng)您點(diǎn)擊“保存簽名”時(shí),您的簽名將出現(xiàn)在這里</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>當(dāng)您點(diǎn)擊“保存簽名”時(shí),您的簽名將出現(xiàn)在這里</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("請(qǐng)先保存簽名"); return; } vm.closeSignature(); }, closeSignature:function(){// 關(guān)閉簽名彈出層 vm.isSignature = false; }, openSignature:function(){ // 打開(kāi)簽名彈出層 vm.isSignature = true; this.$nextTick(function() { if ($('.js-signature').length) { jq('.js-signature').jqSignature(); } }); }
2.頁(yè)面效果圖如下
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Yii-自定義刪除確認(rèn)彈框(zyd)jquery實(shí)現(xiàn)代碼
Yii-自定義刪除確認(rèn)彈框(zyd),簡(jiǎn)單/時(shí)尚/大方適合比較愛(ài)酷的人使用,時(shí)尚的你可不要錯(cuò)過(guò)了哈,希望本文知識(shí)點(diǎn)可以幫助到你2013-03-03jQuery實(shí)現(xiàn)拖拽可編輯模塊功能代碼
這篇文章主要介紹了jQuery實(shí)現(xiàn)拖拽可編輯模塊功能代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下吧2017-01-01jQuery結(jié)合CSS制作漂亮的select下拉菜單
對(duì)于我來(lái)說(shuō),標(biāo)準(zhǔn)的HTML元素(Select)已經(jīng)讓我感到討厭。它不能夠正常的在IE瀏覽器上顯示。還有一點(diǎn)就是他并不僅僅包含簡(jiǎn)單的文本。本實(shí)例將完全摒棄select元素,通過(guò)JQuery和CSS來(lái)構(gòu)建DropDown元素。2015-05-05jquery 簡(jiǎn)單的進(jìn)度條實(shí)現(xiàn)代碼
jquery其實(shí)是有個(gè)進(jìn)度條插件的,叫做jqueryprogressbar.js,可是想練習(xí)一下,就沒(méi)有用,自己寫(xiě)了點(diǎn)代碼。這個(gè)代碼其實(shí)是參考別人的,因?yàn)樽约旱腏S基礎(chǔ)不是很好。2010-03-03精心挑選的15款優(yōu)秀jQuery 本特效插件和教程
今天這篇文章向大家分享15款精心挑選的優(yōu)秀 jQuery 文本特效插件,都帶有詳細(xì)的使用教程。jQuery 是最流行和使用最廣泛的 JavaScript 框架,它簡(jiǎn)化了 HTML 文檔遍歷,事件處理,動(dòng)畫(huà)以及Ajax交互,幫助 Web 開(kāi)發(fā)人員更快速的實(shí)現(xiàn)各種精美的界面效果2012-08-08jquery,js簡(jiǎn)單實(shí)現(xiàn)類(lèi)似Angular.js雙向綁定
本文主要介紹了jquery,js簡(jiǎn)單實(shí)現(xiàn)類(lèi)似Angular.js雙向綁定的方法。具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-01-01jQuery選擇沒(méi)有colspan屬性的td的代碼
為了試著用jQuery找出一個(gè)table中沒(méi)有colspan屬性的td,試了很多種方法,這個(gè)是最好的,記在這里,下次不要再忘了2010-07-07jQuery學(xué)習(xí)筆記之jQuery.extend(),jQuery.fn.extend()分析
給jQuery做過(guò)擴(kuò)展或者制作過(guò)jQuery插件的人這兩個(gè)方法東西可能不陌生. jQuery.extend([deep],target,object1,,object2...[objectN]) jQuery.fn.extend([deep],target,object1,,object2...[objectN]) 這兩個(gè)屬性都是用于合并兩個(gè)或多個(gè)對(duì)象的屬性到target對(duì)象.2014-06-06jQuery實(shí)現(xiàn)ajax的嵌套請(qǐng)求案例分析
這篇文章主要介紹了jQuery實(shí)現(xiàn)ajax的嵌套請(qǐng)求,結(jié)合具體實(shí)例形式分析了ajax嵌套請(qǐng)求的原理、實(shí)現(xiàn)方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-02-02jquery動(dòng)態(tài)創(chuàng)建div與input的實(shí)例代碼
下面小編就為大家?guī)?lái)一篇jquery動(dòng)態(tài)創(chuàng)建div與input的實(shí)例代碼。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-10-10