1 | const wrapper = document.getElementById("signature-pad");
|
2 | const canvasWrapper = document.getElementById("canvas-wrapper");
|
3 | const clearButton = wrapper.querySelector("[data-action=clear]");
|
4 | const changeBackgroundColorButton = wrapper.querySelector("[data-action=change-background-color]");
|
5 | const changeColorButton = wrapper.querySelector("[data-action=change-color]");
|
6 | const changeWidthButton = wrapper.querySelector("[data-action=change-width]");
|
7 | const undoButton = wrapper.querySelector("[data-action=undo]");
|
8 | const redoButton = wrapper.querySelector("[data-action=redo]");
|
9 | const savePNGButton = wrapper.querySelector("[data-action=save-png]");
|
10 | const saveJPGButton = wrapper.querySelector("[data-action=save-jpg]");
|
11 | const saveSVGButton = wrapper.querySelector("[data-action=save-svg]");
|
12 | const saveSVGWithBackgroundButton = wrapper.querySelector("[data-action=save-svg-with-background]");
|
13 | const openInWindowButton = wrapper.querySelector("[data-action=open-in-window]");
|
14 | let undoData = [];
|
15 | const canvas = wrapper.querySelector("canvas");
|
16 | const signaturePad = new SignaturePad(canvas, {
|
17 |
|
18 |
|
19 | backgroundColor: 'rgb(255, 255, 255)'
|
20 | });
|
21 |
|
22 | function randomColor() {
|
23 | const r = Math.round(Math.random() * 255);
|
24 | const g = Math.round(Math.random() * 255);
|
25 | const b = Math.round(Math.random() * 255);
|
26 | return `rgb(${r},${g},${b})`;
|
27 | }
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | function resizeCanvas() {
|
33 |
|
34 |
|
35 |
|
36 | const ratio = Math.max(window.devicePixelRatio || 1, 1);
|
37 |
|
38 |
|
39 | canvas.width = canvas.offsetWidth * ratio;
|
40 | canvas.height = canvas.offsetHeight * ratio;
|
41 | canvas.getContext("2d").scale(ratio, ratio);
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | signaturePad.fromData(signaturePad.toData());
|
52 | }
|
53 |
|
54 |
|
55 |
|
56 | window.onresize = resizeCanvas;
|
57 | resizeCanvas();
|
58 |
|
59 | window.addEventListener("keydown", (event) => {
|
60 | switch (true) {
|
61 | case event.key === "z" && event.ctrlKey:
|
62 | undoButton.click();
|
63 | break;
|
64 | case event.key === "y" && event.ctrlKey:
|
65 | redoButton.click();
|
66 | break;
|
67 | }
|
68 | });
|
69 |
|
70 | function download(dataURL, filename) {
|
71 | const blob = dataURLToBlob(dataURL);
|
72 | const url = window.URL.createObjectURL(blob);
|
73 |
|
74 | const a = document.createElement("a");
|
75 | a.style = "display: none";
|
76 | a.href = url;
|
77 | a.download = filename;
|
78 |
|
79 | document.body.appendChild(a);
|
80 | a.click();
|
81 |
|
82 | window.URL.revokeObjectURL(url);
|
83 | }
|
84 |
|
85 |
|
86 |
|
87 | function dataURLToBlob(dataURL) {
|
88 |
|
89 | const parts = dataURL.split(';base64,');
|
90 | const contentType = parts[0].split(":")[1];
|
91 | const raw = window.atob(parts[1]);
|
92 | const rawLength = raw.length;
|
93 | const uInt8Array = new Uint8Array(rawLength);
|
94 |
|
95 | for (let i = 0; i < rawLength; ++i) {
|
96 | uInt8Array[i] = raw.charCodeAt(i);
|
97 | }
|
98 |
|
99 | return new Blob([uInt8Array], { type: contentType });
|
100 | }
|
101 |
|
102 | signaturePad.addEventListener("endStroke", () => {
|
103 |
|
104 | undoData = [];
|
105 | });
|
106 |
|
107 | clearButton.addEventListener("click", () => {
|
108 | signaturePad.clear();
|
109 | });
|
110 |
|
111 | undoButton.addEventListener("click", () => {
|
112 | const data = signaturePad.toData();
|
113 |
|
114 | if (data && data.length > 0) {
|
115 |
|
116 | const removed = data.pop();
|
117 | undoData.push(removed);
|
118 | signaturePad.fromData(data);
|
119 | }
|
120 | });
|
121 |
|
122 | redoButton.addEventListener("click", () => {
|
123 | if (undoData.length > 0) {
|
124 | const data = signaturePad.toData();
|
125 | data.push(undoData.pop());
|
126 | signaturePad.fromData(data);
|
127 | }
|
128 | });
|
129 |
|
130 | changeBackgroundColorButton.addEventListener("click", () => {
|
131 | signaturePad.backgroundColor = randomColor();
|
132 | const data = signaturePad.toData();
|
133 | signaturePad.clear();
|
134 | signaturePad.fromData(data);
|
135 | });
|
136 |
|
137 | changeColorButton.addEventListener("click", () => {
|
138 | signaturePad.penColor = randomColor();
|
139 | });
|
140 |
|
141 | changeWidthButton.addEventListener("click", () => {
|
142 | const min = Math.round(Math.random() * 100) / 10;
|
143 | const max = Math.round(Math.random() * 100) / 10;
|
144 |
|
145 | signaturePad.minWidth = Math.min(min, max);
|
146 | signaturePad.maxWidth = Math.max(min, max);
|
147 | });
|
148 |
|
149 | savePNGButton.addEventListener("click", () => {
|
150 | if (signaturePad.isEmpty()) {
|
151 | alert("Please provide a signature first.");
|
152 | } else {
|
153 | const dataURL = signaturePad.toDataURL();
|
154 | download(dataURL, "signature.png");
|
155 | }
|
156 | });
|
157 |
|
158 | saveJPGButton.addEventListener("click", () => {
|
159 | if (signaturePad.isEmpty()) {
|
160 | alert("Please provide a signature first.");
|
161 | } else {
|
162 | const dataURL = signaturePad.toDataURL("image/jpeg");
|
163 | download(dataURL, "signature.jpg");
|
164 | }
|
165 | });
|
166 |
|
167 | saveSVGButton.addEventListener("click", () => {
|
168 | if (signaturePad.isEmpty()) {
|
169 | alert("Please provide a signature first.");
|
170 | } else {
|
171 | const dataURL = signaturePad.toDataURL('image/svg+xml');
|
172 | download(dataURL, "signature.svg");
|
173 | }
|
174 | });
|
175 |
|
176 | saveSVGWithBackgroundButton.addEventListener("click", () => {
|
177 | if (signaturePad.isEmpty()) {
|
178 | alert("Please provide a signature first.");
|
179 | } else {
|
180 | const dataURL = signaturePad.toDataURL('image/svg+xml', { includeBackgroundColor: true });
|
181 | download(dataURL, "signature.svg");
|
182 | }
|
183 | });
|
184 |
|
185 | openInWindowButton.addEventListener("click", () => {
|
186 | var externalWin = window.open('', '', `width=${canvas.width / window.devicePixelRatio},height=${canvas.height / window.devicePixelRatio}`);
|
187 | canvas.style.width = "100%";
|
188 | canvas.style.height = "100%";
|
189 | externalWin.onresize = resizeCanvas;
|
190 | externalWin.document.body.style.margin = '0';
|
191 | externalWin.document.body.appendChild(canvas);
|
192 | canvasWrapper.classList.add("empty");
|
193 | externalWin.onbeforeunload = () => {
|
194 | canvas.style.width = "";
|
195 | canvas.style.height = "";
|
196 | canvasWrapper.classList.remove("empty");
|
197 | canvasWrapper.appendChild(canvas);
|
198 | resizeCanvas();
|
199 | };
|
200 | })
|