Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Canvas Loop Behavior Affected by Browser Zoom and Image Size #5

Open
larocquedylan opened this issue Dec 24, 2023 · 1 comment
Open

Comments

@larocquedylan
Copy link
Owner

The canvas loop functionality is inconsistently affected by the browser's zoom level and the original size of the image. This issue results in improper looping of the canvas under certain conditions and this impacts the output of the canvas drawing.

Potential Solution
A proposed solution is to implement a mechanism where:

The image file is read initially.
The size of the image is determined.
The loop parameters are then adjusted based on the image's size.
This approach should theoretically allow the loop to function correctly regardless of the browser zoom level or the image size.

@larocquedylan
Copy link
Owner Author

Did some work on this yesterday...

const drawAsciiImage = async () => {
    const photo = await loadImage(imageSrc);

    // Uniform Scaling & Dynamic Resizing:
    // Calculate aspect ratio of the photo
    const aspectRatio = photo.width / photo.height;
    let resizedWidth, resizedHeight;

    // Adjust resizedWidth and resizedHeight based on aspect ratio to maintain uniform scaling
    if (width / height > aspectRatio) {
        // If canvas is wider relative to its height, adjust width based on height
        resizedHeight = height;
        resizedWidth = height * aspectRatio;
    } else {
        // If canvas is taller relative to its width, adjust height based on width
        resizedWidth = width;
        resizedHeight = width / aspectRatio;
    }

    // Set canvas dimensions
    canvas.width = width;
    canvas.height = height;

    // Handle Zoom Levels:
    // Calculate the dimensions of each 'pixel' on the canvas
    // This is influenced by the size of the canvas and the resized image
    const w = resizedWidth / width; // Width of each 'pixel' in the ASCII art
    const h = resizedHeight / height; // Height of each 'pixel' in the ASCII art

    // Draw the image onto the canvas at the resized dimensions
    ctx.drawImage(photo, 0, 0, resizedWidth, resizedHeight);
   
   // 
   //
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant