-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
160 lines (135 loc) · 5.34 KB
/
script.js
File metadata and controls
160 lines (135 loc) · 5.34 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
function normalizeText(text, caseMode, skipPunctuation) {
if (caseMode === "lower") text = text.toLowerCase();
if (caseMode === "upper") text = text.toUpperCase();
if (skipPunctuation) {
text = text.replace(/[^a-zA-Z]/g, "");
}
return text;
}
function groupText(text, size, padChar) {
const groups = [];
for (let i = 0; i < text.length; i += size) {
let chunk = text.slice(i, i + size);
if (chunk.length < size) {
chunk = chunk.padEnd(size, padChar);
}
groups.push(chunk);
}
return groups;
}
function groupToHexColor(group) {
let hex = "";
for (const char of group) {
hex += char.charCodeAt(0).toString(16).padStart(2, "0");
}
return "#" + hex;
}
function generateBanner() {
const text = document.getElementById("inputText").value;
const caseMode = document.getElementById("caseMode").value;
const skipPunctuation = document.getElementById("skipPunctuation").checked;
const groupSize = parseInt(document.getElementById("groupSize").value);
const padChar = document.getElementById("padChar").value || "0";
const useGradient = document.getElementById("useGradient").checked;
const normalized = normalizeText(text, caseMode, skipPunctuation);
const groups = groupText(normalized, groupSize, padChar);
const colors = groups.map(groupToHexColor);
const banner = document.getElementById("banner");
banner.innerHTML = "";
if (useGradient) {
// Smooth transition between all colors
banner.style.background = `linear-gradient(to right, ${colors.join(", ")})`;
} else {
// Solid blocks
banner.style.background = "none";
colors.forEach(color => {
const div = document.createElement("div");
div.className = "color-block";
div.style.backgroundColor = color;
banner.appendChild(div);
});
}
}
function getDimensions(ratio) {
switch (ratio) {
case "2:3": return { width: 600, height: 400 };
case "1:2": return { width: 700, height: 350 };
case "1:1": return { width: 500, height: 500 };
}
}
function exportPNG(colors, useGradient, ratio) {
const { width, height } = getDimensions(ratio);
const canvas = document.createElement("canvas");
canvas.width = width;
canvas.height = height;
const ctx = canvas.getContext("2d");
if (useGradient) {
const grad = ctx.createLinearGradient(0, 0, width, 0);
const step = 1 / (colors.length - 1);
colors.forEach((color, i) => grad.addColorStop(i * step, color));
ctx.fillStyle = grad;
ctx.fillRect(0, 0, width, height);
} else {
const stripeWidth = width / colors.length;
colors.forEach((color, i) => {
ctx.fillStyle = color;
ctx.fillRect(i * stripeWidth, 0, stripeWidth, height);
});
}
const link = document.createElement("a");
link.download = "flag.png";
link.href = canvas.toDataURL("image/png");
link.click();
}
function exportSVG(colors, useGradient, ratio) {
const { width, height } = getDimensions(ratio);
let svg = `<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="${height}">`;
if (useGradient) {
svg += `
<defs>
<linearGradient id="grad" x1="0%" y1="0%" x2="100%" y2="0%">
${colors.map((c, i) => {
const pct = (i / (colors.length - 1)) * 100;
return `<stop offset="${pct}%" stop-color="${c}" />`;
}).join("")}
</linearGradient>
</defs>
<rect width="100%" height="100%" fill="url(#grad)" />
`;
} else {
const stripeWidth = width / colors.length;
colors.forEach((color, i) => {
svg += `<rect x="${i * stripeWidth}" y="0" width="${stripeWidth}" height="${height}" fill="${color}" />`;
});
}
svg += `</svg>`;
const blob = new Blob([svg], { type: "image/svg+xml" });
const link = document.createElement("a");
link.download = "flag.svg";
link.href = URL.createObjectURL(blob);
link.click();
}
document.getElementById("generateBtn").addEventListener("click", generateBanner);
document.getElementById("themeToggle").addEventListener("click", () => {
document.body.classList.toggle("dark");
const btn = document.getElementById("themeToggle");
btn.textContent = document.body.classList.contains("dark") ? "☀️" : "🌙";
});
document.getElementById("exportBtn").addEventListener("click", () => {
const text = document.getElementById("inputText").value;
const caseMode = document.getElementById("caseMode").value;
const skipPunctuation = document.getElementById("skipPunctuation").checked;
const groupSize = parseInt(document.getElementById("groupSize").value);
const padChar = document.getElementById("padChar").value || "0";
const useGradient = document.getElementById("useGradient").checked;
const format = document.getElementById("exportFormat").value;
const ratio = document.getElementById("exportRatio").value;
const normalized = normalizeText(text, caseMode, skipPunctuation);
const groups = groupText(normalized, groupSize, padChar);
const colors = groups.map(groupToHexColor);
if (format === "png") {
exportPNG(colors, useGradient, ratio);
} else {
exportSVG(colors, useGradient, ratio);
}
});