简体   繁体   English

Fabric.js添加简单路径不起作用

[英]Fabric.js adding simple path not working

I'm trying to create a simple path object using Farbic.js. 我正在尝试使用Farbic.js创建一个简单的路径对象。 When the path is added to canvas, it does not appear to be positioned correctly. 将路径添加到画布后,它似乎没有正确定位。 Here is my code: 这是我的代码:

HTML HTML

<canvas id="c" height="300" width="300"></canvas>

JS JS

var canvas = new fabric.Canvas('c');
canvas.backgroundColor = '#f5f5f5';
var line = new fabric.Path('M 0 0 L 100 100',{
    stroke: 'black',
    fill: ''
});
canvas.add(line);

According to the data I'm passing in, the path should start at point 0,0 and draw a line to point 100, 100. However, my line is actually being placed at point 50,50, as you can see by the alert. 根据我传递的数据,路径应从0,0点开始,并画一条线到100,100点。但是,实际上我的线位于50,50点,如警报所示。

Why isn't the path starting at point 0,0? 为什么路径不是从点0,0开始?

Here's a fiddle: http://jsfiddle.net/flyingL123/h14th5pf/3/ 这是一个小提琴: http : //jsfiddle.net/flyingL123/h14th5pf/3/

You can specify the position properties of your path explicitly. 您可以显式指定路径的位置属性。

line.set({top: 0, left: 0});

Fabric.JS seems to include some sort of computed padding by default, based on the line length and canvas size. 默认情况下,Fabric.JS似乎根据行长和画布大小包括某种计算出的填充。 Try adjusting your canvas dimensions and line length and you'll see the top and left values update automatically. 尝试调整画布尺寸和线长,您会看到topleft值会自动更新。

Edit : It appears Fabric.JS may use the center of the canvas to calculate the X,Y positions for canvases. 编辑 :似乎Fabric.JS可以使用画布的中心来计算画布的X,Y位置。 It may use a similar scheme for path origin points. 它可以对路径原点使用类似的方案。 See this related StackOverflow question: 请参阅以下相关的StackOverflow问题:

Canvas coordinates in Fabric.js have offset Fabric.js中的画布坐标具有偏移

It seems to be part of how the library calculates positions automatically. 这似乎是图书馆如何自动计算位置的一部分。 Per the comments of the linked question, you can fix this with: 根据链接问题的评论,您可以使用以下方法解决此问题:

fabric.Object.prototype.originX = true; 
fabric.Object.prototype.originY = true;

And you wont have to adjust the originX or originY properties on each path object. 而且您不必调整每个路径对象上的originXoriginY属性。

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

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