How can I scale the HTML5 canvas without making it blurry?

前端 未结 3 363
死守一世寂寞
死守一世寂寞 2021-01-20 18:12

I have created a canvas using the following markup:


Now, if I specify width and

相关标签:
3条回答
  • 2021-01-20 18:23

    Canvas CSS properties do not resize canvas, they rescale it.
    You may simply change the properties with your JS:

    $("#canvas")
        .prop("width", window.innerWidth)
        .prop("height", window.innerHeight); 
    
    0 讨论(0)
  • 2021-01-20 18:29
    1. canvas element size is controlled by its attributes width and height. To change the size you need to modify these attributes, not CSS. And they don't need units, they are in pixels.

      <canvas id="canvas" width="600" height="400"></canvas>
      
    2. Canvas draws bitmap, it's not scalable like vectors. You could only redraw the canvas after resized.

    0 讨论(0)
  • 2021-01-20 18:34

    try:

    var canvas = document.getElementById('canvas');
    canvas.height = window.innerHeight;
    canvas.width = window.innerWidth;
    
    0 讨论(0)
提交回复
热议问题