Please bookmark this page to avoid losing your image tool!

Image Grid Line Adder

(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, gridColor = "black", gridLineWidth = 1, horizontalLines = 10, verticalLines = 10) {
    const canvas = document.createElement('canvas');
    const ctx = canvas.getContext('2d');

    // Use naturalWidth and naturalHeight for the true dimensions of the image data
    const imgWidth = originalImg.naturalWidth;
    const imgHeight = originalImg.naturalHeight;

    // Handle cases where the image might not be fully loaded or has no dimensions
    if (imgWidth === 0 || imgHeight === 0) {
        // Set canvas to 0x0 and return. A warning is helpful for the developer.
        canvas.width = 0;
        canvas.height = 0;
        console.warn("Image Grid Line Adder: Original image has zero dimensions (width or height is 0). " +
                     "Ensure the image is fully loaded and has valid dimensions before processing.");
        return canvas;
    }

    canvas.width = imgWidth;
    canvas.height = imgHeight;

    // Draw the original image onto the canvas
    ctx.drawImage(originalImg, 0, 0, imgWidth, imgHeight);

    // Set grid line properties
    ctx.strokeStyle = gridColor;
    
    // According to the HTML Canvas 2D Context specification, values for lineWidth must be positive.
    // If gridLineWidth is 0 or negative, the .lineWidth assignment is ignored, 
    // and ctx.lineWidth retains its current value (which is 1.0 for a new canvas context).
    // Thus, a gridLineWidth <= 0 will effectively result in a 1px line.
    // This is usually acceptable. If the user intends "no line visible for 0 width", 
    // they should set the horizontalLines and/or verticalLines parameters to 0.
    // Users can also pass fractional values like 0.5 for thinner lines if supported by the browser.
    ctx.lineWidth = gridLineWidth;

    // Floor the number of lines and ensure they are not negative.
    // This handles cases like decimal inputs (e.g., 10.5 lines) or negative inputs.
    const numHorizontalLines = Math.max(0, Math.floor(horizontalLines));
    const numVerticalLines = Math.max(0, Math.floor(verticalLines));

    // Draw horizontal lines
    // Only proceed if there's at least one line to draw and the image has positive height.
    if (numHorizontalLines > 0 && imgHeight > 0) {
        const horizontalSpacing = imgHeight / (numHorizontalLines + 1);
        for (let i = 1; i <= numHorizontalLines; i++) {
            let y = i * horizontalSpacing;
            
            // For 1px lines (where ctx.lineWidth is exactly 1.0), 
            // adjust coordinate by +0.5 to ensure a crisp line aligned with pixel boundaries.
            // This avoids anti-aliasing that can make the line appear blurry.
            if (ctx.lineWidth === 1.0) {
                y = Math.floor(y) + 0.5;
            }
            // For other line widths (thicker, thinner, or fractional), 
            // using the calculated y directly is generally fine. Pixel-snapping for these
            // cases can be more complex and might not always be desired.

            ctx.beginPath();
            ctx.moveTo(0, y);
            ctx.lineTo(imgWidth, y);
            ctx.stroke();
        }
    }

    // Draw vertical lines
    // Only proceed if there's at least one line to draw and the image has positive width.
    if (numVerticalLines > 0 && imgWidth > 0) {
        const verticalSpacing = imgWidth / (numVerticalLines + 1);
        for (let i = 1; i <= numVerticalLines; i++) {
            let x = i * verticalSpacing;

            // Apply the same crisp line adjustment for 1px vertical lines.
            if (ctx.lineWidth === 1.0) {
                x = Math.floor(x) + 0.5;
            }

            ctx.beginPath();
            ctx.moveTo(x, 0);
            ctx.lineTo(x, imgHeight);
            ctx.stroke();
        }
    }

    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

The Image Grid Line Adder is a tool that enables users to enhance their images by adding a customizable grid overlay. Users can specify the color and width of the grid lines, as well as the number of horizontal and vertical lines to be drawn. This can be particularly useful for artists, designers, and educators who need to create reference lines for precision, organize layouts, or facilitate visual analysis. Whether for preparing images for presentations, artworks, or educational materials, this tool provides an easy way to incorporate grid lines into any image.

Leave a Reply

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