How to fix blurry edges on JS canvas

I am working on a canvas and I want it to be responsive (look same on different screen sizes), resizable (look same when browser window resizes and resize properly), size-adjustable (user can enter canvas output size) and look sharp (no blurry edges of shapes).

The code I have below satisfies all other criteria but it shows edges of shapes blurry. For example, the top and left edges of the rectangle in the drawing below are blurry, whereas right and bottom edges are sharp.

test.html:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Test</title>
<style>body{overflow:hidden;background:#222;margin:0}#wrp{margin:0 auto}canvas{margin:auto;width:100%;height:100%}
</style>
</head>
<body>
<div id="wrp"><canvas id="canvas"></canvas></div>
<script src="./draw.js"></script>
</body>
</html>
</code>
<code><!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Test</title> <style>body{overflow:hidden;background:#222;margin:0}#wrp{margin:0 auto}canvas{margin:auto;width:100%;height:100%} </style> </head> <body> <div id="wrp"><canvas id="canvas"></canvas></div> <script src="./draw.js"></script> </body> </html> </code>
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Test</title>
    <style>body{overflow:hidden;background:#222;margin:0}#wrp{margin:0 auto}canvas{margin:auto;width:100%;height:100%}
</style>
</head>
<body>
    <div id="wrp"><canvas id="canvas"></canvas></div>
    <script src="./draw.js"></script>
</body>
</html>

draw.js:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>var cnv = document.getElementById("canvas"), ctx = cnv.getContext("2d");
var wrp = document.getElementById("wrp");
// ar (aspect ratio), size (user can enter different values - the canvas size to save as output)
var ar = 0.8, size = 2000;
var width, height;
var scale = size / ((innerWidth < innerHeight * ar) ? innerWidth : innerHeight * ar);
function draw() {
setup();
ctx.fillStyle = "#D4CAA3"; ctx.fillRect(0, 0, width, height);
ctx.fillStyle = "#A15837"; ctx.fillRect(200, 200, 200, 200);
}
function setup() {
const PR = window.devicePixelRatio || 1;
let w = innerWidth, h = innerHeight;
// accounts for portrait and landscape orientations
width = (w < h * ar) ? w : h * ar;
height = (w < h * ar) ? w / ar : h;
wrp.style.width = width + "px";
wrp.style.height = height + "px";
cnv.width = Math.ceil(size * PR);
cnv.height = Math.ceil(size / ar * PR);
ctx.scale(scale, scale);
}
window.addEventListener("resize", draw, false);
draw();
</code>
<code>var cnv = document.getElementById("canvas"), ctx = cnv.getContext("2d"); var wrp = document.getElementById("wrp"); // ar (aspect ratio), size (user can enter different values - the canvas size to save as output) var ar = 0.8, size = 2000; var width, height; var scale = size / ((innerWidth < innerHeight * ar) ? innerWidth : innerHeight * ar); function draw() { setup(); ctx.fillStyle = "#D4CAA3"; ctx.fillRect(0, 0, width, height); ctx.fillStyle = "#A15837"; ctx.fillRect(200, 200, 200, 200); } function setup() { const PR = window.devicePixelRatio || 1; let w = innerWidth, h = innerHeight; // accounts for portrait and landscape orientations width = (w < h * ar) ? w : h * ar; height = (w < h * ar) ? w / ar : h; wrp.style.width = width + "px"; wrp.style.height = height + "px"; cnv.width = Math.ceil(size * PR); cnv.height = Math.ceil(size / ar * PR); ctx.scale(scale, scale); } window.addEventListener("resize", draw, false); draw(); </code>
var cnv = document.getElementById("canvas"), ctx = cnv.getContext("2d");
var wrp = document.getElementById("wrp");

// ar (aspect ratio), size (user can enter different values - the canvas size to save as output)
var ar = 0.8, size = 2000;
var width, height;

var scale = size / ((innerWidth < innerHeight * ar) ? innerWidth : innerHeight * ar);

function draw() {
    setup();
    
    ctx.fillStyle = "#D4CAA3"; ctx.fillRect(0, 0, width, height);
    ctx.fillStyle = "#A15837"; ctx.fillRect(200, 200, 200, 200);
}

function setup() {
    const PR = window.devicePixelRatio || 1;
    
    let w = innerWidth, h = innerHeight;
    
    // accounts for portrait and landscape orientations
    width = (w < h * ar) ? w : h * ar;
    height = (w < h * ar) ? w / ar : h;
    
    wrp.style.width = width + "px";
    wrp.style.height = height + "px";

    cnv.width = Math.ceil(size * PR);
    cnv.height = Math.ceil(size / ar * PR);
    
    ctx.scale(scale, scale);
}

window.addEventListener("resize", draw, false);
draw();

The above code is a result of endless search on here and other resources, but I still couldn’t get rid of the blurry edges. Any ideas how to fix that?

Thanks!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật