繁体   English   中英

图像适合 canvas fabricjs

[英]image fit into canvas fabricjs

我正在使用 Fabricjs 使用当前代码上传图像

图像仅显示完整图像的 1/4。 在此 tscode 中的 canvas 中未显示完整图像,我正在手动设置图像的宽度和高度。

这是我的 Ts 代码

  addImage(canvasInst, imageUrl) {
fabric.Image.fromURL(imageUrl, img => {
  var oImg = img.set({
    left: 0,
    top: 0
  });
  var canvasWidth = canvasInst.width / canvasInst.getZoom();
  oImg.scaleToWidth(canvasWidth);
  canvasInst.add(oImg).renderAll();
  canvasInst.toDataURL({ format: "png", quality: 0.8 });
});

}

但我正在尝试其他方式 scaletowidth 但这在这里不起作用

这是堆栈闪电战

例子

在此处输入图像描述

FabricJS 使用属性scaleXscaleY来确定图像的大小,而widthheight属性用于指定应该使用多少原始图像尺寸(缩小这些值将裁剪图像,而不是缩放图像)。

在您链接的示例中,您在 canvas 上运行scaleToWidth() 您需要在图像上运行它。

fabric.Image.fromURL(imageUrl, img => {
  var oImg = img.set({
    left: 0,
    top: 0
  });
  var canvasWidth = canvasInst.width / canvasInst.getZoom();
  oImg.scaleToWidth(canvasWidth);
  canvasInst.add(oImg).renderAll();
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM