@@ -193,7 +193,7 @@ export default `
193
193
this.throttle
194
194
))
195
195
: SignaturePad.prototype._strokeUpdate;
196
- this.dotSize = options.dotSize || ( this.minWidth + this.maxWidth) / 2;
196
+ this.dotSize = options.dotSize || function dotSize() {return ( this.minWidth + this.maxWidth) / 2;} ;
197
197
this.penColor = options.penColor || "black";
198
198
this.backgroundColor = options.backgroundColor || "rgba(255,255,255,0)";
199
199
this.onBegin = options.onBegin;
@@ -270,14 +270,8 @@ export default `
270
270
this._isEmpty = false;
271
271
if (!this._startingSignature) this._startingSignature = dataUrl;
272
272
};
273
- SignaturePad.prototype.toDataURL = function (type, encoderOptions) {
274
- if (type === void 0) { type = 'image/png'; }
275
- switch (type) {
276
- case 'image/svg+xml':
277
- return this._toSVG();
278
- default:
279
- return this.canvas.toDataURL(type, encoderOptions);
280
- }
273
+ SignaturePad.prototype.toDataURL = function (type = "image/png", encoderOptions) {
274
+ return type === "image/svg+xml" ? this._toSVG() : this.canvas.toDataURL(type, encoderOptions);
281
275
};
282
276
SignaturePad.prototype.on = function () {
283
277
this.canvas.style.touchAction = 'none';
@@ -315,7 +309,7 @@ export default `
315
309
this._fromData(
316
310
pointGroups,
317
311
({ color, curve }) => _this._drawCurve({ color, curve }),
318
- ({ color, point }) => _this._drawDot({ color, point })
312
+ ({ color, point, dotSize }) => _this._drawDot({ color, point, dotSize })
319
313
);
320
314
this._data = pointGroups;
321
315
}
@@ -326,10 +320,10 @@ export default `
326
320
SignaturePad.prototype._strokeBegin = function (event) {
327
321
var newPointGroup = {
328
322
color: this.penColor,
329
- dotSize: this.dotSize, // Enhance undo
330
- minWidth: this.minWidth, // Enhance undo
331
- maxWidth: this.maxWidth, // Enhance undo
332
- compositeOperation: this._ctx.globalCompositeOperation, // Fix undo problem
323
+ dotSize: typeof this.dotSize === 'function' ? this.dotSize() : this.dotSize,
324
+ minWidth: this.minWidth,
325
+ maxWidth: this.maxWidth,
326
+ compositeOperation: this._ctx.globalCompositeOperation,
333
327
points: [],
334
328
};
335
329
if (typeof this.onBegin === "function") {
@@ -392,7 +386,7 @@ export default `
392
386
SignaturePad.prototype._reset = function () {
393
387
this._lastPoints = [];
394
388
this._lastVelocity = 0;
395
- this._lastWidth = ( this.minWidth + this.maxWidth) / 2 ;
389
+ this._lastWidth = typeof this.dotSize === 'function' ? this.dotSize() : this.dotSize ;
396
390
this._ctx.fillStyle = this.penColor;
397
391
};
398
392
SignaturePad.prototype._createPoint = function (x, y) {
@@ -403,24 +397,24 @@ export default `
403
397
return new Point(x - rect.left, y - rect.top, new Date().getTime());
404
398
}
405
399
};
406
- SignaturePad.prototype._addPoint = function (point) {
400
+ SignaturePad.prototype._addPoint = function (point, minWidth = this.minWidth, maxWidth = this.maxWidth ) {
407
401
var _lastPoints = this._lastPoints;
408
402
_lastPoints.push(point);
409
403
if (_lastPoints.length > 2) {
410
404
if (_lastPoints.length === 3) {
411
405
_lastPoints.unshift(_lastPoints[0]);
412
406
}
413
- var widths = this._calculateCurveWidths(_lastPoints[1], _lastPoints[2]);
407
+ var widths = this._calculateCurveWidths(_lastPoints[1], _lastPoints[2], minWidth, maxWidth );
414
408
var curve = Bezier.fromPoints(_lastPoints, widths);
415
409
_lastPoints.shift();
416
410
return curve;
417
411
}
418
412
return null;
419
413
};
420
- SignaturePad.prototype._calculateCurveWidths = function (startPoint, endPoint) {
414
+ SignaturePad.prototype._calculateCurveWidths = function (startPoint, endPoint, minWidth = this.minWidth, maxWidth = this.maxWidth ) {
421
415
var velocity = this.velocityFilterWeight * endPoint.velocityFrom(startPoint) +
422
416
(1 - this.velocityFilterWeight) * this._lastVelocity;
423
- var newWidth = this._strokeWidth(velocity);
417
+ var newWidth = this._strokeWidth(velocity, minWidth, maxWidth );
424
418
var widths = {
425
419
end: newWidth,
426
420
start: this._lastWidth
@@ -429,8 +423,8 @@ export default `
429
423
this._lastWidth = newWidth;
430
424
return widths;
431
425
};
432
- SignaturePad.prototype._strokeWidth = function (velocity) {
433
- return Math.max(this. maxWidth / (velocity + 1), this. minWidth);
426
+ SignaturePad.prototype._strokeWidth = function (velocity, minWidth = this.minWidth, maxWidth = this.maxWidth ) {
427
+ return Math.max(maxWidth / (velocity + 1), minWidth);
434
428
};
435
429
SignaturePad.prototype._drawCurveSegment = function (x, y, width) {
436
430
var ctx = this._ctx;
@@ -467,15 +461,9 @@ export default `
467
461
ctx.fill();
468
462
};
469
463
SignaturePad.prototype._drawDot = function (_a) {
470
- var color = _a.color,
471
- point = _a.point;
464
+ var color = _a.color, point = _a.point;
472
465
var ctx = this._ctx;
473
- //var width = typeof this.dotSize === "function" ? this.dotSize() : this.dotSize;
474
- var width = typeof _a.dotSize
475
- ? _a.dotSize
476
- : typeof this.dotSize === "function"
477
- ? this.dotSize()
478
- : this.dotSize; // Enhance undo
466
+ var width = _a.dotSize ? _a.dotSize : typeof this.dotSize === "function" ? this.dotSize() : this.dotSize;
479
467
ctx.beginPath();
480
468
this._drawCurveSegment(point.x, point.y, width);
481
469
ctx.closePath();
@@ -485,29 +473,20 @@ export default `
485
473
SignaturePad.prototype._fromData = function (pointGroups, drawCurve, drawDot) {
486
474
for (var i = 0; i < pointGroups.length; i++) {
487
475
var group = pointGroups[i];
488
- var color = group.color,
489
- points = group.points;
490
- var minWidth = group.minWidth,
491
- maxWidth = group.maxWidth; // Enhance undo
492
- var compositeOperation = group.compositeOperation; // Fix undo problem
476
+ var color = group.color, points = group.points;
477
+ var minWidth = group.minWidth, maxWidth = group.maxWidth, dotSize = group.dotSize;
478
+ var compositeOperation = group.compositeOperation;
493
479
this._reset();
480
+ this._lastWidth = dotSize;
494
481
if (points.length > 1) {
495
482
for (var j = 0; j < points.length; j++) {
496
- var basicPoint = points[j];
497
- var point = new Point(
498
- basicPoint.x,
499
- basicPoint.y,
500
- basicPoint.time
501
- );
502
- // this.penColor = color;
503
- this.minWidth = minWidth; // Enhance undo
504
- this.maxWidth = maxWidth; // Enhance undo
505
- this._ctx.globalCompositeOperation = compositeOperation; // Fix undo problem
506
- var curve = this._addPoint(point);
483
+ var point = new Point(points[j].x, points[j].y, points[j].time);
484
+ this._ctx.globalCompositeOperation = compositeOperation;
485
+ var curve = this._addPoint(point, minWidth, maxWidth);
507
486
if (curve) drawCurve({ color, curve });
508
- }
509
- } else drawDot({ color, point: points[0] });
510
- }
487
+ };
488
+ } else drawDot({ color, point: points[0], dotSize });
489
+ };
511
490
this._ctx.globalCompositeOperation = this._isDrawing ? "source-over" : "destination-out";
512
491
};
513
492
SignaturePad.prototype._toSVG = function () {
@@ -523,38 +502,28 @@ export default `
523
502
svg.setAttribute('height', this.canvas.height.toString());
524
503
this._fromData(pointGroups, function (_a) {
525
504
var color = _a.color, curve = _a.curve;
526
- var path = document.createElement('path');
527
- if (!isNaN(curve.control1.x) &&
528
- !isNaN(curve.control1.y) &&
529
- !isNaN(curve.control2.x) &&
530
- !isNaN(curve.control2.y)) {
531
- var attr = "M " + curve.startPoint.x.toFixed(3) + "," + curve.startPoint.y.toFixed(3) + " " +
532
- ("C " + curve.control1.x.toFixed(3) + "," + curve.control1.y.toFixed(3) + " ") +
533
- (curve.control2.x.toFixed(3) + "," + curve.control2.y.toFixed(3) + " ") +
534
- (curve.endPoint.x.toFixed(3) + "," + curve.endPoint.y.toFixed(3));
535
- path.setAttribute('d', attr);
536
- path.setAttribute('stroke-width', (curve.endWidth * 2.25).toFixed(3));
537
- path.setAttribute('stroke', color);
538
- path.setAttribute('fill', 'none');
539
- path.setAttribute('stroke-linecap', 'round');
540
- svg.appendChild(path);
505
+ var path = document.createElement("path");
506
+ if (!isNaN(curve.control1.x) && !isNaN(curve.control1.y) && !isNaN(curve.control2.x) && !isNaN(curve.control2.y)) {
507
+ var attr = "M " + curve.startPoint.x.toFixed(3) + "," + curve.startPoint.y.toFixed(3) + " " + ("C " + curve.control1.x.toFixed(3) + "," + curve.control1.y.toFixed(3) + " ") + (curve.control2.x.toFixed(3) + "," + curve.control2.y.toFixed(3) + " ") + (curve.endPoint.x.toFixed(3) + "," + curve.endPoint.y.toFixed(3));
508
+ path.setAttribute("d", attr);
509
+ path.setAttribute("stroke-width", (curve.endWidth * 2.25).toFixed(3));
510
+ path.setAttribute("stroke", color);
511
+ path.setAttribute("fill", "none");
512
+ path.setAttribute("stroke-linecap", "round");
513
+ svg.appendChild(path);
541
514
}
542
- }, function (_a) {
543
- var color = _a.color,
544
- point = _a.point;
545
- var circle = document.createElement("circle");
546
- // var dotSize = typeof _this.dotSize === "function" ? _this.dotSize() : _this.dotSize;
547
- var dotSize = typeof _a.dotSize
548
- ? _a.dotSize
549
- : typeof _this.dotSize === "function"
550
- ? _this.dotSize()
551
- : _this.dotSize; // Enhance undo
552
- circle.setAttribute("r", dotSize.toString());
553
- circle.setAttribute("cx", point.x.toString());
554
- circle.setAttribute("cy", point.y.toString());
555
- circle.setAttribute("fill", color);
556
- svg.appendChild(circle);
557
- });
515
+ },
516
+ function (_a) {
517
+ var color = _a.color,point = _a.point;
518
+ var circle = document.createElement("circle");
519
+ var dotSize = _a.dotSize ? _a.dotSize : typeof _this.dotSize === "function" ? _this.dotSize() : _this.dotSize;
520
+ circle.setAttribute("r", dotSize.toString());
521
+ circle.setAttribute("cx", point.x.toString());
522
+ circle.setAttribute("cy", point.y.toString());
523
+ circle.setAttribute("fill", color);
524
+ svg.appendChild(circle);
525
+ }
526
+ );
558
527
var prefix = 'data:image/svg+xml;base64,';
559
528
var header = '<svg' +
560
529
' xmlns="http://www.w3.org/2000/svg"' +
0 commit comments