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

Pathdata fails to instantiate at new paper.Path(...):TypeError: Cannot read property 'getDirectedAngle' of null #1885

Open
nikkwong opened this issue Dec 20, 2020 · 7 comments · May be fixed by #1957
Assignees

Comments

@nikkwong
Copy link

nikkwong commented Dec 20, 2020

Here's the sketch:

This is the offending pathdata:

new paper.Path("M86.41 449.42a22.39 22.39 0 0128.69-6.59c17.8 9.54 46.13 23.67 67.23 29.67 25.8 7.29 73.27 24.36 93.21 31.62a7.39 7.39 0 009.91-6.44c1-15 2.86-43.37 2.84-57.65 0-32.85 0-21.6 3.38-55.66 1.67-16.72 9.39-45.22 57.75-74.63 21.6-13.13 39.48-8 52.67 1.68-13.31-14.77-35.33-29.15-63.78-11.85-48.35 29.4-56.08 57.91-57.75 74.63-3.4 34.06-3.43 22.81-3.38 55.66 0 20-3.54 67.47-3.54 67.47s-69.19-25.61-102.42-35S86.9 423.2 86.9 423.2l-4.69 22.67s-3-2.33 4.15 3.54z")

http://sketch.paperjs.org/#V/0.12.7/S/TZHRSgNBDEV/JeyTggk7SSYz038QhD5aH9a6UKm2pS0Iiv/unVbQl92byczNSfI17Kb3eVgMy+18Xm+Gu2G9f+nxbv6gw3SYj/IwnTc3q+G+hngi9yauk6pYo+t3pDFplWgckts6FanUJDt5SDJSkygURRS6da0ZNxA3KiaK2MWCGnQiSxI6le5bfs3HJi3B232dOGVSqcFuYqVL51wkMo1siPpfYUFIV85ZIghh4RRSFFjW2LOoEl6VzMUlgIUrnKzTGtqrXClrJ8V57QlDYZdS2LKYMdpImcOkIJtQlb2K5d4ecELG2u0BfSlClyJs4mQuY3RlfXY1ccekK+ZIih763DArL//kiQMFG2NuAZBRsQGQLLGRRo75Kv3JN0a1y2oCD8EKYHLwUjf8XA23WPLzcZ62h/3r7nwaFo9P3z8=

EDIT: Seems like the importing pathData function is not very stable? I am getting this error on about half of the non-trivial path data items I'm trying to import. Here's another one:

new paper.Path("M179.536 51.685c3.163-.518 11.68-.875 13.25-.875 1.004 0 2.092.23 2.687-.692.486-.755.308-2.031.453-2.907.17-1.018.409-2.022.651-3.024.69-2.85 1.733-5.562 3.192-8.108 2.738-4.777 6.75-8.519 11.814-10.696 5.593-2.404 11.921-2.955 17.937-2.467 6.437.522 12.264 2.647 18.342 4.646 5.792 1.906 11.858 2.656 17.916 1.726 6.104-.938 10.715-4.246 15.863-7.386 5.27-3.214 11.293-5.316 17.524-4.833 5.815.451 11.176 2.853 15.985 6.06 4.5 3.002 8.212 6.613 10.308 11.695 2.255 5.467 2.253 11.468-1.455 16.3-5.375 7.003-15.353 8.814-23.654 8.634a45.193 45.193 0 01-14.05-2.575c-6.046-2.127-11.27-5.73-16.926-8.655-2.654-1.372-5.398-2.5-8.274-3.311-2.79-.788-5.539-1.15-8.065.536-2.673 1.784-5.068 3.536-8.126 4.652-2.934 1.071-6.04 1.67-9.166 1.646-6.08-.047-12.075-1.942-17.98-3.21-4.816-1.035-9.97-2.067-14.88-1.072-2.048.415-5.04 1.272-5.78 3.401 12.454 2.257 17.345 17.66 13.834 28.967-1.895 6.103-13.286 13.237-17.715 10.029-2.806-2.033-1.102-4.701 0-6.463 2.111-3.373 2.834-7.706-1.073-10.055-3.37-2.026-6.913 2.785-6.98 2.78-.926-.074-8.928-4.6-14.442-11.204-5.44.323-6.265-3.264-4.966-6.197 1.299-2.933 9.406-6.625 13.776-7.342z")

EDIT: Ok, guess it's because the # of arguments for the arc commands are not 7. For example, it seems to be choking at: a45.193 45.193 0 01-14.05-2.575

However, browsers are rendering this just fine, and seems like SVG tools are exporting arc commands with less than 7 arguments, including illustrator. Is there a possible workaround here?

EDIT: Ok, I see what's happening. The large arc flag and sweep flags are concatenated without a space between them by some programs including illustrator. I.e. the 01 in a45.193 45.193 0 01-14.05-2.575. Browsers seem to understand this and parse each of those parameters individually, but paper understands them as a single value and doesn't split them.

