Copy imageData by value in JavaScript

前端 未结 3 1286
暖寄归人
暖寄归人 2020-12-31 08:52

In my application I have a function that processes Canvas ImageData. It looks something like this:

function processData(imagedata, filters) {
  var data = im         


        
3条回答
  •  渐次进展
    2020-12-31 09:16

    Just thought I would add one more solution to this thread along with a link to the duplicate thread on which I found the solution. I am re-posting it here because, in my case it offered an advantage.

    They propose simply cloning the data of the ImageData object with

    var dataCopy = new Uint8ClampedArray(imageData.data);
    

    Later, if you want to restore this data to an ImageObject you use

    imageData.data.set(dataCopy);
    

    imageData may be the original instance from which you cloned the data or a new ImageData object.

    In my case this method is better because I am working with ImageData objects inside of a WorkerThread. WorekerThreads are not allowed access to the canvas object or its context so as far as I can tell the techniques here would not help me. This solution; however, did the trick. Thanks All.

    Duplicate Thread Offering Other Solutions.

提交回复
热议问题