I am trying to clip part of original image using X,Y coordinates and W,H dimensions, and place it to canvas.
Here is my codepen example
img.onload = function () {
var boxSize = box.getBoundingClientRect();
canvas.width = boxSize.width;
canvas.height = boxSize.height;
var hRatio = boxSize.width / w;
var vRatio = boxSize.height / h;
var ratio = Math.min(hRatio, vRatio);
var centerShift_x = (canvas.width - w * ratio) / 2;
var centerShift_y = (canvas.height - h * ratio) / 2;
var ctx = canvas.getContext("2d");
ctx.fillStyle = "blue";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(
img,
0, 0,
w + x, h + y,
(-x * ratio) + centerShift_x, -y * ratio + centerShift_y,
(w + x) * ratio, (h + y) * ratio
);
};
Image is significantly bigger then canvas, so I'm expecting to not loose quality, at least not too much. But in fact quality dramatically low.
So, how to not loose the quality?