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

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

        如何独立创建新的 ImageData 对象?

        时间:2023-06-21

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

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

                <bdo id='LgWyK'></bdo><ul id='LgWyK'></ul>
                <tfoot id='LgWyK'></tfoot>

                    <tbody id='LgWyK'></tbody>
                  <i id='LgWyK'><tr id='LgWyK'><dt id='LgWyK'><q id='LgWyK'><span id='LgWyK'><b id='LgWyK'><form id='LgWyK'><ins id='LgWyK'></ins><ul id='LgWyK'></ul><sub id='LgWyK'></sub></form><legend id='LgWyK'></legend><bdo id='LgWyK'><pre id='LgWyK'><center id='LgWyK'></center></pre></bdo></b><th id='LgWyK'></th></span></q></dt></tr></i><div id='LgWyK'><tfoot id='LgWyK'></tfoot><dl id='LgWyK'><fieldset id='LgWyK'></fieldset></dl></div>
                  本文介绍了如何独立创建新的 ImageData 对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想在代码中创建一个新的 ImageData 对象.如果我有一个 Uint8ClampedArray 我想从中制作一个图像对象,那么最好的方法是什么?

                  I want to create a new ImageData object in code. If I have a Uint8ClampedArray out of which I want to make an image object, what is the best way to do it?

                  我想我可以创建一个新的 canvas 元素,提取它的 ImageData 并覆盖它的 data 属性,但这似乎是一种错误的方法.

                  I guess I could make a new canvas element, extract its ImageData and overwrite its data attribute, but that seems like a wrong approach.

                  如果我可以直接使用 ImageData 构造函数,那就太好了,但我不知道该怎么做.

                  It would be great if I could use the ImageData constructor directly, but I can't figure out how to.

                  推荐答案

                  这是个有趣的问题...你不能只创建 ImageData 对象:

                  This is interesting problem... You can't just create ImageData object:

                  var test = new ImageData(); // TypeError: Illegal constructor
                  

                  我也试过了:

                  var imageData= context.createImageData(width, height);
                  imageData.data = mydata; // TypeError: Cannot assign to read only property 'data' of #<ImageData> 
                  

                  但如 MDN data 属性是只读的.

                  but as described in MDN data property is readonly.

                  所以我认为唯一的方法是通过迭代创建对象并设置数据属性:

                  So I think the only way is to create object and set data property with iteration:

                  var canvas = document.createElement('canvas');
                  var imageData = canvas.getContext('2d').createImageData(width, height);
                  for(var i = 0; i < myData.length; i++){
                      imageData.data[i] = myData[i];
                  }
                  

                  更新:我在ImageData的data属性中发现了set方法,所以解决方法很简单:

                  Update: I have discovered the set method in data property of ImageData, so solution is very simple:

                  var canvas = document.createElement('canvas');
                  var imageData = canvas.getContext('2d').createImageData(width, height);
                  imageData.data.set(myData);
                  

                  这篇关于如何独立创建新的 ImageData 对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用 Data URI 快速更新图像会导致缓存、内存泄漏 下一篇:使用 pdf.js 和 ImageData 将 .pdf 渲染到单个 Canvas

                  相关文章

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

                      <tfoot id='eesUk'></tfoot>

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

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

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