问题
I want to create an image object in fabric.js from ImageData
object, we can get ImageData from this:
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 ImageData
object?
回答1:
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();
});
来源:https://stackoverflow.com/questions/18799415/fabric-js-create-image-object-from-imagedata-object-of-canvas-api