Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

isMouseOver Calculated Incorrectly for path Elements, Leading to Closing Powertip #135

Open
speedplane opened this issue Apr 6, 2015 · 6 comments
Labels
Bug Software defects or other problems that should be fixed.

Comments

@speedplane
Copy link

Powertip's isMouseOver function does not properly calculate the height of SVG elements, which causes it to determine that the mouse is not over an object when it really is.

The reason for this is that getBoundingClientRect does not include the height of stroke-width in its calculation. I fixed this with the following monkey-hack:

    function isMouseOver(element) {
        // use getBoundingClientRect() because jQuery's width() and height()
        // methods do not work with SVG elements
        // compute width/height because those properties do not exist on the object
        // returned by getBoundingClientRect() in older versions of IE
        var elementPosition = element.offset(),
            elementBox = element[0].getBoundingClientRect(),
            elementWidth = elementBox.right - elementBox.left,
            elementHeight = elementBox.bottom - elementBox.top;

        // Add stroke width to paths
        var (element[0].tagName == 'path') {
            var stroke = window.getComputedStyle(element[0])
                .getPropertyValue("stroke-width");
            if (stroke && stroke.length && parseFloat(stroke) > 0) {
                    elementHeight += parseFloat(stroke)
            }
        }

        return session.currentX >= elementPosition.left &&
            session.currentX <= elementPosition.left + elementWidth &&
            session.currentY >= elementPosition.top &&
            session.currentY <= elementPosition.top + elementHeight;
    }
@speedplane
Copy link
Author

I created a jsfiddle where you can see this bug:
https://jsfiddle.net/ojkkj8Le/
Try scrolling over the blue path. Notice how the powertip appears and then disappears.

@stevenbenner
Copy link
Owner

The JSFiddle example you supplied is using an outdated version of the library, version 1.1.0. SVG support was not added until version 1.2.0.

Here is a fork of your fiddle using version 1.2.0: https://jsfiddle.net/stevenbenner/Lv6t1bvq/

It no longer shows the unexpected closing behavior. However, it does seem to put the tooltip in an unexpected place.

@speedplane
Copy link
Author

It's still disappearing for me. Try moving your mouse below the middle of the svg (see updated fiddle):
https://jsfiddle.net/e6217g42/

@speedplane
Copy link
Author

Actually, try this jsfiddle: https://jsfiddle.net/e6217g42/2/

@stevenbenner
Copy link
Owner

What browser are you using?

I was able to reproduce the issue in Chrome. I cannot repro in Firefox or IE.

It seems that getBoundingClientRect() ignores the stroke width in Chrome (probably WebKit heritage, which would affect Safari and Opera as well). And getBBox() doesn't factor in the stroke on any browser.

This may be a very complicated fix because I have to handle rotation. I'll investigate.

@stevenbenner stevenbenner added the Bug Software defects or other problems that should be fixed. label Apr 6, 2015
@speedplane
Copy link
Author

Yes Chrome. It does seem complicated, my Monkey hack only works for non-rotated paths, and only does height, not width. You may want to consider turning off the desync check for path elements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Software defects or other problems that should be fixed.
Projects
None yet
Development

No branches or pull requests

2 participants