イメージをキャンバス内に収まるように縮小していますが、私が苦労しているのはキャンバス要素の内側に中心を置くことですお願いします?任意のヘルプは、水平及び垂直の両方のために働くJS - センターイメージキャンバス要素内
https://jsfiddle.net/n7xL5c37/
var canvas = document.getElementById('canvas');
var image = new Image();
image.src = 'http://placehold.it/300x550';
image.onload = function() {
var canvasContext = canvas.getContext('2d');
var wrh = image.width/image.height;
var newWidth = canvas.width;
var newHeight = newWidth/wrh;
if (newHeight > canvas.height) {
newHeight = canvas.height;
newWidth = newHeight * wrh;
}
canvasContext.drawImage(image,0,0, newWidth , newHeight);
};
ブリリアント感謝をしたい場合は、このソリューションは正常に動作します
object-fit: contain
に似た何かをしたいです! – Admamza