Javascript 调整画布的背景图像大小-Fabricjs

Javascript 调整画布的背景图像大小-Fabricjs,javascript,canvas,fabricjs,Javascript,Canvas,Fabricjs,我正在使用fabricjs处理画布,并通过javascript将图像加载到画布中 我有一个函数,它可以调整画布的大小,使其具有响应性,因此,我希望调整加载的背景图像的大小,以适应画布,但保持纵横比 我目前还没有找到符合我标准的例子,我希望有人能提供帮助 Javascript var canvas = new fabric.Canvas('drawing_layer'); var img = new Image(); img.onload = function () {

我正在使用fabricjs处理画布,并通过javascript将图像加载到画布中

我有一个函数,它可以调整画布的大小,使其具有响应性,因此,我希望调整加载的背景图像的大小,以适应画布,但保持纵横比

我目前还没有找到符合我标准的例子,我希望有人能提供帮助

Javascript

var canvas = new fabric.Canvas('drawing_layer');
 var img = new Image();
            img.onload = function () {
                canvas.setBackgroundImage(img.src, canvas.renderAll.bind(canvas), {
                    originX: 'left',
                    originY: 'top',
                    left: 0,
                    top: 0
                });

// initially sets width of canvas to fit the image
                canvas.setDimensions({
                    width: img.width,
                    height: img.height
                });
            };
// below is a call to function that resizes the canvas
resizeCanvas();

//sets listener to resize event
            window.addEventListener('resize', resizeCanvas);
您的resizeCanvas()应该如下所示:

resizeCanvas(width, height) {
  canvas.backgroundImage.scaleToWidth(width);
  canvas.backgroundImage.scaleToHeight(height);
  canvas.setDimensions({width: width, height: height});
  canvas.renderAll();
}

你应该没事。

我在Angular 2+中的解

@HostListener('window:resize', ['$event'])
      resizeCanvas(event?: any) {
        const width: number = (window.innerWidth > 0) ? window.innerWidth : screen.width;
        const height: number = (window.innerHeight > 0) ? window.innerHeight : screen.height;
        const widthn: number = width * 0.7;
        const heightn: number = height - 100;
        canvas.setDimensions({
          width: widthn,
          height: heightn,
        });
      }

通过保留图像的纵横比来设置背景图像,并将其设置在画布的中心

const setBackgroundFromDataUrl = (dataUrl, options = {}) => {
  if (!dataUrl) { return true; }

  let img = new Image();
  img.setAttribute('crossOrigin', 'anonymous');
  img.onload = () => {

   // maxWidth // Max width of the canvas
   // maxHeight //Max height of canvas

    let imgWidth = img.width; // Current image width
    let imgHeight = img.height; // Current image height

    let widthAspectRatio = maxWidth / imgWidth;
    let heightAspectRatio = maxHeight / imgHeight;

    let finalAspectRatio = Math.min(widthAspectRatio, heightAspectRatio)

    let finalHeight = imgHeight * finalAspectRatio;
    let finalWidth = imgWidth * finalAspectRatio;

    let imgTop = 0;
    if (maxHeight > finalHeight) {
      imgTop = (Math.round(maxHeight) - Math.round(finalHeight)) / 2;
    }

    let imgLeft = 0;
    if (maxWidth > finalWidth) {
      imgLeft = (Math.round(maxWidth) - Math.round(finalWidth)) / 2;
    }

    let nsgImage = new fabric.Image(img).scale(finalAspectRatio);
    nsgImage.set({ left: imgLeft, top: imgTop });

    canvas.setBackgroundImage(nsgImage, () => canvas.renderAll());

  }

  img.src = dataUrl;

};

我可以用JSFIDLE吗?埃塞尔,我也面临同样的问题。你找到解决方案了吗?是的,刚刚发布了答案0.7和100代表什么?