two.js icon indicating copy to clipboard operation
two.js copied to clipboard

Incorrect SVG loading/interpreting

Open Neglexis opened this issue 8 years ago • 19 comments

I have the following two external SVG's

Left arm left

Right arm right

However, after being loaded with two.load, this is the result

image

This is the result with only the two arms rendinger image

I have tried both two.load and two.interpret, and I've also tried Two.Types.canvas and Two.Types.svg (prefer canvas, but tried both for completeness).

This is the SVG content for leftarm

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 170.68"><title>leftarm</title><path d="M161.38,116h0v0a20.51,20.51,0,0,1-16.73,23.69L56.2,155a20.51,20.51,0,0,1-23.67-16.73l-2.95.51a10.28,10.28,0,0,1-11.85-8.37h0a10.28,10.28,0,0,1,8.36-11.84l13.14-2.22L39,115.09q-.09-.51-.12-1a10.27,10.27,0,0,1,9.5-11,10.47,10.47,0,0,1,10.88,8.8l.16.92L137.7,99.34A20.5,20.5,0,0,1,161.38,116Z" fill="#f8b195"/></svg>

When using Two.Types.svg, we can expect the resulting SVG, which gives me this

<g id="two_200" transform="matrix(1 0 0 1 149 290)" opacity="1"><path transform="matrix(1 0 0 1 0 0)" d="M 71.755 -11.167 L 71.755 -11.167 L 71.755 -11.167 L 71.755 -11.167 L 72.022 -6.583 L 71.263 -2.054 L 69.515 2.193 L 66.866 5.943 L 63.448 9.011 L 59.434 11.242 L 55.025 12.523 L 55.025 12.523 L -33.425 27.833 L -33.425 27.833 L -38.006 28.096 L -42.531 27.334 L -46.774 25.585 L -50.521 22.936 L -53.586 19.521 L -55.814 15.509 L -57.095 11.103 L -57.095 11.103 L -60.045 11.103 L -60.045 11.103 L -62.337 11.233 L -64.601 10.85 L -66.724 9.975 L -68.599 8.65 L -70.134 6.943 L -71.251 4.937 L -71.895 2.733 L -71.895 2.733 L -71.895 2.733 L -71.895 2.733 L -72.023 0.444 L -71.64 -1.817 L -70.766 -3.937 L -69.443 -5.811 L -67.738 -7.344 L -65.736 -8.461 L -63.535 -9.107 L -63.535 -9.107 L -50.395 -11.327 L -50.625 -12.077 C -11.625 103.013 -50.715 -12.587 -50.745 -13.077 L -50.745 -13.077 L -50.656 -15.377 L -50.058 -17.6 L -48.979 -19.633 L -47.474 -21.375 L -45.62 -22.738 L -43.508 -23.654 L -41.245 -24.077 L -41.245 -24.077 L -41.085 -24.077 L 48.075 -27.827 L 48.075 -27.827 L 52.65 -28.097 L 57.17 -27.344 L 61.411 -25.606 L 65.159 -22.969 L 68.228 -19.565 L 70.464 -15.564 L 71.755 -11.167 L 71.755 -11.167 Z " fill="#f8b195" stroke="transparent" stroke-width="1" stroke-opacity="1" fill-opacity="1" class="" visibility="visible" stroke-linecap="butt" stroke-linejoin="miter" stroke-miterlimit="4" id="two_201"></path></g>

Is this an error with the SVG file or is the path translation being funky?

Neglexis avatar Feb 09 '18 10:02 Neglexis

Thanks for posting! Looks like it's an SVG interpret error. FYI load calls interpret under the hood. I'll take a look at this this morning and report back soon.

jonobr1 avatar Feb 09 '18 15:02 jonobr1

Looks like we you have a lot of a commands which I think are ellipses in the <path />'s d attribute. Unfortunately, Two.js doesn't handle those just yet: https://github.com/jonobr1/two.js/blob/dev/src/two.js#L900

Lemme see if I can add it right now though.

