sin-signature.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. <template>
  2. <view class="signature-wrap">
  3. <view class="img-wrap" @tap="showSignature()" @touchstart="touchSignature()">
  4. <image :src="absPrevView" mode="scaleToFill"></image>
  5. </view>
  6. <view v-if="!disabled" v-show="show" class="signature-contain">
  7. <view class="signature-main" style="z-index: 3000;">
  8. <view class="signature-title"><text v-for="t in titles">{{t}}</text></view>
  9. <canvas disable-scroll="true" class="signature" :class="cid" canvas-id="cvs" @touchstart="touchstart"
  10. @touchmove="touchmove" @touchend="touchend"></canvas>
  11. <view class="signature-btns">
  12. <view class="btn btn-cancel cu-btn bg-main margin-tb-sm text-white" @tap="cancelSignature()">
  13. <text>取</text><text>消</text>
  14. </view>
  15. <view class="btn btn-clear cu-btn bg-main margin-tb-sm text-white" @tap="clearSignature();">
  16. <text>清</text><text>空</text>
  17. </view>
  18. <view class="btn btn-ok cu-btn bg-main margin-tb-sm text-white" @tap="onOK()">
  19. <text>确</text><text>定</text>
  20. </view>
  21. </view>
  22. </view>
  23. </view>
  24. </view>
  25. </template>
  26. <script>
  27. let _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  28. var _utf8_encode = function(string) {
  29. string = string.replace(/\r\n/g, "\n");
  30. var utftext = "";
  31. for (var n = 0; n < string.length; n++) {
  32. var c = string.charCodeAt(n);
  33. if (c < 128) {
  34. utftext += String.fromCharCode(c);
  35. } else if ((c > 127) && (c < 2048)) {
  36. utftext += String.fromCharCode((c >> 6) | 192);
  37. utftext += String.fromCharCode((c & 63) | 128);
  38. } else {
  39. utftext += String.fromCharCode((c >> 12) | 224);
  40. utftext += String.fromCharCode(((c >> 6) & 63) | 128);
  41. utftext += String.fromCharCode((c & 63) | 128);
  42. }
  43. }
  44. return utftext;
  45. }
  46. let base64encode = function(input) {
  47. var output = "";
  48. var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
  49. var i = 0;
  50. input = _utf8_encode(input);
  51. while (i < input.length) {
  52. chr1 = input.charCodeAt(i++);
  53. chr2 = input.charCodeAt(i++);
  54. chr3 = input.charCodeAt(i++);
  55. enc1 = chr1 >> 2;
  56. enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
  57. enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
  58. enc4 = chr3 & 63;
  59. if (isNaN(chr2)) {
  60. enc3 = enc4 = 64;
  61. } else if (isNaN(chr3)) {
  62. enc4 = 64;
  63. }
  64. output = output +
  65. _keyStr.charAt(enc1) + _keyStr.charAt(enc2) +
  66. _keyStr.charAt(enc3) + _keyStr.charAt(enc4);
  67. }
  68. return output;
  69. }
  70. export default {
  71. cxt: null,
  72. data() {
  73. return {
  74. VERSION: '1.0.0',
  75. cid: 'cvs',
  76. show: false,
  77. ctrl: null,
  78. listeners: [],
  79. prevView: '',
  80. draws: [],
  81. lines: [],
  82. line: null,
  83. };
  84. },
  85. props: {
  86. value: {
  87. default: '',
  88. },
  89. title: {
  90. type: String,
  91. default: '请签字',
  92. },
  93. disabled: {
  94. type: Boolean,
  95. default: false,
  96. }
  97. },
  98. watch: {
  99. value() {
  100. this.prevView = this.value;
  101. }
  102. },
  103. computed: {
  104. titles() {
  105. return this.title.split('')
  106. },
  107. absPrevView() {
  108. var pv = this.prevView;
  109. // if(pv){
  110. // pv = this.$wrapUrl(pv)
  111. // }
  112. return pv;
  113. }
  114. },
  115. mounted() {
  116. this.prevView = this.value;
  117. console.log('dx')
  118. },
  119. methods: {
  120. onOK() {
  121. let data = this.ctrl.getValue();
  122. this.$emit('input', data);
  123. this.prevView = data;
  124. this.hideSignature();
  125. let f = this.listeners.shift();
  126. if (f) {
  127. f(data);
  128. }
  129. },
  130. touchSignature() {
  131. let sig = this.prevView
  132. if (!sig || !sig.length) {
  133. this.showSignature()
  134. }
  135. },
  136. showSignature() {
  137. if (this.disabled)
  138. return;
  139. if (!this.ctrl) {
  140. this.initCtrl();
  141. } else if (!this.show) {
  142. this.clearSignature();
  143. this.show = true;
  144. }
  145. },
  146. async getSyncSignature() {
  147. this.showSignature();
  148. return await new Promise(async (resolve, reject) => {
  149. this.listeners.push((res) => {
  150. resolve(res);
  151. });
  152. });
  153. },
  154. cancelSignature() {
  155. this.listeners.map((f) => {
  156. f(null);
  157. })
  158. this.hideSignature();
  159. },
  160. hideSignature() {
  161. this.ctrl && this.ctrl.clear();
  162. this.show = false;
  163. },
  164. clearSignature() {
  165. this.ctrl && this.ctrl.clear();
  166. },
  167. async initCtrl() {
  168. this.show = true;
  169. let cxt = uni.createCanvasContext(this.cid, this);
  170. this.cxt = cxt;
  171. // cxt.clearRect(0,0,c.width,c.height);
  172. this.ctrl = {
  173. width: 0,
  174. height: 0,
  175. clear: () => {
  176. this.lines = [];
  177. let info = uni.createSelectorQuery().in(this).select("." + this.cid);
  178. info.boundingClientRect((data) => {
  179. if (data) {
  180. cxt.clearRect(0, 0, data.width, data.height);
  181. if (data.width && data.height) {
  182. this.ctrl.width = data.width;
  183. this.ctrl.height = data.height;
  184. }
  185. }
  186. }).exec();
  187. this.redraw();
  188. },
  189. getValue: () => {
  190. if (!this.lines.length)
  191. return '';
  192. let svg = this._get_svg();
  193. // new Buff
  194. let b64 = base64encode(svg);
  195. let data = 'data:image/svg+xml;base64,' + b64;
  196. // console.log(svg);
  197. // console.log(data);
  198. return data;
  199. },
  200. };
  201. this.$nextTick(function() {
  202. this.ctrl.clear();
  203. })
  204. },
  205. _get_svg() {
  206. let r = -90;
  207. let paths = [];
  208. let raww = this.ctrl.width;
  209. let rawh = this.ctrl.height;
  210. let width = Math.abs(r) != 90 ? raww : rawh;
  211. let height = Math.abs(r) == 90 ? raww : rawh;
  212. let cx = raww / 2;
  213. let cy = rawh / 2;
  214. let PI = Math.PI;
  215. let R = (r || 0) % 360;
  216. let cosv = Math.cos(R * PI / 180);
  217. let sinv = Math.sin(R * PI / 180);
  218. let dcx = (width - raww) / 2;
  219. let dcy = (height - rawh) / 2;
  220. let trans = function(p) {
  221. if (!R) {
  222. return p;
  223. } else {
  224. let nx = (p.x - cx) * cosv - (p.y - cy) * sinv + cx;
  225. let ny = (p.x - cx) * sinv + (p.y - cy) * cosv + cy;
  226. return {
  227. x: nx + dcx,
  228. y: ny + dcy
  229. };
  230. }
  231. return p;
  232. }
  233. this.lines.map(l => {
  234. if (l.points.length < 2) {
  235. return;
  236. }
  237. let sp = trans(l.start)
  238. let pts = [`M ${sp.x} ${Number(sp.y)}`];
  239. l.points.map(p => {
  240. let np = trans(p)
  241. pts.push(`L ${np.x} ${Number(np.y)}`);
  242. });
  243. paths.push(
  244. `<path stroke-linejoin="round" stroke-linecap="round" stroke-width="3" stroke="rgb(0,0,0)" fill="none" d="${pts.join(' ')}"/>`
  245. );
  246. })
  247. let svg =
  248. `<?xml version="1.0" encoding="UTF-8" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="${width}" height="${height}">${paths.join('\n')}</svg>`;
  249. return svg;
  250. },
  251. _get_svg_raw() {
  252. let paths = [];
  253. this.lines.map(l => {
  254. if (l.points.length < 2) {
  255. return;
  256. }
  257. let pts = [`M ${l.start.x} ${Number(l.start.y)}`];
  258. l.points.map(p => {
  259. pts.push(`L ${p.x} ${Number(p.y)}`);
  260. });
  261. paths.push(
  262. `<path stroke-linejoin="round" stroke-linecap="round" stroke-width="3" stroke="rgb(0,0,0)" fill="none" d="${pts.join(' ')}"/>`
  263. );
  264. })
  265. let width = this.ctrl.width;
  266. let height = this.ctrl.height;
  267. let svg =
  268. `<?xml version="1.0" encoding="UTF-8" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="${width}" height="${height}" transform="rotate(-90)">${paths.join('\n')}</svg>`;
  269. return svg;
  270. },
  271. _get_point(e) {
  272. return {
  273. x: e.changedTouches[0].x.toFixed(1),
  274. y: e.changedTouches[0].y.toFixed(1),
  275. }
  276. },
  277. touchstart(e) {
  278. let p = this._get_point(e);
  279. this.line = {
  280. start: p,
  281. points: [p],
  282. }
  283. this.lines.push(this.line);
  284. },
  285. touchmove(e) {
  286. let p = this._get_point(e);
  287. this.line.points.push(p)
  288. if (!this.tm) {
  289. this.tm = setTimeout(() => {
  290. this.redraw();
  291. this.tm = 0;
  292. }, 10)
  293. }
  294. },
  295. touchend(e) {
  296. let p = this._get_point(e);
  297. this.line.points.push(p)
  298. this.line.end = p
  299. this.redraw()
  300. },
  301. redraw() {
  302. let cxt = this.cxt;
  303. cxt.setStrokeStyle("#000");
  304. cxt.setLineWidth(3);
  305. var last = null;
  306. this.lines.map(l => {
  307. cxt.beginPath();
  308. if (l.points.length < 2) {
  309. return;
  310. }
  311. cxt.moveTo(l.start.x, l.start.y);
  312. l.points.map(p => {
  313. cxt.lineTo(p.x, p.y)
  314. })
  315. cxt.stroke()
  316. })
  317. cxt.draw()
  318. },
  319. canvasIdErrorCallback: function(e) {
  320. console.error(e.detail.errMsg)
  321. }
  322. }
  323. }
  324. </script>
  325. <style lang="scss">
  326. .signature-wrap {
  327. height: 100%;
  328. width: 100%;
  329. // padding: 0 5px;
  330. // min-width: 60vw;
  331. .img-wrap {
  332. width: 100%;
  333. min-height: 200rpx;
  334. display: flex;
  335. align-items: center;
  336. text-align: center;
  337. align-content: center;
  338. justify-content: center;
  339. image {
  340. width: 100%;
  341. }
  342. // background: red;
  343. }
  344. }
  345. .signature-contain {
  346. z-index: 9000;
  347. position: fixed;
  348. left: 0;
  349. top: 0;
  350. width: 100%;
  351. .signature-main {
  352. background: white;
  353. flex-direction: row-reverse;
  354. display: flex;
  355. align-items: stretch;
  356. height: 101%;
  357. overflow: scroll;
  358. }
  359. .signature-title {
  360. font-weight: bold;
  361. font-size: 18px;
  362. display: flex;
  363. padding: 0 20rpx;
  364. flex-direction: column;
  365. justify-content: center;
  366. height: 100vh;
  367. color: $uni-text-color;
  368. text {
  369. transform: rotate(90deg);
  370. }
  371. }
  372. .signature {
  373. border: 1px dotted black;
  374. border-bottom: 1px dotted black;
  375. background: #FFF;
  376. margin: 10px 0;
  377. width: 90vw;
  378. height: 90vh;
  379. align-self: center;
  380. // pointer-events:none;
  381. }
  382. .signature-btns {
  383. display: flex;
  384. padding: 2px;
  385. // margin-right: 5px;
  386. flex-direction: column;
  387. .btn {
  388. flex-grow: 1;
  389. flex-shrink: 0;
  390. padding: 20rpx;
  391. font-size: 20px;
  392. margin: 0;
  393. text-align: center;
  394. text-decoration: none;
  395. height: 30vh;
  396. display: flex;
  397. align-content: center;
  398. justify-content: center;
  399. flex-direction: column;
  400. text {
  401. transform: rotate(90deg);
  402. }
  403. &+.btn {
  404. border-top: 1px solid #eee;
  405. }
  406. &.btn-clear {
  407. // background-color: #fc2a07;
  408. color: $uni-color-success;
  409. }
  410. &.btn-cancel {
  411. // background-color: #eff4f4;
  412. color: $uni-color-warning;
  413. }
  414. &.btn-ok {
  415. // background-color: $uni-color-success;
  416. color: $uni-color-primary;
  417. }
  418. }
  419. }
  420. }
  421. </style>