-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconverter.js
68 lines (60 loc) · 2.06 KB
/
converter.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
// Define constants for HTML elements
const downloadButton = document.getElementById('download-button');
const originalImage = document.getElementById('original-image');
const convertedImage = document.getElementById('converted-image');
const formatSelect = document.getElementById('format-select');
const displayCheckbox = document.getElementById('display-checkbox');
// Prevent default behavior when file is dropped on the page
function handleDrop(e) {
e.preventDefault();
e.stopPropagation();
const files = e.dataTransfer.files;
if (files.length > 0) {
originalImage.files = files;
}
}
function convertImage() {
if (!originalImage.files || !originalImage.files[0]) {
console.error('No file selected.');
return;
}
const reader = new FileReader();
reader.onload = function() {
const imageDataUrl = reader.result;
const image = new Image();
image.onload = function() {
const canvas = document.createElement('canvas');
canvas.width = image.width;
canvas.height = image.height;
const context = canvas.getContext('2d');
context.drawImage(image, 0, 0);
const mimeType = `image/${formatSelect.value}`;
const dataURL = canvas.toDataURL(mimeType);
convertedImage.src = dataURL;
convertedImage.alt = `Converted image in ${formatSelect.value.toUpperCase()} format`;
downloadButton.href = dataURL;
downloadButton.download = `image.${formatSelect.value}`;
if (displayCheckbox.checked) {
convertedImage.style.display = 'inline';
} else {
convertedImage.style.display = 'none';
}
downloadButton.style.display = 'block';
};
image.src = imageDataUrl;
};
reader.readAsDataURL(originalImage.files[0]);
}
// Clear selected file when page is refreshed
window.addEventListener('beforeunload', function() {
originalImage.value = '';
});
// Attach event listeners to HTML elements
originalImage.addEventListener('change', convertImage);
formatSelect.addEventListener('change', convertImage);
displayCheckbox.addEventListener('change', convertImage);
document.addEventListener('drop', handleDrop);
document.addEventListener('dragover', function(e) {
e.preventDefault();
e.stopPropagation();
});