You can edit the below JavaScript code to customize the image tool.
Apply Changes
function processImage(
originalImg,
barWidth = 16,
gap = 4,
alignment = "center",
showWindows = "true",
backgroundColor = "#111122",
windowColor = "rgba(255, 255, 200, 0.8)",
windowProbability = 0.7,
normalizeHeight = "true"
) {
// Parse numeric and boolean parameters
barWidth = parseInt(barWidth, 10);
if (isNaN(barWidth) || barWidth < 1) barWidth = 16;
gap = parseInt(gap, 10);
if (isNaN(gap)) gap = 4;
windowProbability = parseFloat(windowProbability);
if (isNaN(windowProbability)) windowProbability = 0.7;
const width = originalImg.width;
const height = originalImg.height;
// Create a working canvas
const canvas = document.createElement("canvas");
canvas.width = width;
canvas.height = height;
const ctx = canvas.getContext("2d", { willReadFrequently: true });
// Draw the original image first without background to correctly read transparent areas
ctx.drawImage(originalImg, 0, 0);
const imgData = ctx.getImageData(0, 0, width, height);
const data = imgData.data;
const pitch = barWidth + gap;
const cols = Math.floor(width / pitch);
if (cols === 0) {
// If the configuration makes the pitch larger than the image, return early
ctx.fillStyle = backgroundColor;
ctx.fillRect(0, 0, width, height);
return canvas;
}
const columns = [];
let minB = 255;
let maxB = 0;
// Calculate brightness and average color metrics for each column slicing through the image
for (let i = 0; i < cols; i++) {
let rSum = 0, gSum = 0, bSum = 0, count = 0;
let xStart = i * pitch;
let xEnd = Math.min(xStart + barWidth, width);
for (let py = 0; py < height; py++) {
for (let px = xStart; px < xEnd; px++) {
let idx = (py * width + px) * 4;
// Skip mostly transparent pixels to form waveform blocks only where content exists
if (data[idx + 3] < 128) continue;
rSum += data[idx];
gSum += data[idx + 1];
bSum += data[idx + 2];
count++;
}
}
if (count === 0) {
columns.push({ empty: true });
continue;
}
let r = rSum / count;
let g = gSum / count;
let b = bSum / count;
// Standard relative luminance (brightness)
let brightness = r * 0.299 + g * 0.587 + b * 0.114;
if (brightness < minB) minB = brightness;
if (brightness > maxB) maxB = brightness;
columns.push({
r: Math.round(r),
g: Math.round(g),
b: Math.round(b),
brightness: brightness,
empty: false
});
}
// Now completely clear and fill the workspace with the backdrop color
ctx.fillStyle = backgroundColor;
ctx.fillRect(0, 0, width, height);
// Center the wave horizontally on the page
const totalWaveWidth = cols * pitch - gap;
const startOffsetX = Math.floor((width - totalWaveWidth) / 2);
const minH = Math.max(barWidth * 2, height * 0.05);
const maxH = height * 0.95;
// Draw the generated sound wave buildings
for (let i = 0; i < cols; i++) {
const col = columns[i];
if (col.empty) continue; // Leave gaps for clear cuts in shape
let norm = 0;
if (String(normalizeHeight).toLowerCase() === "true") {
if (maxB > minB) {
norm = (col.brightness - minB) / (maxB - minB);
} else {
norm = 0.5;
}
} else {
norm = col.brightness / 255;
}
// Apply slight contrast easing for a punchier soundwave
norm = Math.pow(norm, 1.1);
const h = minH + norm * (maxH - minH);
let y = 0;
if (alignment === "bottom") {
y = height - h;
} else if (alignment === "top") {
y = 0;
} else {
// center alignment
y = (height - h) / 2;
}
const x = startOffsetX + i * pitch;
// Draw Building main block
ctx.fillStyle = `rgb(${col.r}, ${col.g}, ${col.b})`;
ctx.fillRect(x, y, barWidth, h);
// Decorate with windows and a roof structure
if (String(showWindows).toLowerCase() === "true") {
const wWinWidth = Math.max(2, Math.floor(barWidth / 4));
const wWinHeight = Math.max(3, Math.floor(wWinWidth * 1.5));
const wWinGap = Math.max(2, Math.floor(barWidth / 8));
const availableWidth = barWidth - wWinGap * 2;
// Render windows on building
if (availableWidth >= wWinWidth) {
const winCols = Math.floor((availableWidth + wWinGap) / (wWinWidth + wWinGap));
const totalWinWidth = winCols * wWinWidth + (winCols - 1) * wWinGap;
const startXOff = x + (barWidth - totalWinWidth) / 2;
ctx.fillStyle = windowColor;
// Basic deterministic PRNG to avoid flickering if regenerated
let seed = i * 1337;
const seededRandom = () => {
let t = seed += 0x6D2B79F5;
t = Math.imul(t ^ t >>> 15, t | 1);
t ^= t + Math.imul(t ^ t >>> 7, t | 61);
return ((t ^ t >>> 14) >>> 0) / 4294967296;
};
for (let wy = Math.floor(y + wWinGap * 2); wy < y + h - wWinHeight - wWinGap; wy += wWinHeight + wWinGap) {
for (let c = 0; c < winCols; c++) {
if (seededRandom() < windowProbability) {
ctx.fillRect(startXOff + c * (wWinWidth + wWinGap), wy, wWinWidth, wWinHeight);
}
}
}
}
// Draw roof and antenna logic to amplify cityscape feeling
ctx.fillStyle = `rgb(${Math.max(0, col.r - 40)}, ${Math.max(0, col.g - 40)}, ${Math.max(0, col.b - 40)})`;
const roofW = Math.max(2, Math.floor(barWidth * 0.5));
const roofH = Math.max(2, Math.floor(barWidth * 0.3));
const antW = Math.max(1, Math.floor(barWidth * 0.1));
const antH = Math.max(2, Math.floor(barWidth * 0.7));
if (alignment === "bottom" || alignment === "center") {
const drawY = Math.floor(y - roofH);
ctx.fillRect(Math.floor(x + (barWidth - roofW) / 2), drawY, roofW, roofH);
ctx.fillRect(Math.floor(x + (barWidth - antW) / 2), drawY - antH, antW, antH);
}
if (alignment === "top" || alignment === "center") {
const drawY = Math.floor(y + h);
ctx.fillRect(Math.floor(x + (barWidth - roofW) / 2), drawY, roofW, roofH);
ctx.fillRect(Math.floor(x + (barWidth - antW) / 2), drawY + roofH, antW, antH);
}
}
}
// Add visual centerline effect for center waveforms
if (alignment === "center") {
ctx.fillStyle = "rgba(255, 255, 255, 0.15)";
ctx.fillRect(0, Math.floor(height / 2) - 1, width, 2);
}
return canvas;
}
Apply Changes