最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - dynamic height and width of canvas based on selected images - Stack Overflow

programmeradmin2浏览0评论

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 badges
Add a ment  | 

3 Answers 3

Reset to default 4

As 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);
    });
发布评论

评论列表(0)

  1. 暂无评论