You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

529 lines
21 KiB

4 years ago
  1. /*!
  2. * Signature Pad v3.0.0-beta.3 | https://github.com/szimek/signature_pad
  3. * (c) 2018 Szymon Nowak | Released under the MIT license
  4. */
  5. (function (global, factory) {
  6. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
  7. typeof define === 'function' && define.amd ? define(factory) :
  8. (global.SignaturePad = factory());
  9. }(this, (function () { 'use strict';
  10. var Point = (function () {
  11. function Point(x, y, time) {
  12. this.x = x;
  13. this.y = y;
  14. this.time = time || Date.now();
  15. }
  16. Point.prototype.distanceTo = function (start) {
  17. return Math.sqrt(Math.pow(this.x - start.x, 2) + Math.pow(this.y - start.y, 2));
  18. };
  19. Point.prototype.equals = function (other) {
  20. return this.x === other.x && this.y === other.y && this.time === other.time;
  21. };
  22. Point.prototype.velocityFrom = function (start) {
  23. return this.time !== start.time
  24. ? this.distanceTo(start) / (this.time - start.time)
  25. : 0;
  26. };
  27. return Point;
  28. }());
  29. var Bezier = (function () {
  30. function Bezier(startPoint, control2, control1, endPoint, startWidth, endWidth) {
  31. this.startPoint = startPoint;
  32. this.control2 = control2;
  33. this.control1 = control1;
  34. this.endPoint = endPoint;
  35. this.startWidth = startWidth;
  36. this.endWidth = endWidth;
  37. }
  38. Bezier.fromPoints = function (points, widths) {
  39. var c2 = this.calculateControlPoints(points[0], points[1], points[2]).c2;
  40. var c3 = this.calculateControlPoints(points[1], points[2], points[3]).c1;
  41. return new Bezier(points[1], c2, c3, points[2], widths.start, widths.end);
  42. };
  43. Bezier.calculateControlPoints = function (s1, s2, s3) {
  44. var dx1 = s1.x - s2.x;
  45. var dy1 = s1.y - s2.y;
  46. var dx2 = s2.x - s3.x;
  47. var dy2 = s2.y - s3.y;
  48. var m1 = { x: (s1.x + s2.x) / 2.0, y: (s1.y + s2.y) / 2.0 };
  49. var m2 = { x: (s2.x + s3.x) / 2.0, y: (s2.y + s3.y) / 2.0 };
  50. var l1 = Math.sqrt(dx1 * dx1 + dy1 * dy1);
  51. var l2 = Math.sqrt(dx2 * dx2 + dy2 * dy2);
  52. var dxm = m1.x - m2.x;
  53. var dym = m1.y - m2.y;
  54. var k = l2 / (l1 + l2);
  55. var cm = { x: m2.x + dxm * k, y: m2.y + dym * k };
  56. var tx = s2.x - cm.x;
  57. var ty = s2.y - cm.y;
  58. return {
  59. c1: new Point(m1.x + tx, m1.y + ty),
  60. c2: new Point(m2.x + tx, m2.y + ty)
  61. };
  62. };
  63. Bezier.prototype.length = function () {
  64. var steps = 10;
  65. var length = 0;
  66. var px;
  67. var py;
  68. for (var i = 0; i <= steps; i += 1) {
  69. var t = i / steps;
  70. var cx = this.point(t, this.startPoint.x, this.control1.x, this.control2.x, this.endPoint.x);
  71. var cy = this.point(t, this.startPoint.y, this.control1.y, this.control2.y, this.endPoint.y);
  72. if (i > 0) {
  73. var xdiff = cx - px;
  74. var ydiff = cy - py;
  75. length += Math.sqrt(xdiff * xdiff + ydiff * ydiff);
  76. }
  77. px = cx;
  78. py = cy;
  79. }
  80. return length;
  81. };
  82. Bezier.prototype.point = function (t, start, c1, c2, end) {
  83. return (start * (1.0 - t) * (1.0 - t) * (1.0 - t))
  84. + (3.0 * c1 * (1.0 - t) * (1.0 - t) * t)
  85. + (3.0 * c2 * (1.0 - t) * t * t)
  86. + (end * t * t * t);
  87. };
  88. return Bezier;
  89. }());
  90. function throttle(fn, wait) {
  91. if (wait === void 0) { wait = 250; }
  92. var previous = 0;
  93. var timeout = null;
  94. var result;
  95. var storedContext;
  96. var storedArgs;
  97. var later = function () {
  98. previous = Date.now();
  99. timeout = null;
  100. result = fn.apply(storedContext, storedArgs);
  101. if (!timeout) {
  102. storedContext = null;
  103. storedArgs = [];
  104. }
  105. };
  106. return function wrapper() {
  107. var args = [];
  108. for (var _i = 0; _i < arguments.length; _i++) {
  109. args[_i] = arguments[_i];
  110. }
  111. var now = Date.now();
  112. var remaining = wait - (now - previous);
  113. storedContext = this;
  114. storedArgs = args;
  115. if (remaining <= 0 || remaining > wait) {
  116. if (timeout) {
  117. clearTimeout(timeout);
  118. timeout = null;
  119. }
  120. previous = now;
  121. result = fn.apply(storedContext, storedArgs);
  122. if (!timeout) {
  123. storedContext = null;
  124. storedArgs = [];
  125. }
  126. }
  127. else if (!timeout) {
  128. timeout = window.setTimeout(later, remaining);
  129. }
  130. return result;
  131. };
  132. }
  133. var SignaturePad = (function () {
  134. function SignaturePad(canvas, options) {
  135. if (options === void 0) { options = {}; }
  136. var _this = this;
  137. this.canvas = canvas;
  138. this.options = options;
  139. this._handleMouseDown = function (event) {
  140. if (event.which === 1) {
  141. _this._mouseButtonDown = true;
  142. _this._strokeBegin(event);
  143. }
  144. };
  145. this._handleMouseMove = function (event) {
  146. if (_this._mouseButtonDown) {
  147. _this._strokeMoveUpdate(event);
  148. }
  149. };
  150. this._handleMouseUp = function (event) {
  151. if (event.which === 1 && _this._mouseButtonDown) {
  152. _this._mouseButtonDown = false;
  153. _this._strokeEnd(event);
  154. }
  155. };
  156. this._handleTouchStart = function (event) {
  157. event.preventDefault();
  158. if (event.targetTouches.length === 1) {
  159. var touch = event.changedTouches[0];
  160. _this._strokeBegin(touch);
  161. }
  162. };
  163. this._handleTouchMove = function (event) {
  164. event.preventDefault();
  165. var touch = event.targetTouches[0];
  166. _this._strokeMoveUpdate(touch);
  167. };
  168. this._handleTouchEnd = function (event) {
  169. var wasCanvasTouched = event.target === _this.canvas;
  170. if (wasCanvasTouched) {
  171. event.preventDefault();
  172. var touch = event.changedTouches[0];
  173. _this._strokeEnd(touch);
  174. }
  175. };
  176. this.velocityFilterWeight = options.velocityFilterWeight || 0.7;
  177. this.minWidth = options.minWidth || 0.5;
  178. this.maxWidth = options.maxWidth || 2.5;
  179. this.throttle = ('throttle' in options ? options.throttle : 16);
  180. this.minDistance = ('minDistance' in options
  181. ? options.minDistance
  182. : 5);
  183. if (this.throttle) {
  184. this._strokeMoveUpdate = throttle(SignaturePad.prototype._strokeUpdate, this.throttle);
  185. }
  186. else {
  187. this._strokeMoveUpdate = SignaturePad.prototype._strokeUpdate;
  188. }
  189. this.dotSize =
  190. options.dotSize ||
  191. function dotSize() {
  192. return (this.minWidth + this.maxWidth) / 2;
  193. };
  194. this.penColor = options.penColor || 'black';
  195. this.backgroundColor = options.backgroundColor || 'rgba(0,0,0,0)';
  196. this.onBegin = options.onBegin;
  197. this.onEnd = options.onEnd;
  198. this._ctx = canvas.getContext('2d');
  199. this.clear();
  200. this.on();
  201. }
  202. SignaturePad.prototype.clear = function () {
  203. var ctx = this._ctx;
  204. var canvas = this.canvas;
  205. ctx.fillStyle = this.backgroundColor;
  206. ctx.clearRect(0, 0, canvas.width, canvas.height);
  207. ctx.fillRect(0, 0, canvas.width, canvas.height);
  208. this._data = [];
  209. this._reset();
  210. this._isEmpty = true;
  211. };
  212. SignaturePad.prototype.fromDataURL = function (dataUrl, options, callback) {
  213. var _this = this;
  214. if (options === void 0) { options = {}; }
  215. var image = new Image();
  216. var ratio = options.ratio || window.devicePixelRatio || 1;
  217. var width = options.width || this.canvas.width / ratio;
  218. var height = options.height || this.canvas.height / ratio;
  219. this._reset();
  220. image.onload = function () {
  221. _this._ctx.drawImage(image, 0, 0, width, height);
  222. if (callback) {
  223. callback();
  224. }
  225. };
  226. image.onerror = function (error) {
  227. if (callback) {
  228. callback(error);
  229. }
  230. };
  231. image.src = dataUrl;
  232. this._isEmpty = false;
  233. };
  234. SignaturePad.prototype.toDataURL = function (type, encoderOptions) {
  235. if (type === void 0) { type = 'image/png'; }
  236. switch (type) {
  237. case 'image/svg+xml':
  238. return this._toSVG();
  239. default:
  240. return this.canvas.toDataURL(type, encoderOptions);
  241. }
  242. };
  243. SignaturePad.prototype.on = function () {
  244. this.canvas.style.touchAction = 'none';
  245. this.canvas.style.msTouchAction = 'none';
  246. if (window.PointerEvent) {
  247. this._handlePointerEvents();
  248. }
  249. else {
  250. this._handleMouseEvents();
  251. if ('ontouchstart' in window) {
  252. this._handleTouchEvents();
  253. }
  254. }
  255. };
  256. SignaturePad.prototype.off = function () {
  257. this.canvas.style.touchAction = 'auto';
  258. this.canvas.style.msTouchAction = 'auto';
  259. this.canvas.removeEventListener('pointerdown', this._handleMouseDown);
  260. this.canvas.removeEventListener('pointermove', this._handleMouseMove);
  261. document.removeEventListener('pointerup', this._handleMouseUp);
  262. this.canvas.removeEventListener('mousedown', this._handleMouseDown);
  263. this.canvas.removeEventListener('mousemove', this._handleMouseMove);
  264. document.removeEventListener('mouseup', this._handleMouseUp);
  265. this.canvas.removeEventListener('touchstart', this._handleTouchStart);
  266. this.canvas.removeEventListener('touchmove', this._handleTouchMove);
  267. this.canvas.removeEventListener('touchend', this._handleTouchEnd);
  268. };
  269. SignaturePad.prototype.isEmpty = function () {
  270. return this._isEmpty;
  271. };
  272. SignaturePad.prototype.fromData = function (pointGroups) {
  273. var _this = this;
  274. this.clear();
  275. this._fromData(pointGroups, function (_a) {
  276. var color = _a.color, curve = _a.curve;
  277. return _this._drawCurve({ color: color, curve: curve });
  278. }, function (_a) {
  279. var color = _a.color, point = _a.point;
  280. return _this._drawDot({ color: color, point: point });
  281. });
  282. this._data = pointGroups;
  283. };
  284. SignaturePad.prototype.toData = function () {
  285. return this._data;
  286. };
  287. SignaturePad.prototype._strokeBegin = function (event) {
  288. var newPointGroup = {
  289. color: this.penColor,
  290. points: []
  291. };
  292. if (typeof this.onBegin === 'function') {
  293. this.onBegin(event);
  294. }
  295. this._data.push(newPointGroup);
  296. this._reset();
  297. this._strokeUpdate(event);
  298. };
  299. SignaturePad.prototype._strokeUpdate = function (event) {
  300. var x = event.clientX;
  301. var y = event.clientY;
  302. var point = this._createPoint(x, y);
  303. var lastPointGroup = this._data[this._data.length - 1];
  304. var lastPoints = lastPointGroup.points;
  305. var lastPoint = lastPoints.length > 0 && lastPoints[lastPoints.length - 1];
  306. var isLastPointTooClose = lastPoint
  307. ? point.distanceTo(lastPoint) <= this.minDistance
  308. : false;
  309. var color = lastPointGroup.color;
  310. if (!lastPoint || !(lastPoint && isLastPointTooClose)) {
  311. var curve = this._addPoint(point);
  312. if (!lastPoint) {
  313. this._drawDot({ color: color, point: point });
  314. }
  315. else if (curve) {
  316. this._drawCurve({ color: color, curve: curve });
  317. }
  318. lastPoints.push({
  319. time: point.time,
  320. x: point.x,
  321. y: point.y
  322. });
  323. }
  324. };
  325. SignaturePad.prototype._strokeEnd = function (event) {
  326. this._strokeUpdate(event);
  327. if (typeof this.onEnd === 'function') {
  328. this.onEnd(event);
  329. }
  330. };
  331. SignaturePad.prototype._handlePointerEvents = function () {
  332. this._mouseButtonDown = false;
  333. this.canvas.addEventListener('pointerdown', this._handleMouseDown);
  334. this.canvas.addEventListener('pointermove', this._handleMouseMove);
  335. document.addEventListener('pointerup', this._handleMouseUp);
  336. };
  337. SignaturePad.prototype._handleMouseEvents = function () {
  338. this._mouseButtonDown = false;
  339. this.canvas.addEventListener('mousedown', this._handleMouseDown);
  340. this.canvas.addEventListener('mousemove', this._handleMouseMove);
  341. document.addEventListener('mouseup', this._handleMouseUp);
  342. };
  343. SignaturePad.prototype._handleTouchEvents = function () {
  344. this.canvas.addEventListener('touchstart', this._handleTouchStart);
  345. this.canvas.addEventListener('touchmove', this._handleTouchMove);
  346. this.canvas.addEventListener('touchend', this._handleTouchEnd);
  347. };
  348. SignaturePad.prototype._reset = function () {
  349. this._lastPoints = [];
  350. this._lastVelocity = 0;
  351. this._lastWidth = (this.minWidth + this.maxWidth) / 2;
  352. this._ctx.fillStyle = this.penColor;
  353. };
  354. SignaturePad.prototype._createPoint = function (x, y) {
  355. var rect = this.canvas.getBoundingClientRect();
  356. return new Point(x - rect.left, y - rect.top, new Date().getTime());
  357. };
  358. SignaturePad.prototype._addPoint = function (point) {
  359. var _lastPoints = this._lastPoints;
  360. _lastPoints.push(point);
  361. if (_lastPoints.length > 2) {
  362. if (_lastPoints.length === 3) {
  363. _lastPoints.unshift(_lastPoints[0]);
  364. }
  365. var widths = this._calculateCurveWidths(_lastPoints[1], _lastPoints[2]);
  366. var curve = Bezier.fromPoints(_lastPoints, widths);
  367. _lastPoints.shift();
  368. return curve;
  369. }
  370. return null;
  371. };
  372. SignaturePad.prototype._calculateCurveWidths = function (startPoint, endPoint) {
  373. var velocity = this.velocityFilterWeight * endPoint.velocityFrom(startPoint) +
  374. (1 - this.velocityFilterWeight) * this._lastVelocity;
  375. var newWidth = this._strokeWidth(velocity);
  376. var widths = {
  377. end: newWidth,
  378. start: this._lastWidth
  379. };
  380. this._lastVelocity = velocity;
  381. this._lastWidth = newWidth;
  382. return widths;
  383. };
  384. SignaturePad.prototype._strokeWidth = function (velocity) {
  385. return Math.max(this.maxWidth / (velocity + 1), this.minWidth);
  386. };
  387. SignaturePad.prototype._drawCurveSegment = function (x, y, width) {
  388. var ctx = this._ctx;
  389. ctx.moveTo(x, y);
  390. ctx.arc(x, y, width, 0, 2 * Math.PI, false);
  391. this._isEmpty = false;
  392. };
  393. SignaturePad.prototype._drawCurve = function (_a) {
  394. var color = _a.color, curve = _a.curve;
  395. var ctx = this._ctx;
  396. var widthDelta = curve.endWidth - curve.startWidth;
  397. var drawSteps = Math.floor(curve.length()) * 2;
  398. ctx.beginPath();
  399. ctx.fillStyle = color;
  400. for (var i = 0; i < drawSteps; i += 1) {
  401. var t = i / drawSteps;
  402. var tt = t * t;
  403. var ttt = tt * t;
  404. var u = 1 - t;
  405. var uu = u * u;
  406. var uuu = uu * u;
  407. var x = uuu * curve.startPoint.x;
  408. x += 3 * uu * t * curve.control1.x;
  409. x += 3 * u * tt * curve.control2.x;
  410. x += ttt * curve.endPoint.x;
  411. var y = uuu * curve.startPoint.y;
  412. y += 3 * uu * t * curve.control1.y;
  413. y += 3 * u * tt * curve.control2.y;
  414. y += ttt * curve.endPoint.y;
  415. var width = curve.startWidth + ttt * widthDelta;
  416. this._drawCurveSegment(x, y, width);
  417. }
  418. ctx.closePath();
  419. ctx.fill();
  420. };
  421. SignaturePad.prototype._drawDot = function (_a) {
  422. var color = _a.color, point = _a.point;
  423. var ctx = this._ctx;
  424. var width = typeof this.dotSize === 'function' ? this.dotSize() : this.dotSize;
  425. ctx.beginPath();
  426. this._drawCurveSegment(point.x, point.y, width);
  427. ctx.closePath();
  428. ctx.fillStyle = color;
  429. ctx.fill();
  430. };
  431. SignaturePad.prototype._fromData = function (pointGroups, drawCurve, drawDot) {
  432. for (var _i = 0, pointGroups_1 = pointGroups; _i < pointGroups_1.length; _i++) {
  433. var group = pointGroups_1[_i];
  434. var color = group.color, points = group.points;
  435. if (points.length > 1) {
  436. for (var j = 0; j < points.length; j += 1) {
  437. var basicPoint = points[j];
  438. var point = new Point(basicPoint.x, basicPoint.y, basicPoint.time);
  439. this.penColor = color;
  440. if (j === 0) {
  441. this._reset();
  442. }
  443. var curve = this._addPoint(point);
  444. if (curve) {
  445. drawCurve({ color: color, curve: curve });
  446. }
  447. }
  448. }
  449. else {
  450. this._reset();
  451. drawDot({
  452. color: color,
  453. point: points[0]
  454. });
  455. }
  456. }
  457. };
  458. SignaturePad.prototype._toSVG = function () {
  459. var _this = this;
  460. var pointGroups = this._data;
  461. var ratio = Math.max(window.devicePixelRatio || 1, 1);
  462. var minX = 0;
  463. var minY = 0;
  464. var maxX = this.canvas.width / ratio;
  465. var maxY = this.canvas.height / ratio;
  466. var svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
  467. svg.setAttribute('width', this.canvas.width.toString());
  468. svg.setAttribute('height', this.canvas.height.toString());
  469. this._fromData(pointGroups, function (_a) {
  470. var color = _a.color, curve = _a.curve;
  471. var path = document.createElement('path');
  472. if (!isNaN(curve.control1.x) &&
  473. !isNaN(curve.control1.y) &&
  474. !isNaN(curve.control2.x) &&
  475. !isNaN(curve.control2.y)) {
  476. var attr = "M " + curve.startPoint.x.toFixed(3) + "," + curve.startPoint.y.toFixed(3) + " " +
  477. ("C " + curve.control1.x.toFixed(3) + "," + curve.control1.y.toFixed(3) + " ") +
  478. (curve.control2.x.toFixed(3) + "," + curve.control2.y.toFixed(3) + " ") +
  479. (curve.endPoint.x.toFixed(3) + "," + curve.endPoint.y.toFixed(3));
  480. path.setAttribute('d', attr);
  481. path.setAttribute('stroke-width', (curve.endWidth * 2.25).toFixed(3));
  482. path.setAttribute('stroke', color);
  483. path.setAttribute('fill', 'none');
  484. path.setAttribute('stroke-linecap', 'round');
  485. svg.appendChild(path);
  486. }
  487. }, function (_a) {
  488. var color = _a.color, point = _a.point;
  489. var circle = document.createElement('circle');
  490. var dotSize = typeof _this.dotSize === 'function' ? _this.dotSize() : _this.dotSize;
  491. circle.setAttribute('r', dotSize.toString());
  492. circle.setAttribute('cx', point.x.toString());
  493. circle.setAttribute('cy', point.y.toString());
  494. circle.setAttribute('fill', color);
  495. svg.appendChild(circle);
  496. });
  497. var prefix = 'data:image/svg+xml;base64,';
  498. var header = '<svg' +
  499. ' xmlns="http://www.w3.org/2000/svg"' +
  500. ' xmlns:xlink="http://www.w3.org/1999/xlink"' +
  501. (" viewBox=\"" + minX + " " + minY + " " + maxX + " " + maxY + "\"") +
  502. (" width=\"" + maxX + "\"") +
  503. (" height=\"" + maxY + "\"") +
  504. '>';
  505. var body = svg.innerHTML;
  506. if (body === undefined) {
  507. var dummy = document.createElement('dummy');
  508. var nodes = svg.childNodes;
  509. dummy.innerHTML = '';
  510. for (var i = 0; i < nodes.length; i += 1) {
  511. dummy.appendChild(nodes[i].cloneNode(true));
  512. }
  513. body = dummy.innerHTML;
  514. }
  515. var footer = '</svg>';
  516. var data = header + body + footer;
  517. return prefix + btoa(data);
  518. };
  519. return SignaturePad;
  520. }());
  521. return SignaturePad;
  522. })));