UNPKG

vant

Version:

Mobile UI Components built on Vue

165 lines (164 loc) 5.87 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name2 in all) __defProp(target, name2, { get: all[name2], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var stdin_exports = {}; __export(stdin_exports, { default: () => stdin_default, signatureProps: () => signatureProps }); module.exports = __toCommonJS(stdin_exports); var import_vue = require("vue"); var import_vue2 = require("vue"); var import_utils = require("../utils"); var import_use = require("@vant/use"); var import_button = require("../button"); const [name, bem, t] = (0, import_utils.createNamespace)("signature"); const signatureProps = { tips: String, type: (0, import_utils.makeStringProp)("png"), penColor: (0, import_utils.makeStringProp)("#000"), lineWidth: (0, import_utils.makeNumberProp)(3), clearButtonText: String, backgroundColor: (0, import_utils.makeStringProp)(""), confirmButtonText: String }; const hasCanvasSupport = () => { var _a; const canvas = document.createElement("canvas"); return !!((_a = canvas.getContext) == null ? void 0 : _a.call(canvas, "2d")); }; var stdin_default = (0, import_vue2.defineComponent)({ name, props: signatureProps, emits: ["submit", "clear", "start", "end", "signing"], setup(props, { emit }) { const canvasRef = (0, import_vue2.ref)(); const wrapRef = (0, import_vue2.ref)(); const state = (0, import_vue2.reactive)({ width: 0, height: 0, ctx: null, ratio: import_utils.inBrowser ? window.devicePixelRatio : 1 }); let canvasRect; const isRenderCanvas = import_utils.inBrowser ? hasCanvasSupport() : true; const touchStart = () => { if (!state.ctx) { return false; } state.ctx.beginPath(); state.ctx.lineWidth = props.lineWidth * state.ratio; state.ctx.strokeStyle = props.penColor; canvasRect = (0, import_use.useRect)(canvasRef); emit("start"); }; const touchMove = (event) => { var _a, _b; if (!state.ctx) { return false; } (0, import_utils.preventDefault)(event); const touch = event.touches[0]; const mouseX = (touch.clientX - ((canvasRect == null ? void 0 : canvasRect.left) || 0)) * state.ratio; const mouseY = (touch.clientY - ((canvasRect == null ? void 0 : canvasRect.top) || 0)) * state.ratio; state.ctx.lineCap = "round"; state.ctx.lineJoin = "round"; (_a = state.ctx) == null ? void 0 : _a.lineTo(mouseX, mouseY); (_b = state.ctx) == null ? void 0 : _b.stroke(); emit("signing", event); }; const touchEnd = (event) => { (0, import_utils.preventDefault)(event); emit("end"); }; const isCanvasEmpty = (canvas) => { const empty = document.createElement("canvas"); empty.width = canvas.width; empty.height = canvas.height; return canvas.toDataURL() === empty.toDataURL(); }; const setCanvasBgColor = () => { if (state.ctx && props.backgroundColor) { state.ctx.fillStyle = props.backgroundColor; state.ctx.fillRect(0, 0, state.width, state.height); } }; const submit = () => { var _a, _b; const canvas = canvasRef.value; if (!canvas) { return; } const isEmpty = isCanvasEmpty(canvas); const image = isEmpty ? "" : ((_b = (_a = { jpg: () => canvas.toDataURL("image/jpeg", 0.8), jpeg: () => canvas.toDataURL("image/jpeg", 0.8) })[props.type]) == null ? void 0 : _b.call(_a)) || canvas.toDataURL(`image/${props.type}`); emit("submit", { image, canvas }); }; const clear = () => { if (state.ctx) { state.ctx.clearRect(0, 0, state.width, state.height); state.ctx.closePath(); setCanvasBgColor(); } emit("clear"); }; (0, import_vue2.onMounted)(() => { var _a, _b, _c; if (isRenderCanvas) { state.ctx = (_a = canvasRef.value) == null ? void 0 : _a.getContext("2d"); state.width = (((_b = wrapRef.value) == null ? void 0 : _b.offsetWidth) || 0) * state.ratio; state.height = (((_c = wrapRef.value) == null ? void 0 : _c.offsetHeight) || 0) * state.ratio; (0, import_vue2.nextTick)(() => { setCanvasBgColor(); }); } }); return () => (0, import_vue.createVNode)("div", { "class": bem() }, [(0, import_vue.createVNode)("div", { "class": bem("content"), "ref": wrapRef }, [isRenderCanvas ? (0, import_vue.createVNode)("canvas", { "ref": canvasRef, "width": state.width, "height": state.height, "onTouchstartPassive": touchStart, "onTouchmove": touchMove, "onTouchend": touchEnd }, null) : (0, import_vue.createVNode)("p", null, [props.tips])]), (0, import_vue.createVNode)("div", { "class": bem("footer") }, [(0, import_vue.createVNode)(import_button.Button, { "size": "small", "onClick": clear }, { default: () => [props.clearButtonText || t("clear")] }), (0, import_vue.createVNode)(import_button.Button, { "type": "primary", "size": "small", "onClick": submit }, { default: () => [props.confirmButtonText || t("confirm")] })])]); } });