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

Override devicePixelRatio scaling #110

Closed
Innders opened this issue Dec 12, 2024 · 5 comments
Closed

Override devicePixelRatio scaling #110

Innders opened this issue Dec 12, 2024 · 5 comments
Milestone

Comments

@Innders
Copy link

Innders commented Dec 12, 2024

User Story

You are drawing over a 4K image to make some annotations. The canvas container size is less than 4K, let's say 1080px. Once the drawing has finished you want to export the full image at 4K with annotations included. The canvas must be scaled up to 4K to match the intrinsic size of the image.

The result is you end up with blurry lines.

Solution

window.devicePixelRatio is taken into account to scale up the canvas to ensure it stays sharp. Could this same method be used to scale the canvas by any factor?

It would just require a new configuration argument called dpr.

If you want the annotation to have 4 times the resolution that you are actually drawing at (in the window) then you could pass dpr=4.

Do you think this would work?

@Innders
Copy link
Author

Innders commented Dec 12, 2024

I don't think the change would be too big. Here is an example plus 4 other lines need changing.

  static #setupCanvas(selector, config) {
    // previous code...

    // NEW DPI CONFIG USED HERE
    canvas.width = (config.width || canvas.width) * (config.dpr || window.devicePixelRatio);
    canvas.height = (config.height || canvas.height) * (config.dpr || window.devicePixelRatio);
    canvas.style.touchAction = 'none';

    return canvas;
  }

@jakubfiala
Copy link
Owner

Hey @Innders - sorry I'm only just getting to this, it's been a busy few weeks. I think this is a great idea and I'd love to add it in the next minor version. The only change I'd make to your proposal is to call the config option resolution - that way it's clear it doesn't have to do with the device.

@jakubfiala jakubfiala added this to the 4.5.0 milestone Jan 5, 2025
@jakubfiala
Copy link
Owner

and thank you for the suggestion!

@Innders
Copy link
Author

Innders commented Jan 5, 2025

I totally agree with the option name, I realized dpr was unclear as soon as I started using it.

I tried this out on a fork and it works really well.

@jakubfiala
Copy link
Owner

@Innders the resolution config option has now been added in v4.5.0, which is now published. Thanks for your contribution!

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

No branches or pull requests

2 participants