我正在尝试使用fabricjs渲染图像并进行一些处理。
在正常渲染到canvas上时,我可以使用它,但是如果使用FabricJS,我将失败:
这是没有fabricjs的画布代码:

剪切图像并将剪切的部分放置在画布上:
JavaScript语法:context.drawImage(img,sx,sy,swidth,sheight,x,y,width,height);

如何使用FabricJS实现相同的目标?

    hiddenContext.putImageData(imageDataArr, 0, 0);
    var hc = document.getElementById(hid);
    //assigning last working canvas.
    LWC = CWC;
    CWC = fabricFH;
    CWC.backgroundColor = "white";
    CWC.add(new fabric.Image(hc,
    {
        alignX : "mid",
        alignY : "mid",
        selectable : false,
        hasBorders : false,
        hasControls : false,
        hasRotatingPoint : false,
        lockUniScaling: true,
        centeredScaling: true,
        scaleX: $("#"+activeCanvas).find("canvas").get(0).width/hc.width,
        scaleY: $("#"+activeCanvas).find("canvas").get(0).height/hc.height,
    }
    ));


上面的代码使我可以保持宽高比并完美地渲染到画布上,但是由于我的画布是1000 * 800,图像是2130 * 1800,所以我失去了图像的质量。

您能帮助我根据实际图像以精确的质量渲染图像吗?

最佳答案

尝试这个:

var canvas = new fabric.Canvas('canvas');
var img = new Image();
img.src = 'http://i2.ooshirts.com/images/lab_shirts/Cobalt-1-F.jpg';
var imgInstance = new fabric.Image(img, {
            left: 10,
            top: 10,
            angle: 0,
            width:300,
            height:240,
            clipTo: function(ctx){
                ctx.rect(-100,-100,200,200);
               }
        });
canvas.add(imgInstance);
canvas.renderAll();


如果我们想将其映射为:

context.drawImage(img,sx,sy,swidth,sheight,x,y,width,height); where

img=Specifies the image ,
sx= Optional. The x coordinate where to start  clipping,
sy= Optional. The y coordinate where to start  clipping,
swidth= Optional. The width of the clipped image,
sheight= ptional. The height of the clipped image,
x= The x coordinate where to place the image on the canvas,
y= The y coordinate where to place the image on the canvas,
width= Optional. The width of the image to use,
height= Optional. The height of the image to use     then

   fabric img <=> img
   clipTo->ctx.rect <=> sx,sy,swidth,sheight,
   left <=> x,
   top <=> y,
   width <=> width ,
   height <=> height


希望现在清楚了:)

10-02 13:21