signature.js 11 KB

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