signature.js 9.8 KB

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