Failed to convert color pictures to black and white with Js

just came into contact with canvas, to learn to convert a picture into black and white.
but can"t succeed all the time. I"m a little confused.

Thank you.

    <body>
        <canvas width="500" height="500"></canvas>

        <script>
            var canvas = document.querySelector("canvas");
            var ctx = canvas.getContext("2d");

            var image = new Image();
            image.onload = function(){
                ctx.drawImage(image,0,0);
                makeGrayScale();
            }

            image.src = "images/background.jpg";

            var makePixelGrayScale = function (r,g,b,a){
                var y = ( 0.3 * r) + ( 0.59 * g ) + ( 0.11 * b);
                return {r:y,g:y,b:y,a:y};
            };

            function makeGrayScale(){
                var r,g,b,a;
                var imageData = ctx.getImageData(0,0,500, 500)
                var numPixels = imageData.data.length/4;
                for (var i=0; i<numPixels; iPP){
                    r = imageData.data[i*4+0];
                    g = imageData.data[i*4+1];
                    b = imageData.data[i*4+2];
                    a = imageData.data[i*4+3];
                    pixel = makePixelGrayScale(r,g,b,a);
                    imageData.data[i*4+0] = pixel.r;
                    imageData.data[i*4+1] = pixel.g;
                    imageData.data[i*4+2] = pixel.b;
                    imageData.data[i*4+3] = pixel.a;
                }
                ctx.putImageData(imageData,0,0);
            };

        </script>
    </body>
Uncaught DOMException: Failed to execute "getImageData" on "CanvasRenderingContext2D": The canvas has been tainted by cross-origin data.
    at makeGrayScale (file:///C:/Users/yocor/Github/aboutCanvas/createCanvas.html:26:37)
    at Image.image.onload (file:///C:/Users/yocor/Github/aboutCanvas/createCanvas.html:14:17)
Mar.11,2021

what is not successful? What mistake did you report? What happened? You can't just post a piece of code to let the responder guess.

There is nothing wrong with the

code. If you can't run it here, you must run the code directly by clicking on html.

if you run in this way, the image you introduce may be thought of as a different site by the browser, so canvas has a cross-domain error when getImageData, and canvas cannot get the information of the rendered cross-domain image.

if you want to run correctly, you need to use something like IIS or nginx to make a local site, or use webpack-dev-server


asking questions is also a technical task. First, describe your problem clearly, provide the necessary material, and the functions and styles you want to achieve.


change the file:/ access mode to http://xxxxxx

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b36922-2ad6b.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b36922-2ad6b.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?