signature.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. const app = getApp();
  2. const util = require("../../../util/util.js")
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. canvasName: 'handWriting',
  9. ctx: '',
  10. okLook:false,
  11. canvasWidth: 0,
  12. canvasHeight: 0,
  13. transparent: 1, // 透明度
  14. selectColor: 'black',
  15. lineColor: '#1A1A1A', // 颜色
  16. lineSize: 1.5, // 笔记倍数
  17. lineMin: 0.5, // 最小笔画半径
  18. lineMax: 4, // 最大笔画半径
  19. pressure: 1, // 默认压力
  20. smoothness: 60, //顺滑度,用60的距离来计算速度
  21. currentPoint: {},
  22. currentLine: [], // 当前线条
  23. firstTouch: true, // 第一次触发
  24. radius: 1, //画圆的半径
  25. cutArea: {
  26. top: 0,
  27. right: 0,
  28. bottom: 0,
  29. left: 0
  30. }, //裁剪区域
  31. prevUrl: '',
  32. bethelPoint: [], //保存所有线条 生成的贝塞尔点;
  33. lastPoint: 0,
  34. chirography: [], //笔迹
  35. currentChirography: {}, //当前笔迹
  36. linePrack: [] //划线轨迹 , 生成线条的实际点
  37. },
  38. /*======所有自定义函数======*/
  39. // 笔迹开始
  40. uploadScaleStart(e) {
  41. if (e.type != 'touchstart') return false;
  42. let ctx = this.data.ctx;
  43. ctx.setFillStyle(this.data.lineColor); // 初始线条设置颜色
  44. ctx.setGlobalAlpha(this.data.transparent); // 设置半透明
  45. let currentPoint = {
  46. x: e.touches[0].x,
  47. y: e.touches[0].y
  48. }
  49. let currentLine = this.data.currentLine;
  50. currentLine.unshift({
  51. time: new Date().getTime(),
  52. dis: 0,
  53. x: currentPoint.x,
  54. y: currentPoint.y
  55. })
  56. this.setData({
  57. currentPoint,
  58. // currentLine
  59. })
  60. if (this.data.firstTouch) {
  61. this.setData({
  62. cutArea: {
  63. top: currentPoint.y,
  64. right: currentPoint.x,
  65. bottom: currentPoint.y,
  66. left: currentPoint.x
  67. },
  68. firstTouch: false
  69. })
  70. }
  71. this.pointToLine(currentLine);
  72. },
  73. // 笔迹移动
  74. uploadScaleMove(e) {
  75. if (e.type != 'touchmove') return false;
  76. if (e.cancelable) {
  77. // 判断默认行为是否已经被禁用
  78. if (!e.defaultPrevented) {
  79. e.preventDefault();
  80. }
  81. }
  82. let point = {
  83. x: e.touches[0].x,
  84. y: e.touches[0].y
  85. }
  86. //测试裁剪
  87. if (point.y < this.data.cutArea.top) {
  88. this.data.cutArea.top = point.y;
  89. }
  90. if (point.y < 0) this.data.cutArea.top = 0;
  91. if (point.x > this.data.cutArea.right) {
  92. this.data.cutArea.right = point.x;
  93. }
  94. if (this.data.canvasWidth - point.x <= 0) {
  95. this.data.cutArea.right = this.data.canvasWidth;
  96. }
  97. if (point.y > this.data.cutArea.bottom) {
  98. this.data.cutArea.bottom = point.y;
  99. }
  100. if (this.data.canvasHeight - point.y <= 0) {
  101. this.data.cutArea.bottom = this.data.canvasHeight;
  102. }
  103. if (point.x < this.data.cutArea.left) {
  104. this.data.cutArea.left = point.x;
  105. }
  106. if (point.x < 0) this.data.cutArea.left = 0;
  107. this.setData({
  108. lastPoint: this.data.currentPoint,
  109. currentPoint: point
  110. })
  111. let currentLine = this.data.currentLine
  112. currentLine.unshift({
  113. time: new Date().getTime(),
  114. dis: this.distance(this.data.currentPoint, this.data.lastPoint),
  115. x: point.x,
  116. y: point.y
  117. })
  118. // this.setData({
  119. // currentLine
  120. // })
  121. this.pointToLine(currentLine);
  122. },
  123. // 笔迹结束
  124. uploadScaleEnd(e) {
  125. if (e.type != 'touchend') return 0;
  126. let point = {
  127. x: e.changedTouches[0].x,
  128. y: e.changedTouches[0].y
  129. }
  130. this.setData({
  131. lastPoint: this.data.currentPoint,
  132. currentPoint: point
  133. })
  134. let currentLine = this.data.currentLine
  135. currentLine.unshift({
  136. time: new Date().getTime(),
  137. dis: this.distance(this.data.currentPoint, this.data.lastPoint),
  138. x: point.x,
  139. y: point.y
  140. })
  141. if (currentLine.length > 2) {
  142. var info = (currentLine[0].time - currentLine[currentLine.length - 1].time) / currentLine.length;
  143. //$("#info").text(info.toFixed(2));
  144. }
  145. //一笔结束,保存笔迹的坐标点,清空,当前笔迹
  146. //增加判断是否在手写区域;
  147. this.pointToLine(currentLine);
  148. var currentChirography = {
  149. lineSize: this.data.lineSize,
  150. lineColor: this.data.lineColor
  151. };
  152. var chirography = this.data.chirography
  153. chirography.unshift(currentChirography);
  154. this.setData({
  155. chirography
  156. })
  157. var linePrack = this.data.linePrack
  158. linePrack.unshift(this.data.currentLine);
  159. this.setData({
  160. linePrack,
  161. currentLine: []
  162. })
  163. },
  164. retDraw() {
  165. this.data.ctx.clearRect(0, 0, 700, 730)
  166. this.data.ctx.draw();
  167. //设置canvas背景
  168. this.setCanvasBg("#fff");
  169. },
  170. //画两点之间的线条;参数为:line,会绘制最近的开始的两个点;
  171. pointToLine(line) {
  172. this.calcBethelLine(line);
  173. return;
  174. },
  175. //计算插值的方式;
  176. calcBethelLine(line) {
  177. if (line.length <= 1) {
  178. line[0].r = this.data.radius;
  179. return;
  180. }
  181. let x0, x1, x2, y0, y1, y2, r0, r1, r2, len, lastRadius, dis = 0,
  182. time = 0,
  183. curveValue = 0.5;
  184. if (line.length <= 2) {
  185. x0 = line[1].x
  186. y0 = line[1].y
  187. x2 = line[1].x + (line[0].x - line[1].x) * curveValue;
  188. y2 = line[1].y + (line[0].y - line[1].y) * curveValue;
  189. //x2 = line[1].x;
  190. //y2 = line[1].y;
  191. x1 = x0 + (x2 - x0) * curveValue;
  192. y1 = y0 + (y2 - y0) * curveValue;;
  193. } else {
  194. x0 = line[2].x + (line[1].x - line[2].x) * curveValue;
  195. y0 = line[2].y + (line[1].y - line[2].y) * curveValue;
  196. x1 = line[1].x;
  197. y1 = line[1].y;
  198. x2 = x1 + (line[0].x - x1) * curveValue;
  199. y2 = y1 + (line[0].y - y1) * curveValue;
  200. }
  201. //从计算公式看,三个点分别是(x0,y0),(x1,y1),(x2,y2) ;(x1,y1)这个是控制点,控制点不会落在曲线上;实际上,这个点还会手写获取的实际点,却落在曲线上
  202. len = this.distance({
  203. x: x2,
  204. y: y2
  205. }, {
  206. x: x0,
  207. y: y0
  208. });
  209. lastRadius = this.data.radius;
  210. for (let n = 0; n < line.length - 1; n++) {
  211. dis += line[n].dis;
  212. time += line[n].time - line[n + 1].time;
  213. if (dis > this.data.smoothness) break;
  214. }
  215. this.setData({
  216. radius: Math.min(time / len * this.data.pressure + this.data.lineMin, this.data.lineMax) * this.data.lineSize
  217. });
  218. line[0].r = this.data.radius;
  219. //计算笔迹半径;
  220. if (line.length <= 2) {
  221. r0 = (lastRadius + this.data.radius) / 2;
  222. r1 = r0;
  223. r2 = r1;
  224. //return;
  225. } else {
  226. r0 = (line[2].r + line[1].r) / 2;
  227. r1 = line[1].r;
  228. r2 = (line[1].r + line[0].r) / 2;
  229. }
  230. let n = 5;
  231. let point = [];
  232. for (let i = 0; i < n; i++) {
  233. let t = i / (n - 1);
  234. let x = (1 - t) * (1 - t) * x0 + 2 * t * (1 - t) * x1 + t * t * x2;
  235. let y = (1 - t) * (1 - t) * y0 + 2 * t * (1 - t) * y1 + t * t * y2;
  236. let r = lastRadius + (this.data.radius - lastRadius) / n * i;
  237. point.push({
  238. x: x,
  239. y: y,
  240. r: r
  241. });
  242. if (point.length == 3) {
  243. let a = this.ctaCalc(point[0].x, point[0].y, point[0].r, point[1].x, point[1].y, point[1].r, point[2].x, point[2].y, point[2].r);
  244. a[0].color = this.data.lineColor;
  245. // let bethelPoint = this.data.bethelPoint;
  246. // console.log(a)
  247. // console.log(this.data.bethelPoint)
  248. // bethelPoint = bethelPoint.push(a);
  249. this.bethelDraw(a, 1);
  250. point = [{
  251. x: x,
  252. y: y,
  253. r: r
  254. }];
  255. }
  256. }
  257. this.setData({
  258. currentLine: line
  259. })
  260. },
  261. //求两点之间距离
  262. distance(a, b) {
  263. let x = b.x - a.x;
  264. let y = b.y - a.y;
  265. return Math.sqrt(x * x + y * y);
  266. },
  267. ctaCalc(x0, y0, r0, x1, y1, r1, x2, y2, r2) {
  268. let a = [],
  269. vx01, vy01, norm, n_x0, n_y0, vx21, vy21, n_x2, n_y2;
  270. vx01 = x1 - x0;
  271. vy01 = y1 - y0;
  272. norm = Math.sqrt(vx01 * vx01 + vy01 * vy01 + 0.0001) * 2;
  273. vx01 = vx01 / norm * r0;
  274. vy01 = vy01 / norm * r0;
  275. n_x0 = vy01;
  276. n_y0 = -vx01;
  277. vx21 = x1 - x2;
  278. vy21 = y1 - y2;
  279. norm = Math.sqrt(vx21 * vx21 + vy21 * vy21 + 0.0001) * 2;
  280. vx21 = vx21 / norm * r2;
  281. vy21 = vy21 / norm * r2;
  282. n_x2 = -vy21;
  283. n_y2 = vx21;
  284. a.push({
  285. mx: x0 + n_x0,
  286. my: y0 + n_y0,
  287. color: "#1A1A1A"
  288. });
  289. a.push({
  290. c1x: x1 + n_x0,
  291. c1y: y1 + n_y0,
  292. c2x: x1 + n_x2,
  293. c2y: y1 + n_y2,
  294. ex: x2 + n_x2,
  295. ey: y2 + n_y2
  296. });
  297. a.push({
  298. c1x: x2 + n_x2 - vx21,
  299. c1y: y2 + n_y2 - vy21,
  300. c2x: x2 - n_x2 - vx21,
  301. c2y: y2 - n_y2 - vy21,
  302. ex: x2 - n_x2,
  303. ey: y2 - n_y2
  304. });
  305. a.push({
  306. c1x: x1 - n_x2,
  307. c1y: y1 - n_y2,
  308. c2x: x1 - n_x0,
  309. c2y: y1 - n_y0,
  310. ex: x0 - n_x0,
  311. ey: y0 - n_y0
  312. });
  313. a.push({
  314. c1x: x0 - n_x0 - vx01,
  315. c1y: y0 - n_y0 - vy01,
  316. c2x: x0 + n_x0 - vx01,
  317. c2y: y0 + n_y0 - vy01,
  318. ex: x0 + n_x0,
  319. ey: y0 + n_y0
  320. });
  321. a[0].mx = a[0].mx.toFixed(1);
  322. a[0].mx = parseFloat(a[0].mx);
  323. a[0].my = a[0].my.toFixed(1);
  324. a[0].my = parseFloat(a[0].my);
  325. for (let i = 1; i < a.length; i++) {
  326. a[i].c1x = a[i].c1x.toFixed(1);
  327. a[i].c1x = parseFloat(a[i].c1x);
  328. a[i].c1y = a[i].c1y.toFixed(1);
  329. a[i].c1y = parseFloat(a[i].c1y);
  330. a[i].c2x = a[i].c2x.toFixed(1);
  331. a[i].c2x = parseFloat(a[i].c2x);
  332. a[i].c2y = a[i].c2y.toFixed(1);
  333. a[i].c2y = parseFloat(a[i].c2y);
  334. a[i].ex = a[i].ex.toFixed(1);
  335. a[i].ex = parseFloat(a[i].ex);
  336. a[i].ey = a[i].ey.toFixed(1);
  337. a[i].ey = parseFloat(a[i].ey);
  338. }
  339. return a;
  340. },
  341. bethelDraw(point, is_fill, color) {
  342. let ctx = this.data.ctx;
  343. ctx.beginPath();
  344. ctx.moveTo(point[0].mx, point[0].my);
  345. if (undefined != color) {
  346. ctx.setFillStyle(color);
  347. ctx.setStrokeStyle(color);
  348. } else {
  349. ctx.setFillStyle(point[0].color);
  350. ctx.setStrokeStyle(point[0].color);
  351. }
  352. for (let i = 1; i < point.length; i++) {
  353. ctx.bezierCurveTo(point[i].c1x, point[i].c1y, point[i].c2x, point[i].c2y, point[i].ex, point[i].ey);
  354. }
  355. ctx.stroke();
  356. if (undefined != is_fill) {
  357. ctx.fill(); //填充图形 ( 后绘制的图形会覆盖前面的图形, 绘制时注意先后顺序 )
  358. }
  359. ctx.draw(true)
  360. },
  361. selectColorEvent(event) {
  362. console.log(event)
  363. var color = event.currentTarget.dataset.colorValue;
  364. var colorSelected = event.currentTarget.dataset.color;
  365. this.setData({
  366. selectColor: colorSelected,
  367. lineColor: color
  368. })
  369. },
  370. //将Canvas内容转成 临时图片 --> cb 为回调函数 形参 tempImgPath 为 生成的图片临时路径
  371. canvasToImg(cb) { //这种写法移动端 出不来
  372. this.data.ctx.draw(true, () => {
  373. wx.canvasToTempFilePath({
  374. canvasId: 'handWriting',
  375. fileType: 'png',
  376. quality: 1, //图片质量
  377. success(res) {
  378. wx.showToast({
  379. title: '执行了吗?',
  380. })
  381. cb(res.tempFilePath);
  382. }
  383. })
  384. });
  385. },
  386. doSaveServer( err, {url} ){
  387. let method='/maker/addContractImg';
  388. util.http( method, {url}, (err,res)=>{
  389. if( err!= 0 ) return;
  390. this.setData({prevUrl:url})
  391. util.showSuccess("合同签约成功")
  392. })
  393. },
  394. //完成
  395. subCanvas() {
  396. wx.canvasToTempFilePath({
  397. canvasId: 'handWriting',
  398. fileType: 'png',
  399. destWidth: 300,
  400. destHeight:600,
  401. success: (res)=> {
  402. util.uploadFile(res.tempFilePath, this.doSaveServer, true)
  403. }
  404. })
  405. },
  406. //保存到相册
  407. saveCanvasAsImg() {
  408. wx.canvasToTempFilePath({
  409. canvasId: 'handWriting',
  410. fileType: 'png',
  411. quality: 1, //图片质量
  412. success(res) {
  413. wx.saveImageToPhotosAlbum({
  414. filePath: res.tempFilePath,
  415. success(res) {
  416. wx.showToast({
  417. title: '已保存到相册',
  418. duration: 2000
  419. });
  420. }
  421. })
  422. }
  423. })
  424. },
  425. //预览
  426. previewCanvasImg() {
  427. let that = this
  428. wx.canvasToTempFilePath({
  429. canvasId: 'handWriting',
  430. fileType: 'png',
  431. quality: 1, //图片质量
  432. success(res) {
  433. console.log("temp", res.tempFilePath)
  434. that.prevViewSign( res.tempFilePath )
  435. // wx.previewImage({
  436. // urls: [res.tempFilePath], //预览图片 数组
  437. // })
  438. }
  439. })
  440. },
  441. previewSign(e){
  442. let url= this.data.prevUrl||app.globalData.signUrl;
  443. console.log("previewSign", url)
  444. wx.previewImage({
  445. urls: [url], //预览图片 数组
  446. })
  447. },
  448. //设置canvas背景色 不设置 导出的canvas的背景为透明
  449. //@params:字符串 color
  450. setCanvasBg(color) {
  451. /* 将canvas背景设置为 白底,不设置 导出的canvas的背景为透明 */
  452. //rect() 参数说明 矩形路径左上角的横坐标,左上角的纵坐标, 矩形路径的宽度, 矩形路径的高度
  453. //这里是 canvasHeight - 4 是因为下边盖住边框了,所以手动减了写
  454. this.data.ctx.rect(0, 0, this.data.canvasWidth, this.data.canvasHeight - 4);
  455. // console.log("drag image")
  456. // this.data.ctx.drawImage(bgPicturePath, 0, 0, this.data.canvasWidth, this.data.canvasHeight - 4);
  457. //
  458. // ctx.setFillStyle('red')
  459. // this.data.ctx.setFillStyle(color)
  460. // this.data.ctx.fill() //设置填充
  461. this.data.ctx.draw() //开画
  462. },
  463. /*======所有自定义函数=END=====*/
  464. /**
  465. * 生命周期函数--监听页面加载
  466. */
  467. onLoad: function (options) {
  468. app.getMakerInfo( res =>{
  469. this.setData( {prevUrl: res.contract_img })
  470. })
  471. let canvasName = this.data.canvasName
  472. let ctx = wx.createCanvasContext(canvasName)
  473. this.setData({
  474. ctx: ctx
  475. })
  476. var query = wx.createSelectorQuery();
  477. query.select('.handCenter').boundingClientRect(rect => {
  478. this.setData({
  479. canvasWidth: rect.width,
  480. canvasHeight: rect.height
  481. })
  482. /* 将canvas背景设置为 白底,不设置 导出的canvas的背景为透明 */
  483. this.setCanvasBg('#fff');
  484. }).exec();
  485. },
  486. goBack(){
  487. wx.navigateBack({
  488. delta: 1,
  489. })
  490. },
  491. /**
  492. * 生命周期函数--监听页面初次渲染完成
  493. */
  494. onReady: function () {
  495. },
  496. /**
  497. * 生命周期函数--监听页面显示
  498. */
  499. onShow: function () {
  500. },
  501. /**
  502. * 生命周期函数--监听页面隐藏
  503. */
  504. onHide: function () {
  505. },
  506. /**
  507. * 生命周期函数--监听页面卸载
  508. */
  509. onUnload: function () {
  510. },
  511. /**
  512. * 页面相关事件处理函数--监听用户下拉动作
  513. */
  514. onPullDownRefresh: function () {
  515. },
  516. /**
  517. * 页面上拉触底事件的处理函数
  518. */
  519. onReachBottom: function () {
  520. },
  521. /**
  522. * 用户点击右上角分享
  523. */
  524. onShareAppMessage: function () {
  525. }
  526. })