Skip to content

Commit

Permalink
000: Refactor scale canvas method
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeldzjap committed Jan 7, 2022
1 parent 1965480 commit e40a20c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
1 change: 0 additions & 1 deletion __tests__/SignaturePad.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ describe('Component', () => {

it('adds the resize event listener on mount', () => {
const spy = jest.spyOn(window, 'addEventListener');

const signaturePad = mount<SignaturePad>(<SignaturePad />);

expect(spy).toHaveBeenCalledWith('resize', Reflect.get(signaturePad.instance(), 'callResizeHandler'));
Expand Down
15 changes: 8 additions & 7 deletions src/SignaturePad.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class SignaturePad extends React.PureComponent<Props, State> {

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

this.scaleCanvas();
this.scaleCanvas(canvas);
}
}

Expand Down Expand Up @@ -318,19 +318,20 @@ class SignaturePad extends React.PureComponent<Props, State> {
* @return {void}
*/
handleResize(): void {
this.scaleCanvas();
const canvas = this.canvasRef.current;

if (canvas) {
this.scaleCanvas(canvas);
}
}

/**
* Scale the canvas.
*
* @param {HTMLCanvasElement} canvas
* @return {void}
*/
scaleCanvas(): void {
const canvas = this.canvasRef.current;

if (!canvas) return;

scaleCanvas(canvas: HTMLCanvasElement): void {
const ratio = Math.max(window.devicePixelRatio || 1, 1);
const width = (this.props.width || canvas.offsetWidth) * ratio;
const height = (this.props.height || canvas.offsetHeight) * ratio;
Expand Down

0 comments on commit e40a20c

Please sign in to comment.