-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
87 lines (75 loc) · 2.61 KB
/
script.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
79
80
81
82
83
84
85
86
87
const changeImageButton = document.querySelector('#change-image');
const changeTextButton = document.querySelector('#change-text');
const previewImage = document.querySelector('#preview-image');
const previewText = document.querySelector('#preview-text');
const senderInput = document.querySelector('#sender');
const previewSender = document.querySelector('.preview-sender');
const downloadButton = document.querySelector('#download');
const previewArea = document.querySelector('.preview');
const assetsCount = 4;
let currentImageIndex = 1;
let currentTextIndex = 1;
// function changeImage() {
// currentImageIndex+=1;
// if(currentImageIndex > assetsCount) {
// currentImageIndex = 1;
// }
// console.log('cambio immagine', currentImageIndex);
// previewImage.src = `./images/img_${currentImageIndex}.png`;
// }
// function changeText() {
// currentTextIndex+=1;
// if(currentTextIndex > assetsCount) {
// currentTextIndex = 1;
// }
// console.log('cambio testo', currentTextIndex);
// previewText.src = `./images/txt_${currentTextIndex}.png`;
// }
// changeImageButton.addEventListener('click', changeImage);
// changeTextButton.addEventListener('click', changeText);
function changeAsset(assetType) {
let currentIndex;
let targetElement;
let srcPrefix;
if (assetType === 'image') {
currentIndex = currentImageIndex;
targetElement = previewImage;
srcPrefix = 'img';
} else if (assetType === 'text') {
currentIndex = currentTextIndex;
targetElement = previewText;
srcPrefix = 'txt';
}
currentIndex += 1;
if (currentIndex > assetsCount) {
currentIndex = 1;
}
// console.log(`cambio ${srcPrefix}`, currentIndex);
targetElement.src = `./images/${srcPrefix}_${currentIndex}.png`;
if (assetType === 'image') {
currentImageIndex = currentIndex;
} else if (assetType === 'text') {
currentTextIndex = currentIndex;
}
}
function updateSender() {
const txt = senderInput.value;
if (txt.trim().length > 0) {
previewSender.innerText = txt;
} else {
previewSender.innerText = 'Augurissimi';
}
}
function downloadImage() {
htmlToImage.toJpeg(previewArea)
.then(function (dataUrl) {
const link = document.createElement('a');
link.download = 'my-image-name.jpeg';
link.href = dataUrl;
link.click();
});
}
changeImageButton.addEventListener('click', () => changeAsset('image'));
changeTextButton.addEventListener('click', () => changeAsset('text'));
senderInput.addEventListener('input', updateSender);
downloadButton.addEventListener('click', downloadImage);