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

.panZoom is not a function on ES6 modules #76

Open
lucafaggianelli opened this issue Oct 8, 2020 · 5 comments
Open

.panZoom is not a function on ES6 modules #76

lucafaggianelli opened this issue Oct 8, 2020 · 5 comments

Comments

@lucafaggianelli
Copy link

I'm trying to use panzoom with SVG.js v3 in a React app with webpack and ES6 modules,

import { SVG } from '@svgdotjs/svg.js'
import '@svgdotjs/svg.panzoom.js'

const draw = SVG()
      .addTo(mapRef.current!)
      .panZoom()

but I receive an error at runtime:

Unhandled Rejection (TypeError): Object(...)(...).addTo(...).panZoom is not a function

consider also that I'm using TypeScript and it is compiling correctly without warnings or error.

I also add a question, I'd like to panzoom an embedded svg, which I'm importing from an HTTP request, that is I do:

const response = await axios.get('/images/world-map.svg')
draw.svg(response.data)

should I call .panzoom() on the root svg object or on the imported one?

@lucafaggianelli lucafaggianelli changed the title panzoom not working on ES6 modules .panZoom is not a function on ES6 modules Oct 8, 2020
@Fuzzyma
Copy link
Member

Fuzzyma commented Oct 9, 2020

Your code should work as is. You can take a look at the transpired source to see what's going wrong.

For your second question: it doesn't matter. You could also just load the whole svg as is with SVG(svg string) and append that somewhere. This way you don't end up with a nested svg

@lucafaggianelli
Copy link
Author

OK thanks for the second question, this is very useful!

About the other issue, I'll run some investigation on a smaller app and keep you updated

@omixer
Copy link

omixer commented Mar 3, 2021

@lucafaggianelli Any updates on this issue? I am getting the same error message in Angular10!

@omixer
Copy link

omixer commented Mar 3, 2021

Actually after changing the svg.js import statement (was working fine in Angular8/9) from

import SVG from '@svgdotjs/svg.js/src/svg'

to

import { SVG } from '@svgdotjs/svg.js'

the error disappeared and panZoom works again!

@lucafaggianelli
Copy link
Author

Hey @omixer at the end I understood there was a strange case with my SVG file, as it included a comment at the beginning, so I needed to use this code, notice the part where I manipulate the SVG to remove the comment, but then no issue anymore with TS, I don't know if there was some update at some point:

import { Box as BBox, SVG, Path } from '@svgdotjs/svg.js'
import '@svgdotjs/svg.panzoom.js'

const response = await axios.get('/images/world-map.svg')

let startOfSvg = response.data.indexOf('<svg')
startOfSvg = startOfSvg >= 0 ? startOfSvg : 0

const map = SVG(response.data.slice(startOfSvg))
  .addTo(mapRef.current)
  // @ts-ignore
  .size('100%', 450)
  .panZoom()

@Fuzzyma the TS issue seems gone (i'm using v3.0.16), but the problem is that when I load an SVG file starting with a comment, my map variable is of type Comment not and SVG element, thus I needed this string manipulation trick

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

3 participants