<i id='CwGy6'><tr id='CwGy6'><dt id='CwGy6'><q id='CwGy6'><span id='CwGy6'><b id='CwGy6'><form id='CwGy6'><ins id='CwGy6'></ins><ul id='CwGy6'></ul><sub id='CwGy6'></sub></form><legend id='CwGy6'></legend><bdo id='CwGy6'><pre id='CwGy6'><center id='CwGy6'></center></pre></bdo></b><th id='CwGy6'></th></span></q></dt></tr></i><div id='CwGy6'><tfoot id='CwGy6'></tfoot><dl id='CwGy6'><fieldset id='CwGy6'></fieldset></dl></div>

  1. <small id='CwGy6'></small><noframes id='CwGy6'>

  2. <tfoot id='CwGy6'></tfoot>

    • <bdo id='CwGy6'></bdo><ul id='CwGy6'></ul>

    1. <legend id='CwGy6'><style id='CwGy6'><dir id='CwGy6'><q id='CwGy6'></q></dir></style></legend>

      如何修复 getImageData() 错误画布已被跨域数据污染?

      时间:2023-06-21
        <bdo id='4hrnW'></bdo><ul id='4hrnW'></ul>

            <tfoot id='4hrnW'></tfoot>
            <i id='4hrnW'><tr id='4hrnW'><dt id='4hrnW'><q id='4hrnW'><span id='4hrnW'><b id='4hrnW'><form id='4hrnW'><ins id='4hrnW'></ins><ul id='4hrnW'></ul><sub id='4hrnW'></sub></form><legend id='4hrnW'></legend><bdo id='4hrnW'><pre id='4hrnW'><center id='4hrnW'></center></pre></bdo></b><th id='4hrnW'></th></span></q></dt></tr></i><div id='4hrnW'><tfoot id='4hrnW'></tfoot><dl id='4hrnW'><fieldset id='4hrnW'></fieldset></dl></div>
              <tbody id='4hrnW'></tbody>

              <small id='4hrnW'></small><noframes id='4hrnW'>

                <legend id='4hrnW'><style id='4hrnW'><dir id='4hrnW'><q id='4hrnW'></q></dir></style></legend>

              • 本文介绍了如何修复 getImageData() 错误画布已被跨域数据污染?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我的代码在我的本地主机上运行良好,但在网站上却无法运行.

                My code is working very well on my localhost but it is not working on the site.

                我从控制台收到此错误,对于这一行 .getImageData(x,y,1,1).data:

                I got this error from the console, for this line .getImageData(x,y,1,1).data:

                Uncaught SecurityError: Failed to execute 'getImageData' on 'CanvasRenderingContext2D': The canvas has been tainted by cross-origin data. 
                

                我的部分代码:

                jQuery.Event.prototype.rgb=function(){
                        var x =  this.offsetX || (this.pageX - $(this.target).offset().left),y =  this.offsetY || (this.pageY - $(this.target).offset().top);
                        if (this.target.nodeName!=="CANVAS")return null;
                        return this.target.getContext('2d').getImageData(x,y,1,1).data;
                    }
                

                注意:我的图片 url (src) 来自子域 url

                Note: my image url (src) is from a subdomain url

                推荐答案

                正如其他人所说,您通过从跨域域加载来污染"画布.

                As others have said you are "tainting" the canvas by loading from a cross origins domain.

                https://developer.mozilla.org/en-US/docs/HTML/CORS_Enabled_Image

                但是,您可以通过简单的设置来防止这种情况发生:

                However, you may be able to prevent this by simply setting:

                img.crossOrigin = "Anonymous";
                

                这仅在远程服务器正确设置以下标头时才有效:

                This only works if the remote server sets the following header appropriately:

                Access-Control-Allow-Origin "*"
                

                使用直接链接"选项时,Dropbox 文件选择器是一个很好的例子.我在 oddprints.com 上使用它来将远程 Dropbox 图像 url 中的图像存储到我的画布中,然后将图像数据提交回我的服务器.全部在javascript中

                The Dropbox file chooser when using the "direct link" option is a great example of this. I use it on oddprints.com to hoover up images from the remote dropbox image url, into my canvas, and then submit the image data back into my server. All in javascript

                这篇关于如何修复 getImageData() 错误画布已被跨域数据污染?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:HTML5 Canvas 调整大小(缩小)图像质量? 下一篇:画布底部有空白区域并且滚动太远

                相关文章

                <legend id='Xc24n'><style id='Xc24n'><dir id='Xc24n'><q id='Xc24n'></q></dir></style></legend>

                1. <i id='Xc24n'><tr id='Xc24n'><dt id='Xc24n'><q id='Xc24n'><span id='Xc24n'><b id='Xc24n'><form id='Xc24n'><ins id='Xc24n'></ins><ul id='Xc24n'></ul><sub id='Xc24n'></sub></form><legend id='Xc24n'></legend><bdo id='Xc24n'><pre id='Xc24n'><center id='Xc24n'></center></pre></bdo></b><th id='Xc24n'></th></span></q></dt></tr></i><div id='Xc24n'><tfoot id='Xc24n'></tfoot><dl id='Xc24n'><fieldset id='Xc24n'></fieldset></dl></div>
                2. <tfoot id='Xc24n'></tfoot>

                  • <bdo id='Xc24n'></bdo><ul id='Xc24n'></ul>

                    <small id='Xc24n'></small><noframes id='Xc24n'>