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.

signature_pad.ums.js 21KB

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