Skip to content

Instantly share code, notes, and snippets.

@irfanm96
Created May 12, 2022 02:26
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save irfanm96/f449861b6388f26abe5b0eab781e9cd9 to your computer and use it in GitHub Desktop.
Save irfanm96/f449861b6388f26abe5b0eab781e9cd9 to your computer and use it in GitHub Desktop.
Signature Pad
<div {{ $attributes }} wire:ignore x-data="{
signaturePadId: $id('signature'),
signaturePad: null,
signature: @entangle($attributes->get('wire:model')),
ratio: null,
init() {
this.resizeCanvas();
this.signaturePad = new SignaturePad(this.$refs.canvas);
if (this.signature) {
this.signaturePad.fromDataURL(this.signature, { ratio: this.ratio });
}
},
save() {
this.signature = this.signaturePad.toDataURL();
this.$dispatch('signature-saved', this.signaturePadId);
},
clear() {
this.signaturePad.clear();
this.signature = null;
},
resizeCanvas() {
this.ratio = Math.max(window.devicePixelRatio || 1, 1);
this.$refs.canvas.width = this.$refs.canvas.offsetWidth * this.ratio;
this.$refs.canvas.height = this.$refs.canvas.offsetHeight * this.ratio;
this.$refs.canvas.getContext('2d').scale(this.ratio, this.ratio);
}
}" @resize.window="resizeCanvas">
<canvas x-ref="canvas" class="w-full h-full border-2 border-gray-300 border-dashed rounded-md "></canvas>
<div class="flex mt-2 space-x-2">
<a href="#" x-on:click.prevent="clear()" class="text-sm font-medium text-gray-700 underline">
Clear
</a>
<a href="#" x-on:click.prevent="save()" class="text-sm font-medium text-gray-700 underline">
Save
</a>
<span x-data="{
open: false,
saved(e) {
if (e.detail != this.signaturePadId) {
return;
}
this.open = true;
setTimeout(() => { this.open = false }, 900);
}
}" x-show="open" @signature-saved.window="saved" x-transition
class="text-sm font-medium text-green-700 " style="display:none">
Saved !
</span>
</div>
</div>
@pushonce('scripts')
<script src="https://cdn.jsdelivr.net/npm/signature_pad@4.0.0/dist/signature_pad.umd.min.js"></script>
@endpushonce
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment