Jump to content

Bug in ActionManager system - wrong event positionning in some cases


Vousk-prod.
 Share

Recommended Posts

Hi !

I think I pointed a bug in ActionManager...

 

Before discovering this marvelous system, I used to use scene.pick(evt.x, evt.y)

 

But when the canvas is not full screen absolute position (0, 0), the (evt.x, evt.y) click position is not the good one for scene.pick (because it is considered as relative to the canvas, but the returned values are in fact absolute inside the HTML page, we then need to take account of the canvas position itself to correct the evt position)

 

To correct that, I wrote this :

evtRelPos = getPointerEventRelativePosition(evt, canvas);function getPointerEventRelativePosition( evt, element ){    return { x: evt.clientX - element.getBoundingClientRect().left, y: evt.clientY - element.getBoundingClientRect().top };}scene.pick(evtRelPos.x, evtRelPos.y);

It was all good.

 

But now with the ActionManager I again suffer from the wrong positionning of the events when the canvas is lost somewhere in the HTML page. Maybe it would be easy to correct this with the little piece of code above.

Link to comment
Share on other sites

Yes... but no ^_^

 

evt.offsetX is stupid, it doesn't take correctly CSS positionning in account, only HTML positionning (or something like that...). Don't even mention when a page is composed of a complex mix of absolute, relative, and fixed elements.

 

Look at your exemple through a little jsfiddle :

http://jsfiddle.net/vouskprod/3ueww/

 

Try to locate your mesh, you will need to click above them...

With my little snippet (getPointerEventRelativePosition), your event logic should be fixed.

Link to comment
Share on other sites

Hmmm... I see no difference :huh: ... which version of bjs should I use to test ?

(I tried 1.12, 1.13-beta and 1.13-beta-debug)

 

Here is the fiddle with 1.13-beta, same behaviour as before :

http://jsfiddle.net/vouskprod/3ueww/

 

[EDIT : it works, I forgot to add hand.js lib in this test, but another bug, see post below]

Link to comment
Share on other sites

In fact, I updated the fiddle to demonstrate that the code is working until we scroll the html page.

 

Try to scroll just a little (reduce your browser size or increase the CSS .some_space class width or height) and you'll have to click your object at a distance equal to scroll amount.

 

http://jsfiddle.net/vouskprod/3ueww/

Link to comment
Share on other sites

I've just did a new fiddle with some debug log to show difference between ActionManager events positionning and classic one.

Scroll the page to see the problem with ActionManager system (I put a 2000px wide DIV so I'm pretty sure everybody will be able to scroll at least horizontally  :P )

 

http://jsfiddle.net/vouskprod/DqRUn/

 

(classic positionning calculations and logging info are at the end of the script)

Link to comment
Share on other sites

Hrm, that's strange, there must be a difference somewhere.


I'll try to figure out the problem, where did you make all these events positionning stuff in your code ?


 


About performance : I did some test here using getBoundingClientRect and it seems that on older PCs it is a little time consuming compared to simple canvas.style.left, canvas.width..., but those don't take windows resizing or dynamic CSS in account, I'll do some experiment with resize event listeners, I'll let you know my results.


Link to comment
Share on other sites

After some tests getBoundingClientRect seems actually little time consuming. The only way I found is to set a permanent var canvasBounds = getBoundingClientRect(), use this var for every calculations relative to positionning, and update this var each time elements are resized. I put that in the window.addEventListener("resize"), but sometimes the canvas is resized and not the window, I then should use something like canvas.addEventListener("resize") but it seems to be never thrown, it's strange...

Deltakosh I tried to locate where you are using getBoundingClientRect(), to try to figure out the difference between my use which works in every browser and yours which bug in Firefox, but I didn't manage to find this function in your code... :huh:

(is it the hand.js file I have to update ?)

Link to comment
Share on other sites

Ah ah your code works perfectly well now that it has been commited :D:lol:

No need to debug anything! B)

 

Regarding getBoundingClientRect()'s cost, this method go recursively through all DOM elements and parents to calculate the actual final pos and width of the element (taking in account margins, paddings, borders, relative/absolute positionning of everything, etc). On my side I confirmed that on older PCs or mobiles/tablets the positionning of elements based on this function is a little delayed when camera or objects are moving fast (positionning is done every frame...), and it cleary makes the whole scene less fluid. That's why I decided to use the following technique instead of recalculating getBoundingClientRect each call (will be a perfect solution when I'll be able to catch a resize event of canvas, but I only manage to catch resize event of windows, don't know why) :

 

The only way I found is to set a permanent var canvasBounds = getBoundingClientRect(), use this var for every calculations relative to positionning, and update this var each time elements are resized. I put that in the window.addEventListener("resize"), but sometimes the canvas is resized and not the window, I then should use something like canvas.addEventListener("resize") but it seems to be never thrown, it's strange...

 

For your code, we can just watch and wait for users feedbacks in case of numerous event listeners. But according to your post :

 

Each time the mouse is moving it checks ALL actionManager to check if they have a trigger to launch. That's why :)

 

I don't exactly know where and how you call getBoundingClientRect(), but if it's really so often I can tell you for sure that it will make a big performance drop sooner or later...

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...