預約系統 IOS / Android Windows

signature.js 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. var zkSignature = (function () {
  2. var empty = true;
  3. return {
  4. //public functions
  5. capture: function (){
  6. var parent = document.getElementById("canvas");
  7. parent.childNodes[0].nodeValue = "";
  8. var canvasArea = document.createElement("canvas");
  9. canvasArea.setAttribute("id", "newSignature");
  10. parent.appendChild(canvasArea);
  11. var canvas = document.getElementById("newSignature");
  12. var context = canvas.getContext("2d");
  13. if (!context) {
  14. throw new Error("Failed to get canvas' 2d context");
  15. }
  16. canvas.height=canvas.height*1.8;
  17. context.fillStyle = "#fff";
  18. context.strokeStyle = "#444";
  19. context.lineWidth = 1;
  20. context.lineCap = "round";
  21. context.fillRect(0, 0, canvas.width, canvas.height);
  22. context.fillStyle = "#000000";
  23. context.strokeStyle = "#000000";
  24. //context.moveTo((canvas.width * 0.05 ), (canvas.height * 0.9));
  25. //context.lineTo((canvas.width * 0.95), (canvas.height * 0.9));
  26. context.stroke();
  27. context.fillStyle = "#fff";
  28. context.strokeStyle = "#444";
  29. context.lineWidth = 3;
  30. var disableSave = true;
  31. var pixels = [];
  32. var cpixels = [];
  33. var xyLast = {};
  34. var xyAddLast = {};
  35. var calculate = false;
  36. //functions
  37. {
  38. function remove_event_listeners() {
  39. canvas.removeEventListener('mousemove', on_mousemove, false);
  40. canvas.removeEventListener('mouseup', on_mouseup, false);
  41. canvas.removeEventListener('touchmove', on_mousemove, false);
  42. canvas.removeEventListener('touchend', on_mouseup, false);
  43. document.body.removeEventListener('mouseup', on_mouseup, false);
  44. document.body.removeEventListener('touchend', on_mouseup, false);
  45. }
  46. function get_board_coords(e) {
  47. var x, y;
  48. if (e.changedTouches && e.changedTouches[0]) {
  49. var offsety = canvas.offsetTop || 0;
  50. var offsetx = canvas.offsetLeft || 0;
  51. x = e.changedTouches[0].pageX - offsetx;
  52. y = e.changedTouches[0].pageY - offsety;
  53. } else if (e.layerX || 0 == e.layerX) {
  54. x = e.layerX;
  55. y = e.layerY;
  56. } else if (e.offsetX || 0 == e.offsetX) {
  57. x = e.offsetX;
  58. y = e.offsetY;
  59. }
  60. x=x*(canvas.width/screen.width);
  61. y=y*(canvas.width/screen.width);
  62. return {
  63. x : x,
  64. y : y
  65. };
  66. };
  67. function on_mousedown(e) {
  68. e.preventDefault();
  69. e.stopPropagation();
  70. canvas.addEventListener('mousemove', on_mousemove, false);
  71. canvas.addEventListener('mouseup', on_mouseup, false);
  72. canvas.addEventListener('touchmove', on_mousemove, false);
  73. canvas.addEventListener('touchend', on_mouseup, false);
  74. document.body.addEventListener('mouseup', on_mouseup, false);
  75. document.body.addEventListener('touchend', on_mouseup, false);
  76. empty = false;
  77. var xy = get_board_coords(e);
  78. context.beginPath();
  79. pixels.push('moveStart');
  80. context.moveTo(xy.x, xy.y);
  81. pixels.push(xy.x, xy.y);
  82. xyLast = xy;
  83. };
  84. function on_mousemove(e, finish) {
  85. e.preventDefault();
  86. e.stopPropagation();
  87. var xy = get_board_coords(e);
  88. var xyAdd = {
  89. x : (xyLast.x + (xy.x)) / 2,
  90. y : (xyLast.y + (xy.y)) / 2
  91. };
  92. if (calculate) {
  93. var xLast = (xyAddLast.x + xyLast.x + xyAdd.x) / 3;
  94. var yLast = (xyAddLast.y + xyLast.y + xyAdd.y) / 3;
  95. pixels.push(xLast, yLast);
  96. } else {
  97. calculate = true;
  98. }
  99. context.quadraticCurveTo(xyLast.x, xyLast.y, xyAdd.x, xyAdd.y);
  100. pixels.push(xyAdd.x, xyAdd.y);
  101. context.stroke();
  102. context.beginPath();
  103. context.moveTo(xyAdd.x, xyAdd.y);
  104. xyAddLast = xyAdd;
  105. xyLast = xy;
  106. };
  107. function on_mouseup(e) {
  108. remove_event_listeners();
  109. disableSave = false;
  110. context.stroke();
  111. pixels.push('e');
  112. calculate = false;
  113. };
  114. }
  115. canvas.addEventListener('mousedown', on_mousedown, false);
  116. canvas.addEventListener('touchstart', on_mousedown, false);
  117. }
  118. ,
  119. save : function(){
  120. var canvas = document.getElementById("newSignature");
  121. // save canvas image as data url (png format by default)
  122. var dataURL = canvas.toDataURL("image/png");
  123. document.getElementById("saveSignature").src = dataURL;
  124. }
  125. ,
  126. clear : function(){
  127. var parent = document.getElementById("canvas");
  128. var child = document.getElementById("newSignature");
  129. parent.removeChild(child);
  130. empty = true;
  131. this.capture();
  132. }
  133. ,
  134. send : function(){
  135. if (empty == false){
  136. var canvas = document.getElementById("newSignature");
  137. var dataURL = canvas.toDataURL("image/png");
  138. document.getElementById("saveSignature").src = dataURL;
  139. var sendemail = document.getElementById('sendemail').value;
  140. var replyemail = document.getElementById('replyemail').value;
  141. var dataform = document.createElement("form");
  142. document.body.appendChild(dataform);
  143. dataform.setAttribute("action","upload_file.php");
  144. dataform.setAttribute("enctype","multipart/form-data");
  145. dataform.setAttribute("method","POST");
  146. dataform.setAttribute("target","_self");
  147. dataform.innerHTML = '<input type="text" name="image" value="' + dataURL + '"/>' + '<input type="email" name="email" value="' + sendemail + '"/>' + '<input type="email" name="replyemail" value="' + replyemail + '"/>'+'<input type="submit" value="Click me" />';
  148. dataform.submit();
  149. }
  150. }
  151. }
  152. })()
  153. var zkSignature;