javascript fabric.js - 从画布 API 的 ImageData 对象创建 Image 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18799415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 13:16:01  来源:igfitidea点击:

fabric.js - create Image object from ImageData object of canvas API

javascriptcanvasfabricjs

提问by Tom

I want to create an image object in fabric.js from ImageDataobject, we can get ImageData from this:

我想从对象在 fabric.js 中创建一个图像对象ImageData,我们可以从中获取 ImageData:

var imgData=ctx.getImageData(10,10,50,50);
//ctx.putImageData(imgData,10,70);

// something liket that
var image = new fabric.Image.fromImageData (...);

Is there any way to create an image object from ImageDataobject?

有没有办法从ImageData对象创建图像对象?

回答by Tom

Let me put my idea here, I don't like this way but have no others around -

让我把我的想法放在这里,我不喜欢这样,但周围没有其他人 -

var ctx = canvas.getContext('2d');
var data = ctx.getImageData(0, 0, 20, 20);

var c = document.createElement('canvas');

c.setAttribute('id', '_temp_canvas');
c.width = 20;
c.height = 20;

c.getContext('2d').putImageData(data, 0, 0);

fabric.Image.fromURL(c.toDataURL(), function(img) {
    img.left = 50;
    img.top = 50;
    canvas.add(img);
    img.bringToFront();
    c = null;
    $('#_temp_canvas').remove();
    canvas.renderAll();
});