JSFiddle - React, Tailwind, and code Playground
HTML
<canvas id="canvas" width="500" height="500" style="border: 1px solid black" />
JavaScript
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);
};