SVGLoader.js 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167
  1. import {
  2. Box2,
  3. BufferGeometry,
  4. FileLoader,
  5. Float32BufferAttribute,
  6. Loader,
  7. Matrix3,
  8. Path,
  9. Shape,
  10. ShapePath,
  11. ShapeUtils,
  12. Vector2,
  13. Vector3
  14. } from 'three';
  15. class SVGLoader extends Loader {
  16. constructor( manager ) {
  17. super( manager );
  18. // Default dots per inch
  19. this.defaultDPI = 90;
  20. // Accepted units: 'mm', 'cm', 'in', 'pt', 'pc', 'px'
  21. this.defaultUnit = 'px';
  22. }
  23. load( url, onLoad, onProgress, onError ) {
  24. const scope = this;
  25. const loader = new FileLoader( scope.manager );
  26. loader.setPath( scope.path );
  27. loader.setRequestHeader( scope.requestHeader );
  28. loader.setWithCredentials( scope.withCredentials );
  29. loader.load( url, function ( text ) {
  30. try {
  31. onLoad( scope.parse( text ) );
  32. } catch ( e ) {
  33. if ( onError ) {
  34. onError( e );
  35. } else {
  36. console.error( e );
  37. }
  38. scope.manager.itemError( url );
  39. }
  40. }, onProgress, onError );
  41. }
  42. parse( text ) {
  43. const scope = this;
  44. function parseNode( node, style ) {
  45. if ( node.nodeType !== 1 ) return;
  46. const transform = getNodeTransform( node );
  47. let isDefsNode = false;
  48. let path = null;
  49. switch ( node.nodeName ) {
  50. case 'svg':
  51. style = parseStyle( node, style );
  52. break;
  53. case 'style':
  54. parseCSSStylesheet( node );
  55. break;
  56. case 'g':
  57. style = parseStyle( node, style );
  58. break;
  59. case 'path':
  60. style = parseStyle( node, style );
  61. if ( node.hasAttribute( 'd' ) ) path = parsePathNode( node );
  62. break;
  63. case 'rect':
  64. style = parseStyle( node, style );
  65. path = parseRectNode( node );
  66. break;
  67. case 'polygon':
  68. style = parseStyle( node, style );
  69. path = parsePolygonNode( node );
  70. break;
  71. case 'polyline':
  72. style = parseStyle( node, style );
  73. path = parsePolylineNode( node );
  74. break;
  75. case 'circle':
  76. style = parseStyle( node, style );
  77. path = parseCircleNode( node );
  78. break;
  79. case 'ellipse':
  80. style = parseStyle( node, style );
  81. path = parseEllipseNode( node );
  82. break;
  83. case 'line':
  84. style = parseStyle( node, style );
  85. path = parseLineNode( node );
  86. break;
  87. case 'defs':
  88. isDefsNode = true;
  89. break;
  90. case 'use':
  91. style = parseStyle( node, style );
  92. const href = node.getAttributeNS( 'http://www.w3.org/1999/xlink', 'href' ) || '';
  93. const usedNodeId = href.substring( 1 );
  94. const usedNode = node.viewportElement.getElementById( usedNodeId );
  95. if ( usedNode ) {
  96. parseNode( usedNode, style );
  97. } else {
  98. console.warn( 'SVGLoader: \'use node\' references non-existent node id: ' + usedNodeId );
  99. }
  100. break;
  101. default:
  102. // console.log( node );
  103. }
  104. if ( path ) {
  105. if ( style.fill !== undefined && style.fill !== 'none' ) {
  106. path.color.setStyle( style.fill );
  107. }
  108. transformPath( path, currentTransform );
  109. paths.push( path );
  110. path.userData = { node: node, style: style };
  111. }
  112. const childNodes = node.childNodes;
  113. for ( let i = 0; i < childNodes.length; i ++ ) {
  114. const node = childNodes[ i ];
  115. if ( isDefsNode && node.nodeName !== 'style' && node.nodeName !== 'defs' ) {
  116. // Ignore everything in defs except CSS style definitions
  117. // and nested defs, because it is OK by the standard to have
  118. // <style/> there.
  119. continue;
  120. }
  121. parseNode( node, style );
  122. }
  123. if ( transform ) {
  124. transformStack.pop();
  125. if ( transformStack.length > 0 ) {
  126. currentTransform.copy( transformStack[ transformStack.length - 1 ] );
  127. } else {
  128. currentTransform.identity();
  129. }
  130. }
  131. }
  132. function parsePathNode( node ) {
  133. const path = new ShapePath();
  134. const point = new Vector2();
  135. const control = new Vector2();
  136. const firstPoint = new Vector2();
  137. let isFirstPoint = true;
  138. let doSetFirstPoint = false;
  139. const d = node.getAttribute( 'd' );
  140. // console.log( d );
  141. const commands = d.match( /[a-df-z][^a-df-z]*/ig );
  142. for ( let i = 0, l = commands.length; i < l; i ++ ) {
  143. const command = commands[ i ];
  144. const type = command.charAt( 0 );
  145. const data = command.slice( 1 ).trim();
  146. if ( isFirstPoint === true ) {
  147. doSetFirstPoint = true;
  148. isFirstPoint = false;
  149. }
  150. let numbers;
  151. switch ( type ) {
  152. case 'M':
  153. numbers = parseFloats( data );
  154. for ( let j = 0, jl = numbers.length; j < jl; j += 2 ) {
  155. point.x = numbers[ j + 0 ];
  156. point.y = numbers[ j + 1 ];
  157. control.x = point.x;
  158. control.y = point.y;
  159. if ( j === 0 ) {
  160. path.moveTo( point.x, point.y );
  161. } else {
  162. path.lineTo( point.x, point.y );
  163. }
  164. if ( j === 0 ) firstPoint.copy( point );
  165. }
  166. break;
  167. case 'H':
  168. numbers = parseFloats( data );
  169. for ( let j = 0, jl = numbers.length; j < jl; j ++ ) {
  170. point.x = numbers[ j ];
  171. control.x = point.x;
  172. control.y = point.y;
  173. path.lineTo( point.x, point.y );
  174. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  175. }
  176. break;
  177. case 'V':
  178. numbers = parseFloats( data );
  179. for ( let j = 0, jl = numbers.length; j < jl; j ++ ) {
  180. point.y = numbers[ j ];
  181. control.x = point.x;
  182. control.y = point.y;
  183. path.lineTo( point.x, point.y );
  184. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  185. }
  186. break;
  187. case 'L':
  188. numbers = parseFloats( data );
  189. for ( let j = 0, jl = numbers.length; j < jl; j += 2 ) {
  190. point.x = numbers[ j + 0 ];
  191. point.y = numbers[ j + 1 ];
  192. control.x = point.x;
  193. control.y = point.y;
  194. path.lineTo( point.x, point.y );
  195. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  196. }
  197. break;
  198. case 'C':
  199. numbers = parseFloats( data );
  200. for ( let j = 0, jl = numbers.length; j < jl; j += 6 ) {
  201. path.bezierCurveTo(
  202. numbers[ j + 0 ],
  203. numbers[ j + 1 ],
  204. numbers[ j + 2 ],
  205. numbers[ j + 3 ],
  206. numbers[ j + 4 ],
  207. numbers[ j + 5 ]
  208. );
  209. control.x = numbers[ j + 2 ];
  210. control.y = numbers[ j + 3 ];
  211. point.x = numbers[ j + 4 ];
  212. point.y = numbers[ j + 5 ];
  213. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  214. }
  215. break;
  216. case 'S':
  217. numbers = parseFloats( data );
  218. for ( let j = 0, jl = numbers.length; j < jl; j += 4 ) {
  219. path.bezierCurveTo(
  220. getReflection( point.x, control.x ),
  221. getReflection( point.y, control.y ),
  222. numbers[ j + 0 ],
  223. numbers[ j + 1 ],
  224. numbers[ j + 2 ],
  225. numbers[ j + 3 ]
  226. );
  227. control.x = numbers[ j + 0 ];
  228. control.y = numbers[ j + 1 ];
  229. point.x = numbers[ j + 2 ];
  230. point.y = numbers[ j + 3 ];
  231. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  232. }
  233. break;
  234. case 'Q':
  235. numbers = parseFloats( data );
  236. for ( let j = 0, jl = numbers.length; j < jl; j += 4 ) {
  237. path.quadraticCurveTo(
  238. numbers[ j + 0 ],
  239. numbers[ j + 1 ],
  240. numbers[ j + 2 ],
  241. numbers[ j + 3 ]
  242. );
  243. control.x = numbers[ j + 0 ];
  244. control.y = numbers[ j + 1 ];
  245. point.x = numbers[ j + 2 ];
  246. point.y = numbers[ j + 3 ];
  247. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  248. }
  249. break;
  250. case 'T':
  251. numbers = parseFloats( data );
  252. for ( let j = 0, jl = numbers.length; j < jl; j += 2 ) {
  253. const rx = getReflection( point.x, control.x );
  254. const ry = getReflection( point.y, control.y );
  255. path.quadraticCurveTo(
  256. rx,
  257. ry,
  258. numbers[ j + 0 ],
  259. numbers[ j + 1 ]
  260. );
  261. control.x = rx;
  262. control.y = ry;
  263. point.x = numbers[ j + 0 ];
  264. point.y = numbers[ j + 1 ];
  265. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  266. }
  267. break;
  268. case 'A':
  269. numbers = parseFloats( data, [ 3, 4 ], 7 );
  270. for ( let j = 0, jl = numbers.length; j < jl; j += 7 ) {
  271. // skip command if start point == end point
  272. if ( numbers[ j + 5 ] == point.x && numbers[ j + 6 ] == point.y ) continue;
  273. const start = point.clone();
  274. point.x = numbers[ j + 5 ];
  275. point.y = numbers[ j + 6 ];
  276. control.x = point.x;
  277. control.y = point.y;
  278. parseArcCommand(
  279. path, numbers[ j ], numbers[ j + 1 ], numbers[ j + 2 ], numbers[ j + 3 ], numbers[ j + 4 ], start, point
  280. );
  281. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  282. }
  283. break;
  284. case 'm':
  285. numbers = parseFloats( data );
  286. for ( let j = 0, jl = numbers.length; j < jl; j += 2 ) {
  287. point.x += numbers[ j + 0 ];
  288. point.y += numbers[ j + 1 ];
  289. control.x = point.x;
  290. control.y = point.y;
  291. if ( j === 0 ) {
  292. path.moveTo( point.x, point.y );
  293. } else {
  294. path.lineTo( point.x, point.y );
  295. }
  296. if ( j === 0 ) firstPoint.copy( point );
  297. }
  298. break;
  299. case 'h':
  300. numbers = parseFloats( data );
  301. for ( let j = 0, jl = numbers.length; j < jl; j ++ ) {
  302. point.x += numbers[ j ];
  303. control.x = point.x;
  304. control.y = point.y;
  305. path.lineTo( point.x, point.y );
  306. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  307. }
  308. break;
  309. case 'v':
  310. numbers = parseFloats( data );
  311. for ( let j = 0, jl = numbers.length; j < jl; j ++ ) {
  312. point.y += numbers[ j ];
  313. control.x = point.x;
  314. control.y = point.y;
  315. path.lineTo( point.x, point.y );
  316. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  317. }
  318. break;
  319. case 'l':
  320. numbers = parseFloats( data );
  321. for ( let j = 0, jl = numbers.length; j < jl; j += 2 ) {
  322. point.x += numbers[ j + 0 ];
  323. point.y += numbers[ j + 1 ];
  324. control.x = point.x;
  325. control.y = point.y;
  326. path.lineTo( point.x, point.y );
  327. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  328. }
  329. break;
  330. case 'c':
  331. numbers = parseFloats( data );
  332. for ( let j = 0, jl = numbers.length; j < jl; j += 6 ) {
  333. path.bezierCurveTo(
  334. point.x + numbers[ j + 0 ],
  335. point.y + numbers[ j + 1 ],
  336. point.x + numbers[ j + 2 ],
  337. point.y + numbers[ j + 3 ],
  338. point.x + numbers[ j + 4 ],
  339. point.y + numbers[ j + 5 ]
  340. );
  341. control.x = point.x + numbers[ j + 2 ];
  342. control.y = point.y + numbers[ j + 3 ];
  343. point.x += numbers[ j + 4 ];
  344. point.y += numbers[ j + 5 ];
  345. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  346. }
  347. break;
  348. case 's':
  349. numbers = parseFloats( data );
  350. for ( let j = 0, jl = numbers.length; j < jl; j += 4 ) {
  351. path.bezierCurveTo(
  352. getReflection( point.x, control.x ),
  353. getReflection( point.y, control.y ),
  354. point.x + numbers[ j + 0 ],
  355. point.y + numbers[ j + 1 ],
  356. point.x + numbers[ j + 2 ],
  357. point.y + numbers[ j + 3 ]
  358. );
  359. control.x = point.x + numbers[ j + 0 ];
  360. control.y = point.y + numbers[ j + 1 ];
  361. point.x += numbers[ j + 2 ];
  362. point.y += numbers[ j + 3 ];
  363. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  364. }
  365. break;
  366. case 'q':
  367. numbers = parseFloats( data );
  368. for ( let j = 0, jl = numbers.length; j < jl; j += 4 ) {
  369. path.quadraticCurveTo(
  370. point.x + numbers[ j + 0 ],
  371. point.y + numbers[ j + 1 ],
  372. point.x + numbers[ j + 2 ],
  373. point.y + numbers[ j + 3 ]
  374. );
  375. control.x = point.x + numbers[ j + 0 ];
  376. control.y = point.y + numbers[ j + 1 ];
  377. point.x += numbers[ j + 2 ];
  378. point.y += numbers[ j + 3 ];
  379. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  380. }
  381. break;
  382. case 't':
  383. numbers = parseFloats( data );
  384. for ( let j = 0, jl = numbers.length; j < jl; j += 2 ) {
  385. const rx = getReflection( point.x, control.x );
  386. const ry = getReflection( point.y, control.y );
  387. path.quadraticCurveTo(
  388. rx,
  389. ry,
  390. point.x + numbers[ j + 0 ],
  391. point.y + numbers[ j + 1 ]
  392. );
  393. control.x = rx;
  394. control.y = ry;
  395. point.x = point.x + numbers[ j + 0 ];
  396. point.y = point.y + numbers[ j + 1 ];
  397. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  398. }
  399. break;
  400. case 'a':
  401. numbers = parseFloats( data, [ 3, 4 ], 7 );
  402. for ( let j = 0, jl = numbers.length; j < jl; j += 7 ) {
  403. // skip command if no displacement
  404. if ( numbers[ j + 5 ] == 0 && numbers[ j + 6 ] == 0 ) continue;
  405. const start = point.clone();
  406. point.x += numbers[ j + 5 ];
  407. point.y += numbers[ j + 6 ];
  408. control.x = point.x;
  409. control.y = point.y;
  410. parseArcCommand(
  411. path, numbers[ j ], numbers[ j + 1 ], numbers[ j + 2 ], numbers[ j + 3 ], numbers[ j + 4 ], start, point
  412. );
  413. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  414. }
  415. break;
  416. case 'Z':
  417. case 'z':
  418. path.currentPath.autoClose = true;
  419. if ( path.currentPath.curves.length > 0 ) {
  420. // Reset point to beginning of Path
  421. point.copy( firstPoint );
  422. path.currentPath.currentPoint.copy( point );
  423. isFirstPoint = true;
  424. }
  425. break;
  426. default:
  427. console.warn( command );
  428. }
  429. // console.log( type, parseFloats( data ), parseFloats( data ).length )
  430. doSetFirstPoint = false;
  431. }
  432. return path;
  433. }
  434. function parseCSSStylesheet( node ) {
  435. if ( ! node.sheet || ! node.sheet.cssRules || ! node.sheet.cssRules.length ) return;
  436. for ( let i = 0; i < node.sheet.cssRules.length; i ++ ) {
  437. const stylesheet = node.sheet.cssRules[ i ];
  438. if ( stylesheet.type !== 1 ) continue;
  439. const selectorList = stylesheet.selectorText
  440. .split( /,/gm )
  441. .filter( Boolean )
  442. .map( i => i.trim() );
  443. for ( let j = 0; j < selectorList.length; j ++ ) {
  444. // Remove empty rules
  445. const definitions = Object.fromEntries(
  446. Object.entries( stylesheet.style ).filter( ( [ , v ] ) => v !== '' )
  447. );
  448. stylesheets[ selectorList[ j ] ] = Object.assign(
  449. stylesheets[ selectorList[ j ] ] || {},
  450. definitions
  451. );
  452. }
  453. }
  454. }
  455. /**
  456. * https://www.w3.org/TR/SVG/implnote.html#ArcImplementationNotes
  457. * https://mortoray.com/2017/02/16/rendering-an-svg-elliptical-arc-as-bezier-curves/ Appendix: Endpoint to center arc conversion
  458. * From
  459. * rx ry x-axis-rotation large-arc-flag sweep-flag x y
  460. * To
  461. * aX, aY, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise, aRotation
  462. */
  463. function parseArcCommand( path, rx, ry, x_axis_rotation, large_arc_flag, sweep_flag, start, end ) {
  464. if ( rx == 0 || ry == 0 ) {
  465. // draw a line if either of the radii == 0
  466. path.lineTo( end.x, end.y );
  467. return;
  468. }
  469. x_axis_rotation = x_axis_rotation * Math.PI / 180;
  470. // Ensure radii are positive
  471. rx = Math.abs( rx );
  472. ry = Math.abs( ry );
  473. // Compute (x1', y1')
  474. const dx2 = ( start.x - end.x ) / 2.0;
  475. const dy2 = ( start.y - end.y ) / 2.0;
  476. const x1p = Math.cos( x_axis_rotation ) * dx2 + Math.sin( x_axis_rotation ) * dy2;
  477. const y1p = - Math.sin( x_axis_rotation ) * dx2 + Math.cos( x_axis_rotation ) * dy2;
  478. // Compute (cx', cy')
  479. let rxs = rx * rx;
  480. let rys = ry * ry;
  481. const x1ps = x1p * x1p;
  482. const y1ps = y1p * y1p;
  483. // Ensure radii are large enough
  484. const cr = x1ps / rxs + y1ps / rys;
  485. if ( cr > 1 ) {
  486. // scale up rx,ry equally so cr == 1
  487. const s = Math.sqrt( cr );
  488. rx = s * rx;
  489. ry = s * ry;
  490. rxs = rx * rx;
  491. rys = ry * ry;
  492. }
  493. const dq = ( rxs * y1ps + rys * x1ps );
  494. const pq = ( rxs * rys - dq ) / dq;
  495. let q = Math.sqrt( Math.max( 0, pq ) );
  496. if ( large_arc_flag === sweep_flag ) q = - q;
  497. const cxp = q * rx * y1p / ry;
  498. const cyp = - q * ry * x1p / rx;
  499. // Step 3: Compute (cx, cy) from (cx', cy')
  500. const cx = Math.cos( x_axis_rotation ) * cxp - Math.sin( x_axis_rotation ) * cyp + ( start.x + end.x ) / 2;
  501. const cy = Math.sin( x_axis_rotation ) * cxp + Math.cos( x_axis_rotation ) * cyp + ( start.y + end.y ) / 2;
  502. // Step 4: Compute θ1 and Δθ
  503. const theta = svgAngle( 1, 0, ( x1p - cxp ) / rx, ( y1p - cyp ) / ry );
  504. const delta = svgAngle( ( x1p - cxp ) / rx, ( y1p - cyp ) / ry, ( - x1p - cxp ) / rx, ( - y1p - cyp ) / ry ) % ( Math.PI * 2 );
  505. path.currentPath.absellipse( cx, cy, rx, ry, theta, theta + delta, sweep_flag === 0, x_axis_rotation );
  506. }
  507. function svgAngle( ux, uy, vx, vy ) {
  508. const dot = ux * vx + uy * vy;
  509. const len = Math.sqrt( ux * ux + uy * uy ) * Math.sqrt( vx * vx + vy * vy );
  510. let ang = Math.acos( Math.max( - 1, Math.min( 1, dot / len ) ) ); // floating point precision, slightly over values appear
  511. if ( ( ux * vy - uy * vx ) < 0 ) ang = - ang;
  512. return ang;
  513. }
  514. /*
  515. * According to https://www.w3.org/TR/SVG/shapes.html#RectElementRXAttribute
  516. * rounded corner should be rendered to elliptical arc, but bezier curve does the job well enough
  517. */
  518. function parseRectNode( node ) {
  519. const x = parseFloatWithUnits( node.getAttribute( 'x' ) || 0 );
  520. const y = parseFloatWithUnits( node.getAttribute( 'y' ) || 0 );
  521. const rx = parseFloatWithUnits( node.getAttribute( 'rx' ) || node.getAttribute( 'ry' ) || 0 );
  522. const ry = parseFloatWithUnits( node.getAttribute( 'ry' ) || node.getAttribute( 'rx' ) || 0 );
  523. const w = parseFloatWithUnits( node.getAttribute( 'width' ) );
  524. const h = parseFloatWithUnits( node.getAttribute( 'height' ) );
  525. // Ellipse arc to Bezier approximation Coefficient (Inversed). See:
  526. // https://spencermortensen.com/articles/bezier-circle/
  527. const bci = 1 - 0.551915024494;
  528. const path = new ShapePath();
  529. // top left
  530. path.moveTo( x + rx, y );
  531. // top right
  532. path.lineTo( x + w - rx, y );
  533. if ( rx !== 0 || ry !== 0 ) {
  534. path.bezierCurveTo(
  535. x + w - rx * bci,
  536. y,
  537. x + w,
  538. y + ry * bci,
  539. x + w,
  540. y + ry
  541. );
  542. }
  543. // bottom right
  544. path.lineTo( x + w, y + h - ry );
  545. if ( rx !== 0 || ry !== 0 ) {
  546. path.bezierCurveTo(
  547. x + w,
  548. y + h - ry * bci,
  549. x + w - rx * bci,
  550. y + h,
  551. x + w - rx,
  552. y + h
  553. );
  554. }
  555. // bottom left
  556. path.lineTo( x + rx, y + h );
  557. if ( rx !== 0 || ry !== 0 ) {
  558. path.bezierCurveTo(
  559. x + rx * bci,
  560. y + h,
  561. x,
  562. y + h - ry * bci,
  563. x,
  564. y + h - ry
  565. );
  566. }
  567. // back to top left
  568. path.lineTo( x, y + ry );
  569. if ( rx !== 0 || ry !== 0 ) {
  570. path.bezierCurveTo( x, y + ry * bci, x + rx * bci, y, x + rx, y );
  571. }
  572. return path;
  573. }
  574. function parsePolygonNode( node ) {
  575. function iterator( match, a, b ) {
  576. const x = parseFloatWithUnits( a );
  577. const y = parseFloatWithUnits( b );
  578. if ( index === 0 ) {
  579. path.moveTo( x, y );
  580. } else {
  581. path.lineTo( x, y );
  582. }
  583. index ++;
  584. }
  585. const regex = /(-?[\d\.?]+)[,|\s](-?[\d\.?]+)/g;
  586. const path = new ShapePath();
  587. let index = 0;
  588. node.getAttribute( 'points' ).replace( regex, iterator );
  589. path.currentPath.autoClose = true;
  590. return path;
  591. }
  592. function parsePolylineNode( node ) {
  593. function iterator( match, a, b ) {
  594. const x = parseFloatWithUnits( a );
  595. const y = parseFloatWithUnits( b );
  596. if ( index === 0 ) {
  597. path.moveTo( x, y );
  598. } else {
  599. path.lineTo( x, y );
  600. }
  601. index ++;
  602. }
  603. const regex = /(-?[\d\.?]+)[,|\s](-?[\d\.?]+)/g;
  604. const path = new ShapePath();
  605. let index = 0;
  606. node.getAttribute( 'points' ).replace( regex, iterator );
  607. path.currentPath.autoClose = false;
  608. return path;
  609. }
  610. function parseCircleNode( node ) {
  611. const x = parseFloatWithUnits( node.getAttribute( 'cx' ) || 0 );
  612. const y = parseFloatWithUnits( node.getAttribute( 'cy' ) || 0 );
  613. const r = parseFloatWithUnits( node.getAttribute( 'r' ) || 0 );
  614. const subpath = new Path();
  615. subpath.absarc( x, y, r, 0, Math.PI * 2 );
  616. const path = new ShapePath();
  617. path.subPaths.push( subpath );
  618. return path;
  619. }
  620. function parseEllipseNode( node ) {
  621. const x = parseFloatWithUnits( node.getAttribute( 'cx' ) || 0 );
  622. const y = parseFloatWithUnits( node.getAttribute( 'cy' ) || 0 );
  623. const rx = parseFloatWithUnits( node.getAttribute( 'rx' ) || 0 );
  624. const ry = parseFloatWithUnits( node.getAttribute( 'ry' ) || 0 );
  625. const subpath = new Path();
  626. subpath.absellipse( x, y, rx, ry, 0, Math.PI * 2 );
  627. const path = new ShapePath();
  628. path.subPaths.push( subpath );
  629. return path;
  630. }
  631. function parseLineNode( node ) {
  632. const x1 = parseFloatWithUnits( node.getAttribute( 'x1' ) || 0 );
  633. const y1 = parseFloatWithUnits( node.getAttribute( 'y1' ) || 0 );
  634. const x2 = parseFloatWithUnits( node.getAttribute( 'x2' ) || 0 );
  635. const y2 = parseFloatWithUnits( node.getAttribute( 'y2' ) || 0 );
  636. const path = new ShapePath();
  637. path.moveTo( x1, y1 );
  638. path.lineTo( x2, y2 );
  639. path.currentPath.autoClose = false;
  640. return path;
  641. }
  642. //
  643. function parseStyle( node, style ) {
  644. style = Object.assign( {}, style ); // clone style
  645. let stylesheetStyles = {};
  646. if ( node.hasAttribute( 'class' ) ) {
  647. const classSelectors = node.getAttribute( 'class' )
  648. .split( /\s/ )
  649. .filter( Boolean )
  650. .map( i => i.trim() );
  651. for ( let i = 0; i < classSelectors.length; i ++ ) {
  652. stylesheetStyles = Object.assign( stylesheetStyles, stylesheets[ '.' + classSelectors[ i ] ] );
  653. }
  654. }
  655. if ( node.hasAttribute( 'id' ) ) {
  656. stylesheetStyles = Object.assign( stylesheetStyles, stylesheets[ '#' + node.getAttribute( 'id' ) ] );
  657. }
  658. function addStyle( svgName, jsName, adjustFunction ) {
  659. if ( adjustFunction === undefined ) adjustFunction = function copy( v ) {
  660. if ( v.startsWith( 'url' ) ) console.warn( 'SVGLoader: url access in attributes is not implemented.' );
  661. return v;
  662. };
  663. if ( node.hasAttribute( svgName ) ) style[ jsName ] = adjustFunction( node.getAttribute( svgName ) );
  664. if ( stylesheetStyles[ svgName ] ) style[ jsName ] = adjustFunction( stylesheetStyles[ svgName ] );
  665. if ( node.style && node.style[ svgName ] !== '' ) style[ jsName ] = adjustFunction( node.style[ svgName ] );
  666. }
  667. function clamp( v ) {
  668. return Math.max( 0, Math.min( 1, parseFloatWithUnits( v ) ) );
  669. }
  670. function positive( v ) {
  671. return Math.max( 0, parseFloatWithUnits( v ) );
  672. }
  673. addStyle( 'fill', 'fill' );
  674. addStyle( 'fill-opacity', 'fillOpacity', clamp );
  675. addStyle( 'fill-rule', 'fillRule' );
  676. addStyle( 'opacity', 'opacity', clamp );
  677. addStyle( 'stroke', 'stroke' );
  678. addStyle( 'stroke-opacity', 'strokeOpacity', clamp );
  679. addStyle( 'stroke-width', 'strokeWidth', positive );
  680. addStyle( 'stroke-linejoin', 'strokeLineJoin' );
  681. addStyle( 'stroke-linecap', 'strokeLineCap' );
  682. addStyle( 'stroke-miterlimit', 'strokeMiterLimit', positive );
  683. addStyle( 'visibility', 'visibility' );
  684. return style;
  685. }
  686. // http://www.w3.org/TR/SVG11/implnote.html#PathElementImplementationNotes
  687. function getReflection( a, b ) {
  688. return a - ( b - a );
  689. }
  690. // from https://github.com/ppvg/svg-numbers (MIT License)
  691. function parseFloats( input, flags, stride ) {
  692. if ( typeof input !== 'string' ) {
  693. throw new TypeError( 'Invalid input: ' + typeof input );
  694. }
  695. // Character groups
  696. const RE = {
  697. SEPARATOR: /[ \t\r\n\,.\-+]/,
  698. WHITESPACE: /[ \t\r\n]/,
  699. DIGIT: /[\d]/,
  700. SIGN: /[-+]/,
  701. POINT: /\./,
  702. COMMA: /,/,
  703. EXP: /e/i,
  704. FLAGS: /[01]/
  705. };
  706. // States
  707. const SEP = 0;
  708. const INT = 1;
  709. const FLOAT = 2;
  710. const EXP = 3;
  711. let state = SEP;
  712. let seenComma = true;
  713. let number = '', exponent = '';
  714. const result = [];
  715. function throwSyntaxError( current, i, partial ) {
  716. const error = new SyntaxError( 'Unexpected character "' + current + '" at index ' + i + '.' );
  717. error.partial = partial;
  718. throw error;
  719. }
  720. function newNumber() {
  721. if ( number !== '' ) {
  722. if ( exponent === '' ) result.push( Number( number ) );
  723. else result.push( Number( number ) * Math.pow( 10, Number( exponent ) ) );
  724. }
  725. number = '';
  726. exponent = '';
  727. }
  728. let current;
  729. const length = input.length;
  730. for ( let i = 0; i < length; i ++ ) {
  731. current = input[ i ];
  732. // check for flags
  733. if ( Array.isArray( flags ) && flags.includes( result.length % stride ) && RE.FLAGS.test( current ) ) {
  734. state = INT;
  735. number = current;
  736. newNumber();
  737. continue;
  738. }
  739. // parse until next number
  740. if ( state === SEP ) {
  741. // eat whitespace
  742. if ( RE.WHITESPACE.test( current ) ) {
  743. continue;
  744. }
  745. // start new number
  746. if ( RE.DIGIT.test( current ) || RE.SIGN.test( current ) ) {
  747. state = INT;
  748. number = current;
  749. continue;
  750. }
  751. if ( RE.POINT.test( current ) ) {
  752. state = FLOAT;
  753. number = current;
  754. continue;
  755. }
  756. // throw on double commas (e.g. "1, , 2")
  757. if ( RE.COMMA.test( current ) ) {
  758. if ( seenComma ) {
  759. throwSyntaxError( current, i, result );
  760. }
  761. seenComma = true;
  762. }
  763. }
  764. // parse integer part
  765. if ( state === INT ) {
  766. if ( RE.DIGIT.test( current ) ) {
  767. number += current;
  768. continue;
  769. }
  770. if ( RE.POINT.test( current ) ) {
  771. number += current;
  772. state = FLOAT;
  773. continue;
  774. }
  775. if ( RE.EXP.test( current ) ) {
  776. state = EXP;
  777. continue;
  778. }
  779. // throw on double signs ("-+1"), but not on sign as separator ("-1-2")
  780. if ( RE.SIGN.test( current )
  781. && number.length === 1
  782. && RE.SIGN.test( number[ 0 ] ) ) {
  783. throwSyntaxError( current, i, result );
  784. }
  785. }
  786. // parse decimal part
  787. if ( state === FLOAT ) {
  788. if ( RE.DIGIT.test( current ) ) {
  789. number += current;
  790. continue;
  791. }
  792. if ( RE.EXP.test( current ) ) {
  793. state = EXP;
  794. continue;
  795. }
  796. // throw on double decimal points (e.g. "1..2")
  797. if ( RE.POINT.test( current ) && number[ number.length - 1 ] === '.' ) {
  798. throwSyntaxError( current, i, result );
  799. }
  800. }
  801. // parse exponent part
  802. if ( state === EXP ) {
  803. if ( RE.DIGIT.test( current ) ) {
  804. exponent += current;
  805. continue;
  806. }
  807. if ( RE.SIGN.test( current ) ) {
  808. if ( exponent === '' ) {
  809. exponent += current;
  810. continue;
  811. }
  812. if ( exponent.length === 1 && RE.SIGN.test( exponent ) ) {
  813. throwSyntaxError( current, i, result );
  814. }
  815. }
  816. }
  817. // end of number
  818. if ( RE.WHITESPACE.test( current ) ) {
  819. newNumber();
  820. state = SEP;
  821. seenComma = false;
  822. } else if ( RE.COMMA.test( current ) ) {
  823. newNumber();
  824. state = SEP;
  825. seenComma = true;
  826. } else if ( RE.SIGN.test( current ) ) {
  827. newNumber();
  828. state = INT;
  829. number = current;
  830. } else if ( RE.POINT.test( current ) ) {
  831. newNumber();
  832. state = FLOAT;
  833. number = current;
  834. } else {
  835. throwSyntaxError( current, i, result );
  836. }
  837. }
  838. // add the last number found (if any)
  839. newNumber();
  840. return result;
  841. }
  842. // Units
  843. const units = [ 'mm', 'cm', 'in', 'pt', 'pc', 'px' ];
  844. // Conversion: [ fromUnit ][ toUnit ] (-1 means dpi dependent)
  845. const unitConversion = {
  846. 'mm': {
  847. 'mm': 1,
  848. 'cm': 0.1,
  849. 'in': 1 / 25.4,
  850. 'pt': 72 / 25.4,
  851. 'pc': 6 / 25.4,
  852. 'px': - 1
  853. },
  854. 'cm': {
  855. 'mm': 10,
  856. 'cm': 1,
  857. 'in': 1 / 2.54,
  858. 'pt': 72 / 2.54,
  859. 'pc': 6 / 2.54,
  860. 'px': - 1
  861. },
  862. 'in': {
  863. 'mm': 25.4,
  864. 'cm': 2.54,
  865. 'in': 1,
  866. 'pt': 72,
  867. 'pc': 6,
  868. 'px': - 1
  869. },
  870. 'pt': {
  871. 'mm': 25.4 / 72,
  872. 'cm': 2.54 / 72,
  873. 'in': 1 / 72,
  874. 'pt': 1,
  875. 'pc': 6 / 72,
  876. 'px': - 1
  877. },
  878. 'pc': {
  879. 'mm': 25.4 / 6,
  880. 'cm': 2.54 / 6,
  881. 'in': 1 / 6,
  882. 'pt': 72 / 6,
  883. 'pc': 1,
  884. 'px': - 1
  885. },
  886. 'px': {
  887. 'px': 1
  888. }
  889. };
  890. function parseFloatWithUnits( string ) {
  891. let theUnit = 'px';
  892. if ( typeof string === 'string' || string instanceof String ) {
  893. for ( let i = 0, n = units.length; i < n; i ++ ) {
  894. const u = units[ i ];
  895. if ( string.endsWith( u ) ) {
  896. theUnit = u;
  897. string = string.substring( 0, string.length - u.length );
  898. break;
  899. }
  900. }
  901. }
  902. let scale = undefined;
  903. if ( theUnit === 'px' && scope.defaultUnit !== 'px' ) {
  904. // Conversion scale from pixels to inches, then to default units
  905. scale = unitConversion[ 'in' ][ scope.defaultUnit ] / scope.defaultDPI;
  906. } else {
  907. scale = unitConversion[ theUnit ][ scope.defaultUnit ];
  908. if ( scale < 0 ) {
  909. // Conversion scale to pixels
  910. scale = unitConversion[ theUnit ][ 'in' ] * scope.defaultDPI;
  911. }
  912. }
  913. return scale * parseFloat( string );
  914. }
  915. // Transforms
  916. function getNodeTransform( node ) {
  917. if ( ! ( node.hasAttribute( 'transform' ) || ( node.nodeName === 'use' && ( node.hasAttribute( 'x' ) || node.hasAttribute( 'y' ) ) ) ) ) {
  918. return null;
  919. }
  920. const transform = parseNodeTransform( node );
  921. if ( transformStack.length > 0 ) {
  922. transform.premultiply( transformStack[ transformStack.length - 1 ] );
  923. }
  924. currentTransform.copy( transform );
  925. transformStack.push( transform );
  926. return transform;
  927. }
  928. function parseNodeTransform( node ) {
  929. const transform = new Matrix3();
  930. const currentTransform = tempTransform0;
  931. if ( node.nodeName === 'use' && ( node.hasAttribute( 'x' ) || node.hasAttribute( 'y' ) ) ) {
  932. const tx = parseFloatWithUnits( node.getAttribute( 'x' ) );
  933. const ty = parseFloatWithUnits( node.getAttribute( 'y' ) );
  934. transform.translate( tx, ty );
  935. }
  936. if ( node.hasAttribute( 'transform' ) ) {
  937. const transformsTexts = node.getAttribute( 'transform' ).split( ')' );
  938. for ( let tIndex = transformsTexts.length - 1; tIndex >= 0; tIndex -- ) {
  939. const transformText = transformsTexts[ tIndex ].trim();
  940. if ( transformText === '' ) continue;
  941. const openParPos = transformText.indexOf( '(' );
  942. const closeParPos = transformText.length;
  943. if ( openParPos > 0 && openParPos < closeParPos ) {
  944. const transformType = transformText.slice( 0, openParPos );
  945. const array = parseFloats( transformText.slice( openParPos + 1 ) );
  946. currentTransform.identity();
  947. switch ( transformType ) {
  948. case 'translate':
  949. if ( array.length >= 1 ) {
  950. const tx = array[ 0 ];
  951. let ty = 0;
  952. if ( array.length >= 2 ) {
  953. ty = array[ 1 ];
  954. }
  955. currentTransform.translate( tx, ty );
  956. }
  957. break;
  958. case 'rotate':
  959. if ( array.length >= 1 ) {
  960. let angle = 0;
  961. let cx = 0;
  962. let cy = 0;
  963. // Angle
  964. angle = array[ 0 ] * Math.PI / 180;
  965. if ( array.length >= 3 ) {
  966. // Center x, y
  967. cx = array[ 1 ];
  968. cy = array[ 2 ];
  969. }
  970. // Rotate around center (cx, cy)
  971. tempTransform1.makeTranslation( - cx, - cy );
  972. tempTransform2.makeRotation( angle );
  973. tempTransform3.multiplyMatrices( tempTransform2, tempTransform1 );
  974. tempTransform1.makeTranslation( cx, cy );
  975. currentTransform.multiplyMatrices( tempTransform1, tempTransform3 );
  976. }
  977. break;
  978. case 'scale':
  979. if ( array.length >= 1 ) {
  980. const scaleX = array[ 0 ];
  981. let scaleY = scaleX;
  982. if ( array.length >= 2 ) {
  983. scaleY = array[ 1 ];
  984. }
  985. currentTransform.scale( scaleX, scaleY );
  986. }
  987. break;
  988. case 'skewX':
  989. if ( array.length === 1 ) {
  990. currentTransform.set(
  991. 1, Math.tan( array[ 0 ] * Math.PI / 180 ), 0,
  992. 0, 1, 0,
  993. 0, 0, 1
  994. );
  995. }
  996. break;
  997. case 'skewY':
  998. if ( array.length === 1 ) {
  999. currentTransform.set(
  1000. 1, 0, 0,
  1001. Math.tan( array[ 0 ] * Math.PI / 180 ), 1, 0,
  1002. 0, 0, 1
  1003. );
  1004. }
  1005. break;
  1006. case 'matrix':
  1007. if ( array.length === 6 ) {
  1008. currentTransform.set(
  1009. array[ 0 ], array[ 2 ], array[ 4 ],
  1010. array[ 1 ], array[ 3 ], array[ 5 ],
  1011. 0, 0, 1
  1012. );
  1013. }
  1014. break;
  1015. }
  1016. }
  1017. transform.premultiply( currentTransform );
  1018. }
  1019. }
  1020. return transform;
  1021. }
  1022. function transformPath( path, m ) {
  1023. function transfVec2( v2 ) {
  1024. tempV3.set( v2.x, v2.y, 1 ).applyMatrix3( m );
  1025. v2.set( tempV3.x, tempV3.y );
  1026. }
  1027. function transfEllipseGeneric( curve ) {
  1028. // For math description see:
  1029. // https://math.stackexchange.com/questions/4544164
  1030. const a = curve.xRadius;
  1031. const b = curve.yRadius;
  1032. const cosTheta = Math.cos( curve.aRotation );
  1033. const sinTheta = Math.sin( curve.aRotation );
  1034. const v1 = new Vector3( a * cosTheta, a * sinTheta, 0 );
  1035. const v2 = new Vector3( - b * sinTheta, b * cosTheta, 0 );
  1036. const f1 = v1.applyMatrix3( m );
  1037. const f2 = v2.applyMatrix3( m );
  1038. const mF = tempTransform0.set(
  1039. f1.x, f2.x, 0,
  1040. f1.y, f2.y, 0,
  1041. 0, 0, 1,
  1042. );
  1043. const mFInv = tempTransform1.copy( mF ).invert();
  1044. const mFInvT = tempTransform2.copy( mFInv ).transpose();
  1045. const mQ = mFInvT.multiply( mFInv );
  1046. const mQe = mQ.elements;
  1047. const ed = eigenDecomposition( mQe[ 0 ], mQe[ 1 ], mQe[ 4 ] );
  1048. const rt1sqrt = Math.sqrt( ed.rt1 );
  1049. const rt2sqrt = Math.sqrt( ed.rt2 );
  1050. curve.xRadius = 1 / rt1sqrt;
  1051. curve.yRadius = 1 / rt2sqrt;
  1052. curve.aRotation = Math.atan2( ed.sn, ed.cs );
  1053. const isFullEllipse =
  1054. ( curve.aEndAngle - curve.aStartAngle ) % ( 2 * Math.PI ) < Number.EPSILON;
  1055. // Do not touch angles of a full ellipse because after transformation they
  1056. // would converge to a sinle value effectively removing the whole curve
  1057. if ( ! isFullEllipse ) {
  1058. const mDsqrt = tempTransform1.set(
  1059. rt1sqrt, 0, 0,
  1060. 0, rt2sqrt, 0,
  1061. 0, 0, 1,
  1062. );
  1063. const mRT = tempTransform2.set(
  1064. ed.cs, ed.sn, 0,
  1065. - ed.sn, ed.cs, 0,
  1066. 0, 0, 1,
  1067. );
  1068. const mDRF = mDsqrt.multiply( mRT ).multiply( mF );
  1069. const transformAngle = phi => {
  1070. const { x: cosR, y: sinR } =
  1071. new Vector3( Math.cos( phi ), Math.sin( phi ), 0 ).applyMatrix3( mDRF );
  1072. return Math.atan2( sinR, cosR );
  1073. };
  1074. curve.aStartAngle = transformAngle( curve.aStartAngle );
  1075. curve.aEndAngle = transformAngle( curve.aEndAngle );
  1076. if ( isTransformFlipped( m ) ) {
  1077. curve.aClockwise = ! curve.aClockwise;
  1078. }
  1079. }
  1080. }
  1081. function transfEllipseNoSkew( curve ) {
  1082. // Faster shortcut if no skew is applied
  1083. // (e.g, a euclidean transform of a group containing the ellipse)
  1084. const sx = getTransformScaleX( m );
  1085. const sy = getTransformScaleY( m );
  1086. curve.xRadius *= sx;
  1087. curve.yRadius *= sy;
  1088. // Extract rotation angle from the matrix of form:
  1089. //
  1090. // | cosθ sx -sinθ sy |
  1091. // | sinθ sx cosθ sy |
  1092. //
  1093. // Remembering that tanθ = sinθ / cosθ; and that
  1094. // `sx`, `sy`, or both might be zero.
  1095. const theta =
  1096. sx > Number.EPSILON
  1097. ? Math.atan2( m.elements[ 1 ], m.elements[ 0 ] )
  1098. : Math.atan2( - m.elements[ 3 ], m.elements[ 4 ] );
  1099. curve.aRotation += theta;
  1100. if ( isTransformFlipped( m ) ) {
  1101. curve.aStartAngle *= - 1;
  1102. curve.aEndAngle *= - 1;
  1103. curve.aClockwise = ! curve.aClockwise;
  1104. }
  1105. }
  1106. const subPaths = path.subPaths;
  1107. for ( let i = 0, n = subPaths.length; i < n; i ++ ) {
  1108. const subPath = subPaths[ i ];
  1109. const curves = subPath.curves;
  1110. for ( let j = 0; j < curves.length; j ++ ) {
  1111. const curve = curves[ j ];
  1112. if ( curve.isLineCurve ) {
  1113. transfVec2( curve.v1 );
  1114. transfVec2( curve.v2 );
  1115. } else if ( curve.isCubicBezierCurve ) {
  1116. transfVec2( curve.v0 );
  1117. transfVec2( curve.v1 );
  1118. transfVec2( curve.v2 );
  1119. transfVec2( curve.v3 );
  1120. } else if ( curve.isQuadraticBezierCurve ) {
  1121. transfVec2( curve.v0 );
  1122. transfVec2( curve.v1 );
  1123. transfVec2( curve.v2 );
  1124. } else if ( curve.isEllipseCurve ) {
  1125. // Transform ellipse center point
  1126. tempV2.set( curve.aX, curve.aY );
  1127. transfVec2( tempV2 );
  1128. curve.aX = tempV2.x;
  1129. curve.aY = tempV2.y;
  1130. // Transform ellipse shape parameters
  1131. if ( isTransformSkewed( m ) ) {
  1132. transfEllipseGeneric( curve );
  1133. } else {
  1134. transfEllipseNoSkew( curve );
  1135. }
  1136. }
  1137. }
  1138. }
  1139. }
  1140. function isTransformFlipped( m ) {
  1141. const te = m.elements;
  1142. return te[ 0 ] * te[ 4 ] - te[ 1 ] * te[ 3 ] < 0;
  1143. }
  1144. function isTransformSkewed( m ) {
  1145. const te = m.elements;
  1146. const basisDot = te[ 0 ] * te[ 3 ] + te[ 1 ] * te[ 4 ];
  1147. // Shortcut for trivial rotations and transformations
  1148. if ( basisDot === 0 ) return false;
  1149. const sx = getTransformScaleX( m );
  1150. const sy = getTransformScaleY( m );
  1151. return Math.abs( basisDot / ( sx * sy ) ) > Number.EPSILON;
  1152. }
  1153. function getTransformScaleX( m ) {
  1154. const te = m.elements;
  1155. return Math.sqrt( te[ 0 ] * te[ 0 ] + te[ 1 ] * te[ 1 ] );
  1156. }
  1157. function getTransformScaleY( m ) {
  1158. const te = m.elements;
  1159. return Math.sqrt( te[ 3 ] * te[ 3 ] + te[ 4 ] * te[ 4 ] );
  1160. }
  1161. // Calculates the eigensystem of a real symmetric 2x2 matrix
  1162. // [ A B ]
  1163. // [ B C ]
  1164. // in the form
  1165. // [ A B ] = [ cs -sn ] [ rt1 0 ] [ cs sn ]
  1166. // [ B C ] [ sn cs ] [ 0 rt2 ] [ -sn cs ]
  1167. // where rt1 >= rt2.
  1168. //
  1169. // Adapted from: https://www.mpi-hd.mpg.de/personalhomes/globes/3x3/index.html
  1170. // -> Algorithms for real symmetric matrices -> Analytical (2x2 symmetric)
  1171. function eigenDecomposition( A, B, C ) {
  1172. let rt1, rt2, cs, sn, t;
  1173. const sm = A + C;
  1174. const df = A - C;
  1175. const rt = Math.sqrt( df * df + 4 * B * B );
  1176. if ( sm > 0 ) {
  1177. rt1 = 0.5 * ( sm + rt );
  1178. t = 1 / rt1;
  1179. rt2 = A * t * C - B * t * B;
  1180. } else if ( sm < 0 ) {
  1181. rt2 = 0.5 * ( sm - rt );
  1182. } else {
  1183. // This case needs to be treated separately to avoid div by 0
  1184. rt1 = 0.5 * rt;
  1185. rt2 = - 0.5 * rt;
  1186. }
  1187. // Calculate eigenvectors
  1188. if ( df > 0 ) {
  1189. cs = df + rt;
  1190. } else {
  1191. cs = df - rt;
  1192. }
  1193. if ( Math.abs( cs ) > 2 * Math.abs( B ) ) {
  1194. t = - 2 * B / cs;
  1195. sn = 1 / Math.sqrt( 1 + t * t );
  1196. cs = t * sn;
  1197. } else if ( Math.abs( B ) === 0 ) {
  1198. cs = 1;
  1199. sn = 0;
  1200. } else {
  1201. t = - 0.5 * cs / B;
  1202. cs = 1 / Math.sqrt( 1 + t * t );
  1203. sn = t * cs;
  1204. }
  1205. if ( df > 0 ) {
  1206. t = cs;
  1207. cs = - sn;
  1208. sn = t;
  1209. }
  1210. return { rt1, rt2, cs, sn };
  1211. }
  1212. //
  1213. const paths = [];
  1214. const stylesheets = {};
  1215. const transformStack = [];
  1216. const tempTransform0 = new Matrix3();
  1217. const tempTransform1 = new Matrix3();
  1218. const tempTransform2 = new Matrix3();
  1219. const tempTransform3 = new Matrix3();
  1220. const tempV2 = new Vector2();
  1221. const tempV3 = new Vector3();
  1222. const currentTransform = new Matrix3();
  1223. const xml = new DOMParser().parseFromString( text, 'image/svg+xml' ); // application/xml
  1224. parseNode( xml.documentElement, {
  1225. fill: '#000',
  1226. fillOpacity: 1,
  1227. strokeOpacity: 1,
  1228. strokeWidth: 1,
  1229. strokeLineJoin: 'miter',
  1230. strokeLineCap: 'butt',
  1231. strokeMiterLimit: 4
  1232. } );
  1233. const data = { paths: paths, xml: xml.documentElement };
  1234. // console.log( paths );
  1235. return data;
  1236. }
  1237. static createShapes( shapePath ) {
  1238. // Param shapePath: a shapepath as returned by the parse function of this class
  1239. // Returns Shape object
  1240. const BIGNUMBER = 999999999;
  1241. const IntersectionLocationType = {
  1242. ORIGIN: 0,
  1243. DESTINATION: 1,
  1244. BETWEEN: 2,
  1245. LEFT: 3,
  1246. RIGHT: 4,
  1247. BEHIND: 5,
  1248. BEYOND: 6
  1249. };
  1250. const classifyResult = {
  1251. loc: IntersectionLocationType.ORIGIN,
  1252. t: 0
  1253. };
  1254. function findEdgeIntersection( a0, a1, b0, b1 ) {
  1255. const x1 = a0.x;
  1256. const x2 = a1.x;
  1257. const x3 = b0.x;
  1258. const x4 = b1.x;
  1259. const y1 = a0.y;
  1260. const y2 = a1.y;
  1261. const y3 = b0.y;
  1262. const y4 = b1.y;
  1263. const nom1 = ( x4 - x3 ) * ( y1 - y3 ) - ( y4 - y3 ) * ( x1 - x3 );
  1264. const nom2 = ( x2 - x1 ) * ( y1 - y3 ) - ( y2 - y1 ) * ( x1 - x3 );
  1265. const denom = ( y4 - y3 ) * ( x2 - x1 ) - ( x4 - x3 ) * ( y2 - y1 );
  1266. const t1 = nom1 / denom;
  1267. const t2 = nom2 / denom;
  1268. if ( ( ( denom === 0 ) && ( nom1 !== 0 ) ) || ( t1 <= 0 ) || ( t1 >= 1 ) || ( t2 < 0 ) || ( t2 > 1 ) ) {
  1269. //1. lines are parallel or edges don't intersect
  1270. return null;
  1271. } else if ( ( nom1 === 0 ) && ( denom === 0 ) ) {
  1272. //2. lines are colinear
  1273. //check if endpoints of edge2 (b0-b1) lies on edge1 (a0-a1)
  1274. for ( let i = 0; i < 2; i ++ ) {
  1275. classifyPoint( i === 0 ? b0 : b1, a0, a1 );
  1276. //find position of this endpoints relatively to edge1
  1277. if ( classifyResult.loc == IntersectionLocationType.ORIGIN ) {
  1278. const point = ( i === 0 ? b0 : b1 );
  1279. return { x: point.x, y: point.y, t: classifyResult.t };
  1280. } else if ( classifyResult.loc == IntersectionLocationType.BETWEEN ) {
  1281. const x = + ( ( x1 + classifyResult.t * ( x2 - x1 ) ).toPrecision( 10 ) );
  1282. const y = + ( ( y1 + classifyResult.t * ( y2 - y1 ) ).toPrecision( 10 ) );
  1283. return { x: x, y: y, t: classifyResult.t, };
  1284. }
  1285. }
  1286. return null;
  1287. } else {
  1288. //3. edges intersect
  1289. for ( let i = 0; i < 2; i ++ ) {
  1290. classifyPoint( i === 0 ? b0 : b1, a0, a1 );
  1291. if ( classifyResult.loc == IntersectionLocationType.ORIGIN ) {
  1292. const point = ( i === 0 ? b0 : b1 );
  1293. return { x: point.x, y: point.y, t: classifyResult.t };
  1294. }
  1295. }
  1296. const x = + ( ( x1 + t1 * ( x2 - x1 ) ).toPrecision( 10 ) );
  1297. const y = + ( ( y1 + t1 * ( y2 - y1 ) ).toPrecision( 10 ) );
  1298. return { x: x, y: y, t: t1 };
  1299. }
  1300. }
  1301. function classifyPoint( p, edgeStart, edgeEnd ) {
  1302. const ax = edgeEnd.x - edgeStart.x;
  1303. const ay = edgeEnd.y - edgeStart.y;
  1304. const bx = p.x - edgeStart.x;
  1305. const by = p.y - edgeStart.y;
  1306. const sa = ax * by - bx * ay;
  1307. if ( ( p.x === edgeStart.x ) && ( p.y === edgeStart.y ) ) {
  1308. classifyResult.loc = IntersectionLocationType.ORIGIN;
  1309. classifyResult.t = 0;
  1310. return;
  1311. }
  1312. if ( ( p.x === edgeEnd.x ) && ( p.y === edgeEnd.y ) ) {
  1313. classifyResult.loc = IntersectionLocationType.DESTINATION;
  1314. classifyResult.t = 1;
  1315. return;
  1316. }
  1317. if ( sa < - Number.EPSILON ) {
  1318. classifyResult.loc = IntersectionLocationType.LEFT;
  1319. return;
  1320. }
  1321. if ( sa > Number.EPSILON ) {
  1322. classifyResult.loc = IntersectionLocationType.RIGHT;
  1323. return;
  1324. }
  1325. if ( ( ( ax * bx ) < 0 ) || ( ( ay * by ) < 0 ) ) {
  1326. classifyResult.loc = IntersectionLocationType.BEHIND;
  1327. return;
  1328. }
  1329. if ( ( Math.sqrt( ax * ax + ay * ay ) ) < ( Math.sqrt( bx * bx + by * by ) ) ) {
  1330. classifyResult.loc = IntersectionLocationType.BEYOND;
  1331. return;
  1332. }
  1333. let t;
  1334. if ( ax !== 0 ) {
  1335. t = bx / ax;
  1336. } else {
  1337. t = by / ay;
  1338. }
  1339. classifyResult.loc = IntersectionLocationType.BETWEEN;
  1340. classifyResult.t = t;
  1341. }
  1342. function getIntersections( path1, path2 ) {
  1343. const intersectionsRaw = [];
  1344. const intersections = [];
  1345. for ( let index = 1; index < path1.length; index ++ ) {
  1346. const path1EdgeStart = path1[ index - 1 ];
  1347. const path1EdgeEnd = path1[ index ];
  1348. for ( let index2 = 1; index2 < path2.length; index2 ++ ) {
  1349. const path2EdgeStart = path2[ index2 - 1 ];
  1350. const path2EdgeEnd = path2[ index2 ];
  1351. const intersection = findEdgeIntersection( path1EdgeStart, path1EdgeEnd, path2EdgeStart, path2EdgeEnd );
  1352. if ( intersection !== null && intersectionsRaw.find( i => i.t <= intersection.t + Number.EPSILON && i.t >= intersection.t - Number.EPSILON ) === undefined ) {
  1353. intersectionsRaw.push( intersection );
  1354. intersections.push( new Vector2( intersection.x, intersection.y ) );
  1355. }
  1356. }
  1357. }
  1358. return intersections;
  1359. }
  1360. function getScanlineIntersections( scanline, boundingBox, paths ) {
  1361. const center = new Vector2();
  1362. boundingBox.getCenter( center );
  1363. const allIntersections = [];
  1364. paths.forEach( path => {
  1365. // check if the center of the bounding box is in the bounding box of the paths.
  1366. // this is a pruning method to limit the search of intersections in paths that can't envelop of the current path.
  1367. // if a path envelops another path. The center of that oter path, has to be inside the bounding box of the enveloping path.
  1368. if ( path.boundingBox.containsPoint( center ) ) {
  1369. const intersections = getIntersections( scanline, path.points );
  1370. intersections.forEach( p => {
  1371. allIntersections.push( { identifier: path.identifier, isCW: path.isCW, point: p } );
  1372. } );
  1373. }
  1374. } );
  1375. allIntersections.sort( ( i1, i2 ) => {
  1376. return i1.point.x - i2.point.x;
  1377. } );
  1378. return allIntersections;
  1379. }
  1380. function isHoleTo( simplePath, allPaths, scanlineMinX, scanlineMaxX, _fillRule ) {
  1381. if ( _fillRule === null || _fillRule === undefined || _fillRule === '' ) {
  1382. _fillRule = 'nonzero';
  1383. }
  1384. const centerBoundingBox = new Vector2();
  1385. simplePath.boundingBox.getCenter( centerBoundingBox );
  1386. const scanline = [ new Vector2( scanlineMinX, centerBoundingBox.y ), new Vector2( scanlineMaxX, centerBoundingBox.y ) ];
  1387. const scanlineIntersections = getScanlineIntersections( scanline, simplePath.boundingBox, allPaths );
  1388. scanlineIntersections.sort( ( i1, i2 ) => {
  1389. return i1.point.x - i2.point.x;
  1390. } );
  1391. const baseIntersections = [];
  1392. const otherIntersections = [];
  1393. scanlineIntersections.forEach( i => {
  1394. if ( i.identifier === simplePath.identifier ) {
  1395. baseIntersections.push( i );
  1396. } else {
  1397. otherIntersections.push( i );
  1398. }
  1399. } );
  1400. const firstXOfPath = baseIntersections[ 0 ].point.x;
  1401. // build up the path hierarchy
  1402. const stack = [];
  1403. let i = 0;
  1404. while ( i < otherIntersections.length && otherIntersections[ i ].point.x < firstXOfPath ) {
  1405. if ( stack.length > 0 && stack[ stack.length - 1 ] === otherIntersections[ i ].identifier ) {
  1406. stack.pop();
  1407. } else {
  1408. stack.push( otherIntersections[ i ].identifier );
  1409. }
  1410. i ++;
  1411. }
  1412. stack.push( simplePath.identifier );
  1413. if ( _fillRule === 'evenodd' ) {
  1414. const isHole = stack.length % 2 === 0 ? true : false;
  1415. const isHoleFor = stack[ stack.length - 2 ];
  1416. return { identifier: simplePath.identifier, isHole: isHole, for: isHoleFor };
  1417. } else if ( _fillRule === 'nonzero' ) {
  1418. // check if path is a hole by counting the amount of paths with alternating rotations it has to cross.
  1419. let isHole = true;
  1420. let isHoleFor = null;
  1421. let lastCWValue = null;
  1422. for ( let i = 0; i < stack.length; i ++ ) {
  1423. const identifier = stack[ i ];
  1424. if ( isHole ) {
  1425. lastCWValue = allPaths[ identifier ].isCW;
  1426. isHole = false;
  1427. isHoleFor = identifier;
  1428. } else if ( lastCWValue !== allPaths[ identifier ].isCW ) {
  1429. lastCWValue = allPaths[ identifier ].isCW;
  1430. isHole = true;
  1431. }
  1432. }
  1433. return { identifier: simplePath.identifier, isHole: isHole, for: isHoleFor };
  1434. } else {
  1435. console.warn( 'fill-rule: "' + _fillRule + '" is currently not implemented.' );
  1436. }
  1437. }
  1438. // check for self intersecting paths
  1439. // TODO
  1440. // check intersecting paths
  1441. // TODO
  1442. // prepare paths for hole detection
  1443. let scanlineMinX = BIGNUMBER;
  1444. let scanlineMaxX = - BIGNUMBER;
  1445. let simplePaths = shapePath.subPaths.map( p => {
  1446. const points = p.getPoints();
  1447. let maxY = - BIGNUMBER;
  1448. let minY = BIGNUMBER;
  1449. let maxX = - BIGNUMBER;
  1450. let minX = BIGNUMBER;
  1451. //points.forEach(p => p.y *= -1);
  1452. for ( let i = 0; i < points.length; i ++ ) {
  1453. const p = points[ i ];
  1454. if ( p.y > maxY ) {
  1455. maxY = p.y;
  1456. }
  1457. if ( p.y < minY ) {
  1458. minY = p.y;
  1459. }
  1460. if ( p.x > maxX ) {
  1461. maxX = p.x;
  1462. }
  1463. if ( p.x < minX ) {
  1464. minX = p.x;
  1465. }
  1466. }
  1467. //
  1468. if ( scanlineMaxX <= maxX ) {
  1469. scanlineMaxX = maxX + 1;
  1470. }
  1471. if ( scanlineMinX >= minX ) {
  1472. scanlineMinX = minX - 1;
  1473. }
  1474. return { curves: p.curves, points: points, isCW: ShapeUtils.isClockWise( points ), identifier: - 1, boundingBox: new Box2( new Vector2( minX, minY ), new Vector2( maxX, maxY ) ) };
  1475. } );
  1476. simplePaths = simplePaths.filter( sp => sp.points.length > 1 );
  1477. for ( let identifier = 0; identifier < simplePaths.length; identifier ++ ) {
  1478. simplePaths[ identifier ].identifier = identifier;
  1479. }
  1480. // check if path is solid or a hole
  1481. const isAHole = simplePaths.map( p => isHoleTo( p, simplePaths, scanlineMinX, scanlineMaxX, ( shapePath.userData ? shapePath.userData.style.fillRule : undefined ) ) );
  1482. const shapesToReturn = [];
  1483. simplePaths.forEach( p => {
  1484. const amIAHole = isAHole[ p.identifier ];
  1485. if ( ! amIAHole.isHole ) {
  1486. const shape = new Shape();
  1487. shape.curves = p.curves;
  1488. const holes = isAHole.filter( h => h.isHole && h.for === p.identifier );
  1489. holes.forEach( h => {
  1490. const hole = simplePaths[ h.identifier ];
  1491. const path = new Path();
  1492. path.curves = hole.curves;
  1493. shape.holes.push( path );
  1494. } );
  1495. shapesToReturn.push( shape );
  1496. }
  1497. } );
  1498. return shapesToReturn;
  1499. }
  1500. static getStrokeStyle( width, color, lineJoin, lineCap, miterLimit ) {
  1501. // Param width: Stroke width
  1502. // Param color: As returned by THREE.Color.getStyle()
  1503. // Param lineJoin: One of "round", "bevel", "miter" or "miter-limit"
  1504. // Param lineCap: One of "round", "square" or "butt"
  1505. // Param miterLimit: Maximum join length, in multiples of the "width" parameter (join is truncated if it exceeds that distance)
  1506. // Returns style object
  1507. width = width !== undefined ? width : 1;
  1508. color = color !== undefined ? color : '#000';
  1509. lineJoin = lineJoin !== undefined ? lineJoin : 'miter';
  1510. lineCap = lineCap !== undefined ? lineCap : 'butt';
  1511. miterLimit = miterLimit !== undefined ? miterLimit : 4;
  1512. return {
  1513. strokeColor: color,
  1514. strokeWidth: width,
  1515. strokeLineJoin: lineJoin,
  1516. strokeLineCap: lineCap,
  1517. strokeMiterLimit: miterLimit
  1518. };
  1519. }
  1520. static pointsToStroke( points, style, arcDivisions, minDistance ) {
  1521. // Generates a stroke with some witdh around the given path.
  1522. // The path can be open or closed (last point equals to first point)
  1523. // Param points: Array of Vector2D (the path). Minimum 2 points.
  1524. // Param style: Object with SVG properties as returned by SVGLoader.getStrokeStyle(), or SVGLoader.parse() in the path.userData.style object
  1525. // Params arcDivisions: Arc divisions for round joins and endcaps. (Optional)
  1526. // Param minDistance: Points closer to this distance will be merged. (Optional)
  1527. // Returns BufferGeometry with stroke triangles (In plane z = 0). UV coordinates are generated ('u' along path. 'v' across it, from left to right)
  1528. const vertices = [];
  1529. const normals = [];
  1530. const uvs = [];
  1531. if ( SVGLoader.pointsToStrokeWithBuffers( points, style, arcDivisions, minDistance, vertices, normals, uvs ) === 0 ) {
  1532. return null;
  1533. }
  1534. const geometry = new BufferGeometry();
  1535. geometry.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  1536. geometry.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
  1537. geometry.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
  1538. return geometry;
  1539. }
  1540. static pointsToStrokeWithBuffers( points, style, arcDivisions, minDistance, vertices, normals, uvs, vertexOffset ) {
  1541. // This function can be called to update existing arrays or buffers.
  1542. // Accepts same parameters as pointsToStroke, plus the buffers and optional offset.
  1543. // Param vertexOffset: Offset vertices to start writing in the buffers (3 elements/vertex for vertices and normals, and 2 elements/vertex for uvs)
  1544. // Returns number of written vertices / normals / uvs pairs
  1545. // if 'vertices' parameter is undefined no triangles will be generated, but the returned vertices count will still be valid (useful to preallocate the buffers)
  1546. // 'normals' and 'uvs' buffers are optional
  1547. const tempV2_1 = new Vector2();
  1548. const tempV2_2 = new Vector2();
  1549. const tempV2_3 = new Vector2();
  1550. const tempV2_4 = new Vector2();
  1551. const tempV2_5 = new Vector2();
  1552. const tempV2_6 = new Vector2();
  1553. const tempV2_7 = new Vector2();
  1554. const lastPointL = new Vector2();
  1555. const lastPointR = new Vector2();
  1556. const point0L = new Vector2();
  1557. const point0R = new Vector2();
  1558. const currentPointL = new Vector2();
  1559. const currentPointR = new Vector2();
  1560. const nextPointL = new Vector2();
  1561. const nextPointR = new Vector2();
  1562. const innerPoint = new Vector2();
  1563. const outerPoint = new Vector2();
  1564. arcDivisions = arcDivisions !== undefined ? arcDivisions : 12;
  1565. minDistance = minDistance !== undefined ? minDistance : 0.001;
  1566. vertexOffset = vertexOffset !== undefined ? vertexOffset : 0;
  1567. // First ensure there are no duplicated points
  1568. points = removeDuplicatedPoints( points );
  1569. const numPoints = points.length;
  1570. if ( numPoints < 2 ) return 0;
  1571. const isClosed = points[ 0 ].equals( points[ numPoints - 1 ] );
  1572. let currentPoint;
  1573. let previousPoint = points[ 0 ];
  1574. let nextPoint;
  1575. const strokeWidth2 = style.strokeWidth / 2;
  1576. const deltaU = 1 / ( numPoints - 1 );
  1577. let u0 = 0, u1;
  1578. let innerSideModified;
  1579. let joinIsOnLeftSide;
  1580. let isMiter;
  1581. let initialJoinIsOnLeftSide = false;
  1582. let numVertices = 0;
  1583. let currentCoordinate = vertexOffset * 3;
  1584. let currentCoordinateUV = vertexOffset * 2;
  1585. // Get initial left and right stroke points
  1586. getNormal( points[ 0 ], points[ 1 ], tempV2_1 ).multiplyScalar( strokeWidth2 );
  1587. lastPointL.copy( points[ 0 ] ).sub( tempV2_1 );
  1588. lastPointR.copy( points[ 0 ] ).add( tempV2_1 );
  1589. point0L.copy( lastPointL );
  1590. point0R.copy( lastPointR );
  1591. for ( let iPoint = 1; iPoint < numPoints; iPoint ++ ) {
  1592. currentPoint = points[ iPoint ];
  1593. // Get next point
  1594. if ( iPoint === numPoints - 1 ) {
  1595. if ( isClosed ) {
  1596. // Skip duplicated initial point
  1597. nextPoint = points[ 1 ];
  1598. } else nextPoint = undefined;
  1599. } else {
  1600. nextPoint = points[ iPoint + 1 ];
  1601. }
  1602. // Normal of previous segment in tempV2_1
  1603. const normal1 = tempV2_1;
  1604. getNormal( previousPoint, currentPoint, normal1 );
  1605. tempV2_3.copy( normal1 ).multiplyScalar( strokeWidth2 );
  1606. currentPointL.copy( currentPoint ).sub( tempV2_3 );
  1607. currentPointR.copy( currentPoint ).add( tempV2_3 );
  1608. u1 = u0 + deltaU;
  1609. innerSideModified = false;
  1610. if ( nextPoint !== undefined ) {
  1611. // Normal of next segment in tempV2_2
  1612. getNormal( currentPoint, nextPoint, tempV2_2 );
  1613. tempV2_3.copy( tempV2_2 ).multiplyScalar( strokeWidth2 );
  1614. nextPointL.copy( currentPoint ).sub( tempV2_3 );
  1615. nextPointR.copy( currentPoint ).add( tempV2_3 );
  1616. joinIsOnLeftSide = true;
  1617. tempV2_3.subVectors( nextPoint, previousPoint );
  1618. if ( normal1.dot( tempV2_3 ) < 0 ) {
  1619. joinIsOnLeftSide = false;
  1620. }
  1621. if ( iPoint === 1 ) initialJoinIsOnLeftSide = joinIsOnLeftSide;
  1622. tempV2_3.subVectors( nextPoint, currentPoint );
  1623. tempV2_3.normalize();
  1624. const dot = Math.abs( normal1.dot( tempV2_3 ) );
  1625. // If path is straight, don't create join
  1626. if ( dot > Number.EPSILON ) {
  1627. // Compute inner and outer segment intersections
  1628. const miterSide = strokeWidth2 / dot;
  1629. tempV2_3.multiplyScalar( - miterSide );
  1630. tempV2_4.subVectors( currentPoint, previousPoint );
  1631. tempV2_5.copy( tempV2_4 ).setLength( miterSide ).add( tempV2_3 );
  1632. innerPoint.copy( tempV2_5 ).negate();
  1633. const miterLength2 = tempV2_5.length();
  1634. const segmentLengthPrev = tempV2_4.length();
  1635. tempV2_4.divideScalar( segmentLengthPrev );
  1636. tempV2_6.subVectors( nextPoint, currentPoint );
  1637. const segmentLengthNext = tempV2_6.length();
  1638. tempV2_6.divideScalar( segmentLengthNext );
  1639. // Check that previous and next segments doesn't overlap with the innerPoint of intersection
  1640. if ( tempV2_4.dot( innerPoint ) < segmentLengthPrev && tempV2_6.dot( innerPoint ) < segmentLengthNext ) {
  1641. innerSideModified = true;
  1642. }
  1643. outerPoint.copy( tempV2_5 ).add( currentPoint );
  1644. innerPoint.add( currentPoint );
  1645. isMiter = false;
  1646. if ( innerSideModified ) {
  1647. if ( joinIsOnLeftSide ) {
  1648. nextPointR.copy( innerPoint );
  1649. currentPointR.copy( innerPoint );
  1650. } else {
  1651. nextPointL.copy( innerPoint );
  1652. currentPointL.copy( innerPoint );
  1653. }
  1654. } else {
  1655. // The segment triangles are generated here if there was overlapping
  1656. makeSegmentTriangles();
  1657. }
  1658. switch ( style.strokeLineJoin ) {
  1659. case 'bevel':
  1660. makeSegmentWithBevelJoin( joinIsOnLeftSide, innerSideModified, u1 );
  1661. break;
  1662. case 'round':
  1663. // Segment triangles
  1664. createSegmentTrianglesWithMiddleSection( joinIsOnLeftSide, innerSideModified );
  1665. // Join triangles
  1666. if ( joinIsOnLeftSide ) {
  1667. makeCircularSector( currentPoint, currentPointL, nextPointL, u1, 0 );
  1668. } else {
  1669. makeCircularSector( currentPoint, nextPointR, currentPointR, u1, 1 );
  1670. }
  1671. break;
  1672. case 'miter':
  1673. case 'miter-clip':
  1674. default:
  1675. const miterFraction = ( strokeWidth2 * style.strokeMiterLimit ) / miterLength2;
  1676. if ( miterFraction < 1 ) {
  1677. // The join miter length exceeds the miter limit
  1678. if ( style.strokeLineJoin !== 'miter-clip' ) {
  1679. makeSegmentWithBevelJoin( joinIsOnLeftSide, innerSideModified, u1 );
  1680. break;
  1681. } else {
  1682. // Segment triangles
  1683. createSegmentTrianglesWithMiddleSection( joinIsOnLeftSide, innerSideModified );
  1684. // Miter-clip join triangles
  1685. if ( joinIsOnLeftSide ) {
  1686. tempV2_6.subVectors( outerPoint, currentPointL ).multiplyScalar( miterFraction ).add( currentPointL );
  1687. tempV2_7.subVectors( outerPoint, nextPointL ).multiplyScalar( miterFraction ).add( nextPointL );
  1688. addVertex( currentPointL, u1, 0 );
  1689. addVertex( tempV2_6, u1, 0 );
  1690. addVertex( currentPoint, u1, 0.5 );
  1691. addVertex( currentPoint, u1, 0.5 );
  1692. addVertex( tempV2_6, u1, 0 );
  1693. addVertex( tempV2_7, u1, 0 );
  1694. addVertex( currentPoint, u1, 0.5 );
  1695. addVertex( tempV2_7, u1, 0 );
  1696. addVertex( nextPointL, u1, 0 );
  1697. } else {
  1698. tempV2_6.subVectors( outerPoint, currentPointR ).multiplyScalar( miterFraction ).add( currentPointR );
  1699. tempV2_7.subVectors( outerPoint, nextPointR ).multiplyScalar( miterFraction ).add( nextPointR );
  1700. addVertex( currentPointR, u1, 1 );
  1701. addVertex( tempV2_6, u1, 1 );
  1702. addVertex( currentPoint, u1, 0.5 );
  1703. addVertex( currentPoint, u1, 0.5 );
  1704. addVertex( tempV2_6, u1, 1 );
  1705. addVertex( tempV2_7, u1, 1 );
  1706. addVertex( currentPoint, u1, 0.5 );
  1707. addVertex( tempV2_7, u1, 1 );
  1708. addVertex( nextPointR, u1, 1 );
  1709. }
  1710. }
  1711. } else {
  1712. // Miter join segment triangles
  1713. if ( innerSideModified ) {
  1714. // Optimized segment + join triangles
  1715. if ( joinIsOnLeftSide ) {
  1716. addVertex( lastPointR, u0, 1 );
  1717. addVertex( lastPointL, u0, 0 );
  1718. addVertex( outerPoint, u1, 0 );
  1719. addVertex( lastPointR, u0, 1 );
  1720. addVertex( outerPoint, u1, 0 );
  1721. addVertex( innerPoint, u1, 1 );
  1722. } else {
  1723. addVertex( lastPointR, u0, 1 );
  1724. addVertex( lastPointL, u0, 0 );
  1725. addVertex( outerPoint, u1, 1 );
  1726. addVertex( lastPointL, u0, 0 );
  1727. addVertex( innerPoint, u1, 0 );
  1728. addVertex( outerPoint, u1, 1 );
  1729. }
  1730. if ( joinIsOnLeftSide ) {
  1731. nextPointL.copy( outerPoint );
  1732. } else {
  1733. nextPointR.copy( outerPoint );
  1734. }
  1735. } else {
  1736. // Add extra miter join triangles
  1737. if ( joinIsOnLeftSide ) {
  1738. addVertex( currentPointL, u1, 0 );
  1739. addVertex( outerPoint, u1, 0 );
  1740. addVertex( currentPoint, u1, 0.5 );
  1741. addVertex( currentPoint, u1, 0.5 );
  1742. addVertex( outerPoint, u1, 0 );
  1743. addVertex( nextPointL, u1, 0 );
  1744. } else {
  1745. addVertex( currentPointR, u1, 1 );
  1746. addVertex( outerPoint, u1, 1 );
  1747. addVertex( currentPoint, u1, 0.5 );
  1748. addVertex( currentPoint, u1, 0.5 );
  1749. addVertex( outerPoint, u1, 1 );
  1750. addVertex( nextPointR, u1, 1 );
  1751. }
  1752. }
  1753. isMiter = true;
  1754. }
  1755. break;
  1756. }
  1757. } else {
  1758. // The segment triangles are generated here when two consecutive points are collinear
  1759. makeSegmentTriangles();
  1760. }
  1761. } else {
  1762. // The segment triangles are generated here if it is the ending segment
  1763. makeSegmentTriangles();
  1764. }
  1765. if ( ! isClosed && iPoint === numPoints - 1 ) {
  1766. // Start line endcap
  1767. addCapGeometry( points[ 0 ], point0L, point0R, joinIsOnLeftSide, true, u0 );
  1768. }
  1769. // Increment loop variables
  1770. u0 = u1;
  1771. previousPoint = currentPoint;
  1772. lastPointL.copy( nextPointL );
  1773. lastPointR.copy( nextPointR );
  1774. }
  1775. if ( ! isClosed ) {
  1776. // Ending line endcap
  1777. addCapGeometry( currentPoint, currentPointL, currentPointR, joinIsOnLeftSide, false, u1 );
  1778. } else if ( innerSideModified && vertices ) {
  1779. // Modify path first segment vertices to adjust to the segments inner and outer intersections
  1780. let lastOuter = outerPoint;
  1781. let lastInner = innerPoint;
  1782. if ( initialJoinIsOnLeftSide !== joinIsOnLeftSide ) {
  1783. lastOuter = innerPoint;
  1784. lastInner = outerPoint;
  1785. }
  1786. if ( joinIsOnLeftSide ) {
  1787. if ( isMiter || initialJoinIsOnLeftSide ) {
  1788. lastInner.toArray( vertices, 0 * 3 );
  1789. lastInner.toArray( vertices, 3 * 3 );
  1790. if ( isMiter ) {
  1791. lastOuter.toArray( vertices, 1 * 3 );
  1792. }
  1793. }
  1794. } else {
  1795. if ( isMiter || ! initialJoinIsOnLeftSide ) {
  1796. lastInner.toArray( vertices, 1 * 3 );
  1797. lastInner.toArray( vertices, 3 * 3 );
  1798. if ( isMiter ) {
  1799. lastOuter.toArray( vertices, 0 * 3 );
  1800. }
  1801. }
  1802. }
  1803. }
  1804. return numVertices;
  1805. // -- End of algorithm
  1806. // -- Functions
  1807. function getNormal( p1, p2, result ) {
  1808. result.subVectors( p2, p1 );
  1809. return result.set( - result.y, result.x ).normalize();
  1810. }
  1811. function addVertex( position, u, v ) {
  1812. if ( vertices ) {
  1813. vertices[ currentCoordinate ] = position.x;
  1814. vertices[ currentCoordinate + 1 ] = position.y;
  1815. vertices[ currentCoordinate + 2 ] = 0;
  1816. if ( normals ) {
  1817. normals[ currentCoordinate ] = 0;
  1818. normals[ currentCoordinate + 1 ] = 0;
  1819. normals[ currentCoordinate + 2 ] = 1;
  1820. }
  1821. currentCoordinate += 3;
  1822. if ( uvs ) {
  1823. uvs[ currentCoordinateUV ] = u;
  1824. uvs[ currentCoordinateUV + 1 ] = v;
  1825. currentCoordinateUV += 2;
  1826. }
  1827. }
  1828. numVertices += 3;
  1829. }
  1830. function makeCircularSector( center, p1, p2, u, v ) {
  1831. // param p1, p2: Points in the circle arc.
  1832. // p1 and p2 are in clockwise direction.
  1833. tempV2_1.copy( p1 ).sub( center ).normalize();
  1834. tempV2_2.copy( p2 ).sub( center ).normalize();
  1835. let angle = Math.PI;
  1836. const dot = tempV2_1.dot( tempV2_2 );
  1837. if ( Math.abs( dot ) < 1 ) angle = Math.abs( Math.acos( dot ) );
  1838. angle /= arcDivisions;
  1839. tempV2_3.copy( p1 );
  1840. for ( let i = 0, il = arcDivisions - 1; i < il; i ++ ) {
  1841. tempV2_4.copy( tempV2_3 ).rotateAround( center, angle );
  1842. addVertex( tempV2_3, u, v );
  1843. addVertex( tempV2_4, u, v );
  1844. addVertex( center, u, 0.5 );
  1845. tempV2_3.copy( tempV2_4 );
  1846. }
  1847. addVertex( tempV2_4, u, v );
  1848. addVertex( p2, u, v );
  1849. addVertex( center, u, 0.5 );
  1850. }
  1851. function makeSegmentTriangles() {
  1852. addVertex( lastPointR, u0, 1 );
  1853. addVertex( lastPointL, u0, 0 );
  1854. addVertex( currentPointL, u1, 0 );
  1855. addVertex( lastPointR, u0, 1 );
  1856. addVertex( currentPointL, u1, 1 );
  1857. addVertex( currentPointR, u1, 0 );
  1858. }
  1859. function makeSegmentWithBevelJoin( joinIsOnLeftSide, innerSideModified, u ) {
  1860. if ( innerSideModified ) {
  1861. // Optimized segment + bevel triangles
  1862. if ( joinIsOnLeftSide ) {
  1863. // Path segments triangles
  1864. addVertex( lastPointR, u0, 1 );
  1865. addVertex( lastPointL, u0, 0 );
  1866. addVertex( currentPointL, u1, 0 );
  1867. addVertex( lastPointR, u0, 1 );
  1868. addVertex( currentPointL, u1, 0 );
  1869. addVertex( innerPoint, u1, 1 );
  1870. // Bevel join triangle
  1871. addVertex( currentPointL, u, 0 );
  1872. addVertex( nextPointL, u, 0 );
  1873. addVertex( innerPoint, u, 0.5 );
  1874. } else {
  1875. // Path segments triangles
  1876. addVertex( lastPointR, u0, 1 );
  1877. addVertex( lastPointL, u0, 0 );
  1878. addVertex( currentPointR, u1, 1 );
  1879. addVertex( lastPointL, u0, 0 );
  1880. addVertex( innerPoint, u1, 0 );
  1881. addVertex( currentPointR, u1, 1 );
  1882. // Bevel join triangle
  1883. addVertex( currentPointR, u, 1 );
  1884. addVertex( nextPointR, u, 0 );
  1885. addVertex( innerPoint, u, 0.5 );
  1886. }
  1887. } else {
  1888. // Bevel join triangle. The segment triangles are done in the main loop
  1889. if ( joinIsOnLeftSide ) {
  1890. addVertex( currentPointL, u, 0 );
  1891. addVertex( nextPointL, u, 0 );
  1892. addVertex( currentPoint, u, 0.5 );
  1893. } else {
  1894. addVertex( currentPointR, u, 1 );
  1895. addVertex( nextPointR, u, 0 );
  1896. addVertex( currentPoint, u, 0.5 );
  1897. }
  1898. }
  1899. }
  1900. function createSegmentTrianglesWithMiddleSection( joinIsOnLeftSide, innerSideModified ) {
  1901. if ( innerSideModified ) {
  1902. if ( joinIsOnLeftSide ) {
  1903. addVertex( lastPointR, u0, 1 );
  1904. addVertex( lastPointL, u0, 0 );
  1905. addVertex( currentPointL, u1, 0 );
  1906. addVertex( lastPointR, u0, 1 );
  1907. addVertex( currentPointL, u1, 0 );
  1908. addVertex( innerPoint, u1, 1 );
  1909. addVertex( currentPointL, u0, 0 );
  1910. addVertex( currentPoint, u1, 0.5 );
  1911. addVertex( innerPoint, u1, 1 );
  1912. addVertex( currentPoint, u1, 0.5 );
  1913. addVertex( nextPointL, u0, 0 );
  1914. addVertex( innerPoint, u1, 1 );
  1915. } else {
  1916. addVertex( lastPointR, u0, 1 );
  1917. addVertex( lastPointL, u0, 0 );
  1918. addVertex( currentPointR, u1, 1 );
  1919. addVertex( lastPointL, u0, 0 );
  1920. addVertex( innerPoint, u1, 0 );
  1921. addVertex( currentPointR, u1, 1 );
  1922. addVertex( currentPointR, u0, 1 );
  1923. addVertex( innerPoint, u1, 0 );
  1924. addVertex( currentPoint, u1, 0.5 );
  1925. addVertex( currentPoint, u1, 0.5 );
  1926. addVertex( innerPoint, u1, 0 );
  1927. addVertex( nextPointR, u0, 1 );
  1928. }
  1929. }
  1930. }
  1931. function addCapGeometry( center, p1, p2, joinIsOnLeftSide, start, u ) {
  1932. // param center: End point of the path
  1933. // param p1, p2: Left and right cap points
  1934. switch ( style.strokeLineCap ) {
  1935. case 'round':
  1936. if ( start ) {
  1937. makeCircularSector( center, p2, p1, u, 0.5 );
  1938. } else {
  1939. makeCircularSector( center, p1, p2, u, 0.5 );
  1940. }
  1941. break;
  1942. case 'square':
  1943. if ( start ) {
  1944. tempV2_1.subVectors( p1, center );
  1945. tempV2_2.set( tempV2_1.y, - tempV2_1.x );
  1946. tempV2_3.addVectors( tempV2_1, tempV2_2 ).add( center );
  1947. tempV2_4.subVectors( tempV2_2, tempV2_1 ).add( center );
  1948. // Modify already existing vertices
  1949. if ( joinIsOnLeftSide ) {
  1950. tempV2_3.toArray( vertices, 1 * 3 );
  1951. tempV2_4.toArray( vertices, 0 * 3 );
  1952. tempV2_4.toArray( vertices, 3 * 3 );
  1953. } else {
  1954. tempV2_3.toArray( vertices, 1 * 3 );
  1955. tempV2_3.toArray( vertices, 3 * 3 );
  1956. tempV2_4.toArray( vertices, 0 * 3 );
  1957. }
  1958. } else {
  1959. tempV2_1.subVectors( p2, center );
  1960. tempV2_2.set( tempV2_1.y, - tempV2_1.x );
  1961. tempV2_3.addVectors( tempV2_1, tempV2_2 ).add( center );
  1962. tempV2_4.subVectors( tempV2_2, tempV2_1 ).add( center );
  1963. const vl = vertices.length;
  1964. // Modify already existing vertices
  1965. if ( joinIsOnLeftSide ) {
  1966. tempV2_3.toArray( vertices, vl - 1 * 3 );
  1967. tempV2_4.toArray( vertices, vl - 2 * 3 );
  1968. tempV2_4.toArray( vertices, vl - 4 * 3 );
  1969. } else {
  1970. tempV2_3.toArray( vertices, vl - 2 * 3 );
  1971. tempV2_4.toArray( vertices, vl - 1 * 3 );
  1972. tempV2_4.toArray( vertices, vl - 4 * 3 );
  1973. }
  1974. }
  1975. break;
  1976. case 'butt':
  1977. default:
  1978. // Nothing to do here
  1979. break;
  1980. }
  1981. }
  1982. function removeDuplicatedPoints( points ) {
  1983. // Creates a new array if necessary with duplicated points removed.
  1984. // This does not remove duplicated initial and ending points of a closed path.
  1985. let dupPoints = false;
  1986. for ( let i = 1, n = points.length - 1; i < n; i ++ ) {
  1987. if ( points[ i ].distanceTo( points[ i + 1 ] ) < minDistance ) {
  1988. dupPoints = true;
  1989. break;
  1990. }
  1991. }
  1992. if ( ! dupPoints ) return points;
  1993. const newPoints = [];
  1994. newPoints.push( points[ 0 ] );
  1995. for ( let i = 1, n = points.length - 1; i < n; i ++ ) {
  1996. if ( points[ i ].distanceTo( points[ i + 1 ] ) >= minDistance ) {
  1997. newPoints.push( points[ i ] );
  1998. }
  1999. }
  2000. newPoints.push( points[ points.length - 1 ] );
  2001. return newPoints;
  2002. }
  2003. }
  2004. }
  2005. export { SVGLoader };