/ / Zmień rozmiar płótna po skalowaniu obrazu - jquery, html5, html5-canvas

Zmień rozmiar płótna po obrazie skali - jquery, html5, html5-canvas

Próbuję przeskalować obraz, który został już narysowany na kanwie. Używam ta odpowiedź do rysowania / obracania obrazu na płótnie.Ten przykład działa dla mnie dobrze i poprawnie zmienia rozmiar płótna. Chcę użyć skali (ZoomIn / ZoomOut), obraz działa dobrze, ale rozmiar płótna nie zmienia się poprawnie. Chcę zmienić rozmiar bazy na podstawie rozmiaru obrazu po skali. Oto przykładowy kod http://jsfiddle.net/6ZsCz/97/

var zoomDelta = 0.1;
var currentScale = 1;
var ctx = canvas.getContext("2d");
var imgWidth;
var imgHeight;
var size = {};
var rotation = 0;
img = new Image();
img.onload = function () {
rotation = 0;
imgWidth = img.width;
imgHeight = img.height;
size = {
width: imgWidth,
height: imgHeight
};
draw();
newSize(imgWidth, imgHeight, rotation);
};

$("#zoomOut").click(function () {
currentScale -= zoomDelta;
draw();
newSize(imgWidth / currentScale, imgHeight / currentScale, rotation);
});

function draw() {
canvas.width = size.width;
canvas.height = size.height;
var cx = canvas.width / 2;
var cy = canvas.height / 2;
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.translate(cx, cy);
ctx.scale(currentScale, currentScale);
ctx.rotate(rotation * deg2Rad);
ctx.drawImage(img, (-imgWidth / 2) , (-imgHeight / 2) );
}
function newSize(w, h, a) {
var rads = a * Math.PI / 180;
var c = Math.cos(rads);
var s = Math.sin(rads);
if (s < 0) {
s = -s;
}
if (c < 0) {
c = -c;
}
size.width = (h * s + w * c) ;
size.height = (h * c + w * s);

}

Odpowiedzi:

2 dla odpowiedzi № 1

Możesz zmienić rozmiar płótna, aby skalować, mnożąc rozmiar płótna przez bieżącą skalę:

canvas.width = size.width*currentScale;
canvas.height = size.height*currentScale;