Please bookmark this page to avoid losing your image tool!

No Valid Title Found For The Provided Description

(Free & Supports Bulk Upload)

Drag & drop your images here or

The result will appear here...
You can edit the below JavaScript code to customize the image tool.
function processImage(originalImg, numberOfTiles = 6, spacing = 5, gapColor = "#ffffff") {
    const canvas = document.createElement('canvas');
    const ctx = canvas.getContext('2d');

    // Interpret the input strictly, ensuring it's a valid integer.
    // Capping at 100 tiles to prevent canvas memory/performance issues.
    let n = parseInt(numberOfTiles, 10);
    if (isNaN(n) || n < 2) n = 6; 
    if (n > 100) n = 100;

    let gap = parseFloat(spacing);
    if (isNaN(gap) || gap < 0) gap = 0;

    // The description provided is: "Any number that has two or more divisors" (a Composite Number).
    // We visually represent this mathematical property by finding the divisors of 'n' 
    // to arrange the image into a perfect grid of exactly 'n' tiles.
    let cols = n;
    let rows = 1;
    
    // Find the pair of divisors closest to the square root to make the most balanced grid
    for (let i = Math.floor(Math.sqrt(n)); i > 0; i--) {
        if (n % i === 0) {
            cols = Math.max(i, n / i);
            rows = Math.min(i, n / i);
            break;
        }
    }

    // Set a target width to keep the output canvas from becoming enormous.
    // We scale the tiles so the final grid width equals the original image width (plus gaps).
    const canvasTargetWidth = originalImg.width;
    
    const totalSpacingX = gap * (cols + 1);
    const tileW = Math.max(1, (canvasTargetWidth - totalSpacingX) / cols);
    
    // Preserve the original image's aspect ratio for the tiles
    const tileH = tileW * (originalImg.height / originalImg.width);
    
    // Final canvas dimensions based on chosen divisors framing
    canvas.width = canvasTargetWidth;
    const totalSpacingY = gap * (rows + 1);
    canvas.height = (tileH * rows) + totalSpacingY;

    // Fill the background (which forms the gaps between tiles)
    ctx.fillStyle = typeof gapColor === 'string' ? gapColor : "#ffffff";
    ctx.fillRect(0, 0, canvas.width, canvas.height);

    // Draw the smaller composite tiles
    for (let r = 0; r < rows; r++) {
        for (let c = 0; c < cols; c++) {
            const x = gap + c * (tileW + gap);
            const y = gap + r * (tileH + gap);
            ctx.drawImage(originalImg, x, y, tileW, tileH);
        }
    }

    return canvas;
}

Free Image Tool Creator

Can't find the image tool you're looking for?
Create one based on your own needs now!

Description

This tool allows users to split a single image into a grid of multiple smaller tiles. Users can customize the number of tiles, the spacing between them, and the color of the gaps. It is useful for creating mosaic-style layouts, social media grid posts, or creative geometric patterns for digital art and design projects.

Leave a Reply

Your email address will not be published. Required fields are marked *