I'm developing a system for filtering images with html5 canvas, however, as I'm at the beginning I've had some doubts and errors. In this "beginning" I want the size of the canvas to be the same as the one of the chosen image, so that it occupies all the space of the canvas.
Here's what I've developed so far:
$(document).ready(function() {
$("#uploadImagem").change(function(e) {
var _URL = window.URL || window.webkitURL,
arquivo = e.target.files[0],
tipoImagem = /image.*/,
reader,
imagem;
if(!arquivo.type.match(tipoImagem)) {
alert("Somente imagens são aceitas!");
return;
}
imagem = new Image();
imagem.onload = function() {
if(this.width > 600 || this.height > 400) {
alert("Somente imagens com largura máxima de 600px e altura máxima de 400px");
return;
} else {
$("#filtrarImagem").width(this.width).height(this.height);
}
};
imagem.src = _URL.createObjectURL(arquivo);
reader = new FileReader();
reader.onload = fileOnload;
reader.readAsDataURL(arquivo);
});
function fileOnload(e) {
var $img = $("<img>", {src: e.target.result}),
canvas = $("#filtrarImagem")[0],
context = canvas.getContext("2d");
$img.load(function() {
context.drawImage(this, 0, 0);
});
}
});
imagem.onload...
I would like that if the pixels in the image were greater than 600 and 400 it gave the alert and stopped there, however, even then the image appears on the canvas. imagem.onload...
if the pixels of the image match the required canvas (from id="filterImage") is the size of the image, but the image that goes to the canvas is smaller than normal and does not occupy all the canvas, being that it was for her to occupy all the canvas and to be of the original size. How to proceed?