jonobr1 avatar Feb 09 '18 19:02 jonobr1

Here's the spec: https://www.w3.org/TR/SVG/paths.html#PathDataEllipticalArcCommands

jonobr1 avatar Feb 09 '18 19:02 jonobr1

So, I've updated the latest dev build to include better parsing for the a command in SVGs. But, actually that wasn't really the issue you had. I don't know how you made these SVGs, but your right-arm.svg has mal-constructed L commands at the end giving the weird behavior in Two.js

If you remove that it renders as I think you're intending it to. Here's the right arm SVG with the last L command removed:

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 168.79">
  <title>rightarm</title>
  <path d="M144.87,87.65a20.5,20.5,0,0,1,25.44,13.91h0A20.5,20.5,0,0,1,156.41,127L70.3,152.26h0a20.49,20.49,0,0,1-25.42-13.9l-2.87.9a10.29,10.29,0,0,1-12.72-7h0a10.28,10.28,0,0,1,6.95-12.72L49,115.83l-.35-1.2a10.27,10.27,0,0,1,7.94-13,10.48,10.48,0,0,1,11.82,7.5Z" fill="#f8b195"/>
</svg>

capture

Hope this helps!

jonobr1 avatar Feb 09 '18 19:02 jonobr1

Hmm, our designer made this in Illustrator and just exported it through there.. I'll have to confirm if deleting the misformed L-command fixes my issue, will report on it later!

Neglexis avatar Feb 10 '18 14:02 Neglexis

Something must have happened between the export and the upload. Because I can't load right arm SVG into Illustrator. Illustrator displays a 1x1px path

jonobr1 avatar Feb 10 '18 15:02 jonobr1

Sorry for the late answer @jonobr1, but was busy with some other priorities. We've now been messing around with other libraries, and they correctly render both arms. I've also redownloaded the .svg files, and they both open correctly in Illustrator for us..

Neglexis avatar Feb 27 '18 13:02 Neglexis

Glad you found a way to work it out! FYI the svgs load fine for me now too and Two.js interprets a commands now.

jonobr1 avatar Feb 27 '18 15:02 jonobr1

@jonobr1 Not using the Two.js though..

Neglexis avatar Feb 27 '18 15:02 Neglexis

I understand you've decided to switch libraries, I'm just letting future people that come across this thread know it should work as intended now.

jonobr1 avatar Feb 27 '18 16:02 jonobr1

FWIW, I'm running into this issue. Again, exported straight from Illustrator, and it displays perfectly fine in the SVG Viewer plugin for VSCode, and displays perfectly fine in this tester on CodePen, which is simply drawing it in the DOM.

What it looks like

What it should look like

