Git Product home page Git Product logo

Comments (5)

jondavidjohn avatar jondavidjohn commented on August 15, 2024

Seems like on resize you'll want to trigger a redraw, if you re-call .getContext('2d') it should adjust the dimensions accordingly.

Though specifically this is definitely outside the scope of the tool.

from hidpi-canvas-polyfill.

sannefoltz avatar sannefoltz commented on August 15, 2024

Doesn't seem to have any effect :/

/* previous code... */

//causing the trouble
$('#thing').attr('width',$('#thing').parent().width());

ctx = $('#thing').get(0).getContext("2d");

from hidpi-canvas-polyfill.

jondavidjohn avatar jondavidjohn commented on August 15, 2024

What about this?

  var ctx = $('#thing').get(0).getContext("2d");
  ctx.beginPath();
  ctx.arc(150, 150, 100, 0, 2* Math.PI, false);
  ctx.strokeStyle = "red";
  ctx.lineWidth = 40; 
  ctx.stroke();

  //causing the trouble
  $('#thing').attr('width',$('#thing').parent().width());


  // now actually redraw stuff
  var ctx = $('#thing').get(0).getContext("2d");
  ctx.beginPath();
  ctx.arc(150, 150, 100, 0, 2* Math.PI, false);
  ctx.strokeStyle = "red";
  ctx.lineWidth = 40; 
  ctx.stroke();

Though you probably need to clear the canvas before redrawing.

from hidpi-canvas-polyfill.

sannefoltz avatar sannefoltz commented on August 15, 2024

I isolated it a bit. This is resulting in a quarter circle on a 2x device but normal on a 1x device.

<canvas id="thing" height="300" width="300"></canvas>
<script>
    document.getElementById('thing').setAttribute('width',100);
    document.getElementById('thing').setAttribute('height',100);

    var ctx = document.getElementById('thing').getContext("2d");
    ctx.beginPath();
    ctx.arc(ctx.canvas.width/2, ctx.canvas.height/2, 
        ctx.canvas.width/4, 0, 2* Math.PI, false);
    ctx.strokeStyle = "red";
    ctx.lineWidth = ctx.canvas.width/6;
    ctx.stroke();
</script>

Off topic; but I actually think browsers should handle all of this.

from hidpi-canvas-polyfill.

jondavidjohn avatar jondavidjohn commented on August 15, 2024

Sooooo, I believe what is happening is that you're using the canvas.height and canvas.width to calculate the inputs here.

In the polyfill, I modify these values behind the scenes so they are actually double what you expect, and the canvas is sized via css back to it's expected size, which produces the retina sharpness.

For example if you replace ctx.canvas.width and ctx.canvas.height with 300, everything works as expected.

If you need to base your calculations on the canvas height/width, you can use the function I provide in the README to get the pixel ratio so you can scale these appropriately as well

from hidpi-canvas-polyfill.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.