Please bookmark this page to avoid losing your image tool!

Image Fibonacci Sequence Generator

(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 Fibonacci spiral visualization using an image as a texture.
 *
 * This function generates a visual representation of the Fibonacci sequence
 * in the form of a spiral composed of nested squares and quarter-circle arcs.
 * The provided image is used as a background fill for the entire spiral shape.
 *
 * @param {Image} originalImg The original image object. It will be used as a background texture for the spiral shape.
 * @param {number} iterations The number of squares/arcs in the spiral. Recommended range: 2-15. Defaults to 10.
 * @param {string} spiralColor The color of the spiral line (e.g., '#FF0000', 'rgba(0,0,0,0.8)'). Defaults to '#000000'.
 * @param {string} boxColor The color of the borders of the Fibonacci squares. Defaults to '#CCCCCC'.
 * @param {number} showBoxes Set to 1 to show the squares, 0 to hide them. Defaults to 1.
 * @param {number} lineWidth The thickness of the spiral and box lines. Defaults to 2.
 * @param {string} startDirection The starting direction of the spiral. Can be 'right', 'up', 'left', or 'down'. Defaults to 'right'.
 * @param {string} backgroundColor The background color of the canvas, visible outside the spiral. Defaults to '#FFFFFF'.
 * @returns {HTMLCanvasElement} A canvas element displaying the Fibonacci spiral.
 */
function processImage(originalImg, iterations = 10, spiralColor = '#000000', boxColor = '#CCCCCC', showBoxes = 1, lineWidth = 2, startDirection = 'right', backgroundColor = '#FFFFFF') {

    // 1. Parameter validation and setup
    const validIterations = Math.max(2, Math.min(15, parseInt(iterations) || 10));
    const shouldShowBoxes = !!Number(showBoxes);
    const dirMap = { 'right': 0, 'up': 1, 'left': 2, 'down': 3 };
    const startDirIndex = dirMap[(startDirection || 'right').toLowerCase()] || 0;

    // 2. Generate Fibonacci numbers
    const fib = [0, 1];
    for (let i = 2; i <= validIterations + 1; i++) {
        fib.push(fib[i - 1] + fib[i - 2]);
    }

    // 3. Pre-computation run to calculate layout and bounding box
    const points = [];
    let comp = { x: 0, y: 0, w: 0, h: 0 }; // The overall composition's bounding box

    // Manually place the first square to initialize the composition
    const r1 = fib[2]; // This is the first '1'
    points.push({ sx: 0, sy: 0, r: r1, dir: (0 + startDirIndex) % 4 });
    comp = { x: 0, y: 0, w: r1, h: r1 };

    for (let i = 2; i <= validIterations; i++) {
        const r = fib[i + 1];
        const dir = (i - 1 + startDirIndex) % 4;
        let sx, sy;

        // This logic correctly places the new square adjacent to the previous composition
        switch (dir) {
            case 0: // Add square to the Right
                sx = comp.x + comp.w;
                sy = comp.y;
                comp.w += r;
                comp.h = Math.max(comp.h, r);
                break;
            case 1: // Add square Up
                sx = comp.x + comp.w - r;
                sy = comp.y - r;
                comp.y -= r;
                comp.h += r;
                break;
            case 2: // Add square to the Left
                sx = comp.x - r;
                sy = comp.y + comp.h - r;
                comp.x -= r;
                comp.w += r;
                break;
            case 3: // Add square Down
                sx = comp.x;
                sy = comp.y + comp.h;
                comp.h += r;
                break;
        }
        points.push({ sx, sy, r, dir });
    }

    // 4. Create Canvas
    const padding = Math.max(10, lineWidth * 2);
    const canvas = document.createElement('canvas');
    const ctx = canvas.getContext('2d');
    canvas.width = comp.w + padding * 2;
    canvas.height = comp.h + padding * 2;
    const offsetX = -comp.x + padding;
    const offsetY = -comp.y + padding;

    // 5. Draw Background and Image Texture
    ctx.fillStyle = backgroundColor;
    ctx.fillRect(0, 0, canvas.width, canvas.height);

    ctx.save();
    ctx.beginPath();
    // Create a path representing the outer bounds of the entire spiral
    points.forEach(p => ctx.rect(p.sx + offsetX, p.sy + offsetY, p.r, p.r));
    ctx.clip(); // Clip all subsequent drawing to this path
    
    // Fill the clipped region with a pattern made from the input image
    const pattern = ctx.createPattern(originalImg, 'repeat');
    if (pattern) {
        ctx.fillStyle = pattern;
        ctx.fillRect(0, 0, canvas.width, canvas.height);
    }
    ctx.restore(); // Remove the clipping path

    // 6. Draw the spiral and boxes on top of the texture
    ctx.lineWidth = lineWidth;
    ctx.lineCap = 'round';

    points.forEach(p => {
        const { sx, sy, r, dir } = p;
        const x = sx + offsetX;
        const y = sy + offsetY;

        // Draw the containing square
        if (shouldShowBoxes && r > 0) {
            ctx.strokeStyle = boxColor;
            ctx.strokeRect(x, y, r, r);
        }

        // Draw the arc
        if (r > 0) {
            ctx.beginPath();
            ctx.strokeStyle = spiralColor;
            let arcX, arcY, startAngle, endAngle;

            // Determine arc parameters based on the direction the square was added
            switch (dir) {
                case 0: // Right
                    arcX = x; arcY = y + r;
                    startAngle = 1.5 * Math.PI; endAngle = 2 * Math.PI;
                    break;
                case 1: // Up
                    arcX = x + r; arcY = y + r;
                    startAngle = Math.PI; endAngle = 1.5 * Math.PI;
                    break;
                case 2: // Left
                    arcX = x + r; arcY = y;
                    startAngle = 0.5 * Math.PI; endAngle = Math.PI;
                    break;
                case 3: // Down
                    arcX = x; arcY = y;
                    startAngle = 0; endAngle = 0.5 * Math.PI;
                    break;
            }
            ctx.arc(arcX, arcY, r, startAngle, endAngle);
            ctx.stroke();
        }
    });

    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 Fibonacci Sequence Generator is a tool that creates a visually appealing Fibonacci spiral using a chosen image as a texture. By inputting an image, users can generate a spiral structure that illustrates the Fibonacci sequence with customizable parameters, such as the number of iterations, spiral color, box color, line width, and starting direction. This tool is useful for educators, artists, and designers looking to visualize mathematical concepts in a creative way or to create unique artistic representations that combine geometry and imagery.

Leave a Reply

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

Other Image Tools:

Image Fibonacci Pattern Generator

Image Background and White Area Remover

Image Line Eraser with Thickness and Direction Options

Image Transparent Line Editor

Image Line Eraser With Skip Options

Image Nth Line Eraser

Image To SVG Converter

Image Digitizer From Scan

Image Design for 10×4 Meter Landscape LED Screen with Running Video

Photo Custom Text Editor for Identification Cards

Image Outline Detection and Marking Tool

Image Diagonal Golden Ratio Overlay Tool

Image Line Drawer and Eraser

Image To Binary Converter For Optimized Storage

Image Japanese Anime Cell Shading Tool

Image Japanese Anime Cel Shading Renderer for Military Vehicles

Image Anime Cel Shade Effect Generator

Image Liquid Metallic Chrome Material Top View

Image Generator for Rainbow Six Siege Logo with Rainbow Fill

Image Transparency Adjuster for Clothing

Photo VHS Found Footage Analog Effect Tool

Image Old Fashioned Wanted Poster Creator

Image Toxic Waste Identifier

Image Realism Enhancer

Image Bulk Date and Location Stamp Adder Without Background Color

Image Time Stamp Removal Tool

Image Bulk Date and Text Stamp Adder

Image Bulk Date and Location Stamp Adder

Image Date and Location Stamp Adder

Image Date and Zone Stamping Tool

Image Bulk Date and Location Stamper

Image Bulk Date and Coordinate Stamper

Photo Artificial Metadata Generator

Photo Artificial Pattern Generator for Deepfake Bypass

Image Ultra Realistic Skin Texture Pore Emulation Tool

Image Chaotic Noise and Blur Generator for Deepfake Detection Evasion

See All →