zoomToElement appears to not function when selection Element inside of SVG
See original GitHub issueWhen 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:
- Created 2 years ago
- Comments:7 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@im-amir No solution at this time.
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!