Please bookmark this page to avoid losing your image tool!

Photo Dramatic Shadow Filter

(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, shadowIntensity = 0.6, highlightReduction = 0.2, desaturation = 0.7, vignetteStrength = 0.5, vignetteSize = 0.7, vignetteSoftness = 0.5) {
    const canvas = document.createElement('canvas');
    // Use { willReadFrequently: true } for potential performance optimization if supported
    const ctx = canvas.getContext('2d', { willReadFrequently: true }); 
    
    canvas.width = originalImg.naturalWidth || originalImg.width;
    canvas.height = originalImg.naturalHeight || originalImg.height;

    if (canvas.width === 0 || canvas.height === 0) {
        console.error("Image dimensions are zero. Cannot process.");
        // Create a small placeholder or return the empty canvas
        canvas.width = canvas.width || 1;
        canvas.height = canvas.height || 1;
        return canvas;
    }

    ctx.drawImage(originalImg, 0, 0, canvas.width, canvas.height);
    
    let imageData;
    try {
        imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
    } catch (e) {
        console.error("Could not getImageData for image processing. This might be due to CORS restrictions if the image is from a different domain.", e);
        // Return the canvas with the original image drawn, as processing is not possible
        return canvas;
    }
    
    const data = imageData.data;

    const centerX = canvas.width / 2;
    const centerY = canvas.height / 2;
    // maxDistToCorner is used for normalizing distance for vignette calculation
    const maxDistToCorner = (canvas.width === 0 && canvas.height === 0) ? 1 : Math.sqrt(centerX * centerX + centerY * centerY);


    // Clamp input parameters to their expected ranges
    const si = Math.max(0, Math.min(1, shadowIntensity));
    const hr = Math.max(0, Math.min(1, highlightReduction));
    const desat = Math.max(0, Math.min(1, desaturation));
    const vigStr = Math.max(0, Math.min(1, vignetteStrength));
    const vigSize = Math.max(0.01, Math.min(1, vignetteSize)); // Must be > 0
    const vigSoft = Math.max(0.01, Math.min(1, vignetteSoftness)); // Must be > 0

    // Effect parameters derived from inputs
    const brightnessAdjust = -si * 50; // e.g., 0 to -50. More negative enhances shadows.
    const contrastParam = si * 120;    // Mapped to actual contrast value, e.g., 0 to 120 (max typical user range is ~127)
    // Standard contrast formula factor.
    const contrastFactor = (259 * (contrastParam + 255)) / (255 * (259 - contrastParam));

    // Helper function for smoothstep, often used in graphics for smooth transitions
    function smoothStep(edge0, edge1, x) {
        // Scale, saturate x to 0..1 range
        x = Math.max(0, Math.min(1, (x - edge0) / (edge1 - edge0)));
        // Evaluate polynomial
        return x * x * (3 - 2 * x);
    }

    for (let i = 0; i < data.length; i += 4) {
        let r = data[i];
        let g = data[i + 1];
        let b = data[i + 2];

        // 1. Desaturation
        if (desat > 0) {
            const L = 0.299 * r + 0.587 * g + 0.114 * b; // Calculate Luminance
            r = r * (1 - desat) + L * desat;
            g = g * (1 - desat) + L * desat;
            b = b * (1 - desat) + L * desat;
        }

        // 2. Highlight Reduction (applied before major contrast/brightness shift)
        if (hr > 0) {
            // This formula reduces brighter pixels more aggressively.
            // Max reduction of (hr * 0.8 * 255) for pure white.
            r = r * (1 - (r / 255.0) * hr * 0.8); 
            g = g * (1 - (g / 255.0) * hr * 0.8);
            b = b * (1 - (b / 255.0) * hr * 0.8);
        }
        
        // 3. Dramatic Shadows: Apply global brightness adjustment then contrast
        // Global Brightness adjustment (makes everything darker, enhancing shadow perception)
        r += brightnessAdjust;
        g += brightnessAdjust;
        b += brightnessAdjust;

        // Clamp after brightness, before applying contrast
        r = Math.max(0, Math.min(255, r));
        g = Math.max(0, Math.min(255, g));
        b = Math.max(0, Math.min(255, b));

        // Contrast adjustment (makes darks darker, lights lighter relative to mid-point 128)
        if (si > 0) { // Only apply contrast if shadowIntensity requests it
            r = contrastFactor * (r - 128) + 128;
            g = contrastFactor * (g - 128) + 128;
            b = contrastFactor * (b - 128) + 128;
        }
        
        // Clamp after contrast
        r = Math.max(0, Math.min(255, r));
        g = Math.max(0, Math.min(255, g));
        b = Math.max(0, Math.min(255, b));

        // 4. Vignette
        if (vigStr > 0 && maxDistToCorner > 0.001) { // Apply vignette if strength > 0 and image has dimensions
            const x = (i / 4) % canvas.width;
            const y = Math.floor((i / 4) / canvas.width);
            
            const dx = x - centerX;
            const dy = y - centerY;
            const distFromCenter = Math.sqrt(dx * dx + dy * dy);
            const relativeDist = distFromCenter / maxDistToCorner; // Normalized distance: 0 at center, 1 at corners

            // R_clear: relative radius from center UP TO which the image is unaffected by vignette.
            // vignetteSize (viz) maps to this. Larger viz = larger bright central area.
            const R_clear = vigSize; 
            
            // transitionWidth: width of the band where vignette fades from 0 to full strength.
            // Controlled by vignetteSoftness (vigSoft). Larger vigSoft = wider, softer transition.
            // The transition occurs over the remaining distance (1.0 - R_clear).
            const transitionWidth = (1.0 - R_clear) * vigSoft;
            
            const edge0_darkening = R_clear; // Vignette starts getting applied beyond this point.
            let edge1_darkening = R_clear + transitionWidth; // Vignette is at full strength beyond this.

            edge1_darkening = Math.min(1.0, edge1_darkening); // Cap at 1.0 (image edge).

            // Handle edge cases for smoothStep: edge0 must be < edge1.
            if (edge0_darkening >= edge1_darkening) {
                 // This typically means R_clear is 1.0 (no vignette) or softness is causing edge case.
                if (R_clear >= 1.0) { // Fully clear, no vignette effect needed.
                     edge0_darkening = 1.0; edge1_darkening = 1.00001; // Ensure darknessAmount is 0 for relDist <= 1
                } else { // Effectively a sharp edge vignette if transitionWidth is zero or negative.
                    edge1_darkening = edge0_darkening + 0.00001; // Minimal transition for smoothStep logic
                }
            }
            
            // darknessAmount: 0 if relativeDist < edge0_darkening (inside clear area)
            //                 1 if relativeDist > edge1_darkening (in full vignette area)
            //                 smooth transition between edge0 and edge1
            const darknessAmount = smoothStep(edge0_darkening, edge1_darkening, relativeDist);
            const vignetteMultiplier = 1.0 - darknessAmount * vigStr; // 1.0 (no effect) to (1.0 - vigStr)
            
            r *= vignetteMultiplier;
            g *= vignetteMultiplier;
            b *= vignetteMultiplier;
        }

        // Final clamping for safety, though previous clamps should mostly handle it.
        data[i] = Math.max(0, Math.min(255, r));
        data[i + 1] = Math.max(0, Math.min(255, g));
        data[i + 2] = Math.max(0, Math.min(255, b));
        // Alpha channel (data[i+3]) is preserved.
    }

    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 Photo Dramatic Shadow Filter is an image enhancement tool designed to apply dramatic shadow effects to photographs, increasing their overall visual impact. Users can adjust various parameters, including shadow intensity, highlight reduction, desaturation, and vignette effects. This tool is ideal for photographers and designers looking to add depth and mood to their images, making it suitable for creative projects, social media content, and personal photo editing.

Leave a Reply

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