SVG Code here:

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 3300 3000">
	<path d="M0,1.14V2998.86H3300V1.14ZM3283.5,1508.45h-12.85v-336h12.85Zm0-1490.81v1135h-76.65L1046.64,2506.44H45.94a9.9,9.9,0,0,0,0,19.8H57.43V2863H16.5V17.64ZM73.93,2863V2526.24H577.57V2863Zm1087.52-4a4,4,0,0,1-4,4H594.07V2526.24h458.26l43.53-27.28v310.87L1023.75,2855h129.7v-63.52a4,4,0,0,1,4-4h104V2724a4,4,0,0,1,4-4h104v-63.52a4,4,0,0,1,4-4h104v-63.52a4,4,0,0,1,4-4h104V2521.4a4,4,0,0,1,4-4h104v-63.52a4,4,0,0,1,4-4h104v-63.52a4,4,0,0,1,4-4h104v-63.52a4,4,0,0,1,4-4h104v-63.52a4,4,0,0,1,4-4h104v-63.53a4,4,0,0,1,4-4h104v-63.52a4,4,0,0,1,4-4h104v-63.52a4,4,0,0,1,4-4h104v-63.52a4,4,0,0,1,4-4h104v-63.52a4,4,0,0,1,4-4h104v-63.52a4,4,0,0,1,4-4h104v-63.52a4,4,0,0,1,4-4h104v-63.52a4,4,0,0,1,4-4h104v-63.52a4,4,0,0,1,4-4h104v-63.52a4,4,0,0,1,4-4h104v-63.66h-31.77L2732,1784.84V1473.62l480.53-301.14h41.61v336H3213v67.66a4,4,0,0,1-4,4H3105v63.52a4,4,0,0,1-4,4H2997v63.52a4,4,0,0,1-4,4H2889v63.52a4,4,0,0,1-4,4h-104v63.52a4,4,0,0,1-4,4h-104v63.52a4,4,0,0,1-4,4h-104v63.52a4,4,0,0,1-4,4h-104v63.52a4,4,0,0,1-4,4h-104v63.52a4,4,0,0,1-4,4h-104v63.52a4,4,0,0,1-4,4h-104v63.53a4,4,0,0,1-4,4h-104v63.52a4,4,0,0,1-4,4h-104v63.52a4,4,0,0,1-4,4h-104v63.52a4,4,0,0,1-4,4h-104v63.52a4,4,0,0,1-4,4h-104v63.52a4,4,0,0,1-4,4h-104v63.52a4,4,0,0,1-4,4h-104V2724a4,4,0,0,1-4,4h-104v63.52a4,4,0,0,1-4,4h-104Zm-49.09-59.51V2488.61l520.3-326.05v311Zm536.8-336.28v-311L2175,1822.67v311.11Zm542.35-339.77V1812.33l524-328.37v311.22Zm-813,858.92v-52.61H1163.73L3212.45,1645.87h71.05V2982.36Z" />
</svg>

Relevant code here:

// import the library



import Two from "two.js";

// create the container
const decorationContainer = document.createElement("div");
decorationContainer.id = "decorations";
document.getElementById("app").appendChild(decorationContainer);

// get the app dimensions
const canvasWidth = document.getElementById("app").offsetWidth;
const canvasHeight = document.getElementById("app").offsetHeight;

// create the Two renderer
const two = new Two({ width: canvasWidth, height: canvasHeight }).appendTo(
	decorationContainer
);

// import the SVG as a string
import stairsSVGInline from "../assets/images/stairs.svg";

// convert that string to an HTML DOM object
const stairsTemplate = document.createElement("template");
stairsTemplate.innerHTML = stairsSVGInline;
const stairsElement = stairsTemplate.content.firstChild;

// draw it with Two.js
const stairsSVG = two.interpret(stairsElement);
stairsSVG.center();
stairsSVG.translation.set(two.width / 2, two.height / 2);

// render it
two.update();

j0hnm4r5 avatar May 25 '18 20:05 j0hnm4r5

Thanks for posting! We've discovered the underlying problem. We need to support double decimal values in SVGs. We're tracking that here: https://github.com/jonobr1/two.js/issues/287

jonobr1 avatar May 25 '18 20:05 jonobr1

Updated dev branch has the fix you need for this. Thanks for reporting @j0hnm4r5 !

jonobr1 avatar May 25 '18 20:05 jonobr1

Also, @j0hnm4r5 that's a cool SVG. I'd love to see what you're making!

jonobr1 avatar May 25 '18 20:05 jonobr1

I am having a similar issue to what the guys previously got. Can you share what elements are currently supported in twojs or where we can find the info?

mamhaidly avatar Feb 14 '19 08:02 mamhaidly

You're using the latest dev branch? All elements should be supported at this stage.

jonobr1 avatar Feb 14 '19 08:02 jonobr1

Yes, I did

mamhaidly avatar Feb 14 '19 15:02 mamhaidly

Unfortunately, without any additional information or context ( like an example or SVG file to reference ) there's not much I can do to help you, @mamhaidly. If you could, please share some of these assets and I'd be happy to try to get to the bottom of your issue.

jonobr1 avatar Feb 15 '19 13:02 jonobr1

No worries, thank you.

mamhaidly avatar Feb 15 '19 18:02 mamhaidly