-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
123 lines (97 loc) · 3.67 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
let selectedColor = '#CCCCCC'; // Initial color
let username = ''; // Initial username
let bannerGenerated = false; // Flag to track if the banner has been generated
let selectedImage = null; // Variable to store the selected image
function generateBanner() {
// Get the updated username
username = document.getElementById('username').value;
// Call the banner generation function
updateBanner();
// Set the flag to indicate that the banner has been generated
bannerGenerated = true;
// Show the download button
document.getElementById('downloadButton').style.display = 'block';
// Display username in the console
console.log(`Username: ${username}`);
}
function handleImageUpload() {
const imageInput = document.getElementById('imageInput');
const file = imageInput.files[0];
if (file) {
const reader = new FileReader();
reader.onload = function (e) {
// Set the selected image source
selectedImage = e.target.result;
// Update the banner with the selected image
updateBanner();
};
// Read the selected image as a data URL
reader.readAsDataURL(file);
}
}
function updateBanner() {
const bannerPreview = document.getElementById('bannerPreview');
// Update banner preview with the selected color, live username, and uploaded image
bannerPreview.style.backgroundColor = selectedColor;
bannerPreview.innerHTML = username;
// Check if an image is selected
if (selectedImage) {
bannerPreview.style.backgroundImage = `url(${selectedImage})`;
} else {
bannerPreview.style.backgroundImage = 'none';
}
// Create or update the canvas in the generatedBannerContainer
updateCanvas();
}
function updateCanvas() {
const generatedBannerContainer = document.getElementById('generatedBannerContainer');
// Check if canvas already exists
let canvas = generatedBannerContainer.querySelector('canvas');
if (canvas) {
// If canvas exists, remove it to recreate
generatedBannerContainer.removeChild(canvas);
}
// Create a new canvas
canvas = document.createElement('canvas');
canvas.width = 300; // Set your desired width
canvas.height = 150; // Set your desired height
generatedBannerContainer.appendChild(canvas);
const context = canvas.getContext('2d');
context.fillStyle = selectedColor;
context.fillRect(0, 0, canvas.width, canvas.height);
// Draw the selected image
if (selectedImage) {
const image = new Image();
image.src = selectedImage;
context.drawImage(image, 0, 0, canvas.width, canvas.height);
}
// Draw the username text on top of the image
context.fillStyle = '#ffffff'; // Set the text color
context.font = '20px Arial'; // Set the font size and type
context.textAlign = 'center';
context.textBaseline = 'middle';
context.fillText(username, canvas.width / 2, canvas.height / 2);
}
function updatePreview() {
// Get the current username input value
username = document.getElementById('username').value;
// Update selectedColor with the color picker value
selectedColor = document.getElementById('backgroundColor').value;
// Update banner on color change only if the banner has been generated
if (bannerGenerated) {
updateBanner();
}
}
function downloadImage() {
const generatedBannerContainer = document.getElementById('generatedBannerContainer');
const canvas = generatedBannerContainer.querySelector('canvas');
if (canvas) {
const url = canvas.toDataURL(); // Convert canvas content to data URL
const link = document.createElement('a');
link.href = url;
link.download = 'generated-banner.png'; // Set the desired file name
link.click();
} else {
console.error('Canvas not found. Banner may not have been generated.');
}
}