Please bookmark this page to avoid losing your image tool!

Image Circular Crop 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.
/**
 * Creates a circular cropped version of an image.
 *
 * @param {HTMLImageElement} originalImg The original image object (must be loaded for naturalWidth/Height to be correct).
 * @param {string|number} [centerX = originalImg.naturalWidth / 2] X-coordinate of the crop center in the original image.
 * @param {string|number} [centerY = originalImg.naturalHeight / 2] Y-coordinate of the crop center in the original image.
 * @param {string|number} [radius = Math.min(originalImg.naturalWidth, originalImg.naturalHeight) / 2] Radius of the circular crop.
 * @returns {HTMLCanvasElement} A canvas element displaying the circularly cropped image.
 */
function processImage(
    originalImg,
    centerX = originalImg.naturalWidth / 2,
    centerY = originalImg.naturalHeight / 2,
    radius = Math.min(originalImg.naturalWidth, originalImg.naturalHeight) / 2
) {
    // Parse numeric parameters. These could be numbers or strings (e.g., "100").
    // parseFloat will convert valid numeric strings to numbers, and leave numbers as numbers.
    // If a parameter was not provided (i.e., it's `undefined`), its default value from the signature is used.
    // parseFloat on a numeric default value works fine.
    let numCenterX = parseFloat(centerX);
    let numCenterY = parseFloat(centerY);
    let numRadius = parseFloat(radius);

    // Fallback to default calculations if parsing failed (e.g., input was a non-numeric string like "abc")
    // or if the radius is invalid (e.g., negative).
    // The default values from the function signature are based on originalImg dimensions.
    if (isNaN(numCenterX)) {
        numCenterX = originalImg.naturalWidth / 2;
    }
    if (isNaN(numCenterY)) {
        numCenterY = originalImg.naturalHeight / 2;
    }
    // A radius of 0 is permissible (e.g., for a 0x0 source image), which will result in a 0x0 canvas.
    // A negative radius is invalid and should be reset to the default.
    if (isNaN(numRadius) || numRadius < 0) {
        numRadius = Math.min(originalImg.naturalWidth, originalImg.naturalHeight) / 2;
    }

    const canvas = document.createElement('canvas');
    // The canvas will be a square with side length equal to the diameter of the circle (2 * radius).
    canvas.width = numRadius * 2;
    canvas.height = numRadius * 2;

    const ctx = canvas.getContext('2d');

    // If context cannot be obtained (highly unlikely for '2d' in modern browsers)
    if (!ctx) {
        console.error("Could not get 2D rendering context for the canvas.");
        // Return an empty or minimal canvas as per error handling strategy
        return canvas; // Returns the 0x0 or un-drawable canvas
    }
    
    // If the radius is 0 (e.g., source image is 0x0, or user explicitly specified radius 0),
    // the canvas will be 0x0. No drawing operations are needed or effectively possible.
    if (numRadius === 0) {
        return canvas;
    }
    
    // Create a circular clipping path.
    // The center of the circle on the canvas is at (numRadius, numRadius)
    // because the canvas dimensions are (2*numRadius x 2*numRadius) and origin is (0,0).
    ctx.beginPath();
    ctx.arc(numRadius, numRadius, numRadius, 0, Math.PI * 2, true); // (cx_on_canvas, cy_on_canvas, r_on_canvas, startAngle, endAngle, anticlockwise)
    ctx.clip();

    // Calculate the source rectangle (sx, sy, sWidth, sHeight) from the original image.
    // This is a square region centered at (numCenterX, numCenterY) in the original image's coordinate system,
    // with side length equal to the diameter (2 * numRadius).
    const sx = numCenterX - numRadius; // Top-left X-coordinate of the source rectangle
    const sy = numCenterY - numRadius; // Top-left Y-coordinate of the source rectangle
    const sWidth = numRadius * 2;      // Width of the source rectangle
    const sHeight = numRadius * 2;     // Height of the source rectangle

    // Define the destination rectangle (dx, dy, dWidth, dHeight) on the canvas.
    // This covers the entire canvas.
    const dx = 0;                      // Top-left X-coordinate on the canvas
    const dy = 0;                      // Top-left Y-coordinate on the canvas
    const dWidth = canvas.width;       // Width of the destination rectangle on canvas (equal to 2*numRadius)
    const dHeight = canvas.height;     // Height of the destination rectangle on canvas (equal to 2*numRadius)

    // Draw the specified portion of the original image onto the (now clipped) canvas.
    // The drawImage method handles cases where the source rectangle (sx, sy, sWidth, sHeight)
    // extends beyond the actual dimensions of originalImg by rendering those areas as transparent.
    ctx.drawImage(originalImg, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight);

    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 Circular Crop Tool allows users to create a circular cropped version of an image. This tool is particularly useful for generating profile pictures, icons, or any visual elements that require a circular format. Users can define the center and radius of the crop to customize the output to their preferences. This functionality can be beneficial for designers, social media content creators, or anyone looking to enhance images by focusing on specific circular sections.

Leave a Reply

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

Other Image Tools:

Image Date Stamp Adder

Image Circular Guides Adder

Image Center Cropper

Image Petzval Lens Swirly Bokeh Effect Creator

Image Mimiya 645 Medium Format Filter Effect Tool

Photo Fujifilm Klasse W Filter Effect Application

Image Deardorff Large Format Filter Effect Application

Image Lomo LC-A Filter Effect Tool

Image Large Format Filter Effect Application

Image Zone Plate Lens Effect Creator

Photo Kodak Retina Filter Effect Tool

Image Polaroid 600 Filter Effect Tool

Photo Black and White Yellow Filter Effect Tool

Image Contax G2 Film Camera Render Effect Applicator

Image 110 Film Format Filter Effect Tool

Photo Jupiter-9 Portrait Lens Filter Effect

Image Fujifilm GW690 Texas Leica Filter Effect Application

Image Zeiss T* Coating Filter Effect Tool

Image Hoya R72 Infrared Filter Effect Tool

Image Filter Effect for Zeiss Ikon Contaflex

Photo Olympus Mju-II/Stylus Epic Filter Effect Tool

Image NiSi Nano IR ND Filter Effect Tool

Image Polaroid SX-70 Filter Effect Tool

Image Linhof Technika Filter Effect Tool

Image Lee Big Stopper 10-Stop ND Filter Effect Tool

Image Minolta X-700 Film Camera Render Effect Creator

Image ORWO UN54 Motion Picture Film Effect Applicator

Image Shen-Hao Large Format Filter Effect Tool

Image Impossible Project Polaroid Filter Effect Tool

Photo Foma Retropan 320 Film Filter Effect Tool

Image Fuji QuickSnap Disposable Filter Effect Application

Image 220 Film Format Filter Effect

Image Black and White with Green #61 Filter Effect Tool

Image 35mm Panoramic Camera Filter Effect Tool

Image Hitech Firecrest ND Filter Effect Formatter

Photo Rodenstock Digital Vario ND Filter Effect Tool

See All →