You can edit the below JavaScript code to customize the image tool.
Apply Changes
function processImage(originalImg, text = "Hmm...", bubblePosition = "top-right", bubbleColor = "#ffffff", textColor = "#000000") {
// Basic setup
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");
const width = originalImg.width;
const height = originalImg.height;
canvas.width = width;
canvas.height = height;
// Draw original background image
ctx.drawImage(originalImg, 0, 0, width, height);
// Typography and text scaling definitions
const fontSize = Math.max(16, width * 0.04);
ctx.font = `bold ${fontSize}px "Comic Sans MS", "Comic Neue", "Chalkboard SE", sans-serif`;
// Line height and max paragraph width
const lineHeight = fontSize * 1.3;
const maxTextWidth = width * 0.35;
// Helper function to wrap text
function getLines(ctx, text, maxWidth) {
const lines = text.split('\n');
const finalLines = [];
for (let line of lines) {
const words = line.split(' ');
let currentLine = '';
for (let word of words) {
let testLine = currentLine + (currentLine.length ? ' ' : '') + word;
if (ctx.measureText(testLine).width > maxWidth && currentLine.length > 0) {
finalLines.push(currentLine);
currentLine = word;
} else {
currentLine = testLine;
}
}
finalLines.push(currentLine);
}
return finalLines;
}
const lines = getLines(ctx, text, maxTextWidth);
// Determine bounds and ellipse dimensions
let maxLineWidth = 0;
lines.forEach(l => {
maxLineWidth = Math.max(maxLineWidth, ctx.measureText(l).width);
});
const totalTextHeight = lines.length * lineHeight;
// Shape dimensions (ellipse parameters based on text bounds)
const rx = Math.max((maxLineWidth / 2) * 1.4, width * 0.08); // Radii X
const ry = Math.max((totalTextHeight / 2) * 1.6, height * 0.06); // Radii Y
const bumpR = Math.sqrt(rx * rx + ry * ry) * 0.2; // Size of the cloud bumps
const totalMaxRadius = Math.max(rx, ry) + bumpR;
const padding = width * 0.05 + totalMaxRadius;
// Figure out the placement based on configuration
let cx, cy;
const isLeft = bubblePosition.toLowerCase().includes("left");
const isTop = bubblePosition.toLowerCase().includes("top");
cx = isLeft ? Math.min(padding, width / 2) : Math.max(width / 2, width - padding);
cy = isTop ? Math.min(padding, height / 2) : Math.max(height / 2, height - padding);
// Shadow parameters for depth
ctx.shadowColor = 'rgba(0, 0, 0, 0.45)';
ctx.shadowBlur = Math.max(6, width * 0.015);
ctx.shadowOffsetX = Math.max(3, width * 0.005);
ctx.shadowOffsetY = Math.max(3, width * 0.005);
ctx.fillStyle = bubbleColor;
// Begin comprehensive cloud path
ctx.beginPath();
// Main inner ellipse
ctx.ellipse(cx, cy, rx, ry, 0, 0, Math.PI * 2);
// Form cloud ripples (scalloped edge)
const numBumps = 16;
for (let i = 0; i < numBumps; i++) {
const angle = (i / numBumps) * Math.PI * 2;
const bx = cx + Math.cos(angle) * rx;
const by = cy + Math.sin(angle) * ry;
// Move to start angle of arc to prevent straggler intersecting lines
ctx.moveTo(bx + bumpR, by);
ctx.arc(bx, by, bumpR, 0, Math.PI * 2);
}
// Calculate trail dots going toward image subject (assumed around center screen)
const targetX = width / 2;
const targetY = height / 2;
const angleDot = Math.atan2(targetY - cy, targetX - cx);
// Distance from center out to the edge of the ellipse on the dot angle
const edgeDist = (rx * ry) / Math.sqrt(Math.pow(ry * Math.cos(angleDot), 2) + Math.pow(rx * Math.sin(angleDot), 2));
// Trailing Bubbles Size & Positions Matrix
const dotBaseDist = edgeDist + bumpR + width * 0.01;
const dot1R = width * 0.018;
const dot1Dist = dotBaseDist + dot1R;
const dot1X = cx + Math.cos(angleDot) * dot1Dist;
const dot1Y = cy + Math.sin(angleDot) * dot1Dist;
const dot2R = width * 0.012;
const dot2Dist = dot1Dist + dot1R + width * 0.01 + dot2R;
const dot2X = cx + Math.cos(angleDot) * dot2Dist;
const dot2Y = cy + Math.sin(angleDot) * dot2Dist;
const dot3R = width * 0.006;
const dot3Dist = dot2Dist + dot2R + width * 0.005 + dot3R;
const dot3X = cx + Math.cos(angleDot) * dot3Dist;
const dot3Y = cy + Math.sin(angleDot) * dot3Dist;
// Draw the dots as part of the overall single path (for unison drop shadows)
ctx.moveTo(dot1X + dot1R, dot1Y);
ctx.arc(dot1X, dot1Y, dot1R, 0, Math.PI * 2);
ctx.moveTo(dot2X + dot2R, dot2Y);
ctx.arc(dot2X, dot2Y, dot2R, 0, Math.PI * 2);
ctx.moveTo(dot3X + dot3R, dot3Y);
ctx.arc(dot3X, dot3Y, dot3R, 0, Math.PI * 2);
// Fills main cloud shape + trail bubbles natively unifying all overlapping areas
ctx.fill();
// Clear shadow state securely so it doesn't duplicate behind the text
ctx.shadowColor = 'transparent';
ctx.shadowBlur = 0;
ctx.shadowOffsetX = 0;
ctx.shadowOffsetY = 0;
// Render the actual thought text properly aligned
ctx.fillStyle = textColor;
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
const startY = cy - (totalTextHeight / 2) + (lineHeight / 2);
for (let i = 0; i < lines.length; i++) {
ctx.fillText(lines[i], cx, startY + (i * lineHeight));
}
return canvas;
}
Apply Changes