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

Brush mousedown/touchstart not getting correct position in scaled SVG #140

Open
ericgj opened this issue May 9, 2023 · 1 comment
Open

Comments

@ericgj
Copy link
Contributor

ericgj commented May 9, 2023

Not sure if I'm not doing something right, but I have a 1d (X) brush that works perfectly only when the outer width exactly matches the viewBox width. When it is scaled up (the actual width > viewBox width), the left side of the brush overlay gets shifted to the right, and by a factor of the x position in the SVG so that towards the left side it's almost correct, but as you move right, it gets further and further offset. Likewise when it is scaled down (actual width < viewBox width), the left side of the overlay gets shifted to the left. Otherwise, other manipulation of the brush (moving, stretching, dragging etc.) works fine, it's just the initial positioning of the brush that's off.

Unless I'm doing something wrong, I suspect something in the event decoders is not working right. Perhaps decodeSVGTransformMatrix is not finding and therefore adjusting by the viewBox dimensions? Maybe be related to #92 ? My understanding of this corner of the DOM is pretty sketchy, so I'm not clear how you are getting to the viewBox which is in an ancestor node, from the top-level brush g which I'd think is the currentTarget ? Does baseVal somehow magically get you the viewBox of its ancestor nodes?

Thanks for any suggestions. elm-visualization rocks.

decodeSVGTransformMatrix : Decoder (Maybe Matrix2x3)
decodeSVGTransformMatrix =
    D.oneOf
        [ D.map3
            (\viewBox width height ->
                Just ( ( viewBox.width / width, 0, 0 ), ( 0, viewBox.height / height, 0 ) )
            )
            (D.at [ "currentTarget", "viewBox", "baseVal" ] decodeRect)
            (D.at [ "currentTarget", "width", "baseVal", "value" ] D.float)
            (D.at [ "currentTarget", "height", "baseVal", "value" ] D.float)
        , D.succeed Nothing
        ]
@ericgj
Copy link
Contributor Author

ericgj commented May 9, 2023

Looking in Chrome dev tools it looks like the viewBox, width, and height data is put under currentTarget.viewportElement - described here as "The SVGElement which established the current viewport. Often the nearest ancestor <svg> element. null if the given element is the outermost <svg> element."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant