-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignature.js
78 lines (66 loc) · 2.9 KB
/
signature.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
function displaySignature(e) {
const nameInput = document.getElementById('nameInput').value.trim();
const signatureDisplay = document.getElementById('signatureDisplay');
const fontStyle = document.getElementById('fontSelector').value;
const btn = e.currentTarget;
const loader = btn.querySelector('.loader');
const btnText = btn.querySelector('.btn-text');
if (nameInput === '') {
alert('Please enter your name!');
return;
}
// Show loader
btnText.style.display = 'none';
loader.style.display = 'block';
btn.style.opacity = '0.7';
// Create ripple effect
const ripple = document.createElement('div');
ripple.className = 'ripple';
ripple.style.left = `${e.clientX - btn.offsetLeft}px`;
ripple.style.top = `${e.clientY - btn.offsetTop}px`;
btn.appendChild(ripple);
setTimeout(() => {
// Generate signature
signatureDisplay.textContent = '';
void signatureDisplay.offsetWidth;
const variations = ['variation-1', 'variation-2', 'variation-3'];
signatureDisplay.className = `signature ${fontStyle} ${variations[Math.floor(Math.random() * variations.length)]}`;
signatureDisplay.textContent = nameInput;
// Restart animations
signatureDisplay.style.animation = 'none';
setTimeout(() => {
signatureDisplay.style.animation = 'typing 2s steps(30, end), blink-caret 0.75s step-end infinite, signature-fade 0.5s ease-in, float 3s ease-in-out infinite';
}, 10);
// Hide loader and show download button
btnText.style.display = 'block';
loader.style.display = 'none';
btn.style.opacity = '1';
document.getElementById('downloadBtn').style.display = 'inline-block';
}, 1000);
}
// Function to download the signature
async function downloadSignature() {
const signatureDisplay = document.getElementById('signatureDisplay');
// Temporarily disable animations and transformations
signatureDisplay.style.animation = 'none';
signatureDisplay.style.transform = 'none';
// Wait for fonts to load
await new Promise((resolve) => {
document.fonts.ready.then(resolve);
});
// Capture the signature using html2canvas
html2canvas(signatureDisplay, {
scale: 2, // Increase resolution for better quality
allowTaint: true,
useCORS: true,
}).then(canvas => {
// Re-enable animations and transformations
signatureDisplay.style.animation = '';
signatureDisplay.style.transform = '';
// Convert canvas to image and trigger download
const link = document.createElement('a');
link.href = canvas.toDataURL('image/png');
link.download = 'signature.png';
link.click();
});
}