【问题标题】:What is Fabricjs alternative for context.drawImage full syntax?什么是 context.drawImage 完整语法的 Fabricjs 替代方案?
【发布时间】:2015-10-26 21:53:52
【问题描述】:

我正在尝试使用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。

您能帮我按照实际图像以准确的质量渲染图像吗?

【问题讨论】:

    标签: javascript canvas fabricjs


    【解决方案1】:

    试试这个:

    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
    

    希望现在清楚了 :)

    【讨论】:

    • 感谢您的回答。你能根据问题把论点联系起来吗?
    • 您的回答似乎很有希望。您可以添加一个 jsFiddle,我可以在其中将更高分辨率的图像调整到较小的画布上。让我们将 1500*1000 的图像放到 500*500 的画布上。
    • 我更新了处理纵横比的代码,但我无法以相同的质量渲染图像。你能做到吗?
    猜你喜欢
    • 2021-11-13
    • 2018-12-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-21
    • 1970-01-01
    • 2015-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多