Please bookmark this page to avoid losing your image tool!

Image Preview With Color Inversion And Flipping Adjustments Tool

(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.
/**
 * Previews an image with optional color inversion and flipping adjustments.
 * This function takes an image and applies horizontal flipping, vertical flipping,
 * and color inversion based on the provided parameters.
 *
 * @param {HTMLImageElement} originalImg - The original image object to process.
 * @param {string} flipHorizontal - A string ('true' or 'false') to indicate if the image should be flipped horizontally. Defaults to 'false'.
 * @param {string} flipVertical - A string ('true' or 'false') to indicate if the image should be flipped vertically. Defaults to 'false'.
 * @param {string} invertColors - A string ('true' or 'false') to indicate if the image colors should be inverted. Defaults to 'false'.
 * @returns {HTMLCanvasElement} A new canvas element displaying the processed image.
 */
function processImage(originalImg, flipHorizontal = 'false', flipVertical = 'false', invertColors = 'false') {
    // Create a new canvas element in memory
    const canvas = document.createElement('canvas');
    const ctx = canvas.getContext('2d');

    // Use naturalWidth/Height to get the original image size, falling back to width/height
    const width = originalImg.naturalWidth || originalImg.width;
    const height = originalImg.naturalHeight || originalImg.height;

    // Set the canvas dimensions to match the image
    canvas.width = width;
    canvas.height = height;

    // Parse the string parameters into boolean values for easier conditional logic
    const doFlipHorizontal = String(flipHorizontal).toLowerCase() === 'true';
    const doFlipVertical = String(flipVertical).toLowerCase() === 'true';
    const doInvertColors = String(invertColors).toLowerCase() === 'true';

    // Save the default state of the canvas context (e.g., transformations, styles)
    ctx.save();

    // --- Apply flipping transformations ---
    const scaleH = doFlipHorizontal ? -1 : 1; // -1 to flip, 1 to keep as is
    const scaleV = doFlipVertical ? -1 : 1;
    const translateX = doFlipHorizontal ? width : 0;  // Translate to the right edge if flipping horizontally
    const translateY = doFlipVertical ? height : 0; // Translate to the bottom edge if flipping vertically

    // Translate the canvas origin to the correct corner before scaling
    ctx.translate(translateX, translateY);
    ctx.scale(scaleH, scaleV);

    // Draw the image onto the transformed canvas.
    // The image is drawn at (0,0) relative to the new, transformed origin.
    ctx.drawImage(originalImg, 0, 0, width, height);

    // Restore the context to its original state (removes all transformations).
    // This is crucial so that subsequent operations like getImageData work on the untransformed canvas coordinates.
    ctx.restore();

    // --- Apply color inversion ---
    if (doInvertColors) {
        // Get the pixel data for the entire canvas
        const imageData = ctx.getImageData(0, 0, width, height);
        const data = imageData.data; // This is a Uint8ClampedArray

        // Iterate through every pixel. Each pixel is represented by 4 array elements (R, G, B, A).
        for (let i = 0; i < data.length; i += 4) {
            // Invert the Red, Green, and Blue channels by subtracting them from the max value (255)
            data[i] = 255 - data[i];       // Red channel
            data[i + 1] = 255 - data[i + 1]; // Green channel
            data[i + 2] = 255 - data[i + 2]; // Blue channel
            // The Alpha channel (data[i + 3]) is left untouched to preserve transparency
        }

        // Write the modified pixel data back to the canvas
        ctx.putImageData(imageData, 0, 0);
    }

    // Return the final canvas element, which can be appended to the DOM
    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 Preview with Color Inversion and Flipping Adjustments Tool allows users to preview images with interactive modifications. It provides options to flip images horizontally or vertically and to invert the colors of the image. This tool is useful for graphic designers or anyone working with images, as it enables quick adjustments to image orientation and color schemes. Users can easily visualize how these changes will affect their images, making it a practical utility for enhancing visual content.

Leave a Reply

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