admin管理员组文章数量:1431037
I am trying to have a canvas image drawn based on the selected image
<canvas id="canvas" ></canvas>
<input type="file" id="file-input">
Javascript
$(function() {
$('#file-input').change(function(e) {
var file = e.target.files[0],
imageType = /image.*/;
if (!file.type.match(imageType))
return;
var reader = new FileReader();
reader.onload = fileOnload;
reader.readAsDataURL(file);
});
function fileOnload(e) {
var $img = $('<img>', { src: e.target.result });
var canvas = $('#canvas')[0];
var context = canvas.getContext('2d');
$img.load(function() {
context.drawImage(this, 0, 0);
});
}
});
This writes the file to canvas, but the problem i face is, its writing the image only till the height and width of canvas.
How can i have the canvas width height automatically set to the height and width of the image selected
JSFIDDLE DEMO
Also the image that is being drawn is very huge pared to the original image
I am trying to have a canvas image drawn based on the selected image
<canvas id="canvas" ></canvas>
<input type="file" id="file-input">
Javascript
$(function() {
$('#file-input').change(function(e) {
var file = e.target.files[0],
imageType = /image.*/;
if (!file.type.match(imageType))
return;
var reader = new FileReader();
reader.onload = fileOnload;
reader.readAsDataURL(file);
});
function fileOnload(e) {
var $img = $('<img>', { src: e.target.result });
var canvas = $('#canvas')[0];
var context = canvas.getContext('2d');
$img.load(function() {
context.drawImage(this, 0, 0);
});
}
});
This writes the file to canvas, but the problem i face is, its writing the image only till the height and width of canvas.
How can i have the canvas width height automatically set to the height and width of the image selected
JSFIDDLE DEMO
Also the image that is being drawn is very huge pared to the original image
Share Improve this question edited Aug 6, 2015 at 11:10 Vignesh Subramanian asked Aug 6, 2015 at 10:48 Vignesh SubramanianVignesh Subramanian 7,30914 gold badges96 silver badges158 bronze badges3 Answers
Reset to default 4As I don't have enough points, I can't ment on previous answer.
You should update the height & width of canvas dom element.
var canvas = document.getElementsByTagName('canvas')[0];
$img.load(function() {
canvas.width = this.width;
canvas.height = this.height;
context.drawImage(this, 0, 0);
});
Working Demo: jsfiddle
$(function() {
$('#file-input').change(function(e) {
var file = e.target.files[0],
imageType = /image.*/;
if (!file.type.match(imageType))
return;
var reader = new FileReader();
reader.onload = fileOnload;
reader.readAsDataURL(file);
});
function fileOnload(e) {
var $img = $('<img>', { src: e.target.result });
var canvas = $('#canvas')[0];
var context = canvas.getContext('2d');
$img.load(function() {
canvas.width = this.width;
canvas.height = this.height;
context.drawImage(this, 0, 0);
});
}
});
You have to get dimensions (width
and height
) of the image once it loaded and set dimensions of the canvas to their values:
$img.load(function() {
canvas.width = this.width;
canvas.height = this.height;
context.drawImage(this, 0, 0);
});
本文标签: javascriptdynamic height and width of canvas based on selected imagesStack Overflow
版权声明:本文标题:javascript - dynamic height and width of canvas based on selected images - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745440380a2658419.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论