简体   繁体   English

为什么FabricJS在不同的设备/浏览器上缩放模式会有所不同?

[英]Why does FabricJS scale pattern differently on different devices/browsers?

See Fiddle : I am trying to render a background image on a FabricJS polygon, but for some reason the scale of the pattern is different on different devices / browsers for no obvious reason. 参见Fiddle :我试图在FabricJS多边形上渲染背景图像,但是由于某种原因,图案的比例在不同的设备/浏览器上是不同的,没有明显的原因。

Here is the code: 这是代码:

HTML: HTML:

<canvas id="tcanvas" width="200" height="200" />

JavaScript: JavaScript的:

function testPattern(id) {
  var url = 'https://dummyimage.com/200x200/aff/000',
      tCanvas = new fabric.Canvas(id);

  new fabric.Image.fromURL(url, function(img) {
     var tPoints = [{
       x: 0,
       y: 0
     },
     {
       x: 0,
       y: 200
     },
     {
       x: 200,
       y: 200
     },
     {
       x: 200,
       y: 0
     }
   ];

   var tPatternWidth = 200;
   var tPatternHeight = 200;

   var tImgXScale = tPatternWidth / img.width;
   var tImgYScale = tPatternHeight / img.height;

   img.set({
     left: 0,
     top: 0,
     scaleX: tImgXScale,
     scaleY: tImgYScale
   });

   var tPatternSourceCanvas = new fabric.StaticCanvas();
   tPatternSourceCanvas.add(img);
   tPatternSourceCanvas.renderAll();

   var tPattern = new fabric.Pattern({
     offsetX: 0,
     offsetY: 0,
     source: function() {
        tPatternSourceCanvas.setDimensions({
          width: tPatternWidth,
          height: tPatternHeight
        });
        tPatternSourceCanvas.renderAll();
        return tPatternSourceCanvas.getElement();
      },
      repeat: 'no-repeat'
    });

    var tImgPoly = new fabric.Polygon(tPoints, {
      left: 100,
      top: 100,
      originX: 'center',
      originY: 'center',
      fill: tPattern,
      objectCaching: false,
      selectable: false,
      stroke: 'red',
      strokeWidth: 1
    });

    tCanvas.add(tImgPoly);
    tCanvas.renderAll();
  });
}

testPattern('tcanvas');

This is what I see on my desktop Chrome: 这是我在台式机Chrome上看到的内容:

在此处输入图片说明

... and this what I see on a (Samsung) tablet Chrome: ...这就是我在(Samsung)平板电脑Chrome上看到的内容:

在此处输入图片说明

How can I fix the code so that the both patterns look the same, ie, like the first one? 我该如何修复代码,使这两种模式看起来相同,即像第一个一样?

It sa retina support side effect. 这是视网膜支持的副作用。

When initializing the canvas for the Pattern, please pass as option: 为模式初始化画布时,请作为选项传递:

enableRetinaScaling: false

var tPatternSourceCanvas = new fabric.StaticCanvas(null, {enableRetinaScaling: false});

This will avoid fabric trying to enhance the canvas twice. 这样可以避免织物尝试两次增强画布。

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

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