Related to #1808, which renders 'correctly' in browsers, but chokes in paperjs. Seems like the way browsers handle this one is even more bizarre: a150 150 0 011.593 9.98, it will eagerly match digits to fill the sweep and arc flags, so that command is actually interpreted as a 150 150 0 0 1 .593 9.98. Think the way to fix this would be by fixing the very complicated regex here: part.match(/[+-]?(?:\d*\.\d+|\d+\.?)(?:[eE][+-]?\d+)?/g); not sure if I'm up for it..

@nikkwong nikkwong changed the title Path fails to import with importSVG: TypeError: Cannot read property 'getDirectedAngle' of null Pathdata fails to instantiate at new paper.Path(...):TypeError: Cannot read property 'getDirectedAngle' of null Dec 20, 2020
@nikkwong
Copy link
Author

Another annoying case of these:

a4.695 4.695 0 014.672-3.082 79.28 79.28 0 001.697-4.434

@nikkwong
Copy link
Author

nikkwong commented Dec 20, 2020

This logic passes several hundred cases for me (line 8187 of paper-full.js):

if (coords.find((x,i) => i && (!(i % 3) || !(i % 4)) && (x != "0" && x != "1"))) {

    let i = 0;
    while(i++ < coords.length) {

        if (i % 7 == 3) {

            const item = coords[i]

            if (item.length > 1) {

                if (item.length == 2)

                    coords = [].concat(
                        coords.slice(0, i),
                        item[0],
                        item[1],
                        coords.slice(i + 1)
                    )

                else if (item.length > 2)
                    
                    coords = [].concat(
                        coords.slice(0, i),
                        item[0],
                        item[1],
                        item.slice(2),
                        coords.slice(i+1)
                    )

            }

        }

    }

    length = coords.length

}

@lehni
Copy link
Member

lehni commented Mar 22, 2021

I'm finally looking into this. @nikkwong could you post more context of the code above? It doesn't seem to make sense at lie 8187 of paper-full.js, v0.12.11 - v0.12.15…

@lehni
Copy link
Member

lehni commented Mar 22, 2021

I did a test with https://github.com/nfroidure/svg-pathdata, which parses it correctly:

Input: a4.695 4.695 0 014.672-3.082 79.28 79.28 0 001.697-4.434
Output:

{
  commands: [
    {
      type: 512,
      relative: true,
      rX: 4.695,
      rY: 4.695,
      xRot: 0,
      lArcFlag: 0,
      sweepFlag: 1,
      x: 4.672,
      y: -3.082
    },
    {
      type: 512,
      relative: true,
      rX: 79.28,
      rY: 79.28,
      xRot: 0,
      lArcFlag: 0,
      sweepFlag: 0,
      x: 1.697,
      y: -4.434
    }
  ]
}

@GrahamSH-LLK
Copy link

GrahamSH-LLK commented Mar 31, 2021

I get the same error when running

paper.project.importSVG(`<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor"> <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15" /> </svg>`);

on http://sketch.paperjs.org/#V/0.12.15/S/ZZBRT8MgEMe/CuFJE0uBFrfVuUR9cC8+LfFlNRm2OFhbIJS1S4zf3eu2+GJCDu73/3Mc942t7BQu8KZRsdL4DleunnIvvQrEB3dQVSSm8y7EzfvrzW7ZD3t06lrbP5ZYx+iLNB3HkYwZcWGfckppCpYSoy/TtuCxzirIBqPGZ3cCQBFFPIcFtI/BNQpgdQxB2fjiWhdKvEJLL6O+yklrrKqkB1twR1v/3TsLB2fsf2U0ddSAOaAa9rcc5YPQRMx5xwTJxD3iT3NCKUOXCF3RfJLRooPzetExhhgbEqETwGyCcrJm6BIhZ8m51Cy51pwhvmYCXkzhA9MUVrvbB5jpZ1Cy8dBn7HGx/fj5BQ==

@amcc
Copy link

amcc commented Jun 16, 2021

i'm also getting this error. Is there a fix, or at least a find/replace i can do in the svg code to make it work. Used illustrator files with single path - tried running them through SVGOMG and that doesn't help

@amcc
Copy link

amcc commented Jun 16, 2021

my issue before was though using illustrator's Export for Screens, or Export as... - however if i use Save as and make the same file an SVG i do not get this issue.

running that same file through SVGOMG trims anything unnecessary and this still works fine. Do other people with this issue also use illustrator for exporting?

@lehni lehni self-assigned this Oct 30, 2021
AS-MCMXCIX pushed a commit to AS-MCMXCIX/paper.js that referenced this issue Dec 19, 2021
@AS-MCMXCIX AS-MCMXCIX linked a pull request Dec 19, 2021 that will close this issue
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants