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

不打扰是莪最后的温柔 提交于 2021-01-21 10:03:53

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!