javascript - 为什么在 FabricJS 中没有任何缩进的靠近放置的对象彼此之间有一个空格

标签 javascript html5-canvas fabricjs

我需要在 Fabric JS 中为我​​的图像构建一个框架。该框架应该动态构建以适应任何尺寸的图像。 为此,我使用两个图像:

我开发了一种算法,它可以实现并且有效!那太棒了。 但我对框架各部分的定位有疑问。 正如您在我的示例中看到的, block 之间存在奇怪的空间,并且它们在 Y 轴上不在同一水平上。我非常惊讶,因为这些部件具有相同的高度,并且它们的左侧和顶部具有正确的值。 有人知道这个问题的原因是什么吗? 提前致谢

JS fiddle https://jsfiddle.net/Eugene_Ilyin/gzjxhLtm/9/

var sideObj = new fabric.Rect(),  
  frameSideTopImage = new fabric.Image(new Image(), {type: 'image'}),            
  frameCornerTLImage = new fabric.Image(new Image(), {type: 'image'}),
  imageSize = 172,// Size of the image (width = height).
  leftOffset = 100,
  topOffset = 100;
  
// Load image for corner.
fabric.util.loadImage('/image/VnQpL.png', function (img) {
  // Set image and options for top left corner.
  frameCornerTLImage.setElement(img);
  frameCornerTLImage.setOptions({  	
  	strokeWidth: 0,
    left: leftOffset,
    top: topOffset
  });
   
  // Load image for side.
  fabric.util.loadImage('/image/QARFj.png', function (img) {
   	frameSideTopImage.setElement(img);
    
    // Create canvas for pattern.
    var canvasForFill = new fabric.StaticCanvas(fabric.util.createCanvasElement(), {enableRetinaScaling: false});
    canvasForFill.add(frameSideTopImage);

		// Configure top side for the frame.
    sideObj.setOptions({
    	strokeWidth: 0,
      objectCaching: false,
      originX: 'left',
      originY: 'top',
      left: imageSize + 100,
      top: 100,
      width: 800,
      height: imageSize,
      fill: new fabric.Pattern({
        source: function () {
          canvasForFill.setDimensions({
            width: imageSize,
            height: imageSize
          });
          return canvasForFill.getElement();
        },
        repeat: 'repeat-x'
      })
    });
    
    var canvas = new fabric.Canvas('c');
    var frame = new fabric.Group([sideObj, frameCornerTLImage], {
        strokeWidth: 0,
        width: 800 * 3,
        height: imageSize * 3,
        scaleX: 1/3,
        scaleY: 1/3,        
      }
    );
    canvas.add(frame);
    canvas.renderAll();
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.7.7/fabric.js"></script>
<body>
  <canvas id="c" width="1000" height="1000"></canvas>
</body>

最佳答案

var sideObj = new fabric.Rect(),  
  frameSideTopImage = new fabric.Image(new Image(), {type: 'image'}),            
  frameCornerTLImage = new fabric.Image(new Image(), {type: 'image'}),
  imageSize = 172,// Size of the image (width = height).
  leftOffset = 100,
  topOffset = 100;
  
// Load image for corner.
fabric.util.loadImage('http://i.imgur.com/w41HYN3.png', function (img) {
  // Set image and options for top left corner.
  frameCornerTLImage.setElement(img);
  frameCornerTLImage.setOptions({  	
    left: leftOffset,
    top: topOffset
  });
   
  // Load image for side.
  fabric.util.loadImage('http://i.imgur.com/3VqKv1O.png', function (img) {
   	frameSideTopImage.setElement(img);
    
    // Create canvas for pattern.
    var canvasForFill = new fabric.StaticCanvas(fabric.util.createCanvasElement(), {enableRetinaScaling: false});
    canvasForFill.add(frameSideTopImage);

	// Configure top side for the frame.
    sideObj.setOptions({
      objectCaching: false,
      originX: 'left',
      originY: 'top',
      left: imageSize + 100,
      top: 100,
      width: 800,
      strokeWidth:0,
      height: imageSize,
      fill: new fabric.Pattern({
        source: function () {
          canvasForFill.setDimensions({
            width: imageSize,
            height: imageSize
          });
          return canvasForFill.getElement();
        },
        repeat: 'repeat-x'
      })
    });
    
    var canvas = new fabric.Canvas('c');
    canvas.add(sideObj);
    canvas.add(frameCornerTLImage);
    canvas.renderAll();
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.7.7/fabric.js"></script>
<body>
  <canvas id="c" width="1000" height="1000"></canvas>
</body>

设置 strokeWidth:0 ,StrokeWidth 默认为 1,因此它会创建宽度为 1 的边框。

关于javascript - 为什么在 FabricJS 中没有任何缩进的靠近放置的对象彼此之间有一个空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46109525/

相关文章:

javascript - Fabric JS 自定义光标样式

javascript - Fabric.js 动画对象/图像

javascript - dom完成后调用javascript

javascript - Html Webpack 插件 - 如何将页眉/页脚 html 部分导入正文模板

javascript - 使用 HTML5 Canvas 以编程方式绘制此图案

javascript - HTML5 Canvas 方格图案

javascript - 将值的总和放在圆环图的中心?

z-index - 在 Fabric.js 中控制 z-index

javascript - Rails 3.1 - 调用 AJAX 请求两次

Php 视频播放百分比