Cargando una imagen en un lienzo con javaScript
Hola todos estoy probando actualmente usando el elemento lienzo para dibujar todos los fondos (agregaré efectos más adelante a estas imágenes más adelante y es la razón por la que no estoy usando css para cargar las imágenes), lo que dice que actualmente tengo dificultades para cargar una imagen a la lona. Aquí está el código:
<html>
<head>
<title>Canvas image testing</title>
<script type="text/javascript">
function Test1() {
var ctx = document.getElementById('canvas');
if (canvas.getContext) {
var ctx = canvas.getContext('2d');
//Loading of the home test image - img1
var img1 = new Image();
img1.src = 'img/Home.jpg';
//drawing of the test image - img1
img1.onload = function () {
//draw background image
ctx.drawimage(img1, 0, 0);
//draw a box over the top
ctx.fillStyle = "rgba(200, 0, 0, 0.5)";
ctx.fillRect(0, 0, 500, 500);
};
}
}
</script>
<style type="text/css">
canvas { border: 2px solid black; width: 100%; height: 98%; }
</style>
</head>
<body onload="Test1();">
<canvas id="canvas" width="1280" height="720"></canvas>
</body>
Creo que no estoy cargando la imagen correctamente pero no estoy seguro.