signature.js 9.8 KB

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