Signature.mjs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import { computed, ref, onMounted, defineComponent, watch, createVNode as _createVNode } from "vue";
  2. import { inBrowser, makeNumberProp, makeStringProp, createNamespace, preventDefault, windowWidth } from "../utils/index.mjs";
  3. import { useRect } from "@vant/use";
  4. import { useExpose } from "../composables/use-expose.mjs";
  5. import { Button } from "../button/index.mjs";
  6. const [name, bem, t] = createNamespace("signature");
  7. const signatureProps = {
  8. tips: String,
  9. type: makeStringProp("png"),
  10. penColor: makeStringProp("#000"),
  11. lineWidth: makeNumberProp(3),
  12. clearButtonText: String,
  13. backgroundColor: makeStringProp(""),
  14. confirmButtonText: String
  15. };
  16. const hasCanvasSupport = () => {
  17. var _a;
  18. const canvas = document.createElement("canvas");
  19. return !!((_a = canvas.getContext) == null ? void 0 : _a.call(canvas, "2d"));
  20. };
  21. var stdin_default = defineComponent({
  22. name,
  23. props: signatureProps,
  24. emits: ["submit", "clear", "start", "end", "signing"],
  25. setup(props, {
  26. emit
  27. }) {
  28. const canvasRef = ref();
  29. const wrapRef = ref();
  30. const ctx = computed(() => {
  31. if (!canvasRef.value) return null;
  32. return canvasRef.value.getContext("2d");
  33. });
  34. const isRenderCanvas = inBrowser ? hasCanvasSupport() : true;
  35. let canvasWidth = 0;
  36. let canvasHeight = 0;
  37. let canvasRect;
  38. const touchStart = () => {
  39. if (!ctx.value) {
  40. return false;
  41. }
  42. ctx.value.beginPath();
  43. ctx.value.lineWidth = props.lineWidth;
  44. ctx.value.strokeStyle = props.penColor;
  45. canvasRect = useRect(canvasRef);
  46. emit("start");
  47. };
  48. const touchMove = (event) => {
  49. if (!ctx.value) {
  50. return false;
  51. }
  52. preventDefault(event);
  53. const touch = event.touches[0];
  54. const mouseX = touch.clientX - ((canvasRect == null ? void 0 : canvasRect.left) || 0);
  55. const mouseY = touch.clientY - ((canvasRect == null ? void 0 : canvasRect.top) || 0);
  56. ctx.value.lineCap = "round";
  57. ctx.value.lineJoin = "round";
  58. ctx.value.lineTo(mouseX, mouseY);
  59. ctx.value.stroke();
  60. emit("signing", event);
  61. };
  62. const touchEnd = (event) => {
  63. preventDefault(event);
  64. emit("end");
  65. };
  66. const isCanvasEmpty = (canvas) => {
  67. const empty = document.createElement("canvas");
  68. empty.width = canvas.width;
  69. empty.height = canvas.height;
  70. if (props.backgroundColor) {
  71. const emptyCtx = empty.getContext("2d");
  72. setCanvasBgColor(emptyCtx);
  73. }
  74. return canvas.toDataURL() === empty.toDataURL();
  75. };
  76. const setCanvasBgColor = (ctx2) => {
  77. if (ctx2 && props.backgroundColor) {
  78. ctx2.fillStyle = props.backgroundColor;
  79. ctx2.fillRect(0, 0, canvasWidth, canvasHeight);
  80. }
  81. };
  82. const submit = () => {
  83. var _a, _b;
  84. const canvas = canvasRef.value;
  85. if (!canvas) {
  86. return;
  87. }
  88. const isEmpty = isCanvasEmpty(canvas);
  89. const image = isEmpty ? "" : ((_b = (_a = {
  90. jpg: () => canvas.toDataURL("image/jpeg", 0.8),
  91. jpeg: () => canvas.toDataURL("image/jpeg", 0.8)
  92. })[props.type]) == null ? void 0 : _b.call(_a)) || canvas.toDataURL(`image/${props.type}`);
  93. emit("submit", {
  94. image,
  95. canvas
  96. });
  97. };
  98. const clear = () => {
  99. if (ctx.value) {
  100. ctx.value.clearRect(0, 0, canvasWidth, canvasHeight);
  101. ctx.value.closePath();
  102. setCanvasBgColor(ctx.value);
  103. }
  104. emit("clear");
  105. };
  106. const initialize = () => {
  107. var _a, _b, _c;
  108. if (isRenderCanvas && canvasRef.value) {
  109. const canvas = canvasRef.value;
  110. const dpr = inBrowser ? window.devicePixelRatio : 1;
  111. canvasWidth = canvas.width = (((_a = wrapRef.value) == null ? void 0 : _a.offsetWidth) || 0) * dpr;
  112. canvasHeight = canvas.height = (((_b = wrapRef.value) == null ? void 0 : _b.offsetHeight) || 0) * dpr;
  113. (_c = ctx.value) == null ? void 0 : _c.scale(dpr, dpr);
  114. setCanvasBgColor(ctx.value);
  115. }
  116. };
  117. const resize = () => {
  118. if (ctx.value) {
  119. const data = ctx.value.getImageData(0, 0, canvasWidth, canvasHeight);
  120. initialize();
  121. ctx.value.putImageData(data, 0, 0);
  122. }
  123. };
  124. watch(windowWidth, resize);
  125. onMounted(initialize);
  126. useExpose({
  127. resize,
  128. clear,
  129. submit
  130. });
  131. return () => _createVNode("div", {
  132. "class": bem()
  133. }, [_createVNode("div", {
  134. "class": bem("content"),
  135. "ref": wrapRef
  136. }, [isRenderCanvas ? _createVNode("canvas", {
  137. "ref": canvasRef,
  138. "onTouchstartPassive": touchStart,
  139. "onTouchmove": touchMove,
  140. "onTouchend": touchEnd
  141. }, null) : _createVNode("p", null, [props.tips])]), _createVNode("div", {
  142. "class": bem("footer")
  143. }, [_createVNode(Button, {
  144. "size": "small",
  145. "onClick": clear
  146. }, {
  147. default: () => [props.clearButtonText || t("clear")]
  148. }), _createVNode(Button, {
  149. "type": "primary",
  150. "size": "small",
  151. "onClick": submit
  152. }, {
  153. default: () => [props.confirmButtonText || t("confirm")]
  154. })])]);
  155. }
  156. });
  157. export {
  158. stdin_default as default,
  159. signatureProps
  160. };