网站
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

WritingPad.js 7.3 KiB

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