Canvas drawings, like lines, are blurry

前端 未结 10 967
野的像风
野的像风 2020-11-27 15:15

I have a

and canvas, which is drawn using:

context.lineWidth = 1;
context.strokeStyle = \"gray\         


        
10条回答
  •  长情又很酷
    2020-11-27 15:55

    Lines are blurred because the canvas virtual size is zoomed to its HTML element actual size. To overcome this issue you need to adjust canvas virtual size before drawing:

    function Draw () {
    	var e, surface;
    	e = document.getElementById ("surface");
    	/* Begin size adjusting. */
    	e.width = e.offsetWidth;
    	e.height = e.offsetHeight;
    	/* End size adjusting. */
    	surface = e.getContext ("2d");
    	surface.strokeRect (10, 10, 20, 20);
    }
    window.onload = Draw ()
    
    
    
    Canvas size adjusting demo
    
    
    
    
    

    HTML:

提交回复
热议问题