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.

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