简体   繁体   English

Fabric.js 中的画布坐标有偏移

[英]Canvas coordinates in Fabric.js have offset

I just started with fabric.js and I have a (probably beginner) error:我刚从 fabric.js 开始,我有一个(可能是初学者)错误:

I am using fabric with jquery with the following code:我正在使用带有以下代码的 jquery 结构:

$(document).ready(function () {

canvas = new fabric.StaticCanvas('scroller');

canvas.add(
  new fabric.Rect({ top: 0, left: 0, width: 140, height: 100, fill: '#f55' })
);

});

This code should draw a 140x100 Rectangle on the canvas.此代码应在画布上绘制一个 140x100 的矩形。 Sadly, only a quarter of the Rectangle appears.遗憾的是,只出现了 Rectangle 的四分之一。 If I change the top and left values to higher numbers, more of the Rectangle appears on the canvas.如果我将顶部和左侧的值更改为更高的数字,更多的 Rectangle 会出现在画布上。

So it seems, that the canvas origin is not at 0/0, but at sth.因此,画布原点似乎不在 0/0,而是在某处。 higher.更高。

Does someone know how to fix this or what I am doing wrong?有人知道如何解决这个问题或我做错了什么吗?

Thanks in advance, McFarlane在此先感谢,麦克法兰

Here is a jsfiddle link with some examples http://jsfiddle.net/pukster/sdQ7U/2/这是一个带有一些示例的 jsfiddle 链接http://jsfiddle.net/pukster/sdQ7U/2/

My guess is that fabric.js calculates everything from the origin (middle point) since it is drawing exactly one quarter of a rectangle even with a canvas 10 times the size of the rectangle.我的猜测是,fabric.js 从原点(中点)计算所有内容,因为即使画布是矩形大小的 10 倍,它也恰好绘制了矩形的四分之一。 My guess is that top and left actually refer to the origin and not the top and left sides of the imaginary bounding box.我的猜测是topleft实际上指的是原点,而不是假想边界框的顶部和左侧。 Trouble is there is very little documentation on fabricjs.问题是关于fabricjs 的文档很少。 Is there any reason you are using fabricjs and not easeljs你有什么理由使用fabricjs而不是easeljs

EDIT Here's the same fiddle but with squares instead of rectangles (it is more clear) http://jsfiddle.net/pukster/sdQ7U/3/编辑这是相同的小提琴,但使用正方形而不是矩形(更清楚) http://jsfiddle.net/pukster/sdQ7U/3/

EDIT OK I am now almost absolutely certain that fabric.js uses the center as the top / left .编辑好的我现在几乎可以肯定fabric.js 使用中心作为top / left I ripped their example off of their site and overlayed it with the transparent couterpart to those shapes had they been coded in pure canvas http://jsfiddle.net/pukster/uathZ/2/ (blue border is the limit of the canvas element).我把他们的例子从他们的网站上撕下来,并用透明的部分覆盖到那些形状上,如果它们是用纯画布编码的http://jsfiddle.net/pukster/uathZ/2/ (蓝色边框是画布元素的限制) .

叠加的纯帆布图像

What you see is that the boxes are exactly offset by half but the circle (I only drew a semi circle otherwise it would not have been discernable) is perfectly overlapped.你看到的是,盒子正好偏移了一半,但圆圈(我只画了一个半圆,否则它就无法辨别)完全重叠。 This is b/c in HTML Canvas, the circle coordinates (x,y) refer to the origin and not the top left.这是 HTML Canvas 中的 b/c,圆坐标(x,y)指的是原点而不是左上角。 I did not bother with the triangle b/c my trigonometry is a bit rusty.我没有理会三角形 b/c 我的三角函数有点生锈。 I personally think it's misleading to use the terms top and left , when x and y would have been more representative and shorter.我个人认为使用术语topleft会产生误导,因为xy会更具代表性和更短。

Yes, this is highly unexpected and even more undocumented.是的,这是非常出乎意料的,甚至更没有记录。

Workaround:解决方法:

Set

originX: "left"
originY: "top"

for each created object.对于每个创建的对象。

edit : or use kangax simpler solution in the comment below.编辑:或在下面的评论中使用 kangax 更简单的解决方案。

I want to comment but lack the reputation to do so.我想发表评论,但缺乏这样做的声誉。 So anyway, here is what happens when I do the following:所以无论如何,当我执行以下操作时会发生以下情况:

fabric.Object.prototype.originX = "left";
fabric.Object.prototype.originY = "top";

The shape gets rendered fine but when I select it for resizing or moving, it gets offset to a different location.形状渲染得很好,但是当我选择它来调整大小或移动时,它会偏移到不同的位置。 The best approach seems to be to set the coordinates for every object separately using the set() method.最好的方法似乎是使用 set() 方法分别为每个对象设置坐标。

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

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