Canvas.toDataURL() working in all browsers except IE10

后端 未结 2 888
耶瑟儿~
耶瑟儿~ 2020-12-08 03:35

I\'m working on a project that uses a canvas to automatically crop an image, then return its data URL. It uses images from an external server, which has the appropriate CORS

2条回答
  •  广开言路
    2020-12-08 03:50

    Unfortunately, IE10 still remains the only popular browser that doesn't support CORS for image drawn to Canvas even when CORS headers are properly set. But there is workaround for that via XMLHttpRequest even without proxying image on server-side:

    var xhr = new XMLHttpRequest();
    xhr.onload = function () {
        var url = URL.createObjectURL(this.response);
        img.src = url;
    
        // here you can use img for drawing to canvas and handling
    
        // don't forget to free memory up when you're done (you can do this as soon as image is drawn to canvas)
        URL.revokeObjectURL(url);
    };
    xhr.open('GET', url, true);
    xhr.responseType = 'blob';
    xhr.send();
    

提交回复
热议问题