简体   繁体   English

画布中的鼠标偏移[fabric.js]

[英]Mouse offset in canvas [fabric.js]

I've read 3-5 topics about mouse offset, but i still can't get where is the mess. 我已经阅读了关于鼠标偏移的3-5个主题,但我仍然无法得到混乱的地方。

In my case everything works fine in 60%. 在我的情况下,一切正常,60%。 In other 40% mouse is offset. 在其他40%的鼠标是抵消。 Demo here . 在这里演示

Sometimes object position doesn't relate to mouse behavior. 有时对象位置与鼠标行为无关。 (IE & Chrome have the bigest mess) (IE和Chrome有最大的混乱)

I've tried to edit stylesheets and parent div but nothing. 我试过编辑样式表和父div但什么都没有。 The worst thing: i don't see any regularity. 最糟糕的事情:我没有看到任何规律性。 I will be greatful for any help. 我会很乐意帮助你。

You can do this: 你可以这样做:

canvas.on("after:render", function(){ canvas.calcOffset() });

I only do this after I create canvas. 我只在创建画布后才这样做。 This is interim call when there is no resize event. 当没有调整大小事件时,这是临时调用。 That is when bug appears. 那就是bug出现的时候。

Due to positioning of other elements on the page the canvas offset value may change. 由于页面上其他元素的定位,画布偏移值可能会改变。

You just needed to call canvas.calcOffset() after adding an element in the canvas that caused the problem or after calling the canvas.renderAll() method in the code. 您只需要在canvas.calcOffset()添加导致问题的元素或在代码中调用canvas.renderAll()方法之后调用canvas.renderAll()

This offset issue occurs whenever the canvas is moved within the HTML document. 只要在HTML文档中移动画布,就会发生此偏移问题。 For example, if you add an element of 10px in height above the canvas, after the canvas is first rendered, your objects will be offset by 10px. 例如,如果在画布上方添加高度为10px的元素,则在首次渲染画布后,对象将偏移10px。 Add this code whenever the canvas is repositioned within the HTML doc, and it should recalculate the mouse interactivity: 每当画布在HTML文档中重新定位时添加此代码,它应重新计算鼠标交互性:

canvas.on("after:render", function(){
    canvas.calcOffset();
});

Fabric.js automatically calls this method whenever the window is resized, which is why you don't see the issue on that event. 每当窗口调整大小时,Fabric.js都会自动调用此方法,这就是您在该事件上看不到问题的原因。

It is unbelievable! 真令人难以置信! I've fixed the bug. 我修复了这个bug。 You will never believe me... 你永远不会相信我......

In the top of the page it was code like: 在页面顶部,代码如下:

<div class="logo">
<a href="/"><img src="logo.png" alt="" /></a>
</div>

This code has no reasonable relation to canvas. 此代码与canvas没有合理的关系。 This class have simple css: {float: left; margin: 10px 0 0 0;} 这个类有简单的css: {float: left; margin: 10px 0 0 0;} {float: left; margin: 10px 0 0 0;}

But for some reason this code forced mouse offset in canvas. 但由于某种原因,此代码强制在画布中使用鼠标偏移。 I remade this code like: 我重新编写了这样的代码:

<div class="logo">
<a href="/" class="logoHref"></a><!--- image is in css bg --->
</div>

... and now everything thinks to work fine. ......现在一切都认为工作正常。

I dont see any corelation between these events but the fact is the fact. 我没有看到这些事件之间的任何关联,但事实是事实。 That was a damn hard day for me... 那对我来说真是个艰难的一天......

Offset positioning of an object within a canvas is most likely caused by the original coordinates of the canvas and child objects being changed by DOM manipulation. 画布中对象的偏移定位很可能是由画布的原始坐标和DOM操作更改的子对象引起的。

A simple solution I found is to make sure I set the coordinates of any object that I add to the canvas immediately after adding it. 我发现一个简单的解决方案是确保在添加后立即设置我添加到画布的任何对象的坐标。 For example, if you assume the object is an image, you would do the following: 例如,如果您假设对象是图像,则执行以下操作:

var canvas = new fabric.Canvas(); 
var image = new Image();

canvas.add( image );
image.center() // Optional if you wish the object centered on canvas
image.setCoords();

Hope this helps. 希望这可以帮助。 Good luck! 祝好运!

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

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