signature.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. class Handwriting {
  2. // 内置数据
  3. ctx = '';
  4. canvasWidth = 300;
  5. canvasHeight = 900;
  6. linePrack = []; //划线轨迹 ; 生成线条的实际点
  7. currentLine = [];
  8. transparent = 1; // 透明度
  9. pressure = 0.5; // 默认压力
  10. smoothness = 100; //顺滑度,用60的距离来计算速度
  11. lineSize = 1.5; // 笔记倍数
  12. lineMin = 0.5; // 最小笔画半径
  13. lineMax = 2; // 最大笔画半径
  14. currentPoint = {};
  15. firstTouch = true; // 第一次触发
  16. radius = 1; //画圆的半径
  17. cutArea = {
  18. top: 0,
  19. right: 0,
  20. bottom: 0,
  21. left: 0
  22. }; //裁剪区域
  23. lastPoint = 0;
  24. chirography = []; //笔迹
  25. startY = 0;
  26. deltaY = 0;
  27. startValue = 0;
  28. constructor(opts) {
  29. console.log(opts);
  30. console.log('canvas', opts)
  31. this.lineColor = opts.lineColor || '#1A1A1A' // 颜色
  32. this.slideValue = opts.slideValue || 50
  33. this.canvasName = opts.canvasName || 'handWriting'
  34. this.init()
  35. }
  36. init() {
  37. this.ctx = uni.createCanvasContext(this.canvasName)
  38. var query = uni.createSelectorQuery();
  39. query.select('.handCenter').boundingClientRect(rect => {
  40. if(rect) {
  41. if (rect.width) {
  42. this.canvasWidth = rect.width;
  43. }
  44. if (rect.height) {
  45. this.canvasHeight = rect.height;
  46. }
  47. }
  48. }).exec();
  49. this.selectSlideValue(this.slideValue);
  50. }
  51. // 笔迹开始
  52. uploadScaleStart(event) {
  53. console.log('start');
  54. let e = event.mp
  55. console.log(e.touches[0])
  56. if (e.type != 'touchstart') return false;
  57. this.ctx.setFillStyle(this.lineColor); // 初始线条设置颜色
  58. this.ctx.setGlobalAlpha(this.transparent); // 设置半透明
  59. this.currentPoint = {
  60. x: e.touches[0].x,
  61. y: e.touches[0].y
  62. }
  63. this.currentLine.unshift({
  64. time: new Date().getTime(),
  65. dis: 0,
  66. x: this.currentPoint.x,
  67. y: this.currentPoint.y
  68. })
  69. if (this.firstTouch) {
  70. this.cutArea = {
  71. top: this.currentPoint.y,
  72. right: this.currentPoint.x,
  73. bottom: this.currentPoint.y,
  74. left: this.currentPoint.x
  75. }
  76. this.firstTouch = false
  77. }
  78. this.pointToLine(this.currentLine);
  79. }
  80. // 笔迹移动
  81. uploadScaleMove(event) {
  82. let e = event.mp
  83. if (e.type != 'touchmove') return false;
  84. if (e.cancelable) {
  85. // 判断默认行为是否已经被禁用
  86. if (!e.defaultPrevented) {
  87. e.preventDefault();
  88. }
  89. }
  90. let point = {
  91. x: e.touches[0].x,
  92. y: e.touches[0].y
  93. }
  94. //测试裁剪
  95. if (point.y < this.cutArea.top) {
  96. this.cutArea.top = point.y;
  97. }
  98. if (point.y < 0) this.cutArea.top = 0;
  99. if (point.x > this.cutArea.right) {
  100. this.cutArea.right = point.x;
  101. }
  102. if (this.canvasWidth - point.x <= 0) {
  103. this.cutArea.right = this.canvasWidth;
  104. }
  105. if (point.y > this.cutArea.bottom) {
  106. this.cutArea.bottom = point.y;
  107. }
  108. if (this.canvasHeight - point.y <= 0) {
  109. this.cutArea.bottom = this.canvasHeight;
  110. }
  111. if (point.x < this.cutArea.left) {
  112. this.cutArea.left = point.x;
  113. }
  114. if (point.x < 0) this.cutArea.left = 0;
  115. this.lastPoint = this.currentPoint;
  116. this.currentPoint = point
  117. this.currentLine.unshift({
  118. time: new Date().getTime(),
  119. dis: this.distance(this.currentPoint, this.lastPoint, 'move'),
  120. x: point.x,
  121. y: point.y
  122. })
  123. this.pointToLine(this.currentLine);
  124. }
  125. // 笔迹结束
  126. uploadScaleEnd(event) {
  127. let e = event.mp
  128. if (e.type != 'touchend') return 0;
  129. console.log(e);
  130. let point = {
  131. x: e.changedTouches[0].x,
  132. y: e.changedTouches[0].y
  133. }
  134. this.lastPoint = this.currentPoint;
  135. this.currentPoint = point
  136. this.currentLine.unshift({
  137. time: new Date().getTime(),
  138. dis: this.distance(this.currentPoint, this.lastPoint, 'end'),
  139. x: point.x,
  140. y: point.y
  141. })
  142. if (this.currentLine.length > 2) {
  143. var info = (this.currentLine[0].time - this.currentLine[this.currentLine.length - 1].time) / this
  144. .currentLine.length;
  145. //$("#info").text(info.toFixed(2));
  146. }
  147. //一笔结束,保存笔迹的坐标点,清空,当前笔迹
  148. //增加判断是否在手写区域;
  149. this.pointToLine(this.currentLine);
  150. var currentChirography = {
  151. lineSize: this.lineSize,
  152. lineColor: this.lineColor
  153. };
  154. this.chirography.unshift(currentChirography);
  155. this.linePrack.unshift(this.currentLine);
  156. this.currentLine = []
  157. }
  158. retDraw() {
  159. this.ctx.clearRect(0, 0, 700, 730)
  160. this.ctx.draw()
  161. this.linePrack = []
  162. }
  163. //画两点之间的线条;参数为:line,会绘制最近的开始的两个点;
  164. pointToLine(line) {
  165. this.calcBethelLine(line);
  166. // this.calcBethelLine1(line);
  167. return;
  168. }
  169. //计算插值的方式;
  170. calcBethelLine(line) {
  171. if (line.length <= 1) {
  172. line[0].r = this.radius;
  173. return;
  174. }
  175. let x0, x1, x2, y0, y1, y2, r0, r1, r2, len, lastRadius, dis = 0,
  176. time = 0,
  177. curveValue = 0.5;
  178. if (line.length <= 2) {
  179. x0 = line[1].x
  180. y0 = line[1].y
  181. x2 = line[1].x + (line[0].x - line[1].x) * curveValue;
  182. y2 = line[1].y + (line[0].y - line[1].y) * curveValue;
  183. //x2 = line[1].x;
  184. //y2 = line[1].y;
  185. x1 = x0 + (x2 - x0) * curveValue;
  186. y1 = y0 + (y2 - y0) * curveValue;;
  187. } else {
  188. x0 = line[2].x + (line[1].x - line[2].x) * curveValue;
  189. y0 = line[2].y + (line[1].y - line[2].y) * curveValue;
  190. x1 = line[1].x;
  191. y1 = line[1].y;
  192. x2 = x1 + (line[0].x - x1) * curveValue;
  193. y2 = y1 + (line[0].y - y1) * curveValue;
  194. }
  195. //从计算公式看,三个点分别是(x0,y0),(x1,y1),(x2,y2) ;(x1,y1)这个是控制点,控制点不会落在曲线上;实际上,这个点还会手写获取的实际点,却落在曲线上
  196. len = this.distance({
  197. x: x2,
  198. y: y2
  199. }, {
  200. x: x0,
  201. y: y0
  202. }, 'calc');
  203. lastRadius = this.radius;
  204. for (let n = 0; n < line.length - 1; n++) {
  205. dis += line[n].dis;
  206. time += line[n].time - line[n + 1].time;
  207. if (dis > this.smoothness) break;
  208. }
  209. this.radius = Math.min(time / len * this.pressure + this.lineMin, this.lineMax) * this.lineSize
  210. line[0].r = this.radius;
  211. //计算笔迹半径;
  212. if (line.length <= 2) {
  213. r0 = (lastRadius + this.radius) / 2;
  214. r1 = r0;
  215. r2 = r1;
  216. //return;
  217. } else {
  218. r0 = (line[2].r + line[1].r) / 2;
  219. r1 = line[1].r;
  220. r2 = (line[1].r + line[0].r) / 2;
  221. }
  222. let n = 5;
  223. let point = [];
  224. for (let i = 0; i < n; i++) {
  225. let t = i / (n - 1);
  226. let x = (1 - t) * (1 - t) * x0 + 2 * t * (1 - t) * x1 + t * t * x2;
  227. let y = (1 - t) * (1 - t) * y0 + 2 * t * (1 - t) * y1 + t * t * y2;
  228. let r = lastRadius + (this.radius - lastRadius) / n * i;
  229. point.push({
  230. x: x,
  231. y: y,
  232. r: r
  233. });
  234. if (point.length == 3) {
  235. let a = this.ctaCalc(point[0].x, point[0].y, point[0].r, point[1].x, point[1].y, point[1].r, point[
  236. 2].x, point[2].y, point[2].r);
  237. a[0].color = this.lineColor;
  238. this.bethelDraw(a, 1);
  239. point = [{
  240. x: x,
  241. y: y,
  242. r: r
  243. }];
  244. }
  245. }
  246. }
  247. //求两点之间距离
  248. distance(a, b, type) {
  249. let x = b.x - a.x;
  250. let y = b.y - a.y;
  251. return Math.sqrt(x * x + y * y) * 5;
  252. }
  253. ctaCalc(x0, y0, r0, x1, y1, r1, x2, y2, r2) {
  254. let a = [],
  255. vx01, vy01, norm, n_x0, n_y0, vx21, vy21, n_x2, n_y2;
  256. vx01 = x1 - x0;
  257. vy01 = y1 - y0;
  258. norm = Math.sqrt(vx01 * vx01 + vy01 * vy01 + 0.0001) * 2;
  259. vx01 = vx01 / norm * r0;
  260. vy01 = vy01 / norm * r0;
  261. n_x0 = vy01;
  262. n_y0 = -vx01;
  263. vx21 = x1 - x2;
  264. vy21 = y1 - y2;
  265. norm = Math.sqrt(vx21 * vx21 + vy21 * vy21 + 0.0001) * 2;
  266. vx21 = vx21 / norm * r2;
  267. vy21 = vy21 / norm * r2;
  268. n_x2 = -vy21;
  269. n_y2 = vx21;
  270. a.push({
  271. mx: x0 + n_x0,
  272. my: y0 + n_y0,
  273. color: "#1A1A1A"
  274. });
  275. a.push({
  276. c1x: x1 + n_x0,
  277. c1y: y1 + n_y0,
  278. c2x: x1 + n_x2,
  279. c2y: y1 + n_y2,
  280. ex: x2 + n_x2,
  281. ey: y2 + n_y2
  282. });
  283. a.push({
  284. c1x: x2 + n_x2 - vx21,
  285. c1y: y2 + n_y2 - vy21,
  286. c2x: x2 - n_x2 - vx21,
  287. c2y: y2 - n_y2 - vy21,
  288. ex: x2 - n_x2,
  289. ey: y2 - n_y2
  290. });
  291. a.push({
  292. c1x: x1 - n_x2,
  293. c1y: y1 - n_y2,
  294. c2x: x1 - n_x0,
  295. c2y: y1 - n_y0,
  296. ex: x0 - n_x0,
  297. ey: y0 - n_y0
  298. });
  299. a.push({
  300. c1x: x0 - n_x0 - vx01,
  301. c1y: y0 - n_y0 - vy01,
  302. c2x: x0 + n_x0 - vx01,
  303. c2y: y0 + n_y0 - vy01,
  304. ex: x0 + n_x0,
  305. ey: y0 + n_y0
  306. });
  307. a[0].mx = a[0].mx.toFixed(1);
  308. a[0].mx = parseFloat(a[0].mx);
  309. a[0].my = a[0].my.toFixed(1);
  310. a[0].my = parseFloat(a[0].my);
  311. for (let i = 1; i < a.length; i++) {
  312. a[i].c1x = a[i].c1x.toFixed(1);
  313. a[i].c1x = parseFloat(a[i].c1x);
  314. a[i].c1y = a[i].c1y.toFixed(1);
  315. a[i].c1y = parseFloat(a[i].c1y);
  316. a[i].c2x = a[i].c2x.toFixed(1);
  317. a[i].c2x = parseFloat(a[i].c2x);
  318. a[i].c2y = a[i].c2y.toFixed(1);
  319. a[i].c2y = parseFloat(a[i].c2y);
  320. a[i].ex = a[i].ex.toFixed(1);
  321. a[i].ex = parseFloat(a[i].ex);
  322. a[i].ey = a[i].ey.toFixed(1);
  323. a[i].ey = parseFloat(a[i].ey);
  324. }
  325. return a;
  326. }
  327. bethelDraw(point, is_fill, color) {
  328. this.ctx.beginPath();
  329. this.ctx.moveTo(point[0].mx, point[0].my);
  330. if (undefined != color) {
  331. this.ctx.setFillStyle(color);
  332. this.ctx.setStrokeStyle(color);
  333. } else {
  334. this.ctx.setFillStyle(point[0].color);
  335. this.ctx.setStrokeStyle(point[0].color);
  336. }
  337. for (let i = 1; i < point.length; i++) {
  338. this.ctx.bezierCurveTo(point[i].c1x, point[i].c1y, point[i].c2x, point[i].c2y, point[i].ex, point[i]
  339. .ey);
  340. }
  341. this.ctx.stroke();
  342. if (undefined != is_fill) {
  343. this.ctx.fill(); //填充图形 ( 后绘制的图形会覆盖前面的图形, 绘制时注意先后顺序 )
  344. }
  345. this.ctx.draw(true)
  346. }
  347. selectColorEvent(lineColor) {
  348. this.lineColor = lineColor;
  349. }
  350. selectSlideValue(slideValue) {
  351. switch (slideValue) {
  352. case 0:
  353. this.lineSize = 0.1;
  354. this.lineMin = 0.1;
  355. this.lineMax = 0.1;
  356. break;
  357. case 25:
  358. this.lineSize = 1;
  359. this.lineMin = 0.5;
  360. this.lineMax = 2;
  361. break;
  362. case 50:
  363. this.lineSize = 1.5;
  364. this.lineMin = 1;
  365. this.lineMax = 3;
  366. break;
  367. case 75:
  368. this.lineSize = 1.5;
  369. this.lineMin = 2;
  370. this.lineMax = 3.5;
  371. break;
  372. case 100:
  373. this.lineSize = 3;
  374. this.lineMin = 2;
  375. this.lineMax = 3.5;
  376. break;
  377. }
  378. }
  379. saveCanvas() {
  380. console.log('canvasName',this.canvasName)
  381. return new Promise((resolve, rej) => {
  382. uni.canvasToTempFilePath({
  383. canvasId: this.canvasName,
  384. success: function(res) {
  385. resolve(res.tempFilePath);
  386. },
  387. fail: function(err) {
  388. console.log('图片生成失败:' + err)
  389. rej(err);
  390. }
  391. })
  392. })
  393. }
  394. }
  395. export default Handwriting;