Skip to content

Commit e40a20c

Browse files
committed
000: Refactor scale canvas method
1 parent 1965480 commit e40a20c

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

__tests__/SignaturePad.test.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,6 @@ describe('Component', () => {
193193

194194
it('adds the resize event listener on mount', () => {
195195
const spy = jest.spyOn(window, 'addEventListener');
196-
197196
const signaturePad = mount<SignaturePad>(<SignaturePad />);
198197

199198
expect(spy).toHaveBeenCalledWith('resize', Reflect.get(signaturePad.instance(), 'callResizeHandler'));

src/SignaturePad.tsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class SignaturePad extends React.PureComponent<Props, State> {
6767

6868
this.signaturePad = new SigPad(canvas, this.props.options);
6969

70-
this.scaleCanvas();
70+
this.scaleCanvas(canvas);
7171
}
7272
}
7373

@@ -318,19 +318,20 @@ class SignaturePad extends React.PureComponent<Props, State> {
318318
* @return {void}
319319
*/
320320
handleResize(): void {
321-
this.scaleCanvas();
321+
const canvas = this.canvasRef.current;
322+
323+
if (canvas) {
324+
this.scaleCanvas(canvas);
325+
}
322326
}
323327

324328
/**
325329
* Scale the canvas.
326330
*
331+
* @param {HTMLCanvasElement} canvas
327332
* @return {void}
328333
*/
329-
scaleCanvas(): void {
330-
const canvas = this.canvasRef.current;
331-
332-
if (!canvas) return;
333-
334+
scaleCanvas(canvas: HTMLCanvasElement): void {
334335
const ratio = Math.max(window.devicePixelRatio || 1, 1);
335336
const width = (this.props.width || canvas.offsetWidth) * ratio;
336337
const height = (this.props.height || canvas.offsetHeight) * ratio;

0 commit comments

Comments
 (0)