signature.jsp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
  2. <%@include file="/context/mytags.jsp" %>
  3. <t:base type="jquery,DatePicker"></t:base>
  4. <div class="easyui-layout" fit="true">
  5. <div align="center">
  6. <canvas id="myCanvas" width="900px" height="300px" style="border:10px solid #90939964"></canvas>
  7. <div class="control-ops control">
  8. Line width : <select id="selWidth" onchange="aaa()">
  9. <option value="1">1</option>
  10. <option value="3">3</option>
  11. <option value="5" selected="selected">5</option>
  12. <option value="7">7</option>
  13. <option value="9">9</option>
  14. <option value="11">11</option>
  15. </select> Color : <select id="selColor" onchange="aaa2()">
  16. <option value="black" selected="selected">black</option>
  17. <option value="blue">blue</option>
  18. <option value="red">red</option>
  19. <option value="green">green</option>
  20. <option value="yellow">yellow</option>
  21. <option value="gray">gray</option>
  22. </select>
  23. </div>
  24. </div>
  25. </div>
  26. <script type="text/javascript">
  27. var mousePressed = false;
  28. var lastX, lastY;
  29. var ctx = document.getElementById('myCanvas').getContext("2d");
  30. var c = document.getElementById("myCanvas");
  31. var control = document.getElementsByClassName("control")[0];
  32. window.onload = function () {
  33. InitThis();
  34. }
  35. function saveImageInfo() {
  36. var image = c.toDataURL("image/png");
  37. return image;
  38. }
  39. var selected1 = 5, selected2;
  40. function aaa() {
  41. var sel = document.getElementById('selWidth');
  42. var value = sel.selectedIndex;
  43. return selected1 = sel[value].value;
  44. }
  45. function aaa2() {
  46. var sel2 = document.getElementById('selColor');
  47. var value = sel2.selectedIndex;
  48. return selected2 = sel2[value].value;
  49. }
  50. function InitThis() {
  51. // 触摸屏
  52. c.addEventListener('touchstart', function (event) {
  53. if (event.targetTouches.length == 1) {
  54. event.preventDefault(); // 阻止浏览器默认事件,重要
  55. var touch = event.targetTouches[0];
  56. mousePressed = true;
  57. Draw(touch.pageX - this.offsetLeft, touch.pageY - this.offsetTop, false);
  58. }
  59. }, false);
  60. c.addEventListener('touchmove', function (event) {
  61. if (event.targetTouches.length == 1) {
  62. event.preventDefault(); // 阻止浏览器默认事件,重要
  63. var touch = event.targetTouches[0];
  64. if (mousePressed) {
  65. Draw(touch.pageX - this.offsetLeft, touch.pageY - this.offsetTop, true);
  66. }
  67. }
  68. }, false);
  69. c.addEventListener('touchend', function (event) {
  70. event.preventDefault(); // 阻止浏览器默认事件,防止手写的时候拖动屏幕,重要
  71. // var touch = event.targetTouches[0];
  72. mousePressed = false;
  73. }, false);
  74. /*c.addEventListener('touchcancel', function (event) {
  75. console.log(4)
  76. mousePressed = false;
  77. },false);*/
  78. // 鼠标
  79. c.onmousedown = function (event) {
  80. mousePressed = true;
  81. Draw(event.pageX - this.offsetLeft, event.pageY - this.offsetTop, false);
  82. };
  83. c.onmousemove = function (event) {
  84. if (mousePressed) {
  85. //只有PC端才有这个问题
  86. if (event.pageX - this.offsetLeft > 10 && event.pageX - this.offsetLeft < 910 && event.pageY - this.offsetTop > 10 && event.pageY - this.offsetTop < 310) {
  87. Draw(event.pageX - this.offsetLeft, event.pageY - this.offsetTop, true);
  88. } else {
  89. mousePressed = false;
  90. }
  91. }
  92. };
  93. c.onmouseup = function (event) {
  94. mousePressed = false;
  95. };
  96. }
  97. function Draw(x, y, isDown) {
  98. if (isDown) {
  99. ctx.beginPath();
  100. ctx.strokeStyle = selected2;
  101. ctx.lineWidth = selected1;
  102. ctx.lineJoin = "round";
  103. ctx.moveTo(lastX - 10, lastY - 10);
  104. ctx.lineTo(x - 10, y - 10);
  105. ctx.closePath();
  106. ctx.stroke();
  107. }
  108. lastX = x;
  109. lastY = y;
  110. }
  111. function clearArea() {
  112. ctx.setTransform(1, 0, 0, 1, 0, 0);
  113. ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
  114. }
  115. </script>
  116. <style>
  117. .saveimg {
  118. text-align: center;
  119. }
  120. .saveimgs span {
  121. display: inline-block;
  122. margin-top: 5px;
  123. }
  124. </style>