How to generate an Image from imageData in javascript?

后端 未结 4 748
日久生厌
日久生厌 2020-12-25 10:45

I would like to know if there is any way to create a new Image from imageData, which was previously obtained from a canvas element?

I\'ve searched for a solution, bu

4条回答
  •  余生分开走
    2020-12-25 11:31

    None of the previous answers provide an answer to the question as it was presented in the subject - getting an Image from ImageData. So here's a solution.

    function imagedata_to_image(imagedata) {
        var canvas = document.createElement('canvas');
        var ctx = canvas.getContext('2d');
        canvas.width = imagedata.width;
        canvas.height = imagedata.height;
        ctx.putImageData(imagedata, 0, 0);
    
        var image = new Image();
        image.src = canvas.toDataURL();
        return image;
    }
    

提交回复
热议问题