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 对象?

        How to create a new ImageData object independently?(如何独立创建新的 ImageData 对象?)

            <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 对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to make rooftext effect and valley text effect in HTML5 (or Fabric.js)(如何在 HTML5(或 Fabric.js)中制作屋顶文字效果和山谷文字效果)
                  Draw border around nontransparent part of image on canvas(在画布上的图像不透明部分周围绘制边框)
                  dragging and resizing an image on html5 canvas(在 html5 画布上拖动图像并调整其大小)
                  What#39;s the difference between a boolean as primitive and a boolean as property of an object?(作为原始对象的布尔值和作为对象属性的布尔值有什么区别?)
                  I want to do animation of an object along a particular path(我想沿特定路径对对象进行动画处理)
                  How to upload image into HTML5 canvas(如何将图像上传到 HTML5 画布中)

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

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

                        • <legend id='eesUk'><style id='eesUk'><dir id='eesUk'><q id='eesUk'></q></dir></style></legend>

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

                          • <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>