Skip to content

Commit 1f68996

Browse files
committed
feat: add redraw and to SVG data URL methods
1 parent 4342dff commit 1f68996

File tree

2 files changed

+41
-22
lines changed

2 files changed

+41
-22
lines changed

__tests__/SignaturePad.test.tsx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,16 @@ describe('Component', () => {
119119
expect(signaturePad.toDataURL()).toContain('data:image/png;base64');
120120
});
121121

122+
it('returns a signature as an SVG data URL', () => {
123+
const instance = React.createRef<SignaturePad>();
124+
125+
render(<SignaturePad ref={instance} redrawOnResize />);
126+
127+
const signaturePad = instance.current as SignaturePad;
128+
129+
expect(signaturePad.toSvgDataUrl({ includeDataUrl: true })).toContain('data:image/svg+xml;base64');
130+
});
131+
122132
it('returns a signature as an SVG string', () => {
123133
const instance = React.createRef<SignaturePad>();
124134

@@ -200,14 +210,14 @@ describe('Component', () => {
200210
render(<SignaturePad ref={instance} />);
201211

202212
const signaturePad = instance.current as SignaturePad;
203-
const spy = jest.spyOn(signaturePad.instance, 'clear');
213+
const spy = jest.spyOn(signaturePad.instance, 'redraw');
204214

205215
scaleCanvas(768, 768);
206216
React.act(() => {
207217
signaturePad.handleResize();
208218
});
209219

210-
expect(spy).toHaveBeenCalled();
220+
expect(spy).not.toHaveBeenCalled();
211221
});
212222

213223
it('redraws a signature when the viewport dimensions change', () => {
@@ -219,7 +229,7 @@ describe('Component', () => {
219229

220230
signaturePad.fromDataURL(signature);
221231

222-
const spy = jest.spyOn(signaturePad.instance, 'toDataURL');
232+
const spy = jest.spyOn(signaturePad.instance, 'redraw');
223233

224234
scaleCanvas(768, 768);
225235
React.act(() => {
@@ -238,7 +248,7 @@ describe('Component', () => {
238248

239249
signaturePad.fromDataURL(signature);
240250

241-
const spy = jest.spyOn(signaturePad.instance, 'toDataURL');
251+
const spy = jest.spyOn(signaturePad.instance, 'redraw');
242252

243253
signaturePad.handleResize();
244254

src/SignaturePad.tsx

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from 'react';
22
import * as PropTypes from 'prop-types';
3-
import SigPad, { type Options, type PointGroup, type ToSVGOptions } from 'signature_pad';
3+
import SigPad, { type FromDataUrlOptions, type Options, type PointGroup, type ToSVGOptions } from 'signature_pad';
44
import { debounce } from 'throttle-debounce';
55

66
type Props = {
@@ -251,7 +251,7 @@ class SignaturePad extends React.PureComponent<Props, State> {
251251
*
252252
* @return {boolean}
253253
*/
254-
isEmpty(): boolean {
254+
public isEmpty(): boolean {
255255
return this.signaturePad.isEmpty();
256256
}
257257

@@ -260,21 +260,27 @@ class SignaturePad extends React.PureComponent<Props, State> {
260260
*
261261
* @return {void}
262262
*/
263-
clear(): void {
263+
public clear(): void {
264264
this.signaturePad.clear();
265265
}
266266

267+
/**
268+
* Redraw the signature.
269+
*
270+
* @return {void}
271+
*/
272+
public redraw(): void {
273+
this.signaturePad.redraw();
274+
}
275+
267276
/**
268277
* Draw a signature from a data URL.
269278
*
270279
* @param {string} dataUrl
271280
* @param {object} options
272281
* @return {void}
273282
*/
274-
fromDataURL(
275-
dataUrl: string,
276-
options: Partial<{ ratio: number; width: number; height: number; xOffset: number; yOffset: number }> = {},
277-
): void {
283+
public fromDataURL(dataUrl: string, options: FromDataUrlOptions = {}): void {
278284
this.signaturePad.fromDataURL(dataUrl, options);
279285
}
280286

@@ -285,10 +291,21 @@ class SignaturePad extends React.PureComponent<Props, State> {
285291
* @param {?number} encoderOptions
286292
* @return {string}
287293
*/
288-
toDataURL(type?: string, encoderOptions?: number): string {
294+
public toDataURL(type?: string, encoderOptions?: number): string {
289295
return this.signaturePad.toDataURL(type, encoderOptions);
290296
}
291297

298+
/**
299+
* Get the signature data as an SVG data URL.
300+
*
301+
* @param {string} mime
302+
* @param {?ToSVGOptions} encoderOptions
303+
* @return {string}
304+
*/
305+
public toSvgDataUrl(encoderOptions?: ToSVGOptions): string {
306+
return this.signaturePad.toDataURL('image/svg+xml', encoderOptions);
307+
}
308+
292309
/**
293310
* Get the signature data as an SVG string without converting to base64.
294311
*
@@ -365,12 +382,6 @@ class SignaturePad extends React.PureComponent<Props, State> {
365382

366383
if (width === canvasWidth && height === canvasHeight) return;
367384

368-
let data;
369-
370-
if (this.props.redrawOnResize && this.signaturePad && !this.signaturePad.isEmpty()) {
371-
data = this.signaturePad.toDataURL();
372-
}
373-
374385
canvas.width = width;
375386
canvas.height = height;
376387

@@ -382,10 +393,8 @@ class SignaturePad extends React.PureComponent<Props, State> {
382393
ctx.scale(ratio, ratio);
383394
}
384395

385-
if (data) {
386-
this.signaturePad.fromDataURL(data);
387-
} else if (this.signaturePad) {
388-
this.signaturePad.clear();
396+
if (this.props.redrawOnResize && this.signaturePad) {
397+
this.redraw();
389398
}
390399
}
391400

0 commit comments

Comments
 (0)