Last active
August 22, 2024 19:31
-
-
Save drnic/1136174589dedcdcc36eea41f002db8c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Controller } from "@hotwired/stimulus" | |
import SignaturePad from 'signature_pad' | |
export default class extends Controller { | |
static targets = ["canvas", "input"] | |
connect() { | |
this.signaturePad = new SignaturePad(this.canvasTarget) | |
this.signaturePad.addEventListener("endStroke", this.endStroke) | |
this.resizeCanvas() | |
if (this.inputTarget.value) { | |
this.signaturePad.fromDataURL(this.inputTarget.value) | |
} | |
} | |
disconnect() { | |
this.signaturePad.off() | |
} | |
clear() { | |
this.signaturePad.clear() | |
this.inputTarget.value = "" | |
} | |
endStroke = (_) => { | |
this.inputTarget.value = this.signaturePad.toDataURL("image/svg+xml") | |
} | |
resizeCanvas() { | |
// When zoomed out to less than 100%, for some very strange reason, | |
// some browsers report devicePixelRatio as less than 1 | |
// and only part of the canvas is cleared then. | |
var ratio = Math.max(window.devicePixelRatio || 1, 1) | |
this.canvasTarget.width = this.canvasTarget.offsetWidth * ratio | |
this.canvasTarget.height = this.canvasTarget.offsetHeight * ratio | |
this.canvasTarget.getContext("2d").scale(ratio, ratio) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment