JSFiddle - React, Tailwind, and code Playground
HTML
<canvas id="myCanvas" height = 600 width = 600></canvas>
CSS
myCanvas {
border: 2px solid black;
}
JavaScript
var canvas = document.getElementById('myCanvas');
var ctx = canvas.getContext("2d");
var image = new Image();
image.src = 'http://www.menucool.com/slider/jsImgSlider/images/image-slider-2.jpg';
// calculates width and height with respect to canvas
var width = (canvas.height * image.width) / image.height;
var height = (canvas.width * image.height) / image.width;
// takes width or height full choosing the larger value and centers the image
if(image.height > image.width){
ctx.drawImage(image, canvas.width / 2 - width / 2, 0, width, canvas.height);
}else{
ctx.drawImage(image, 0, canvas.height / 2 - height / 2, canvas.width, height);
}