writingpad.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>手写板绘制</title>
  6. <style>
  7. .canvas {
  8. border: 1px solid rgb(170, 170, 170);
  9. cursor: pointer;
  10. }
  11. .mgTop {
  12. margin-top: 30px;
  13. text-align: center;
  14. }
  15. </style>
  16. <script src="../js/fabric.min.js"></script>
  17. </head>
  18. <body style="padding-left: 10px;text-align:center;">
  19. <h1>手写板测试</h1>
  20. <div class="mgTop">
  21. <canvas width="1880" height="1329" id="freeCanvas" class="canvas"></canvas>
  22. </div>
  23. <button style="display: none;" onclick="toData()"> 转换 </button>
  24. <button onclick="clearCanvas()"> 清空画板 </button>
  25. <script>
  26. var canvas = new fabric.Canvas("freeCanvas", {
  27. isDrawingMode: true,
  28. selection: false,
  29. width: 1880,
  30. height: 1329
  31. });
  32. window.open("http://cmd.com/setpadcanvas?" + canvas.width + "&" + canvas.height);
  33. function toData() {
  34. console.log(canvas.toJSON());
  35. console.log(drawingPath.path);
  36. }
  37. var invalidCoord = -1000000; //无效的坐标值
  38. var prevX = invalidCoord, prevY = invalidCoord; //绘制中上一个节点的x,y
  39. var drawingIndex = 0; //绘制对象的下标
  40. var drawingPath = undefined;
  41. var drawColor = '#E34F51',
  42. strokeWidth = 2,
  43. tempCount = 0;
  44. //手写板绘制 type:M=>开始绘制 Q=>绘制中 L=>结束绘制
  45. function writingPad(type, x, y) {
  46. // window.open("http://cmd.com/WriteLog?writingPad—type:" + type + " x:" + x + " y:" + y);
  47. if (type) {
  48. switch (type.toLowerCase()) {
  49. case 'm':
  50. //开始绘制
  51. drawingPath = new fabric.Path('M ' + x + ' ' + y + ' ', {
  52. stroke: drawColor,
  53. fill: "rgba(255, 255, 255, 0)",
  54. strokeWidth: strokeWidth
  55. });
  56. canvas.add(drawingPath);
  57. break;
  58. case 'q':
  59. //绘制中
  60. if (drawingPath) {
  61. if (prevX == invalidCoord || prevY == invalidCoord) {
  62. prevX = x, prevY = y;
  63. } else {
  64. ++tempCount;
  65. if (tempCount % 2) {
  66. drawingPath.path.push(["Q", prevX, prevY, x, y]);
  67. var _path = drawingPath.path;
  68. canvas.remove(_path); //先移除
  69. //再添加(变相实现更新)
  70. drawingPath = new fabric.Path(_path, {
  71. stroke: drawColor,
  72. fill: "rgba(255, 255, 255, 0)",
  73. strokeWidth: strokeWidth
  74. });
  75. canvas.add(drawingPath);
  76. prevX = invalidCoord, prevY = invalidCoord;
  77. // window.open("http://cmd.com/WriteLog?drawingPath—type:" + type + " x:" + x + " y:" + y);
  78. }
  79. }
  80. } else {
  81. alert('绘制失败:画板异常,请重新连接画板重试!');
  82. }
  83. break;
  84. case 'l':
  85. //结束绘制
  86. drawingPath.path.push(["L", x, y]);
  87. // canvas.loadFromJSON(canvas.toJSON());
  88. drawingPath = undefined;
  89. prevX = invalidCoord, prevY = invalidCoord;
  90. tempCount = 0;
  91. break;
  92. default:
  93. break;
  94. }
  95. }
  96. }
  97. function clearCanvas() {
  98. canvas.clear();
  99. }
  100. </script>
  101. </body>
  102. </html>