Please bookmark this page to avoid losing your image tool!

Star-Shaped Image Cropper

(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, numStarPointsStr = "5", pointinessStr = "0.4") {
    // originalImg is expected to be a fully loaded HTMLImageElement, HTMLCanvasElement,
    // HTMLVideoElement, ImageBitmap, or other valid CanvasImageSource,
    // with positive, non-zero width and height properties.

    // 1. Parameter parsing and validation
    let numPoints = parseInt(numStarPointsStr, 10);
    // A star needs at least 3 points (e.g., a triangle with pinched sides can be a "3-pointed star").
    if (isNaN(numPoints) || numPoints < 3) {
        numPoints = 5; // Default to 5 points if input is invalid
    }

    let pointinessRatio = parseFloat(pointinessStr);
    // PointinessRatio defines how "sharp" the star points are. It's innerRadius / outerRadius.
    // It must be between 0 (exclusive) and 1 (exclusive).
    // A value too close to 0 would make spikes very thin (innerRadius approaches 0).
    // A value too close to 1 would make the star look like a regular polygon (innerRadius approaches outerRadius).
    if (isNaN(pointinessRatio) || pointinessRatio <= 0.01 || pointinessRatio >= 0.99) {
        pointinessRatio = 0.4; // Default pointiness (0.4 is common for a 5-point star)
    }

    // 2. Canvas setup
    const canvas = document.createElement('canvas');
    const ctx = canvas.getContext('2d');

    // Determine the dimension for the square crop area.
    // The star will be inscribed within this square.
    // This dimension is taken from the smaller side of the original image to ensure the star fits.
    let cropDim = Math.min(originalImg.width, originalImg.height);

    // Ensure the dimension is at least 1px, as canvas width/height must be >= 1.
    if (cropDim <= 0) {
        // If the image has no valid dimensions (e.g., width or height is 0 or negative),
        // return a minimal 1x1 transparent canvas. This handles uninitialized/unloaded images.
        canvas.width = 1;
        canvas.height = 1;
        return canvas;
    }

    canvas.width = cropDim;
    canvas.height = cropDim;
    
    // Use 'size' for clarity in geometric calculations related to the canvas.
    const size = cropDim;

    // 3. Star geometry parameters
    const centerX = size / 2;
    const centerY = size / 2;
    const outerRadius = size / 2; // Star's outer points will touch the edges of the canvas.
    const innerRadius = outerRadius * pointinessRatio;

    // 4. Create the star path
    ctx.beginPath();
    for (let i = 0; i < 2 * numPoints; i++) {
        // Calculate the angle for each vertex of the star.
        // There are `2 * numPoints` vertices in total (outer points and inner "valleys").
        // `i * Math.PI / numPoints` ensures points are evenly angularly spaced.
        // `- Math.PI / 2` subtraction rotates the star so that the first point is at the top center.
        const angle = (i * Math.PI) / numPoints - (Math.PI / 2);
        
        // Alternate between outerRadius (for star points) and innerRadius (for star valleys)
        const radius = (i % 2 === 0) ? outerRadius : innerRadius;
        
        const x = centerX + radius * Math.cos(angle);
        const y = centerY + radius * Math.sin(angle);

        if (i === 0) {
            ctx.moveTo(x, y); // Move to the first point of the star
        } else {
            ctx.lineTo(x, y); // Draw a line to subsequent points
        }
    }
    ctx.closePath(); // Close the path to form a closed polygon (the star shape)

    // 5. Clip the drawing context to the star path
    // Any subsequent drawing operations will only affect pixels within this defined star path.
    ctx.clip();

    // 6. Draw the image onto the canvas
    // The source rectangle (sx, sy, sWidth, sHeight) is from the original image parts.
    // This will be a square of 'size' dimensions, centered within the original image.
    const sx = (originalImg.width - size) / 2;
    const sy = (originalImg.height - size) / 2;
    // const sWidth = size; // Source width for cropping from originalImg
    // const sHeight = size; // Source height for cropping from originalImg

    // The destination rectangle (dx, dy, dWidth, dHeight) on the canvas.
    // This covers the entire canvas, as the canvas itself is 'size' x 'size'.
    // const dx = 0;
    // const dy = 0;
    // const dWidth = size; // Destination width on canvas
    // const dHeight = size; // Destination height on canvas

    try {
        // Draw the calculated square portion of the original image into the
        // star-clipped area of the canvas.
        ctx.drawImage(originalImg, sx, sy, size, size, 0, 0, size, size);
    } catch (e) {
        // This catch block handles potential errors during drawImage.
        // Errors can occur if:
        // - The image isn't fully loaded.
        // - The image is CORS-tainted and its use is restricted.
        // - Other miscellaneous drawImage errors.
        console.error("Error drawing image in processImage function:", e);
        // If an error occurs, the star-clipped area on the canvas will remain transparent.
        // This is often a reasonable default behavior for a graceful failure.
        // Optionally, one could fill the star with a placeholder color:
        // ctx.fillStyle = 'rgba(128, 128, 128, 0.3)'; // A semi-transparent gray
        // ctx.fill(); // This fill would respect the clip path.
    }

    // 7. Return the canvas element
    // This canvas now contains the original image cropped into a star shape.
    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 Star-Shaped Image Cropper is an online tool that allows users to crop their images into a unique star shape. With customizable features, users can specify the number of points on the star and adjust the pointiness to create different star designs. This tool is perfect for creating visually striking graphics for social media posts, website designs, invitations, and other creative projects where a star-shaped image is desired. Simply upload your image, choose your star shape preferences, and the tool will generate the cropped image for you.

Leave a Reply

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