Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
348 views
in Technique[技术] by (71.8m points)

javascript - Cross-origin canvas tainted by local image

I'm creating an image on a local file and rendering on a canvas, however when I try to getImageData from it I get the error:

The canvas has been tainted by cross-origin data.

I searched about it but I found only server side solutions. I even tryied to add img.crossOrigin ='Anonymous', img.crossOrigin ='Anonymous' or img.setAttribute('crossOrigin', '') but nothing worked.

By the way, this is the code:

async function start(){
    createCanvas(0,0);
    let img = await loadImage('./cat.jpeg');
    canvasResize(img.width / 2, img.height / 2);

    image(img, null, null, null, null, 0, 0, canvas.width, canvas.height);

    canvasImage = canvasGetPixels();
    [...]
}
async function loadImage(src){
    return new Promise((resolve) => {
        const img = new Image();
        img.src = src;
        img.onload = function() {
            resolve(this);
        };
    });
}

* Using vanilla javascript and Chrome version 88

question from:https://stackoverflow.com/questions/65838479/cross-origin-canvas-tainted-by-local-image

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...