index.html 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <!DOCTYPE html>
  2. <html lang="zh">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  7. <title></title>
  8. <style type="text/css">
  9. html,
  10. body,
  11. canvas {
  12. padding: 0;
  13. margin: 0;
  14. width: 100%;
  15. height: 100%;
  16. overflow-y: hidden;
  17. background-color: transparent;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <canvas id="lime-signature"></canvas>
  23. <script type="text/javascript" src="./uni.webview.1.5.3.js"></script>
  24. <script type="text/javascript" src="./signature.js"></script>
  25. <script>
  26. var signature = null;
  27. var timer = null;
  28. var isStart = false;
  29. var options = null
  30. console.log = function(...args) {
  31. postMessage(args);
  32. };
  33. // function stringify(key, value) {
  34. // if (typeof value === 'object' && value !== null) {
  35. // if (cache.indexOf(value) !== -1) {
  36. // return;
  37. // }
  38. // cache.push(value);
  39. // }
  40. // return value;
  41. // };
  42. function emit(event, data) {
  43. postMessage({
  44. event,
  45. data: typeof data !== "object" && data !== null ? data : JSON.stringify(data),
  46. });
  47. // cache = [];
  48. }
  49. function postMessage(data) {
  50. uni.postMessage({
  51. data
  52. });
  53. }
  54. function update(v = {}) {
  55. if (signature) {
  56. options = v
  57. signature.pen.setOption(v);
  58. } else {
  59. signature = new Signature.Signature({el: "lime-signature"});
  60. canvasEl = signature.canvas.get("el");
  61. options = v
  62. signature.pen.setOption(v)
  63. const width = signature.canvas.get("width");
  64. const height = signature.canvas.get("height");
  65. emit({changeSize: {width,height}})
  66. }
  67. }
  68. function clear() {
  69. signature.clear()
  70. }
  71. function undo() {
  72. signature.undo()
  73. }
  74. function isEmpty() {
  75. const isEmpty = signature.isEmpty()
  76. emit({isEmpty});
  77. }
  78. function save(args) {
  79. // delete args.success;
  80. // delete args.fail;
  81. clearTimeout(timer);
  82. timer = setTimeout(() => {
  83. let path = canvasEl.toDataURL()
  84. if(options.backgroundColor) {
  85. const canvas = document.createElement('canvas')
  86. const width = signature.canvas.get('width')
  87. const height = signature.canvas.get('height')
  88. const pixelRatio = signature.canvas.get('pixelRatio')
  89. canvas.width = width * pixelRatio
  90. canvas.height = height * pixelRatio
  91. const context = canvas.getContext('2d')
  92. context.scale(pixelRatio, pixelRatio)
  93. context.fillStyle = backgroundColor
  94. context.fillRect(0,0, width, height)
  95. context.drawImage(signature.canvas.get('el'), 0, 0, width, height)
  96. path = canvas.toDataURL()
  97. canvas.remove()
  98. }
  99. if (typeof path == "string") {
  100. const index = Math.ceil(path.length / 8);
  101. for (var i = 0; i < 8; i++) {
  102. if (i == 7) {
  103. emit({"success": path.substr(i * index, index)});
  104. } else {
  105. emit({"file": path.substr(i * index, index)});
  106. }
  107. }
  108. } else {
  109. console.error("canvas no data");
  110. emit({"fail": "canvas no data"});
  111. }
  112. }, 30);
  113. }
  114. </script>
  115. </body>
  116. </html>