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

javascript - Canvas drawImage Crash on large images - Stack Overflow

programmeradmin2浏览0评论

I am creating a mobile app., where the user can choose a picture from mobile then I blur it.... The problem is that when the user choose a large picture(More than 2 MB), the app. crash.

JS Code:

convert_local_image_base64: function(url, callback) {
    var canvas = document.createElement('CANVAS'),
            ctx = canvas.getContext('2d'),
            img = new Image;
    img.crossOrigin = 'anonymous';
    img.onload = function() {
        canvas.height = img.height;
        canvas.width = img.width;
        ctx.drawImage(img, 0, 0);
        var dataURL = canvas.toDataURL('image/png');
        callback.call(this, dataURL);
        canvas = null;
    };
    img.src = url;
},

So does there are another way to achieve similar operation?!.

I am creating a mobile app., where the user can choose a picture from mobile then I blur it.... The problem is that when the user choose a large picture(More than 2 MB), the app. crash.

JS Code:

convert_local_image_base64: function(url, callback) {
    var canvas = document.createElement('CANVAS'),
            ctx = canvas.getContext('2d'),
            img = new Image;
    img.crossOrigin = 'anonymous';
    img.onload = function() {
        canvas.height = img.height;
        canvas.width = img.width;
        ctx.drawImage(img, 0, 0);
        var dataURL = canvas.toDataURL('image/png');
        callback.call(this, dataURL);
        canvas = null;
    };
    img.src = url;
},

So does there are another way to achieve similar operation?!.

Share Improve this question asked May 13, 2014 at 11:12 Moussawi7Moussawi7 13.3k7 gold badges39 silver badges50 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

ON PC: -
I tried multiple large sized images (more than 2 MB) to draw on a canvas. I also tried to get its dataURL and redraw it on different canvas. Every thing worked fine for me. (Tried on IE11, Chrome and Firefox.)

Please make sure you not messing with Cross-origin resource sharing (CORS).

Also I found some posts here on stackoverflow related to resolutions on canvas.

Maximum size of a <canvas> element

Is there a max size for images being loaded onto canvas on iPhone?

EDITED: ON Mobile devices-
Here is the restrictions for the canvas for mobile devices:-

The maximum size for a canvas element is 3 megapixels for devices with less than 256 MB RAM and 5 megapixels for devices with greater or equal than 256 MB RAM.

So for example - if you want to support Apple’s older hardware, the size of your canvas cannot exceed 2048×1464.

Hope these resources will help you to pull you out.

There seems to be a size limit with the canvas drawImage function on android devices. There is a 3 mega pixels limit but the limit is for the size of the image being used in the drawImage function and not a limit on canvas size, i.e. cropping/shrinking the size of the drawn image has no affect.

ctx.drawImage(imagebiggerthan3mp, 100, 100, 63, 63, 50, 50, 50, 50); // still Aw Snap! crashes on android!

发布评论

评论列表(0)

  1. 暂无评论