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

set dpr on the canvas, for optimal quality #55

Open
catdad opened this issue Feb 22, 2019 · 1 comment
Open

set dpr on the canvas, for optimal quality #55

catdad opened this issue Feb 22, 2019 · 1 comment
Labels
feature request A neat new idea for this project good first issue Good for newcomers

Comments

@catdad
Copy link
Owner

catdad commented Feb 22, 2019

function setupCanvas(canvas) {
  // Get the device pixel ratio, falling back to 1.
  var dpr = window.devicePixelRatio || 1;
  // Get the size of the canvas in CSS pixels.
  var rect = canvas.getBoundingClientRect();
  // Give the canvas pixel dimensions of their CSS
  // size * the device pixel ratio.
  canvas.width = rect.width * dpr;
  canvas.height = rect.height * dpr;
  var ctx = canvas.getContext('2d');
  // Scale all drawing operations by the dpr, so you
  // don't have to worry about the difference.
  ctx.scale(dpr, dpr);
  return ctx;
}

// Now this line will be the same size on the page
// but will look sharper on high-DPI devices!
var ctx = setupCanvas(document.querySelector('.my-canvas'));
ctx.lineWidth = 5;
ctx.beginPath();
ctx.moveTo(100, 100);
ctx.lineTo(200, 200);
ctx.stroke();

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

@catdad catdad added feature request A neat new idea for this project good first issue Good for newcomers labels Feb 22, 2019
@nelsonvassalo
Copy link

Probem with the solution: breaks all further scaling operations further down on canvas

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request A neat new idea for this project good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants