You can edit the below JavaScript code to customize the image tool.
Apply Changes
async function processImage(originalImg, theme = 'light', url = 'https://example.com') {
/**
* Creates a website preview mockup by placing the image inside a styled browser window.
*
* @param {Image} originalImg The original image object to be placed in the preview.
* @param {string} theme The browser theme, can be 'light' or 'dark'.
* @param {string} url The URL to display in the browser's title/address bar area.
* @returns {HTMLCanvasElement} A canvas element with the generated preview.
*/
// --- Configuration ---
const headerHeight = 45;
const borderRadius = 10;
const padding = 12;
const shadowBlur = 30;
const shadowOffsetY = 10;
const colors = theme === 'dark' ? {
shadow: 'rgba(0, 0, 0, 0.5)',
background: '#2d2d2d',
header: '#2d2d2d',
textColor: '#bfbfbf',
borderColor: '#4a4a4a',
btnRed: '#ff5f57',
btnYellow: '#febc2e',
btnGreen: '#28c840',
} : {
shadow: 'rgba(100, 100, 100, 0.15)',
background: '#ffffff',
header: '#f1f1f1',
textColor: '#5e5e5e',
borderColor: '#e5e5e5',
btnRed: '#ff5f57',
btnYellow: '#febc2e',
btnGreen: '#28c840',
};
// --- Canvas Setup ---
const canvas = document.createElement('canvas');
// Add extra space for padding and the shadow
const canvasPadding = padding + shadowBlur;
canvas.width = originalImg.width + canvasPadding * 2;
canvas.height = originalImg.height + headerHeight + canvasPadding * 2;
const ctx = canvas.getContext('2d');
// Coordinates for the main window frame, centered on the canvas
const frameX = canvasPadding;
const frameY = canvasPadding;
const frameWidth = originalImg.width;
const frameHeight = originalImg.height + headerHeight;
// --- Drawing ---
// 1. Draw shadow for the window
ctx.shadowColor = colors.shadow;
ctx.shadowBlur = shadowBlur;
ctx.shadowOffsetY = shadowOffsetY;
// 2. Draw main window background with a border
ctx.fillStyle = colors.background;
ctx.strokeStyle = colors.borderColor;
ctx.lineWidth = 1;
ctx.beginPath();
ctx.roundRect(frameX, frameY, frameWidth, frameHeight, borderRadius);
ctx.fill();
ctx.stroke();
// Reset shadow for inner elements
ctx.shadowColor = 'transparent';
ctx.shadowBlur = 0;
ctx.shadowOffsetY = 0;
// 3. Draw the header bar
// We clip the top part of the filled rectangle we just drew to create the header.
// This is simpler than drawing a custom path.
ctx.fillStyle = colors.header;
ctx.beginPath();
ctx.roundRect(frameX, frameY, frameWidth, frameHeight, borderRadius);
ctx.clip(); // Clip everything drawn after this to the main window shape
ctx.fillRect(frameX, frameY, frameWidth, headerHeight);
// Draw a subtle line separating header and content
ctx.strokeStyle = colors.borderColor;
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(frameX, frameY + headerHeight);
ctx.lineTo(frameX + frameWidth, frameY + headerHeight);
ctx.stroke();
// 4. Draw "traffic light" window control buttons
const buttonRadius = 6;
const buttonY = frameY + headerHeight / 2;
const buttonXStart = frameX + 20;
ctx.fillStyle = colors.btnRed;
ctx.beginPath();
ctx.arc(buttonXStart, buttonY, buttonRadius, 0, 2 * Math.PI);
ctx.fill();
ctx.fillStyle = colors.btnYellow;
ctx.beginPath();
ctx.arc(buttonXStart + 20, buttonY, buttonRadius, 0, 2 * Math.PI);
ctx.fill();
ctx.fillStyle = colors.btnGreen;
ctx.beginPath();
ctx.arc(buttonXStart + 40, buttonY, buttonRadius, 0, 2 * Math.PI);
ctx.fill();
// 5. Draw URL text
ctx.fillStyle = colors.textColor;
ctx.font = '14px -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText(url, frameX + frameWidth / 2, buttonY);
// 6. Draw the user's image content
ctx.drawImage(originalImg, frameX, frameY + headerHeight, originalImg.width, originalImg.height);
// --- Return Result ---
return canvas;
}
Apply Changes