网站
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

jq-signature.js 8.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. 
  2. var WritingPad = function () {
  3. var current = null;
  4. $(function () {
  5. initHtml();
  6. initTable();
  7. initSignature();
  8. //if ($(".modal")) {
  9. // $(".modal").modal("toggle");
  10. //} else {
  11. // alert("没用手写面板");
  12. //}
  13. $(document).on("click", "#myClose,.close", null, function () {
  14. $('#mymodal').modal('hide');
  15. $("#mymodal").remove();
  16. });
  17. $(document).on("click", "#mySave", null, function () {
  18. var myImg = $('#myImg').empty();
  19. var dataUrl = $('.js-signature').jqSignature('getDataURL');
  20. var img = $('<img>').attr('src', dataUrl);
  21. $(myImg).append($('<p>').text("图片保存在这里"));
  22. $(myImg).append(img);
  23. });
  24. $(document).on("click", "#myEmpty", null, function () {
  25. $('.js-signature').jqSignature('clearCanvas');
  26. });
  27. $(document).on("click", "#myBackColor", null, function () {
  28. $('#colorpanel').css('left', '95px').css('top', '45px').css("display", "block").fadeIn();
  29. //$("canvas").css("background", "#EEEEEE");
  30. $("#btnSave").data("sender", "#myBackColor");
  31. });
  32. $(document).on("click", "#myColor", null, function () {
  33. $('#colorpanel').css('left', '205px').css('top', '45px').css("display", "block").fadeIn();
  34. $("#btnSave").data("sender", "#myColor");
  35. });
  36. $(document).on("mouseover", "#myTable", null, function () {
  37. if ((event.srcElement.tagName == "TD") && (current != event.srcElement)) {
  38. if (current != null) { current.style.backgroundColor = current._background }
  39. event.srcElement._background = event.srcElement.style.backgroundColor;
  40. //$("input[name=DisColor]").css("background-color", event.srcElement.style.backgroundColor);
  41. //var color = event.srcElement.style.backgroundColor;
  42. //var strArr = color.substring(4, color.length - 1).split(',');
  43. //var num = showRGB(strArr);
  44. //$("input[name=HexColor]").val(num);
  45. current = event.srcElement;
  46. }
  47. });
  48. $(document).on("mouseout", "#myTable", null, function () {
  49. if (current != null) current.style.backgroundColor = current._background
  50. });
  51. $(document).on("click", "#myTable", null, function () {
  52. if (event.srcElement.tagName == "TD") {
  53. var color = event.srcElement._background;
  54. if (color) {
  55. $("input[name=DisColor]").css("background-color", color);
  56. var strArr = color.substring(4, color.length - 1).split(',');
  57. var num = showRGB(strArr);
  58. $("input[name=HexColor]").val(num);
  59. }
  60. }
  61. });
  62. $(document).on("click", "#btnSave", null, function () {
  63. $('#colorpanel').css("display", "none");
  64. var typeData = $("#btnSave").data("sender");
  65. var HexColor = $("input[name=HexColor]").val();
  66. var data = $(".js-signature").data();
  67. if (typeData == "#myColor") {
  68. data["plugin_jqSignature"]["settings"]["lineColor"] = HexColor;
  69. $('.js-signature').jqSignature('reLoadData');
  70. }
  71. if (typeData == "#myBackColor") {
  72. data["plugin_jqSignature"]["settings"]["background"] = HexColor;
  73. $('.js-signature').jqSignature('reLoadData');
  74. }
  75. });
  76. $("#mymodal").on('hide.bs.modal', function () {
  77. $("#colorpanel").remove();
  78. $("#mymodal").remove();
  79. $("#myTable").remove();
  80. });
  81. });
  82. function initHtml() {
  83. var html = '<div class="modal" id="mymodal">' +
  84. '<div class="modal-dialog">' +
  85. '<div class="modal-content">' +
  86. '<div class="modal-header">' +
  87. '' +
  88. '<h4 class="modal-title">手写面板</h4>' +
  89. '</div>' +
  90. '<div class="modal-body">' +
  91. '<div class="js-signature" id="mySignature">' +
  92. '</div>' +
  93. '<div>' +
  94. '<button type="button" class="btn btn-default" id="myEmpty">清空面板</button>' +
  95. '<button type="button" class="btn btn-default" id="myBackColor">设置背景颜色</button>' +
  96. //'<div style="position:absolute;relative">' +
  97. '<button type="button" class="btn btn-default" id="myColor">设置字体颜色</button>' +
  98. '<div id="colorpanel" style="position:absolute;z-index:99;display:none"></div>' +
  99. //'</div>'+
  100. '</div>' +
  101. '</div>' +
  102. '<div class="modal-footer">' +
  103. '<button type="button" class="btn btn-default" id="myClose">关闭</button>' +
  104. '<button type="button" class="btn btn-primary" id="mySave" onclick="savaimg();" >保存</button>' +
  105. '<div id="myImg">' +
  106. '<div>' +
  107. '</div>' +
  108. '</div>' +
  109. '</div>' +
  110. '</div>';
  111. $('body').append(html);
  112. }
  113. function initTable() {
  114. var colorTable = "";
  115. var ColorHex = new Array('00', '33', '66', '99', 'CC', 'FF');
  116. var SpColorHex = new Array('FF0000', '00FF00', '0000FF', 'FFFF00', '00FFFF', 'FF00FF');
  117. for (var i = 0; i < 2; i++) {
  118. for (var j = 0; j < 6; j++) {
  119. colorTable = colorTable + '<tr height=12>';
  120. colorTable = colorTable + '<td width=11 style="background-color:#000000"></td>';
  121. if (i == 0) {
  122. colorTable = colorTable + '<td width=11 style="background-color:#' + ColorHex[j] + ColorHex[j] + ColorHex[j] + '"></td>';
  123. }
  124. else {
  125. colorTable = colorTable + '<td width=11 style="background-color:#' + SpColorHex[j] + '"></td>';
  126. }
  127. //colorTable = colorTable + '<td width=11 style="background-color:#000000"></td>';
  128. for (var k = 0; k < 3; k++) {
  129. for (l = 0; l < 6; l++) {
  130. colorTable = colorTable + '<td width=11 style="background-color:#' + ColorHex[k + i * 3] + ColorHex[l] + ColorHex[j] + '"></td>';
  131. }
  132. }
  133. colorTable = colorTable + '</tr>';
  134. }
  135. }
  136. colorTable =
  137. '<table border="1" id="myTable" cellspacing="0" cellpadding="0" style="border-collapse: collapse;cursor:pointer;" bordercolor="000000">'
  138. + colorTable + '</table>' +
  139. '<table width=225 border="0" cellspacing="0" cellpadding="0" style="border:1px #000000 solid;border-collapse: collapse;background-color:#000000">' +
  140. '<tr style="height:30px">' +
  141. '<td colspan=21 bgcolor=#cccccc>' +
  142. '<table cellpadding="0" cellspacing="1" border="0" style="border-collapse: collapse">' +
  143. '<tr>' +
  144. '<td width="3"><input type="text" name="DisColor" size="6" disabled style="border:solid 1px #000000;background-color:#ffff00"></td>' +
  145. '<td width="3"><input type="text" name="HexColor" size="7" style="border:inset 1px;font-family:Arial;" value="#000000"></td>' +
  146. '<td width="3"><button type="button" class="btn btn-primary btn-sm" id="btnSave">确认</button></td>' +
  147. '</tr>' +
  148. '</table>' +
  149. '</td>' +
  150. '</tr>' +
  151. '</table>';
  152. $("#colorpanel").append(colorTable);
  153. }
  154. function initSignature() {
  155. // debugger;
  156. if (window.requestAnimFrame) {
  157. var signature = $("#mySignature");
  158. signature.jqSignature({ width: 500, height: 200, border: '1px solid red', background: '#16A085', lineColor: '#ABCDEF', lineWidth: 2, autoFit: false });
  159. //{ width: 600, height: 200, border: '1px solid red', background: '#16A085', lineColor: '#ABCDEF', lineWidth: 2, autoFit: true }
  160. } else {
  161. alert("请加载jq-signature.js");
  162. return;
  163. }
  164. }
  165. function showRGB(arr) {
  166. hexcode = "#";
  167. for (x = 0; x < 3; x++) {
  168. var n = arr[x];
  169. if (n == "") n = "0";
  170. if (parseInt(n) != n)
  171. return alert("RGB颜色值不是数字!");
  172. if (n > 255)
  173. return alert("RGB颜色数字必须在0-255之间!");
  174. var c = "0123456789ABCDEF", b = "", a = n % 16;
  175. b = c.substr(a, 1); a = (n - a) / 16;
  176. hexcode += c.substr(a, 1) + b
  177. }
  178. return hexcode;
  179. }
  180. function init() {
  181. }
  182. return {
  183. init: function () {
  184. init();
  185. }
  186. };
  187. }