Please bookmark this page to avoid losing your image tool!

Image Fog Filter Effect 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.
function processImage(originalImg, fogColorStr = "200,200,200", fogDensity = 0.5) {
    const canvas = document.createElement('canvas');
    let ctx;

    // Attempt to get 2D context with willReadFrequently hint
    try {
        ctx = canvas.getContext('2d', { willReadFrequently: true });
    } catch (e) {
        // Fallback for older browsers or environments that don't support the options object
        ctx = canvas.getContext('2d');
    }

    // Ensure originalImg has dimensions (i.e., it's loaded and is a valid image)
    const imgWidth = originalImg.naturalWidth;
    const imgHeight = originalImg.naturalHeight;

    if (imgWidth === 0 || imgHeight === 0) {
        console.error("Image has no dimensions (width or height is 0). Ensure the image is loaded and valid before processing.");
        // Return an empty canvas (0x0)
        canvas.width = 0;
        canvas.height = 0;
        return canvas;
    }

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

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

    // Try to get image data; this can fail due to CORS if the image is cross-origin
    let imageData;
    try {
        imageData = ctx.getImageData(0, 0, imgWidth, imgHeight);
    } catch (e) {
        console.error("Could not get image data from canvas. This might be due to CORS policy if the image is cross-origin. Error: ", e);
        // Return the canvas with the original image drawn, but without the effect,
        // as pixel manipulation is not possible.
        return canvas;
    }
    
    const data = imageData.data;

    // Parse and validate fogColorStr
    // Default fog color components (light gray)
    let rFog = 200, gFog = 200, bFog = 200; 
    
    // Ensure fogColorStr is treated as a string for split
    const colorParts = String(fogColorStr).split(',').map(s => parseInt(s.trim(), 10));

    if (colorParts.length === 3 && colorParts.every(c => !isNaN(c))) {
        rFog = Math.max(0, Math.min(255, colorParts[0])); // Clamp R to 0-255
        gFog = Math.max(0, Math.min(255, colorParts[1])); // Clamp G to 0-255
        bFog = Math.max(0, Math.min(255, colorParts[2])); // Clamp B to 0-255
    } else {
        console.warn(`Invalid fogColorStr "${fogColorStr}" provided. Using default RGB(${rFog},${gFog},${bFog}).`);
    }

    // Parse and validate fogDensity
    let density = parseFloat(String(fogDensity)); // Ensure fogDensity is treated as string for parseFloat, then convert to number
    
    if (isNaN(density)) {
        console.warn(`Invalid fogDensity "${fogDensity}" provided. Using default 0.5.`);
        density = 0.5; // Default density if parsing fails
    }
    // Clamp density between 0 (no fog) and 1 (full fog color)
    density = Math.max(0, Math.min(1, density)); 
    
    const invDensity = 1 - density;

    // Apply the fog effect by blending each pixel with the fog color
    for (let i = 0; i < data.length; i += 4) {
        const r = data[i];     // Original Red component
        const g = data[i + 1]; // Original Green component
        const b = data[i + 2]; // Original Blue component
        // data[i + 3] is the Alpha component, which we'll leave unchanged for this effect

        // Blend original pixel color with fog color based on density
        data[i] = Math.round(r * invDensity + rFog * density);     // New Red
        data[i + 1] = Math.round(g * invDensity + gFog * density); // New Green
        data[i + 2] = Math.round(b * invDensity + bFog * density); // New Blue
    }

    // Put the modified pixel data back onto the canvas
    ctx.putImageData(imageData, 0, 0);

    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 Fog Filter Effect Tool allows users to apply a fog effect to images, enhancing their visual appeal by creating a misty overlay. Users can customize the fog’s color and density, providing flexibility for various artistic needs. This tool is ideal for photographers, graphic designers, and hobbyists who wish to add a dreamy or atmospheric quality to their images, making it suitable for landscape photos, digital artwork, or any image where a soft fog effect might enhance the mood.

Leave a Reply

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

Other Image Tools:

Image Ektachrome E100 Filter Effect Application

Image RED Cinema Camera Filter Effect Tool

Image Reverse Graduated ND Filter Effect Tool

Image Kaleidoscope Filter Effect Tool

Image Platinum Palladium Print Filter Effect

Image Light Leak Filter Effect Tool

Image Moire Pattern Filter Effect Tool

Image Pull Processing Filter Effect Tool

Photo Ambrotype Filter Effect Tool

Image Cross-Screen Star Filter Effect Tool

Image Tiffen Ultra Contrast Filter Effect Application

Photo Telephoto Lens Compression Filter Effect Tool

Image Leica M6 Camera Render Filter Effect

Image Cokin Sunset Filter Effect Application

Image CineScope Aspect Ratio Filter Effect Tool

Image Ilford FP4 Plus Filter Effect Tool

Image Infrared 850nm Filter Effect Tool

Image Pentax 67 Medium Format Filter Effect Tool

Image ARRI Alexa Cinema Camera Filter Effect Enhancer

Image Cinestill 50D Filter Effect Application

Image Kodachrome 64 Filter Effect Tool

Photo Lomography Berlin Kino Filter Effect Tool

Image Fujifilm Neopan Filter Effect Tool

Image Polaroid Instant Film Filter Effect Tool

Image Holga Camera Filter Effect Tool

Image Fisheye Lens Distortion Filter Effect Tool

Image Kodak Gold 200 Film Filter Effect Tool

Photo Macro Filter Effect Tool

Image Neutral Density Filter Effect Tool

Image Green Filter Black And White Effect Tool

Image Large Format Film Filter Effect Creator

Image Pinhole Camera Filter Effect Tool

Image Warming Filter Effect Tool

Image Fujifilm Pro 400H Filter Effect Application

Image Diffusion Filter Effect Tool

Image Push-Processed Film Filter Effect Tool

See All →