2

I need to dynamically change the width of signature pad. How can i do it?

<signature-pad class="signature w-50-p" id="sign_canvas" fxFlexAlign.xs="center"></signature-pad>

(angular2-signaturepad)

Markomar
  • 143
  • 1
  • 9

4 Answers4

7

html:

<signature-pad fxFlex="1 1 50" (window:resize)="resizeSignaturePad()"
                         class="signature" 
                         id="sign_canvas" fxFlexAlign.xs="center"></signature-pad>

ts:

resizeSignaturePad() {
    this.signaturePad.set('canvasWidth', document.getElementById("sign_canvas").offsetWidth);
    this.signaturePad.clear();
}
Markomar
  • 143
  • 1
  • 9
1

I implemented this on Angular 11 today. This answer builds on Markomar's answer. Hopefully this saves someone some headache. In short I had to manually set the canvas size using the queryPad() method because of some weird underscore. I used a template variable (#form) on an element I liked the size of and used AfterViewInit instead of OnInit so it initialized properly.

@ViewChild('form') formElement: ElementRef;

resizeSignature() {
    this.signature.queryPad()._canvas.width = this.formElement?.nativeElement.offsetWidth;
}

ngAfterViewInit(): void { this.resizeSignature(); }
  • Nice one, 3.0.4 and it was saying that cannot read width of undefined, and it was canvas that it was trying to access instead of _canvas. – Silencer Nov 11 '21 at 14:45
0

Try this

In your TS:

@ViewChild('signatureDiv') signatureDiv: ElementRef;
@HostListener('window:resize', ['$event'])
onResize(event) {
    this.handleSignatureCanvasSize(); //handle the resizing of signature pad canvas on resizing screen
}

ngAfterViewInit() {
    this.handleSignatureCanvasSize(); //handle the resizing of signature pad canvas on different screens
}

handleSignatureCanvasSize() {
    //handle the resizing of signature pad canvas on resizing screen
    const canvas = document.querySelector("canvas");
    const tempWidth = String(this.signatureDiv.nativeElement.clientWidth);
    const widthCanvas = tempWidth.concat("px");
    canvas.setAttribute("width", widthCanvas);
}

In your HTML add the reference:

                                <div class="signature-container" #signatureDiv align="center">
                                    <signature-pad [options]="signaturePadOptions" (onEndEvent)="drawComplete()">
                                    </signature-pad>
                                </div>
Hammad Manzoor
  • 144
  • 1
  • 11
-5

You can try this:

input signature ng-model="signatureOne"></input>
<input signature ng-model="signatureTwo 
Dino
  • 7,779
  • 12
  • 46
  • 85