什么是contextjs替代context.drawImage的完整语法?

时间:2015-10-24 06:29:04

标签: javascript canvas fabricjs

我正在尝试使用fabricjs渲染图像并进行一些处理。 我可以在正常渲染到画布时使用它,但是在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。

你能帮我按照实际图像精确渲染图像吗?

1 个答案:

答案 0 :(得分:2)

试试这个:

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

希望现在很清楚:)