Canvas tainted by cross-origin data

后端 未结 4 1722
南笙
南笙 2020-11-27 19:05

I\'m loading a motion jpeg from third-party site, which I can trust. I\'m trying to getImageData() but the browser (Chrome 23.0) complains that:



        
4条回答
  •  萌比男神i
    2020-11-27 19:52

    You can use base64 of the image on canvas, While converting into base64 you can use a proxy URL (https://cors-anywhere.herokuapp.com/) before your image path to avoid cross-origin issue

    check full details here

    https://stackoverflow.com/a/44199382/5172571

    var getDataUri = function (targetUrl, callback) {
        var xhr = new XMLHttpRequest();
        xhr.onload = function () {
            var reader = new FileReader();
            reader.onloadend = function () {
                callback(reader.result);
            };
            reader.readAsDataURL(xhr.response);
        };
        var proxyUrl = 'https://cors-anywhere.herokuapp.com/';
        xhr.open('GET', proxyUrl + targetUrl);
        xhr.responseType = 'blob';
        xhr.send();
    };
    getDataUri(path, function (base64) {
        // base64 availlable here
    })
    

提交回复
热议问题