• <bdo id='4cHuf'></bdo><ul id='4cHuf'></ul>

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

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

        使用 img.crossOrigin = "Anonymous" 将图像绘制到

        时间:2023-10-14

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

          <tfoot id='7XNmm'></tfoot>

            <tbody id='7XNmm'></tbody>
            <bdo id='7XNmm'></bdo><ul id='7XNmm'></ul>

                  <small id='7XNmm'></small><noframes id='7XNmm'>

                1. 本文介绍了使用 img.crossOrigin = "Anonymous" 将图像绘制到画布上不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  在客户端独立的 JS 应用程序中,我正在尝试制作它,以便可以在画布上调用 toDataURL(),在该画布上我绘制了一些由 URL 指定的图像.即,我可以在文本框中输入要在画布上绘制的任何图像(托管在 imgur 上)的 URL,单击绘制"按钮,它将在画布上绘制.最终用户应该能够将他们的最终渲染保存为单个图像,为此我使用 toDataURL().

                  In a client-side standalone JS application, I'm trying to make it so I can call toDataURL() on a canvas on which I've drawn some images specified by a URL. Ie I can input into a textbox the url to any image (hosted on, say, imgur) that I want to draw on the canvas, click a "draw" button and it will draw on the canvas. The end user should be able to save their final render as a single image, for this I'm using toDataURL().

                  无论如何,直到他们真正解决了那个烦人的操作不安全"错误(天哪,你要告诉最终用户他们可以用自己的数据做什么和不能做什么?)我遵循了一个变通方法,说将图像添加到 DOM 并将其 crossOrigin 属性设置为Anonmyous",然后将其绘制到画布上.

                  Anyway, until they actually fix that annoying "operation is insecure" error (gee, you're going to tell the end user what they can and can't do with their own data?) I followed a workaround that said to add the image to the DOM and set its crossOrigin property to "Anonmyous" and then draw it to the canvas.

                  这是我的代码的完整工作简化版本(但实际上会有更多功能):

                  Here's a full working simplified version of my code (but in reality there will be many more features):

                  <!DOCTYPE html5>
                  <html>
                  <head>
                  <style>
                  #canvas {border:10px solid green;background-color:black;}
                  #imgbox {border:2px solid black;}
                  </style>
                  </head>
                  <body>
                  <canvas id="canvas" width=336 height=336></canvas>
                  <br><br>
                  <input size=60 id="imgbox">
                  <input type="submit" value="Draw" onclick=draw()>
                  <script>
                  function draw() {
                      var canvas = document.getElementById("canvas");
                      var context = canvas.getContext("2d");
                      var img = new Image();
                      img.src = document.getElementById("imgbox").value;
                      img.crossOrigin = "Anonymous";
                      context.drawImage(img, 40, 40);
                  }
                  </script>
                  </body>
                  </html>
                  

                  没有 img.crossOrigin = "Anonymous"; 行,我可以在文本框中输入 http://i.imgur.com/c2wRzfD.jpg 并点击画,它会工作.但是,一旦我添加了那条线,整个东西就坏了,甚至根本不会被绘制到画布上.

                  Without the img.crossOrigin = "Anonymous"; line, I could input http://i.imgur.com/c2wRzfD.jpg into the textbox and click draw and it would work. However as soon as I added that line, the whole thing broke and it won't even be drawn to the canvas at all.

                  我需要改变什么来解决这个问题?我真的需要能够为最终用户实现保存最终图像的功能,而编写 html5 规范的人故意引入了这个 bug,这非常烦人.

                  What do I need to change to fix this? I really need to be able to implement the functionality for the end user to save their final image and it's extremely annoying that the people who wrote the html5 spec purposely introduced this bug.

                  推荐答案

                  您必须在 src 之前设置 CORS 请求 - 只需将这些行换成:

                  You must set the CORS request before the src - just swap the lines into:

                  img.crossOrigin = "Anonymous";
                  img.src = document.getElementById("imgbox").value;
                  

                  您还需要为图像添加一个 onload 处理程序,因为加载是异步的:

                  You will also need to add an onload handler to the image as loading is asynchronous:

                  img.onload = function() {
                      context.drawImage(this, 40, 40);
                      // call next step in your code here, f.ex: nextStep();
                  };
                  img.crossOrigin = "Anonymous";
                  img.src = document.getElementById("imgbox").value;
                  

                  这篇关于使用 img.crossOrigin = "Anonymous" 将图像绘制到画布上不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Jquery AJAX:请求的资源上不存在“Access-Control-Allo 下一篇:jQuery AJAX 调用导致错误状态 403

                  相关文章

                  最新文章

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

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

                        <bdo id='MZ1Np'></bdo><ul id='MZ1Np'></ul>
                      <legend id='MZ1Np'><style id='MZ1Np'><dir id='MZ1Np'><q id='MZ1Np'></q></dir></style></legend>