question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

zoomToElement appears to not function when selection Element inside of SVG

See original GitHub issue

When passing the ID of a <polygon> element inside an SVG to the zoomToElement function as a string (or as the event.target object of a click event) no transformation occurs.

Browser: Chrome Version 91.0.4472.114 (Official Build) (x86_64)

package.json

...
"dependencies": {
    "@material-ui/core": "^4.11.4",
    "@material-ui/icons": "^4.11.2",
    "@types/react": "^17.0.11",
    "@types/react-dom": "^17.0.8",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-pdf": "^5.3.0",
    "react-scripts": "^4.0.3",
    "react-zoom-pan-pinch": "^2.1.1",
    "typescript": "^4.3.4",
}
...

My code:

import React from 'react';
import {ReactComponent as ActualSVG} from '../assets/someSVG.svg'
import {TransformWrapper, TransformComponent} from "react-zoom-pan-pinch";
import {Button} from "@material-ui/core";

function SVGComponent
return (
            <TransformWrapper initialScale={1}
                              limitToBounds={false}
                              centerOnInit={true}
                              minScale={0.5}
            >
                {({zoomToElement, resetTransform}) => (
                        <TransformComponent>
                            <Button onClick={()=>zoomToElement("Box_2")}/>
                            <div>
                                <ActualSVG />
                            </div>
                        </TransformComponent>
                )}
            </TransformWrapper>
    )
}

When clicking the <Button> I was expecting the wrapped SVG to zoom to the element with the id “Box_2” but no transformations occur. If I continue to click the <Button> every 3-4 times an error is logged in console saying “No mouse or touch offset found”.

Please let me know if there is anything more I can provide.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
Tylermarquescommented, Sep 8, 2022

@im-amir No solution at this time.

0reactions
ggomaengcommented, Sep 30, 2022

I was trying to solve this issue too and found a workaround this issue, but it’s kinda hacky.

I created a separate div container that only holds divs, which are mapped to the x, y positions of each svg elements.

Not the best solution, but works!

image

Read more comments on GitHub >

github_iconTop Results From Across the Web

D3 Selection Works while svg Selection Doesn't - Stack Overflow
d3.select() accepts two types of arguments -- a selector string and a DOM element. selection.select() ( svg.select() in your case) accepts ...
Read more >
<foreignObject> - SVG: Scalable Vector Graphics | MDN
The <foreignObject> SVG element includes elements from a different XML namespace. In the context of a browser, it is most likely (X)HTML.
Read more >
Detecting click events on SVGs with vanilla JS event delegation
An SVG is an HTML object, with nested items inside it. When you click on the SVG, the event.target is no longer the...
Read more >
Create SVG Elements using D3.js - TutorialsTeacher
And all your SVG elements like <rect> , <line> , <circle> , <text> would go inside this <svg> tag. Each SVG element has...
Read more >
Mimic Relative Positioning Inside an SVG with Nested SVGs
Positioning elements inside an SVG image is very similar—if not ... By selecting an element in the graphics editor, you can retrieve the ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found