react-native-signature-canvas 4.2.1 → 4.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -58,7 +58,7 @@ import SignatureScreen from 'react-native-signature-canvas';
58
58
  | bgSrc | `string` | background image source uri (_url_) |
59
59
  | clearText | `string` | clear button text |
60
60
  | confirmText | `string` | save button text |
61
- | customHtml | `function` | html string that lets you modify things like the layout or elements |
61
+ | customHtml | `(injectedJavaScript: string) => string` | html string that lets you modify things like the layout or elements |
62
62
  | dataURL | `string` | default is "", Base64 string, draws saved signature from dataURL. |
63
63
  | descriptionText | `string` | description text for signature |
64
64
  | dotSize | `number` | radius of a single dot _(not stroke width)_ |
@@ -76,6 +76,7 @@ import SignatureScreen from 'react-native-signature-canvas';
76
76
  | onDraw | `function` | callback function when drawing is enabled |
77
77
  | onErase | `function` | callback function when erasing is enabled |
78
78
  | onChangePenColor | `function` | callback function after changing the pen color |
79
+ | onChangePenSize | `function` | callback function after changing the pen size
79
80
  |overlayHeight|`number`|height of the overlay image|
80
81
  |overlayWidth|`number`|width of the overlay image|
81
82
  |overlaySrc|`string`|overlay image source uri (url) _must be .png with a transparent background_
@@ -89,16 +90,17 @@ import SignatureScreen from 'react-native-signature-canvas';
89
90
 
90
91
  ---
91
92
 
92
- | Function | Description |
93
- | :-------------------- | :------------------------------------------------------------------------------------------ |
94
- | clearSignature() | Clear the current signature |
95
- | changePenColor(color) | Change pen color |
96
- | draw() | Enable drawing signature |
97
- | erase() | Enable erasing signature |
98
- |getData()| Triggers the `onGetData` callback with a single `data` JSON string |
99
- | readSignature() | Reads the current signature on the canvas and triggers either the `onOK` or `onEmpty` callbacks |
100
- | undo() | Undo last stroke |
101
- | redo() | Redo last stroke |
93
+ | Function | Description |
94
+ | :-------------------- | :-----------------------------------------------------------------------------------------------|
95
+ | clearSignature() | Clear the current signature |
96
+ | changePenColor(color) | Change pen color |
97
+ | changePenSize(minW, maxW) | Change pen size |
98
+ | draw() | Enable drawing signature |
99
+ | erase() | Enable erasing signature |
100
+ | getData() | Triggers the `onGetData` callback with a single `data` JSON string |
101
+ | readSignature() | Reads the current signature on the canvas and triggers either the `onOK` or `onEmpty` callbacks |
102
+ | undo() | Undo last stroke |
103
+ | redo() | Redo last stroke |
102
104
 
103
105
  To call the methods use the `useRef` hook:
104
106
 
package/h5/js/app.js CHANGED
@@ -50,10 +50,16 @@ export default `
50
50
  window.ReactNativeWebView.postMessage("REDO");
51
51
  }
52
52
 
53
- function changePenColor(color){
53
+ function changePenColor(color) {
54
54
  signaturePad.penColor = color;
55
55
  window.ReactNativeWebView.postMessage("CHANGE_PEN");
56
56
  }
57
+
58
+ function changePenSize(minW, maxW) {
59
+ signaturePad.minWidth = minW;
60
+ signaturePad.maxWidth = maxW;
61
+ window.ReactNativeWebView.postMessage("CHANGE_PEN_SIZE");
62
+ }
57
63
 
58
64
  function getData () {
59
65
  var data = signaturePad.toData();
@@ -61,15 +67,11 @@ export default `
61
67
  }
62
68
 
63
69
  function draw() {
64
- //signaturePad.minWidth= 0.5;
65
- //signaturePad.maxWidth= 2.5;
66
70
  signaturePad.draw();
67
71
  window.ReactNativeWebView.postMessage("DRAW");
68
72
  }
69
73
 
70
74
  function erase() {
71
- //signaturePad.minWidth= 5;
72
- //signaturePad.maxWidth= 10;
73
75
  signaturePad.erase();
74
76
  window.ReactNativeWebView.postMessage("ERASE");
75
77
  }
