signature.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // I found this code on codepen
  2. // https://codepen.io/dus7/pen/qGQbVP
  3. (function() {
  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. var canvas = document.getElementById("sig-canvas");
  15. var ctx = canvas.getContext("2d");
  16. ctx.strokeStyle = "#222222";
  17. ctx.lineWidth = 4;
  18. var drawing = false;
  19. var mousePos = {
  20. x: 0,
  21. y: 0
  22. };
  23. var lastPos = mousePos;
  24. canvas.addEventListener("mousedown", function(e) {
  25. drawing = true;
  26. lastPos = getMousePos(canvas, e);
  27. }, false);
  28. canvas.addEventListener("mouseup", function(e) {
  29. drawing = false;
  30. }, false);
  31. canvas.addEventListener("mousemove", function(e) {
  32. mousePos = getMousePos(canvas, e);
  33. }, false);
  34. // Add touch event support for mobile
  35. canvas.addEventListener("touchstart", function(e) {
  36. }, false);
  37. canvas.addEventListener("touchmove", function(e) {
  38. var touch = e.touches[0];
  39. var me = new MouseEvent("mousemove", {
  40. clientX: touch.clientX,
  41. clientY: touch.clientY
  42. });
  43. canvas.dispatchEvent(me);
  44. }, false);
  45. canvas.addEventListener("touchstart", function(e) {
  46. mousePos = getTouchPos(canvas, e);
  47. var touch = e.touches[0];
  48. var me = new MouseEvent("mousedown", {
  49. clientX: touch.clientX,
  50. clientY: touch.clientY
  51. });
  52. canvas.dispatchEvent(me);
  53. }, false);
  54. canvas.addEventListener("touchend", function(e) {
  55. var me = new MouseEvent("mouseup", {});
  56. canvas.dispatchEvent(me);
  57. }, false);
  58. function getMousePos(canvasDom, mouseEvent) {
  59. var rect = canvasDom.getBoundingClientRect();
  60. return {
  61. x: mouseEvent.clientX - rect.left,
  62. y: mouseEvent.clientY - rect.top
  63. }
  64. }
  65. function getTouchPos(canvasDom, touchEvent) {
  66. var rect = canvasDom.getBoundingClientRect();
  67. return {
  68. x: touchEvent.touches[0].clientX - rect.left,
  69. y: touchEvent.touches[0].clientY - rect.top
  70. }
  71. }
  72. function renderCanvas() {
  73. if (drawing) {
  74. ctx.moveTo(lastPos.x, lastPos.y);
  75. ctx.lineTo(mousePos.x, mousePos.y);
  76. ctx.stroke();
  77. lastPos = mousePos;
  78. }
  79. }
  80. // Prevent scrolling when touching the canvas
  81. document.body.addEventListener("touchstart", function(e) {
  82. if (e.target == canvas) {
  83. e.preventDefault();
  84. }
  85. }, false);
  86. document.body.addEventListener("touchend", function(e) {
  87. if (e.target == canvas) {
  88. e.preventDefault();
  89. }
  90. }, false);
  91. document.body.addEventListener("touchmove", function(e) {
  92. if (e.target == canvas) {
  93. e.preventDefault();
  94. }
  95. }, false);
  96. (function drawLoop() {
  97. requestAnimFrame(drawLoop);
  98. renderCanvas();
  99. })();
  100. function clearCanvas() {
  101. canvas.width = canvas.width;
  102. }
  103. // Set up the UI
  104. var sigText = document.getElementById("sig-dataUrl");
  105. var sigImage = document.getElementById("sig-image");
  106. var clearBtn = document.getElementById("sig-clearBtn");
  107. var submitBtn = document.getElementById("sig-submitBtn");
  108. clearBtn.addEventListener("click", function(e) {
  109. clearCanvas();
  110. sigText.innerHTML = "Data URL for your signature will go here!";
  111. sigImage.setAttribute("src", "");
  112. }, false);
  113. submitBtn.addEventListener("click", function(e) {
  114. var dataUrl = canvas.toDataURL();
  115. sigText.innerHTML = dataUrl;
  116. sigImage.setAttribute("src", dataUrl);
  117. }, false);
  118. })();