新媒体-合同签约,CA签约

jq-signature.js 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. (function(window, document, $) {
  2. 'use strict';
  3. // Get a regular interval for drawing to the screen
  4. window.requestAnimFrame = (function (callback) {
  5. return window.requestAnimationFrame ||
  6. window.webkitRequestAnimationFrame ||
  7. window.mozRequestAnimationFrame ||
  8. window.oRequestAnimationFrame ||
  9. window.msRequestAnimaitonFrame ||
  10. function (callback) {
  11. window.setTimeout(callback, 1000/60);
  12. };
  13. })();
  14. /*
  15. * Plugin Constructor
  16. */
  17. var pluginName = 'jqSignature',
  18. defaults = {
  19. lineColor: '#222222',
  20. lineWidth: 1,
  21. border: '1px dashed #AAAAAA',
  22. background: '#FFFFFF',
  23. width: $(window).height(),
  24. height: 100,
  25. autoFit: false
  26. },
  27. canvasFixture = '<canvas></canvas>';
  28. function Signature(element, options) {
  29. // DOM elements/objects
  30. this.element = element;
  31. this.$element = $(this.element);
  32. this.canvas = false;
  33. this.$canvas = false;
  34. this.ctx = false;
  35. // Drawing state
  36. this.drawing = false;
  37. this.currentPos = {
  38. x: 0,
  39. y: 0
  40. };
  41. this.lastPos = this.currentPos;
  42. // Determine plugin settings
  43. this._data = this.$element.data();
  44. this.settings = $.extend({}, defaults, options, this._data);
  45. // Initialize the plugin
  46. this.init();
  47. }
  48. Signature.prototype = {
  49. // Initialize the signature canvas
  50. init: function() {
  51. // Set up the canvas
  52. this.$canvas = $(canvasFixture).appendTo(this.$element);
  53. this.$canvas.attr({
  54. width: this.settings.width,
  55. height: this.settings.height
  56. });
  57. this.$canvas.css({
  58. boxSizing: 'border-box',
  59. width: this.settings.width + 'px',
  60. height: this.settings.height + 'px',
  61. border: this.settings.border,
  62. background: this.settings.background,
  63. cursor: 'crosshair'
  64. });
  65. // Fit canvas to width of parent
  66. if (this.settings.autoFit === true) {
  67. this._resizeCanvas();
  68. // TO-DO - allow for dynamic canvas resizing
  69. // (need to save canvas state before changing width to avoid getting cleared)
  70. // var timeout = false;
  71. // $(window).on('resize', $.proxy(function(e) {
  72. // clearTimeout(timeout);
  73. // timeout = setTimeout($.proxy(this._resizeCanvas, this), 250);
  74. // }, this));
  75. }
  76. this.canvas = this.$canvas[0];
  77. this._resetCanvas();
  78. // Set up mouse events
  79. this.$canvas.on('mousedown touchstart', $.proxy(function(e) {
  80. this.drawing = true;
  81. this.lastPos = this.currentPos = this._getPosition(e);
  82. }, this));
  83. this.$canvas.on('mousemove touchmove', $.proxy(function(e) {
  84. this.currentPos = this._getPosition(e);
  85. }, this));
  86. this.$canvas.on('mouseup touchend', $.proxy(function(e) {
  87. this.drawing = false;
  88. // Trigger a change event
  89. var changedEvent = $.Event('jq.signature.changed');
  90. this.$element.trigger(changedEvent);
  91. }, this));
  92. // Prevent document scrolling when touching canvas
  93. $(document).on('touchstart touchmove touchend', $.proxy(function(e) {
  94. if (e.target === this.canvas) {
  95. e.preventDefault();
  96. }
  97. }, this));
  98. // Start drawing
  99. var that = this;
  100. (function drawLoop() {
  101. window.requestAnimFrame(drawLoop);
  102. that._renderCanvas();
  103. })();
  104. },
  105. // Clear the canvas
  106. clearCanvas: function() {
  107. this.canvas.width = this.canvas.width;
  108. this._resetCanvas();
  109. },
  110. // Get the content of the canvas as a base64 data URL
  111. getDataURL: function() {
  112. return this.canvas.toDataURL();
  113. },
  114. // Get the position of the mouse/touch
  115. _getPosition: function(event) {
  116. var xPos, yPos, rect;
  117. rect = this.canvas.getBoundingClientRect();
  118. event = event.originalEvent;
  119. // Touch event
  120. if (event.type.indexOf('touch') !== -1) { // event.constructor === TouchEvent
  121. xPos = event.touches[0].clientX - rect.left;
  122. yPos = event.touches[0].clientY - rect.top;
  123. }
  124. // Mouse event
  125. else {
  126. xPos = event.clientX - rect.left;
  127. yPos = event.clientY - rect.top;
  128. }
  129. return {
  130. x: xPos,
  131. y: yPos
  132. };
  133. },
  134. // Render the signature to the canvas
  135. _renderCanvas: function() {
  136. if (this.drawing) {
  137. this.ctx.moveTo(this.lastPos.x, this.lastPos.y);
  138. this.ctx.lineTo(this.currentPos.x, this.currentPos.y);
  139. this.ctx.stroke();
  140. this.lastPos = this.currentPos;
  141. }
  142. },
  143. // Reset the canvas context
  144. _resetCanvas: function() {
  145. this.ctx = this.canvas.getContext("2d");
  146. this.ctx.strokeStyle = this.settings.lineColor;
  147. this.ctx.lineWidth = this.settings.lineWidth;
  148. },
  149. // Resize the canvas element
  150. _resizeCanvas: function() {
  151. var width = this.$element.outerWidth();
  152. this.$canvas.attr('width', width);
  153. this.$canvas.css('width', width + 'px');
  154. }
  155. };
  156. /*
  157. * Plugin wrapper and initialization
  158. */
  159. $.fn[pluginName] = function ( options ) {
  160. var args = arguments;
  161. if (options === undefined || typeof options === 'object') {
  162. return this.each(function () {
  163. if (!$.data(this, 'plugin_' + pluginName)) {
  164. $.data(this, 'plugin_' + pluginName, new Signature( this, options ));
  165. }
  166. });
  167. }
  168. else if (typeof options === 'string' && options[0] !== '_' && options !== 'init') {
  169. var returns;
  170. this.each(function () {
  171. var instance = $.data(this, 'plugin_' + pluginName);
  172. if (instance instanceof Signature && typeof instance[options] === 'function') {
  173. returns = instance[options].apply( instance, Array.prototype.slice.call( args, 1 ) );
  174. }
  175. if (options === 'destroy') {
  176. $.data(this, 'plugin_' + pluginName, null);
  177. }
  178. });
  179. return returns !== undefined ? returns : this;
  180. }
  181. };
  182. })(window, document, jQuery);