Is there a way to display an image within an HTML5 Canvas of a specific height and width while maintaining the image's aspect ratio? I need the image to appear at 100% size within a scrollable container.
The image I want to use is of a tree: ''
To achieve this, I tried using the "overflow: scroll" option as shown in blue on this website.
The Javascript code snippet below demonstrates how to source and display the image in the canvas:
var canvas = document.getElementById('my_canvas');
var ctx = canvas.getContext('2d');
var imageObj = new Image();
ctx.canvas.width = 100; ctx.canvas.height = 100;
imageObj.onload = function() {
ctx.drawImage(imageObj, 0, 0, 100, 100);
};
imageObj.src = 'http://kornyezet.sze.hu/images/oktatok/tree3.jpg';
You can add the following HTML code to create the canvas element:
<canvas id="my_canvas"></canvas>
If you want to see an example, here's a JSFIDDLE with a Google image that has been resized. Ideally, I'd like the full-size image to be visible with a scrolling option: http://jsfiddle.net/jzF5R/
Thank you!