@@ -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"' +
package/index.d.ts CHANGED
@@ -4,7 +4,7 @@ declare module "react-native-signature-canvas" {
4
4
 
5
5
  type ImageType = "image/png" | "image/jpeg" | "image/svg+xml";
6
6
 
7
- type DataURL = "Base64";
7
+ type DataURL = "Base64" | string;
8
8
 
9
9
  type ForwardRef<T, P> = React.ForwardRefExoticComponent<React.PropsWithoutRef<P> & React.RefAttributes<T>>;
10
10
 
@@ -17,7 +17,7 @@ declare module "react-native-signature-canvas" {
17
17
  bgSrc?: string;
18
18
  clearText?: string;
19
19
  confirmText?: string;
20
- customHtml?: string | null | undefined;
20
+ customHtml?: (injectedJavaScript: string) => string;
21
21
  dataURL?: DataURL;
22
22
  descriptionText?: string;
23
23
  dotSize?: number;
@@ -31,8 +31,9 @@ declare module "react-native-signature-canvas" {
31
31
  onRedo?: () => void;
32
32
  onDraw?: () => void;
33
33
  onErase?: () => void;
34
- onGetData?: () => void;
35
- onChangePenColo?: () => void;
34
+ onGetData?: (data: any) => void;
35
+ onChangePenColor?: () => void;
36
+ onChangePenSize?: () => void;
36
37
  onBegin?: () => void;
37
38
  onEnd?: () => void;
38
39
  overlayHeight?: number;
@@ -49,6 +50,7 @@ declare module "react-native-signature-canvas" {
49
50
 
50
51
  export type SignatureViewRef = {
51
52
  changePenColor: (color: string) => void;
53
+ changePenSize: (minW: number, maxW: number) => void;
52
54
  clearSignature: () => void;
53
55
  cropWhitespace: (url: string) => void;
54
56
  draw: () => void;
package/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import React, { useState, useMemo, useRef, forwardRef, useImperativeHandle } from "react";
1
+ import React, { useState, useEffect, useMemo, useRef, forwardRef, useImperativeHandle } from "react";
2
2
  import { View, StyleSheet, ActivityIndicator } from "react-native";
3
3
 
4
4
  import htmlContent from "./h5/html";
@@ -28,7 +28,7 @@ const SignatureView = forwardRef(({
28
28
  customHtml = null,
29
29
  dataURL = "",
30
30
  descriptionText = "Sign above",
31
- dotSize = 0,
31
+ dotSize = null,
32
32
  imageType = "",
33
33
  minWidth = 0.5,
34
34
  maxWidth = 2.5,
@@ -41,6 +41,7 @@ const SignatureView = forwardRef(({
41
41
  onErase=()=>{},
42
42
  onGetData=()=>{},
43
43
  onChangePenColor=()=>{},
44
+ onChangePenSize=()=>{},
44
45
  onBegin = () => {},
45
46
  onEnd = () => {},
46
47
  overlayHeight = 0,
@@ -68,7 +69,7 @@ const SignatureView = forwardRef(({
68
69
  injectedJavaScript = injectedJavaScript.replace(/<%dotSize%>/g, dotSize);
69
70
  injectedJavaScript = injectedJavaScript.replace(/<%minWidth%>/g, minWidth);
70
71
  injectedJavaScript = injectedJavaScript.replace(/<%maxWidth%>/g, maxWidth);
71
-
72
+
72
73
  let html = htmlContentValue(injectedJavaScript);
73
74
  html = html.replace(/<%bgWidth%>/g, bgWidth);
74
75
  html = html.replace(/<%bgHeight%>/g, bgHeight);
@@ -83,8 +84,13 @@ const SignatureView = forwardRef(({
83
84
  html = html.replace(/<%orientation%>/g, rotated);
84
85
 
85
86
  return { html };
86
- }, [customHtml, autoClear, trimWhitespace, rotated, imageType, webStyle, descriptionText, confirmText, clearText, dataURL])
87
-
87
+ }, [customHtml, autoClear, trimWhitespace, rotated, imageType, webStyle, descriptionText, confirmText, clearText, dataURL, bgSrc, bgWidth, bgHeight])
88
+
89
+ useEffect(()=> {
90
+ if(webViewRef.current) {
91
+ webViewRef.current.reload();
92
+ } }, [source]);
93
+
88
94
  const isJson = (str)=> {
89
95
  try {
90
96
  JSON.parse(str);
@@ -123,6 +129,9 @@ const SignatureView = forwardRef(({
123
129
  case "CHANGE_PEN":
124
130
  onChangePenColor();
125
131
  break;
132
+ case "CHANGE_PEN_SIZE":
133
+ onChangePenSize();
134
+ break;
126
135
  default:
127
136
  isJson(e.nativeEvent.data)? onGetData(e.nativeEvent.data): onOK(e.nativeEvent.data);
128
137
  }
@@ -164,6 +173,11 @@ const SignatureView = forwardRef(({
164
173
  webViewRef.current.injectJavaScript("changePenColor('"+color+"');true;");
165
174
  }
166
175
  },
176
+ changePenSize: (minW, maxW) => {
177
+ if (webViewRef.current) {
178
+ webViewRef.current.injectJavaScript("changePenSize("+minW+','+maxW+");true;");
179
+ }
180
+ },
167
181
  getData: () => {
168
182
  if (webViewRef.current) {
169
183
  webViewRef.current.injectJavaScript("getData();true;");
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "react-native-signature-canvas",
3
- "version": "4.2.1",
3
+ "version": "4.4.0",
4
4
  "description": "React Native Signature Component based Canvas for Android && IOS && expo",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
- "build": "npm publish",
7
+ "publish": "npm publish",
8
8
  "genlog": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0",
9
9
  "postversion": "git push --follow-tags"
10
10
  },
@@ -28,10 +28,7 @@
28
28
  "url": "https://github.com/YanYuanFE/react-native-signature-canvas/issues"
29
29
  },
30
30
  "homepage": "https://github.com/YanYuanFE/react-native-signature-canvas#readme",
31
- "devDependencies": {
32
- "react-native-webview": "^11.0.3"
33
- },
34
31
  "peerDependencies": {
35
- "react-native-webview": "^11.0.3"
32
+ "react-native-webview": ">=11.22.1"
36
33
  }
37
34
  }