I'm experimenting a bit with the new canvas element in HTML.
I simply want to add an image to the canvas but it doesn't work for some reason.
I have the following code:
HTML
<canvas id="viewport"></canvas>
CSS
canvas#viewport { border: 1px solid white; width: 900px; }
JS
var canvas = document.getElementById('viewport'),
context = canvas.getContext('2d');
make_base();
function make_base()
{
base_image = new Image();
base_image.src = 'img/base.png';
context.drawImage(base_image, 100, 100);
}
The image exists and I get no JavaScript errors. The image just doesn't display.
It must be something really simple I've missed...
Maybe you should have to wait until the image is loaded before you draw it. Try this instead:
I.e. draw the image in the onload callback of the image.
In my case, I was mistaken the function parameters, which are:
If you expect them to be
you will place the image just outside the canvas with the same effects as described in the question.
here is the sample code to draw image on canvas-
In the above code selectedImage is an input control which can be used to browse image on system. For more details of sample code to draw image on canvas while maintaining the aspect ratio:
http://newapputil.blogspot.in/2016/09/show-image-on-canvas-html5.html