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

Support high DPI screens #18

Merged
merged 1 commit into from
Nov 3, 2020
Merged

Support high DPI screens #18

merged 1 commit into from
Nov 3, 2020

Conversation

mxdvl
Copy link
Contributor

@mxdvl mxdvl commented Nov 3, 2020

What does this change?

This PR adds support for high-DPI screens. It has been tested locally.

Alternatively, you could use SVGs instead of canvas. I would be happy to create the work for this.

Screenshots

Before After
before after

Click images to see them at full resolution.

@p01
Copy link

p01 commented Nov 3, 2020

It would be easier to size the canvas and form using CSS and the vmin unit ;)

@mxdvl
Copy link
Contributor Author

mxdvl commented Nov 3, 2020

@p01, can you explain a little further?

This proposal is only to render at double or triple resolution, but now changing the actual size on the screen.

vmin relates to the smallest of viewport width and height.

@p01
Copy link

p01 commented Nov 3, 2020

Oh right! So there are two parts to making tixy work well on different screens sizes.

One that you address by update the width and height of the canvas, taking into account the DPR.
The other is about the size of the Canvas, and other elements, in CSS pixels, and that's where vmin ( or similar ) units come into play to make size things relative to the viewport size instead of absolute, and arbitrary, pixel sizes.

output.width = output.height = width;
output.width = output.height = width * dpr;
context.scale(dpr,dpr);
output.style.width = output.style.height = `${width}px`;
Copy link

@p01 p01 Nov 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TL;DR: We should skip the line setting the output.style.foo, and instead size the canvas and form elements using vmin or similar viewport unit.

With this change the size of the canvas will adapt to the DPR but not the actual viewport size. Furthermore, it can end up having a different size than the labels and form inputs below.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's the type of CSS change I mean,

style.styl

html {
  margin: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 90%;
  background: #000;
  color: #fff;
}

body {
  margin: 0;
  width: 64vmin;
  height: 76vmin;
}

* {
  font-size: 2vmin;
  font-family: monospace;
  line-height: 2vmin;
}

canvas,form,input,label {
  padding: 0;
  background: transparent;
  width: 64vmin;
  display: block;
  margin: 0 auto;
  color: inherit;
  border: none; 
}

#output {
  height: 64vmin;
  margin-bottom: 4vmin;
  background: #222;
}

#comment * {
  color: #F24;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these are great suggestions, but probably for a subsequent PR

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair
I filed the PR #19 ;)

@mxdvl
Copy link
Contributor Author

mxdvl commented Nov 3, 2020

Thanks for the suggestions, making tixy work on different screen is outside the scope of this PR, however.

The solution of setting a width and height attributes that get overriden by CSS is documented in several places on the interner as the way handle high-DPI canvas rendering.

2012: https://www.html5rocks.com/tutorials/canvas/hidpi/

2014/17: https://stackoverflow.com/a/26047748

2011/19: https://gist.github.com/joubertnel/870190

2020 (?): https://www.kirupa.com/canvas/canvas_high_dpi_retina.htm

@p01
Copy link

p01 commented Nov 3, 2020

See how the articles you mentioned only change the width and height, but leave the CSS resolution. ;)
We still need to remove the line 22, otherwise the layout will look weird in high DPRs.

@mxdvl
Copy link
Contributor Author

mxdvl commented Nov 3, 2020

@p01 I’ve tested this on Chrome desktop, mobile Safari, Firefox on high DPI devices (DPR: 2) and it works exactly as expected. I’ve added screenshots in the description to showcase.

Whether we set the width: 272px; via external CSS/.styl or inline styles, as with line 22, does not make much difference. I prefer setting it via JS, so it automatically updates if, say, the width were to change in the future.

@aemkei aemkei merged commit f2bfdc4 into aemkei:main Nov 3, 2020
@aemkei
Copy link
Owner

aemkei commented Nov 3, 2020

This looks super crisp! Thank you.

@mxdvl
Copy link
Contributor Author

mxdvl commented Nov 3, 2020

This looks super crisp! Thank you.

Thank YOU! https://tixy.land/?code=Math.sin%28x%2Bt*3%29%2BMath.cos%28y%2Bt*2%29

@mxdvl mxdvl deleted the high-dpi branch November 24, 2023 16:25
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

Successfully merging this pull request may close these issues.

3 participants