SVGLoader.js 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439
  1. import {
  2. BufferGeometry,
  3. FileLoader,
  4. Float32BufferAttribute,
  5. Loader,
  6. Matrix3,
  7. Path,
  8. ShapePath,
  9. Vector2,
  10. Vector3
  11. } from '../../../build/three.module.js';
  12. var SVGLoader = function ( manager ) {
  13. Loader.call( this, manager );
  14. // Default dots per inch
  15. this.defaultDPI = 90;
  16. // Accepted units: 'mm', 'cm', 'in', 'pt', 'pc', 'px'
  17. this.defaultUnit = 'px';
  18. };
  19. SVGLoader.prototype = Object.assign( Object.create( Loader.prototype ), {
  20. constructor: SVGLoader,
  21. load: function ( url, onLoad, onProgress, onError ) {
  22. var scope = this;
  23. var loader = new FileLoader( scope.manager );
  24. loader.setPath( scope.path );
  25. loader.setRequestHeader( scope.requestHeader );
  26. loader.setWithCredentials( scope.withCredentials );
  27. loader.load( url, function ( text ) {
  28. try {
  29. onLoad( scope.parse( text ) );
  30. } catch ( e ) {
  31. if ( onError ) {
  32. onError( e );
  33. } else {
  34. console.error( e );
  35. }
  36. scope.manager.itemError( url );
  37. }
  38. }, onProgress, onError );
  39. },
  40. parse: function ( text ) {
  41. var scope = this;
  42. function parseNode( node, style ) {
  43. if ( node.nodeType !== 1 ) return;
  44. var transform = getNodeTransform( node );
  45. var traverseChildNodes = true;
  46. var path = null;
  47. switch ( node.nodeName ) {
  48. case 'svg':
  49. break;
  50. case 'style':
  51. parseCSSStylesheet( node );
  52. break;
  53. case 'g':
  54. style = parseStyle( node, style );
  55. break;
  56. case 'path':
  57. style = parseStyle( node, style );
  58. if ( node.hasAttribute( 'd' ) ) path = parsePathNode( node );
  59. break;
  60. case 'rect':
  61. style = parseStyle( node, style );
  62. path = parseRectNode( node );
  63. break;
  64. case 'polygon':
  65. style = parseStyle( node, style );
  66. path = parsePolygonNode( node );
  67. break;
  68. case 'polyline':
  69. style = parseStyle( node, style );
  70. path = parsePolylineNode( node );
  71. break;
  72. case 'circle':
  73. style = parseStyle( node, style );
  74. path = parseCircleNode( node );
  75. break;
  76. case 'ellipse':
  77. style = parseStyle( node, style );
  78. path = parseEllipseNode( node );
  79. break;
  80. case 'line':
  81. style = parseStyle( node, style );
  82. path = parseLineNode( node );
  83. break;
  84. case 'defs':
  85. traverseChildNodes = false;
  86. break;
  87. case 'use':
  88. style = parseStyle( node, style );
  89. var usedNodeId = node.href.baseVal.substring( 1 );
  90. var usedNode = node.viewportElement.getElementById( usedNodeId );
  91. if ( usedNode ) {
  92. parseNode( usedNode, style );
  93. } else {
  94. console.warn( 'SVGLoader: \'use node\' references non-existent node id: ' + usedNodeId );
  95. }
  96. break;
  97. default:
  98. // console.log( node );
  99. }
  100. if ( path ) {
  101. if ( style.fill !== undefined && style.fill !== 'none' ) {
  102. path.color.setStyle( style.fill );
  103. }
  104. transformPath( path, currentTransform );
  105. paths.push( path );
  106. path.userData = { node: node, style: style };
  107. }
  108. if ( traverseChildNodes ) {
  109. var nodes = node.childNodes;
  110. for ( var i = 0; i < nodes.length; i ++ ) {
  111. parseNode( nodes[ i ], style );
  112. }
  113. }
  114. if ( transform ) {
  115. transformStack.pop();
  116. if ( transformStack.length > 0 ) {
  117. currentTransform.copy( transformStack[ transformStack.length - 1 ] );
  118. } else {
  119. currentTransform.identity();
  120. }
  121. }
  122. }
  123. function parsePathNode( node ) {
  124. var path = new ShapePath();
  125. var point = new Vector2();
  126. var control = new Vector2();
  127. var firstPoint = new Vector2();
  128. var isFirstPoint = true;
  129. var doSetFirstPoint = false;
  130. var d = node.getAttribute( 'd' );
  131. // console.log( d );
  132. var commands = d.match( /[a-df-z][^a-df-z]*/ig );
  133. for ( var i = 0, l = commands.length; i < l; i ++ ) {
  134. var command = commands[ i ];
  135. var type = command.charAt( 0 );
  136. var data = command.substr( 1 ).trim();
  137. if ( isFirstPoint === true ) {
  138. doSetFirstPoint = true;
  139. isFirstPoint = false;
  140. }
  141. switch ( type ) {
  142. case 'M':
  143. var numbers = parseFloats( data );
  144. for ( var j = 0, jl = numbers.length; j < jl; j += 2 ) {
  145. point.x = numbers[ j + 0 ];
  146. point.y = numbers[ j + 1 ];
  147. control.x = point.x;
  148. control.y = point.y;
  149. if ( j === 0 ) {
  150. path.moveTo( point.x, point.y );
  151. } else {
  152. path.lineTo( point.x, point.y );
  153. }
  154. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  155. }
  156. break;
  157. case 'H':
  158. var numbers = parseFloats( data );
  159. for ( var j = 0, jl = numbers.length; j < jl; j ++ ) {
  160. point.x = numbers[ j ];
  161. control.x = point.x;
  162. control.y = point.y;
  163. path.lineTo( point.x, point.y );
  164. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  165. }
  166. break;
  167. case 'V':
  168. var numbers = parseFloats( data );
  169. for ( var j = 0, jl = numbers.length; j < jl; j ++ ) {
  170. point.y = 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 'L':
  178. var numbers = parseFloats( data );
  179. for ( var j = 0, jl = numbers.length; j < jl; j += 2 ) {
  180. point.x = numbers[ j + 0 ];
  181. point.y = numbers[ j + 1 ];
  182. control.x = point.x;
  183. control.y = point.y;
  184. path.lineTo( point.x, point.y );
  185. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  186. }
  187. break;
  188. case 'C':
  189. var numbers = parseFloats( data );
  190. for ( var j = 0, jl = numbers.length; j < jl; j += 6 ) {
  191. path.bezierCurveTo(
  192. numbers[ j + 0 ],
  193. numbers[ j + 1 ],
  194. numbers[ j + 2 ],
  195. numbers[ j + 3 ],
  196. numbers[ j + 4 ],
  197. numbers[ j + 5 ]
  198. );
  199. control.x = numbers[ j + 2 ];
  200. control.y = numbers[ j + 3 ];
  201. point.x = numbers[ j + 4 ];
  202. point.y = numbers[ j + 5 ];
  203. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  204. }
  205. break;
  206. case 'S':
  207. var numbers = parseFloats( data );
  208. for ( var j = 0, jl = numbers.length; j < jl; j += 4 ) {
  209. path.bezierCurveTo(
  210. getReflection( point.x, control.x ),
  211. getReflection( point.y, control.y ),
  212. numbers[ j + 0 ],
  213. numbers[ j + 1 ],
  214. numbers[ j + 2 ],
  215. numbers[ j + 3 ]
  216. );
  217. control.x = numbers[ j + 0 ];
  218. control.y = numbers[ j + 1 ];
  219. point.x = numbers[ j + 2 ];
  220. point.y = numbers[ j + 3 ];
  221. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  222. }
  223. break;
  224. case 'Q':
  225. var numbers = parseFloats( data );
  226. for ( var j = 0, jl = numbers.length; j < jl; j += 4 ) {
  227. path.quadraticCurveTo(
  228. numbers[ j + 0 ],
  229. numbers[ j + 1 ],
  230. numbers[ j + 2 ],
  231. numbers[ j + 3 ]
  232. );
  233. control.x = numbers[ j + 0 ];
  234. control.y = numbers[ j + 1 ];
  235. point.x = numbers[ j + 2 ];
  236. point.y = numbers[ j + 3 ];
  237. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  238. }
  239. break;
  240. case 'T':
  241. var numbers = parseFloats( data );
  242. for ( var j = 0, jl = numbers.length; j < jl; j += 2 ) {
  243. var rx = getReflection( point.x, control.x );
  244. var ry = getReflection( point.y, control.y );
  245. path.quadraticCurveTo(
  246. rx,
  247. ry,
  248. numbers[ j + 0 ],
  249. numbers[ j + 1 ]
  250. );
  251. control.x = rx;
  252. control.y = ry;
  253. point.x = numbers[ j + 0 ];
  254. point.y = numbers[ j + 1 ];
  255. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  256. }
  257. break;
  258. case 'A':
  259. var numbers = parseFloats( data );
  260. for ( var j = 0, jl = numbers.length; j < jl; j += 7 ) {
  261. // skip command if start point == end point
  262. if ( numbers[ j + 5 ] == point.x && numbers[ j + 6 ] == point.y ) continue;
  263. var start = point.clone();
  264. point.x = numbers[ j + 5 ];
  265. point.y = numbers[ j + 6 ];
  266. control.x = point.x;
  267. control.y = point.y;
  268. parseArcCommand(
  269. path, numbers[ j ], numbers[ j + 1 ], numbers[ j + 2 ], numbers[ j + 3 ], numbers[ j + 4 ], start, point
  270. );
  271. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  272. }
  273. break;
  274. case 'm':
  275. var numbers = parseFloats( data );
  276. for ( var j = 0, jl = numbers.length; j < jl; j += 2 ) {
  277. point.x += numbers[ j + 0 ];
  278. point.y += numbers[ j + 1 ];
  279. control.x = point.x;
  280. control.y = point.y;
  281. if ( j === 0 ) {
  282. path.moveTo( point.x, point.y );
  283. } else {
  284. path.lineTo( point.x, point.y );
  285. }
  286. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  287. }
  288. break;
  289. case 'h':
  290. var numbers = parseFloats( data );
  291. for ( var j = 0, jl = numbers.length; j < jl; j ++ ) {
  292. point.x += numbers[ j ];
  293. control.x = point.x;
  294. control.y = point.y;
  295. path.lineTo( point.x, point.y );
  296. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  297. }
  298. break;
  299. case 'v':
  300. var numbers = parseFloats( data );
  301. for ( var j = 0, jl = numbers.length; j < jl; j ++ ) {
  302. point.y += 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 'l':
  310. var numbers = parseFloats( data );
  311. for ( var j = 0, jl = numbers.length; j < jl; j += 2 ) {
  312. point.x += numbers[ j + 0 ];
  313. point.y += numbers[ j + 1 ];
  314. control.x = point.x;
  315. control.y = point.y;
  316. path.lineTo( point.x, point.y );
  317. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  318. }
  319. break;
  320. case 'c':
  321. var numbers = parseFloats( data );
  322. for ( var j = 0, jl = numbers.length; j < jl; j += 6 ) {
  323. path.bezierCurveTo(
  324. point.x + numbers[ j + 0 ],
  325. point.y + numbers[ j + 1 ],
  326. point.x + numbers[ j + 2 ],
  327. point.y + numbers[ j + 3 ],
  328. point.x + numbers[ j + 4 ],
  329. point.y + numbers[ j + 5 ]
  330. );
  331. control.x = point.x + numbers[ j + 2 ];
  332. control.y = point.y + numbers[ j + 3 ];
  333. point.x += numbers[ j + 4 ];
  334. point.y += numbers[ j + 5 ];
  335. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  336. }
  337. break;
  338. case 's':
  339. var numbers = parseFloats( data );
  340. for ( var j = 0, jl = numbers.length; j < jl; j += 4 ) {
  341. path.bezierCurveTo(
  342. getReflection( point.x, control.x ),
  343. getReflection( point.y, control.y ),
  344. point.x + numbers[ j + 0 ],
  345. point.y + numbers[ j + 1 ],
  346. point.x + numbers[ j + 2 ],
  347. point.y + numbers[ j + 3 ]
  348. );
  349. control.x = point.x + numbers[ j + 0 ];
  350. control.y = point.y + numbers[ j + 1 ];
  351. point.x += numbers[ j + 2 ];
  352. point.y += numbers[ j + 3 ];
  353. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  354. }
  355. break;
  356. case 'q':
  357. var numbers = parseFloats( data );
  358. for ( var j = 0, jl = numbers.length; j < jl; j += 4 ) {
  359. path.quadraticCurveTo(
  360. point.x + numbers[ j + 0 ],
  361. point.y + numbers[ j + 1 ],
  362. point.x + numbers[ j + 2 ],
  363. point.y + numbers[ j + 3 ]
  364. );
  365. control.x = point.x + numbers[ j + 0 ];
  366. control.y = point.y + numbers[ j + 1 ];
  367. point.x += numbers[ j + 2 ];
  368. point.y += numbers[ j + 3 ];
  369. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  370. }
  371. break;
  372. case 't':
  373. var numbers = parseFloats( data );
  374. for ( var j = 0, jl = numbers.length; j < jl; j += 2 ) {
  375. var rx = getReflection( point.x, control.x );
  376. var ry = getReflection( point.y, control.y );
  377. path.quadraticCurveTo(
  378. rx,
  379. ry,
  380. point.x + numbers[ j + 0 ],
  381. point.y + numbers[ j + 1 ]
  382. );
  383. control.x = rx;
  384. control.y = ry;
  385. point.x = point.x + numbers[ j + 0 ];
  386. point.y = point.y + numbers[ j + 1 ];
  387. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  388. }
  389. break;
  390. case 'a':
  391. var numbers = parseFloats( data );
  392. for ( var j = 0, jl = numbers.length; j < jl; j += 7 ) {
  393. // skip command if no displacement
  394. if ( numbers[ j + 5 ] == 0 && numbers[ j + 6 ] == 0 ) continue;
  395. var start = point.clone();
  396. point.x += numbers[ j + 5 ];
  397. point.y += numbers[ j + 6 ];
  398. control.x = point.x;
  399. control.y = point.y;
  400. parseArcCommand(
  401. path, numbers[ j ], numbers[ j + 1 ], numbers[ j + 2 ], numbers[ j + 3 ], numbers[ j + 4 ], start, point
  402. );
  403. if ( j === 0 && doSetFirstPoint === true ) firstPoint.copy( point );
  404. }
  405. break;
  406. case 'Z':
  407. case 'z':
  408. path.currentPath.autoClose = true;
  409. if ( path.currentPath.curves.length > 0 ) {
  410. // Reset point to beginning of Path
  411. point.copy( firstPoint );
  412. path.currentPath.currentPoint.copy( point );
  413. isFirstPoint = true;
  414. }
  415. break;
  416. default:
  417. console.warn( command );
  418. }
  419. // console.log( type, parseFloats( data ), parseFloats( data ).length )
  420. doSetFirstPoint = false;
  421. }
  422. return path;
  423. }
  424. function parseCSSStylesheet( node ) {
  425. if ( ! node.sheet || ! node.sheet.cssRules || ! node.sheet.cssRules.length ) return;
  426. for ( var i = 0; i < node.sheet.cssRules.length; i ++ ) {
  427. var stylesheet = node.sheet.cssRules[ i ];
  428. if ( stylesheet.type !== 1 ) continue;
  429. var selectorList = stylesheet.selectorText
  430. .split( /,/gm )
  431. .filter( Boolean )
  432. .map( i => i.trim() );
  433. for ( var j = 0; j < selectorList.length; j ++ ) {
  434. stylesheets[ selectorList[ j ] ] = Object.assign(
  435. stylesheets[ selectorList[ j ] ] || {},
  436. stylesheet.style
  437. );
  438. }
  439. }
  440. }
  441. /**
  442. * https://www.w3.org/TR/SVG/implnote.html#ArcImplementationNotes
  443. * https://mortoray.com/2017/02/16/rendering-an-svg-elliptical-arc-as-bezier-curves/ Appendix: Endpoint to center arc conversion
  444. * From
  445. * rx ry x-axis-rotation large-arc-flag sweep-flag x y
  446. * To
  447. * aX, aY, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise, aRotation
  448. */
  449. function parseArcCommand( path, rx, ry, x_axis_rotation, large_arc_flag, sweep_flag, start, end ) {
  450. if ( rx == 0 || ry == 0 ) {
  451. // draw a line if either of the radii == 0
  452. path.lineTo( end.x, end.y );
  453. return;
  454. }
  455. x_axis_rotation = x_axis_rotation * Math.PI / 180;
  456. // Ensure radii are positive
  457. rx = Math.abs( rx );
  458. ry = Math.abs( ry );
  459. // Compute (x1', y1')
  460. var dx2 = ( start.x - end.x ) / 2.0;
  461. var dy2 = ( start.y - end.y ) / 2.0;
  462. var x1p = Math.cos( x_axis_rotation ) * dx2 + Math.sin( x_axis_rotation ) * dy2;
  463. var y1p = - Math.sin( x_axis_rotation ) * dx2 + Math.cos( x_axis_rotation ) * dy2;
  464. // Compute (cx', cy')
  465. var rxs = rx * rx;
  466. var rys = ry * ry;
  467. var x1ps = x1p * x1p;
  468. var y1ps = y1p * y1p;
  469. // Ensure radii are large enough
  470. var cr = x1ps / rxs + y1ps / rys;
  471. if ( cr > 1 ) {
  472. // scale up rx,ry equally so cr == 1
  473. var s = Math.sqrt( cr );
  474. rx = s * rx;
  475. ry = s * ry;
  476. rxs = rx * rx;
  477. rys = ry * ry;
  478. }
  479. var dq = ( rxs * y1ps + rys * x1ps );
  480. var pq = ( rxs * rys - dq ) / dq;
  481. var q = Math.sqrt( Math.max( 0, pq ) );
  482. if ( large_arc_flag === sweep_flag ) q = - q;
  483. var cxp = q * rx * y1p / ry;
  484. var cyp = - q * ry * x1p / rx;
  485. // Step 3: Compute (cx, cy) from (cx', cy')
  486. var cx = Math.cos( x_axis_rotation ) * cxp - Math.sin( x_axis_rotation ) * cyp + ( start.x + end.x ) / 2;
  487. var cy = Math.sin( x_axis_rotation ) * cxp + Math.cos( x_axis_rotation ) * cyp + ( start.y + end.y ) / 2;
  488. // Step 4: Compute θ1 and Δθ
  489. var theta = svgAngle( 1, 0, ( x1p - cxp ) / rx, ( y1p - cyp ) / ry );
  490. var delta = svgAngle( ( x1p - cxp ) / rx, ( y1p - cyp ) / ry, ( - x1p - cxp ) / rx, ( - y1p - cyp ) / ry ) % ( Math.PI * 2 );
  491. path.currentPath.absellipse( cx, cy, rx, ry, theta, theta + delta, sweep_flag === 0, x_axis_rotation );
  492. }
  493. function svgAngle( ux, uy, vx, vy ) {
  494. var dot = ux * vx + uy * vy;
  495. var len = Math.sqrt( ux * ux + uy * uy ) * Math.sqrt( vx * vx + vy * vy );
  496. var ang = Math.acos( Math.max( - 1, Math.min( 1, dot / len ) ) ); // floating point precision, slightly over values appear
  497. if ( ( ux * vy - uy * vx ) < 0 ) ang = - ang;
  498. return ang;
  499. }
  500. /*
  501. * According to https://www.w3.org/TR/SVG/shapes.html#RectElementRXAttribute
  502. * rounded corner should be rendered to elliptical arc, but bezier curve does the job well enough
  503. */
  504. function parseRectNode( node ) {
  505. var x = parseFloatWithUnits( node.getAttribute( 'x' ) || 0 );
  506. var y = parseFloatWithUnits( node.getAttribute( 'y' ) || 0 );
  507. var rx = parseFloatWithUnits( node.getAttribute( 'rx' ) || 0 );
  508. var ry = parseFloatWithUnits( node.getAttribute( 'ry' ) || 0 );
  509. var w = parseFloatWithUnits( node.getAttribute( 'width' ) );
  510. var h = parseFloatWithUnits( node.getAttribute( 'height' ) );
  511. var path = new ShapePath();
  512. path.moveTo( x + 2 * rx, y );
  513. path.lineTo( x + w - 2 * rx, y );
  514. if ( rx !== 0 || ry !== 0 ) path.bezierCurveTo( x + w, y, x + w, y, x + w, y + 2 * ry );
  515. path.lineTo( x + w, y + h - 2 * ry );
  516. if ( rx !== 0 || ry !== 0 ) path.bezierCurveTo( x + w, y + h, x + w, y + h, x + w - 2 * rx, y + h );
  517. path.lineTo( x + 2 * rx, y + h );
  518. if ( rx !== 0 || ry !== 0 ) {
  519. path.bezierCurveTo( x, y + h, x, y + h, x, y + h - 2 * ry );
  520. }
  521. path.lineTo( x, y + 2 * ry );
  522. if ( rx !== 0 || ry !== 0 ) {
  523. path.bezierCurveTo( x, y, x, y, x + 2 * rx, y );
  524. }
  525. return path;
  526. }
  527. function parsePolygonNode( node ) {
  528. function iterator( match, a, b ) {
  529. var x = parseFloatWithUnits( a );
  530. var y = parseFloatWithUnits( b );
  531. if ( index === 0 ) {
  532. path.moveTo( x, y );
  533. } else {
  534. path.lineTo( x, y );
  535. }
  536. index ++;
  537. }
  538. var regex = /(-?[\d\.?]+)[,|\s](-?[\d\.?]+)/g;
  539. var path = new ShapePath();
  540. var index = 0;
  541. node.getAttribute( 'points' ).replace( regex, iterator );
  542. path.currentPath.autoClose = true;
  543. return path;
  544. }
  545. function parsePolylineNode( node ) {
  546. function iterator( match, a, b ) {
  547. var x = parseFloatWithUnits( a );
  548. var y = parseFloatWithUnits( b );
  549. if ( index === 0 ) {
  550. path.moveTo( x, y );
  551. } else {
  552. path.lineTo( x, y );
  553. }
  554. index ++;
  555. }
  556. var regex = /(-?[\d\.?]+)[,|\s](-?[\d\.?]+)/g;
  557. var path = new ShapePath();
  558. var index = 0;
  559. node.getAttribute( 'points' ).replace( regex, iterator );
  560. path.currentPath.autoClose = false;
  561. return path;
  562. }
  563. function parseCircleNode( node ) {
  564. var x = parseFloatWithUnits( node.getAttribute( 'cx' ) );
  565. var y = parseFloatWithUnits( node.getAttribute( 'cy' ) );
  566. var r = parseFloatWithUnits( node.getAttribute( 'r' ) );
  567. var subpath = new Path();
  568. subpath.absarc( x, y, r, 0, Math.PI * 2 );
  569. var path = new ShapePath();
  570. path.subPaths.push( subpath );
  571. return path;
  572. }
  573. function parseEllipseNode( node ) {
  574. var x = parseFloatWithUnits( node.getAttribute( 'cx' ) );
  575. var y = parseFloatWithUnits( node.getAttribute( 'cy' ) );
  576. var rx = parseFloatWithUnits( node.getAttribute( 'rx' ) );
  577. var ry = parseFloatWithUnits( node.getAttribute( 'ry' ) );
  578. var subpath = new Path();
  579. subpath.absellipse( x, y, rx, ry, 0, Math.PI * 2 );
  580. var path = new ShapePath();
  581. path.subPaths.push( subpath );
  582. return path;
  583. }
  584. function parseLineNode( node ) {
  585. var x1 = parseFloatWithUnits( node.getAttribute( 'x1' ) );
  586. var y1 = parseFloatWithUnits( node.getAttribute( 'y1' ) );
  587. var x2 = parseFloatWithUnits( node.getAttribute( 'x2' ) );
  588. var y2 = parseFloatWithUnits( node.getAttribute( 'y2' ) );
  589. var path = new ShapePath();
  590. path.moveTo( x1, y1 );
  591. path.lineTo( x2, y2 );
  592. path.currentPath.autoClose = false;
  593. return path;
  594. }
  595. //
  596. function parseStyle( node, style ) {
  597. style = Object.assign( {}, style ); // clone style
  598. var stylesheetStyles = {};
  599. if ( node.hasAttribute( 'class' ) ) {
  600. var classSelectors = node.getAttribute( 'class' )
  601. .split( /\s/ )
  602. .filter( Boolean )
  603. .map( i => i.trim() );
  604. for ( var i = 0; i < classSelectors.length; i ++ ) {
  605. stylesheetStyles = Object.assign( stylesheetStyles, stylesheets[ '.' + classSelectors[ i ] ] );
  606. }
  607. }
  608. if ( node.hasAttribute( 'id' ) ) {
  609. stylesheetStyles = Object.assign( stylesheetStyles, stylesheets[ '#' + node.getAttribute( 'id' ) ] );
  610. }
  611. function addStyle( svgName, jsName, adjustFunction ) {
  612. if ( adjustFunction === undefined ) adjustFunction = function copy( v ) {
  613. if ( v.startsWith( 'url' ) ) console.warn( 'SVGLoader: url access in attributes is not implemented.' );
  614. return v;
  615. };
  616. if ( node.hasAttribute( svgName ) ) style[ jsName ] = adjustFunction( node.getAttribute( svgName ) );
  617. if ( stylesheetStyles[ svgName ] ) style[ jsName ] = adjustFunction( stylesheetStyles[ svgName ] );
  618. if ( node.style && node.style[ svgName ] !== '' ) style[ jsName ] = adjustFunction( node.style[ svgName ] );
  619. }
  620. function clamp( v ) {
  621. return Math.max( 0, Math.min( 1, parseFloatWithUnits( v ) ) );
  622. }
  623. function positive( v ) {
  624. return Math.max( 0, parseFloatWithUnits( v ) );
  625. }
  626. addStyle( 'fill', 'fill' );
  627. addStyle( 'fill-opacity', 'fillOpacity', clamp );
  628. addStyle( 'opacity', 'opacity', clamp );
  629. addStyle( 'stroke', 'stroke' );
  630. addStyle( 'stroke-opacity', 'strokeOpacity', clamp );
  631. addStyle( 'stroke-width', 'strokeWidth', positive );
  632. addStyle( 'stroke-linejoin', 'strokeLineJoin' );
  633. addStyle( 'stroke-linecap', 'strokeLineCap' );
  634. addStyle( 'stroke-miterlimit', 'strokeMiterLimit', positive );
  635. addStyle( 'visibility', 'visibility' );
  636. return style;
  637. }
  638. // http://www.w3.org/TR/SVG11/implnote.html#PathElementImplementationNotes
  639. function getReflection( a, b ) {
  640. return a - ( b - a );
  641. }
  642. // from https://github.com/ppvg/svg-numbers (MIT License)
  643. function parseFloats( input ) {
  644. if ( typeof input !== 'string' ) {
  645. throw new TypeError( 'Invalid input: ' + typeof input );
  646. }
  647. // Character groups
  648. var RE = {
  649. SEPARATOR: /[ \t\r\n\,.\-+]/,
  650. WHITESPACE: /[ \t\r\n]/,
  651. DIGIT: /[\d]/,
  652. SIGN: /[-+]/,
  653. POINT: /\./,
  654. COMMA: /,/,
  655. EXP: /e/i
  656. };
  657. // States
  658. var SEP = 0;
  659. var INT = 1;
  660. var FLOAT = 2;
  661. var EXP = 3;
  662. var state = SEP;
  663. var seenComma = true;
  664. var result = [], number = '', exponent = '';
  665. function throwSyntaxError( current, i, partial ) {
  666. var error = new SyntaxError( 'Unexpected character "' + current + '" at index ' + i + '.' );
  667. error.partial = partial;
  668. throw error;
  669. }
  670. function newNumber() {
  671. if ( number !== '' ) {
  672. if ( exponent === '' ) result.push( Number( number ) );
  673. else result.push( Number( number ) * Math.pow( 10, Number( exponent ) ) );
  674. }
  675. number = '';
  676. exponent = '';
  677. }
  678. var current, i = 0, length = input.length;
  679. for ( i = 0; i < length; i ++ ) {
  680. current = input[ i ];
  681. // parse until next number
  682. if ( state === SEP ) {
  683. // eat whitespace
  684. if ( RE.WHITESPACE.test( current ) ) {
  685. continue;
  686. }
  687. // start new number
  688. if ( RE.DIGIT.test( current ) || RE.SIGN.test( current ) ) {
  689. state = INT;
  690. number = current;
  691. continue;
  692. }
  693. if ( RE.POINT.test( current ) ) {
  694. state = FLOAT;
  695. number = current;
  696. continue;
  697. }
  698. // throw on double commas (e.g. "1, , 2")
  699. if ( RE.COMMA.test( current ) ) {
  700. if ( seenComma ) {
  701. throwSyntaxError( current, i, result );
  702. }
  703. seenComma = true;
  704. }
  705. }
  706. // parse integer part
  707. if ( state === INT ) {
  708. if ( RE.DIGIT.test( current ) ) {
  709. number += current;
  710. continue;
  711. }
  712. if ( RE.POINT.test( current ) ) {
  713. number += current;
  714. state = FLOAT;
  715. continue;
  716. }
  717. if ( RE.EXP.test( current ) ) {
  718. state = EXP;
  719. continue;
  720. }
  721. // throw on double signs ("-+1"), but not on sign as separator ("-1-2")
  722. if ( RE.SIGN.test( current )
  723. && number.length === 1
  724. && RE.SIGN.test( number[ 0 ] ) ) {
  725. throwSyntaxError( current, i, result );
  726. }
  727. }
  728. // parse decimal part
  729. if ( state === FLOAT ) {
  730. if ( RE.DIGIT.test( current ) ) {
  731. number += current;
  732. continue;
  733. }
  734. if ( RE.EXP.test( current ) ) {
  735. state = EXP;
  736. continue;
  737. }
  738. // throw on double decimal points (e.g. "1..2")
  739. if ( RE.POINT.test( current ) && number[ number.length - 1 ] === '.' ) {
  740. throwSyntaxError( current, i, result );
  741. }
  742. }
  743. // parse exponent part
  744. if ( state == EXP ) {
  745. if ( RE.DIGIT.test( current ) ) {
  746. exponent += current;
  747. continue;
  748. }
  749. if ( RE.SIGN.test( current ) ) {
  750. if ( exponent === '' ) {
  751. exponent += current;
  752. continue;
  753. }
  754. if ( exponent.length === 1 && RE.SIGN.test( exponent ) ) {
  755. throwSyntaxError( current, i, result );
  756. }
  757. }
  758. }
  759. // end of number
  760. if ( RE.WHITESPACE.test( current ) ) {
  761. newNumber();
  762. state = SEP;
  763. seenComma = false;
  764. } else if ( RE.COMMA.test( current ) ) {
  765. newNumber();
  766. state = SEP;
  767. seenComma = true;
  768. } else if ( RE.SIGN.test( current ) ) {
  769. newNumber();
  770. state = INT;
  771. number = current;
  772. } else if ( RE.POINT.test( current ) ) {
  773. newNumber();
  774. state = FLOAT;
  775. number = current;
  776. } else {
  777. throwSyntaxError( current, i, result );
  778. }
  779. }
  780. // add the last number found (if any)
  781. newNumber();
  782. return result;
  783. }
  784. // Units
  785. var units = [ 'mm', 'cm', 'in', 'pt', 'pc', 'px' ];
  786. // Conversion: [ fromUnit ][ toUnit ] (-1 means dpi dependent)
  787. var unitConversion = {
  788. 'mm': {
  789. 'mm': 1,
  790. 'cm': 0.1,
  791. 'in': 1 / 25.4,
  792. 'pt': 72 / 25.4,
  793. 'pc': 6 / 25.4,
  794. 'px': - 1
  795. },
  796. 'cm': {
  797. 'mm': 10,
  798. 'cm': 1,
  799. 'in': 1 / 2.54,
  800. 'pt': 72 / 2.54,
  801. 'pc': 6 / 2.54,
  802. 'px': - 1
  803. },
  804. 'in': {
  805. 'mm': 25.4,
  806. 'cm': 2.54,
  807. 'in': 1,
  808. 'pt': 72,
  809. 'pc': 6,
  810. 'px': - 1
  811. },
  812. 'pt': {
  813. 'mm': 25.4 / 72,
  814. 'cm': 2.54 / 72,
  815. 'in': 1 / 72,
  816. 'pt': 1,
  817. 'pc': 6 / 72,
  818. 'px': - 1
  819. },
  820. 'pc': {
  821. 'mm': 25.4 / 6,
  822. 'cm': 2.54 / 6,
  823. 'in': 1 / 6,
  824. 'pt': 72 / 6,
  825. 'pc': 1,
  826. 'px': - 1
  827. },
  828. 'px': {
  829. 'px': 1
  830. }
  831. };
  832. function parseFloatWithUnits( string ) {
  833. var theUnit = 'px';
  834. if ( typeof string === 'string' || string instanceof String ) {
  835. for ( var i = 0, n = units.length; i < n; i ++ ) {
  836. var u = units[ i ];
  837. if ( string.endsWith( u ) ) {
  838. theUnit = u;
  839. string = string.substring( 0, string.length - u.length );
  840. break;
  841. }
  842. }
  843. }
  844. var scale = undefined;
  845. if ( theUnit === 'px' && scope.defaultUnit !== 'px' ) {
  846. // Conversion scale from pixels to inches, then to default units
  847. scale = unitConversion[ 'in' ][ scope.defaultUnit ] / scope.defaultDPI;
  848. } else {
  849. scale = unitConversion[ theUnit ][ scope.defaultUnit ];
  850. if ( scale < 0 ) {
  851. // Conversion scale to pixels
  852. scale = unitConversion[ theUnit ][ 'in' ] * scope.defaultDPI;
  853. }
  854. }
  855. return scale * parseFloat( string );
  856. }
  857. // Transforms
  858. function getNodeTransform( node ) {
  859. if ( ! ( node.hasAttribute( 'transform' ) || ( node.nodeName === 'use' && ( node.hasAttribute( 'x' ) || node.hasAttribute( 'y' ) ) ) ) ) {
  860. return null;
  861. }
  862. var transform = parseNodeTransform( node );
  863. if ( transformStack.length > 0 ) {
  864. transform.premultiply( transformStack[ transformStack.length - 1 ] );
  865. }
  866. currentTransform.copy( transform );
  867. transformStack.push( transform );
  868. return transform;
  869. }
  870. function parseNodeTransform( node ) {
  871. var transform = new Matrix3();
  872. var currentTransform = tempTransform0;
  873. if ( node.nodeName === 'use' && ( node.hasAttribute( 'x' ) || node.hasAttribute( 'y' ) ) ) {
  874. var tx = parseFloatWithUnits( node.getAttribute( 'x' ) );
  875. var ty = parseFloatWithUnits( node.getAttribute( 'y' ) );
  876. transform.translate( tx, ty );
  877. }
  878. if ( node.hasAttribute( 'transform' ) ) {
  879. var transformsTexts = node.getAttribute( 'transform' ).split( ')' );
  880. for ( var tIndex = transformsTexts.length - 1; tIndex >= 0; tIndex -- ) {
  881. var transformText = transformsTexts[ tIndex ].trim();
  882. if ( transformText === '' ) continue;
  883. var openParPos = transformText.indexOf( '(' );
  884. var closeParPos = transformText.length;
  885. if ( openParPos > 0 && openParPos < closeParPos ) {
  886. var transformType = transformText.substr( 0, openParPos );
  887. var array = parseFloats( transformText.substr( openParPos + 1, closeParPos - openParPos - 1 ) );
  888. currentTransform.identity();
  889. switch ( transformType ) {
  890. case 'translate':
  891. if ( array.length >= 1 ) {
  892. var tx = array[ 0 ];
  893. var ty = tx;
  894. if ( array.length >= 2 ) {
  895. ty = array[ 1 ];
  896. }
  897. currentTransform.translate( tx, ty );
  898. }
  899. break;
  900. case 'rotate':
  901. if ( array.length >= 1 ) {
  902. var angle = 0;
  903. var cx = 0;
  904. var cy = 0;
  905. // Angle
  906. angle = - array[ 0 ] * Math.PI / 180;
  907. if ( array.length >= 3 ) {
  908. // Center x, y
  909. cx = array[ 1 ];
  910. cy = array[ 2 ];
  911. }
  912. // Rotate around center (cx, cy)
  913. tempTransform1.identity().translate( - cx, - cy );
  914. tempTransform2.identity().rotate( angle );
  915. tempTransform3.multiplyMatrices( tempTransform2, tempTransform1 );
  916. tempTransform1.identity().translate( cx, cy );
  917. currentTransform.multiplyMatrices( tempTransform1, tempTransform3 );
  918. }
  919. break;
  920. case 'scale':
  921. if ( array.length >= 1 ) {
  922. var scaleX = array[ 0 ];
  923. var scaleY = scaleX;
  924. if ( array.length >= 2 ) {
  925. scaleY = array[ 1 ];
  926. }
  927. currentTransform.scale( scaleX, scaleY );
  928. }
  929. break;
  930. case 'skewX':
  931. if ( array.length === 1 ) {
  932. currentTransform.set(
  933. 1, Math.tan( array[ 0 ] * Math.PI / 180 ), 0,
  934. 0, 1, 0,
  935. 0, 0, 1
  936. );
  937. }
  938. break;
  939. case 'skewY':
  940. if ( array.length === 1 ) {
  941. currentTransform.set(
  942. 1, 0, 0,
  943. Math.tan( array[ 0 ] * Math.PI / 180 ), 1, 0,
  944. 0, 0, 1
  945. );
  946. }
  947. break;
  948. case 'matrix':
  949. if ( array.length === 6 ) {
  950. currentTransform.set(
  951. array[ 0 ], array[ 2 ], array[ 4 ],
  952. array[ 1 ], array[ 3 ], array[ 5 ],
  953. 0, 0, 1
  954. );
  955. }
  956. break;
  957. }
  958. }
  959. transform.premultiply( currentTransform );
  960. }
  961. }
  962. return transform;
  963. }
  964. function transformPath( path, m ) {
  965. function transfVec2( v2 ) {
  966. tempV3.set( v2.x, v2.y, 1 ).applyMatrix3( m );
  967. v2.set( tempV3.x, tempV3.y );
  968. }
  969. var isRotated = isTransformRotated( m );
  970. var subPaths = path.subPaths;
  971. for ( var i = 0, n = subPaths.length; i < n; i ++ ) {
  972. var subPath = subPaths[ i ];
  973. var curves = subPath.curves;
  974. for ( var j = 0; j < curves.length; j ++ ) {
  975. var curve = curves[ j ];
  976. if ( curve.isLineCurve ) {
  977. transfVec2( curve.v1 );
  978. transfVec2( curve.v2 );
  979. } else if ( curve.isCubicBezierCurve ) {
  980. transfVec2( curve.v0 );
  981. transfVec2( curve.v1 );
  982. transfVec2( curve.v2 );
  983. transfVec2( curve.v3 );
  984. } else if ( curve.isQuadraticBezierCurve ) {
  985. transfVec2( curve.v0 );
  986. transfVec2( curve.v1 );
  987. transfVec2( curve.v2 );
  988. } else if ( curve.isEllipseCurve ) {
  989. if ( isRotated ) {
  990. console.warn( 'SVGLoader: Elliptic arc or ellipse rotation or skewing is not implemented.' );
  991. }
  992. tempV2.set( curve.aX, curve.aY );
  993. transfVec2( tempV2 );
  994. curve.aX = tempV2.x;
  995. curve.aY = tempV2.y;
  996. curve.xRadius *= getTransformScaleX( m );
  997. curve.yRadius *= getTransformScaleY( m );
  998. }
  999. }
  1000. }
  1001. }
  1002. function isTransformRotated( m ) {
  1003. return m.elements[ 1 ] !== 0 || m.elements[ 3 ] !== 0;
  1004. }
  1005. function getTransformScaleX( m ) {
  1006. var te = m.elements;
  1007. return Math.sqrt( te[ 0 ] * te[ 0 ] + te[ 1 ] * te[ 1 ] );
  1008. }
  1009. function getTransformScaleY( m ) {
  1010. var te = m.elements;
  1011. return Math.sqrt( te[ 3 ] * te[ 3 ] + te[ 4 ] * te[ 4 ] );
  1012. }
  1013. //
  1014. var paths = [];
  1015. var stylesheets = {};
  1016. var transformStack = [];
  1017. var tempTransform0 = new Matrix3();
  1018. var tempTransform1 = new Matrix3();
  1019. var tempTransform2 = new Matrix3();
  1020. var tempTransform3 = new Matrix3();
  1021. var tempV2 = new Vector2();
  1022. var tempV3 = new Vector3();
  1023. var currentTransform = new Matrix3();
  1024. var xml = new DOMParser().parseFromString( text, 'image/svg+xml' ); // application/xml
  1025. parseNode( xml.documentElement, {
  1026. fill: '#000',
  1027. fillOpacity: 1,
  1028. strokeOpacity: 1,
  1029. strokeWidth: 1,
  1030. strokeLineJoin: 'miter',
  1031. strokeLineCap: 'butt',
  1032. strokeMiterLimit: 4
  1033. } );
  1034. var data = { paths: paths, xml: xml.documentElement };
  1035. // console.log( paths );
  1036. return data;
  1037. }
  1038. } );
  1039. SVGLoader.getStrokeStyle = function ( width, color, lineJoin, lineCap, miterLimit ) {
  1040. // Param width: Stroke width
  1041. // Param color: As returned by THREE.Color.getStyle()
  1042. // Param lineJoin: One of "round", "bevel", "miter" or "miter-limit"
  1043. // Param lineCap: One of "round", "square" or "butt"
  1044. // Param miterLimit: Maximum join length, in multiples of the "width" parameter (join is truncated if it exceeds that distance)
  1045. // Returns style object
  1046. width = width !== undefined ? width : 1;
  1047. color = color !== undefined ? color : '#000';
  1048. lineJoin = lineJoin !== undefined ? lineJoin : 'miter';
  1049. lineCap = lineCap !== undefined ? lineCap : 'butt';
  1050. miterLimit = miterLimit !== undefined ? miterLimit : 4;
  1051. return {
  1052. strokeColor: color,
  1053. strokeWidth: width,
  1054. strokeLineJoin: lineJoin,
  1055. strokeLineCap: lineCap,
  1056. strokeMiterLimit: miterLimit
  1057. };
  1058. };
  1059. SVGLoader.pointsToStroke = function ( points, style, arcDivisions, minDistance ) {
  1060. // Generates a stroke with some witdh around the given path.
  1061. // The path can be open or closed (last point equals to first point)
  1062. // Param points: Array of Vector2D (the path). Minimum 2 points.
  1063. // Param style: Object with SVG properties as returned by SVGLoader.getStrokeStyle(), or SVGLoader.parse() in the path.userData.style object
  1064. // Params arcDivisions: Arc divisions for round joins and endcaps. (Optional)
  1065. // Param minDistance: Points closer to this distance will be merged. (Optional)
  1066. // Returns BufferGeometry with stroke triangles (In plane z = 0). UV coordinates are generated ('u' along path. 'v' across it, from left to right)
  1067. var vertices = [];
  1068. var normals = [];
  1069. var uvs = [];
  1070. if ( SVGLoader.pointsToStrokeWithBuffers( points, style, arcDivisions, minDistance, vertices, normals, uvs ) === 0 ) {
  1071. return null;
  1072. }
  1073. var geometry = new BufferGeometry();
  1074. geometry.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  1075. geometry.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
  1076. geometry.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
  1077. return geometry;
  1078. };
  1079. SVGLoader.pointsToStrokeWithBuffers = function () {
  1080. var tempV2_1 = new Vector2();
  1081. var tempV2_2 = new Vector2();
  1082. var tempV2_3 = new Vector2();
  1083. var tempV2_4 = new Vector2();
  1084. var tempV2_5 = new Vector2();
  1085. var tempV2_6 = new Vector2();
  1086. var tempV2_7 = new Vector2();
  1087. var lastPointL = new Vector2();
  1088. var lastPointR = new Vector2();
  1089. var point0L = new Vector2();
  1090. var point0R = new Vector2();
  1091. var currentPointL = new Vector2();
  1092. var currentPointR = new Vector2();
  1093. var nextPointL = new Vector2();
  1094. var nextPointR = new Vector2();
  1095. var innerPoint = new Vector2();
  1096. var outerPoint = new Vector2();
  1097. return function ( points, style, arcDivisions, minDistance, vertices, normals, uvs, vertexOffset ) {
  1098. // This function can be called to update existing arrays or buffers.
  1099. // Accepts same parameters as pointsToStroke, plus the buffers and optional offset.
  1100. // Param vertexOffset: Offset vertices to start writing in the buffers (3 elements/vertex for vertices and normals, and 2 elements/vertex for uvs)
  1101. // Returns number of written vertices / normals / uvs pairs
  1102. // if 'vertices' parameter is undefined no triangles will be generated, but the returned vertices count will still be valid (useful to preallocate the buffers)
  1103. // 'normals' and 'uvs' buffers are optional
  1104. arcDivisions = arcDivisions !== undefined ? arcDivisions : 12;
  1105. minDistance = minDistance !== undefined ? minDistance : 0.001;
  1106. vertexOffset = vertexOffset !== undefined ? vertexOffset : 0;
  1107. // First ensure there are no duplicated points
  1108. points = removeDuplicatedPoints( points );
  1109. var numPoints = points.length;
  1110. if ( numPoints < 2 ) return 0;
  1111. var isClosed = points[ 0 ].equals( points[ numPoints - 1 ] );
  1112. var currentPoint;
  1113. var previousPoint = points[ 0 ];
  1114. var nextPoint;
  1115. var strokeWidth2 = style.strokeWidth / 2;
  1116. var deltaU = 1 / ( numPoints - 1 );
  1117. var u0 = 0;
  1118. var innerSideModified;
  1119. var joinIsOnLeftSide;
  1120. var isMiter;
  1121. var initialJoinIsOnLeftSide = false;
  1122. var numVertices = 0;
  1123. var currentCoordinate = vertexOffset * 3;
  1124. var currentCoordinateUV = vertexOffset * 2;
  1125. // Get initial left and right stroke points
  1126. getNormal( points[ 0 ], points[ 1 ], tempV2_1 ).multiplyScalar( strokeWidth2 );
  1127. lastPointL.copy( points[ 0 ] ).sub( tempV2_1 );
  1128. lastPointR.copy( points[ 0 ] ).add( tempV2_1 );
  1129. point0L.copy( lastPointL );
  1130. point0R.copy( lastPointR );
  1131. for ( var iPoint = 1; iPoint < numPoints; iPoint ++ ) {
  1132. currentPoint = points[ iPoint ];
  1133. // Get next point
  1134. if ( iPoint === numPoints - 1 ) {
  1135. if ( isClosed ) {
  1136. // Skip duplicated initial point
  1137. nextPoint = points[ 1 ];
  1138. } else nextPoint = undefined;
  1139. } else {
  1140. nextPoint = points[ iPoint + 1 ];
  1141. }
  1142. // Normal of previous segment in tempV2_1
  1143. var normal1 = tempV2_1;
  1144. getNormal( previousPoint, currentPoint, normal1 );
  1145. tempV2_3.copy( normal1 ).multiplyScalar( strokeWidth2 );
  1146. currentPointL.copy( currentPoint ).sub( tempV2_3 );
  1147. currentPointR.copy( currentPoint ).add( tempV2_3 );
  1148. var u1 = u0 + deltaU;
  1149. innerSideModified = false;
  1150. if ( nextPoint !== undefined ) {
  1151. // Normal of next segment in tempV2_2
  1152. getNormal( currentPoint, nextPoint, tempV2_2 );
  1153. tempV2_3.copy( tempV2_2 ).multiplyScalar( strokeWidth2 );
  1154. nextPointL.copy( currentPoint ).sub( tempV2_3 );
  1155. nextPointR.copy( currentPoint ).add( tempV2_3 );
  1156. joinIsOnLeftSide = true;
  1157. tempV2_3.subVectors( nextPoint, previousPoint );
  1158. if ( normal1.dot( tempV2_3 ) < 0 ) {
  1159. joinIsOnLeftSide = false;
  1160. }
  1161. if ( iPoint === 1 ) initialJoinIsOnLeftSide = joinIsOnLeftSide;
  1162. tempV2_3.subVectors( nextPoint, currentPoint );
  1163. tempV2_3.normalize();
  1164. var dot = Math.abs( normal1.dot( tempV2_3 ) );
  1165. // If path is straight, don't create join
  1166. if ( dot !== 0 ) {
  1167. // Compute inner and outer segment intersections
  1168. var miterSide = strokeWidth2 / dot;
  1169. tempV2_3.multiplyScalar( - miterSide );
  1170. tempV2_4.subVectors( currentPoint, previousPoint );
  1171. tempV2_5.copy( tempV2_4 ).setLength( miterSide ).add( tempV2_3 );
  1172. innerPoint.copy( tempV2_5 ).negate();
  1173. var miterLength2 = tempV2_5.length();
  1174. var segmentLengthPrev = tempV2_4.length();
  1175. tempV2_4.divideScalar( segmentLengthPrev );
  1176. tempV2_6.subVectors( nextPoint, currentPoint );
  1177. var segmentLengthNext = tempV2_6.length();
  1178. tempV2_6.divideScalar( segmentLengthNext );
  1179. // Check that previous and next segments doesn't overlap with the innerPoint of intersection
  1180. if ( tempV2_4.dot( innerPoint ) < segmentLengthPrev && tempV2_6.dot( innerPoint ) < segmentLengthNext ) {
  1181. innerSideModified = true;
  1182. }
  1183. outerPoint.copy( tempV2_5 ).add( currentPoint );
  1184. innerPoint.add( currentPoint );
  1185. isMiter = false;
  1186. if ( innerSideModified ) {
  1187. if ( joinIsOnLeftSide ) {
  1188. nextPointR.copy( innerPoint );
  1189. currentPointR.copy( innerPoint );
  1190. } else {
  1191. nextPointL.copy( innerPoint );
  1192. currentPointL.copy( innerPoint );
  1193. }
  1194. } else {
  1195. // The segment triangles are generated here if there was overlapping
  1196. makeSegmentTriangles();
  1197. }
  1198. switch ( style.strokeLineJoin ) {
  1199. case 'bevel':
  1200. makeSegmentWithBevelJoin( joinIsOnLeftSide, innerSideModified, u1 );
  1201. break;
  1202. case 'round':
  1203. // Segment triangles
  1204. createSegmentTrianglesWithMiddleSection( joinIsOnLeftSide, innerSideModified );
  1205. // Join triangles
  1206. if ( joinIsOnLeftSide ) {
  1207. makeCircularSector( currentPoint, currentPointL, nextPointL, u1, 0 );
  1208. } else {
  1209. makeCircularSector( currentPoint, nextPointR, currentPointR, u1, 1 );
  1210. }
  1211. break;
  1212. case 'miter':
  1213. case 'miter-clip':
  1214. default:
  1215. var miterFraction = ( strokeWidth2 * style.strokeMiterLimit ) / miterLength2;
  1216. if ( miterFraction < 1 ) {
  1217. // The join miter length exceeds the miter limit
  1218. if ( style.strokeLineJoin !== 'miter-clip' ) {
  1219. makeSegmentWithBevelJoin( joinIsOnLeftSide, innerSideModified, u1 );
  1220. break;
  1221. } else {
  1222. // Segment triangles
  1223. createSegmentTrianglesWithMiddleSection( joinIsOnLeftSide, innerSideModified );
  1224. // Miter-clip join triangles
  1225. if ( joinIsOnLeftSide ) {
  1226. tempV2_6.subVectors( outerPoint, currentPointL ).multiplyScalar( miterFraction ).add( currentPointL );
  1227. tempV2_7.subVectors( outerPoint, nextPointL ).multiplyScalar( miterFraction ).add( nextPointL );
  1228. addVertex( currentPointL, u1, 0 );
  1229. addVertex( tempV2_6, u1, 0 );
  1230. addVertex( currentPoint, u1, 0.5 );
  1231. addVertex( currentPoint, u1, 0.5 );
  1232. addVertex( tempV2_6, u1, 0 );
  1233. addVertex( tempV2_7, u1, 0 );
  1234. addVertex( currentPoint, u1, 0.5 );
  1235. addVertex( tempV2_7, u1, 0 );
  1236. addVertex( nextPointL, u1, 0 );
  1237. } else {
  1238. tempV2_6.subVectors( outerPoint, currentPointR ).multiplyScalar( miterFraction ).add( currentPointR );
  1239. tempV2_7.subVectors( outerPoint, nextPointR ).multiplyScalar( miterFraction ).add( nextPointR );
  1240. addVertex( currentPointR, u1, 1 );
  1241. addVertex( tempV2_6, u1, 1 );
  1242. addVertex( currentPoint, u1, 0.5 );
  1243. addVertex( currentPoint, u1, 0.5 );
  1244. addVertex( tempV2_6, u1, 1 );
  1245. addVertex( tempV2_7, u1, 1 );
  1246. addVertex( currentPoint, u1, 0.5 );
  1247. addVertex( tempV2_7, u1, 1 );
  1248. addVertex( nextPointR, u1, 1 );
  1249. }
  1250. }
  1251. } else {
  1252. // Miter join segment triangles
  1253. if ( innerSideModified ) {
  1254. // Optimized segment + join triangles
  1255. if ( joinIsOnLeftSide ) {
  1256. addVertex( lastPointR, u0, 1 );
  1257. addVertex( lastPointL, u0, 0 );
  1258. addVertex( outerPoint, u1, 0 );
  1259. addVertex( lastPointR, u0, 1 );
  1260. addVertex( outerPoint, u1, 0 );
  1261. addVertex( innerPoint, u1, 1 );
  1262. } else {
  1263. addVertex( lastPointR, u0, 1 );
  1264. addVertex( lastPointL, u0, 0 );
  1265. addVertex( outerPoint, u1, 1 );
  1266. addVertex( lastPointL, u0, 0 );
  1267. addVertex( innerPoint, u1, 0 );
  1268. addVertex( outerPoint, u1, 1 );
  1269. }
  1270. if ( joinIsOnLeftSide ) {
  1271. nextPointL.copy( outerPoint );
  1272. } else {
  1273. nextPointR.copy( outerPoint );
  1274. }
  1275. } else {
  1276. // Add extra miter join triangles
  1277. if ( joinIsOnLeftSide ) {
  1278. addVertex( currentPointL, u1, 0 );
  1279. addVertex( outerPoint, u1, 0 );
  1280. addVertex( currentPoint, u1, 0.5 );
  1281. addVertex( currentPoint, u1, 0.5 );
  1282. addVertex( outerPoint, u1, 0 );
  1283. addVertex( nextPointL, u1, 0 );
  1284. } else {
  1285. addVertex( currentPointR, u1, 1 );
  1286. addVertex( outerPoint, u1, 1 );
  1287. addVertex( currentPoint, u1, 0.5 );
  1288. addVertex( currentPoint, u1, 0.5 );
  1289. addVertex( outerPoint, u1, 1 );
  1290. addVertex( nextPointR, u1, 1 );
  1291. }
  1292. }
  1293. isMiter = true;
  1294. }
  1295. break;
  1296. }
  1297. } else {
  1298. // The segment triangles are generated here when two consecutive points are collinear
  1299. makeSegmentTriangles();
  1300. }
  1301. } else {
  1302. // The segment triangles are generated here if it is the ending segment
  1303. makeSegmentTriangles();
  1304. }
  1305. if ( ! isClosed && iPoint === numPoints - 1 ) {
  1306. // Start line endcap
  1307. addCapGeometry( points[ 0 ], point0L, point0R, joinIsOnLeftSide, true, u0 );
  1308. }
  1309. // Increment loop variables
  1310. u0 = u1;
  1311. previousPoint = currentPoint;
  1312. lastPointL.copy( nextPointL );
  1313. lastPointR.copy( nextPointR );
  1314. }
  1315. if ( ! isClosed ) {
  1316. // Ending line endcap
  1317. addCapGeometry( currentPoint, currentPointL, currentPointR, joinIsOnLeftSide, false, u1 );
  1318. } else if ( innerSideModified && vertices ) {
  1319. // Modify path first segment vertices to adjust to the segments inner and outer intersections
  1320. var lastOuter = outerPoint;
  1321. var lastInner = innerPoint;
  1322. if ( initialJoinIsOnLeftSide !== joinIsOnLeftSide ) {
  1323. lastOuter = innerPoint;
  1324. lastInner = outerPoint;
  1325. }
  1326. if ( joinIsOnLeftSide ) {
  1327. if ( isMiter || initialJoinIsOnLeftSide ) {
  1328. lastInner.toArray( vertices, 0 * 3 );
  1329. lastInner.toArray( vertices, 3 * 3 );
  1330. if ( isMiter ) {
  1331. lastOuter.toArray( vertices, 1 * 3 );
  1332. }
  1333. }
  1334. } else {
  1335. if ( isMiter || ! initialJoinIsOnLeftSide ) {
  1336. lastInner.toArray( vertices, 1 * 3 );
  1337. lastInner.toArray( vertices, 3 * 3 );
  1338. if ( isMiter ) {
  1339. lastOuter.toArray( vertices, 0 * 3 );
  1340. }
  1341. }
  1342. }
  1343. }
  1344. return numVertices;
  1345. // -- End of algorithm
  1346. // -- Functions
  1347. function getNormal( p1, p2, result ) {
  1348. result.subVectors( p2, p1 );
  1349. return result.set( - result.y, result.x ).normalize();
  1350. }
  1351. function addVertex( position, u, v ) {
  1352. if ( vertices ) {
  1353. vertices[ currentCoordinate ] = position.x;
  1354. vertices[ currentCoordinate + 1 ] = position.y;
  1355. vertices[ currentCoordinate + 2 ] = 0;
  1356. if ( normals ) {
  1357. normals[ currentCoordinate ] = 0;
  1358. normals[ currentCoordinate + 1 ] = 0;
  1359. normals[ currentCoordinate + 2 ] = 1;
  1360. }
  1361. currentCoordinate += 3;
  1362. if ( uvs ) {
  1363. uvs[ currentCoordinateUV ] = u;
  1364. uvs[ currentCoordinateUV + 1 ] = v;
  1365. currentCoordinateUV += 2;
  1366. }
  1367. }
  1368. numVertices += 3;
  1369. }
  1370. function makeCircularSector( center, p1, p2, u, v ) {
  1371. // param p1, p2: Points in the circle arc.
  1372. // p1 and p2 are in clockwise direction.
  1373. tempV2_1.copy( p1 ).sub( center ).normalize();
  1374. tempV2_2.copy( p2 ).sub( center ).normalize();
  1375. var angle = Math.PI;
  1376. var dot = tempV2_1.dot( tempV2_2 );
  1377. if ( Math.abs( dot ) < 1 ) angle = Math.abs( Math.acos( dot ) );
  1378. angle /= arcDivisions;
  1379. tempV2_3.copy( p1 );
  1380. for ( var i = 0, il = arcDivisions - 1; i < il; i ++ ) {
  1381. tempV2_4.copy( tempV2_3 ).rotateAround( center, angle );
  1382. addVertex( tempV2_3, u, v );
  1383. addVertex( tempV2_4, u, v );
  1384. addVertex( center, u, 0.5 );
  1385. tempV2_3.copy( tempV2_4 );
  1386. }
  1387. addVertex( tempV2_4, u, v );
  1388. addVertex( p2, u, v );
  1389. addVertex( center, u, 0.5 );
  1390. }
  1391. function makeSegmentTriangles() {
  1392. addVertex( lastPointR, u0, 1 );
  1393. addVertex( lastPointL, u0, 0 );
  1394. addVertex( currentPointL, u1, 0 );
  1395. addVertex( lastPointR, u0, 1 );
  1396. addVertex( currentPointL, u1, 1 );
  1397. addVertex( currentPointR, u1, 0 );
  1398. }
  1399. function makeSegmentWithBevelJoin( joinIsOnLeftSide, innerSideModified, u ) {
  1400. if ( innerSideModified ) {
  1401. // Optimized segment + bevel triangles
  1402. if ( joinIsOnLeftSide ) {
  1403. // Path segments triangles
  1404. addVertex( lastPointR, u0, 1 );
  1405. addVertex( lastPointL, u0, 0 );
  1406. addVertex( currentPointL, u1, 0 );
  1407. addVertex( lastPointR, u0, 1 );
  1408. addVertex( currentPointL, u1, 0 );
  1409. addVertex( innerPoint, u1, 1 );
  1410. // Bevel join triangle
  1411. addVertex( currentPointL, u, 0 );
  1412. addVertex( nextPointL, u, 0 );
  1413. addVertex( innerPoint, u, 0.5 );
  1414. } else {
  1415. // Path segments triangles
  1416. addVertex( lastPointR, u0, 1 );
  1417. addVertex( lastPointL, u0, 0 );
  1418. addVertex( currentPointR, u1, 1 );
  1419. addVertex( lastPointL, u0, 0 );
  1420. addVertex( innerPoint, u1, 0 );
  1421. addVertex( currentPointR, u1, 1 );
  1422. // Bevel join triangle
  1423. addVertex( currentPointR, u, 1 );
  1424. addVertex( nextPointR, u, 0 );
  1425. addVertex( innerPoint, u, 0.5 );
  1426. }
  1427. } else {
  1428. // Bevel join triangle. The segment triangles are done in the main loop
  1429. if ( joinIsOnLeftSide ) {
  1430. addVertex( currentPointL, u, 0 );
  1431. addVertex( nextPointL, u, 0 );
  1432. addVertex( currentPoint, u, 0.5 );
  1433. } else {
  1434. addVertex( currentPointR, u, 1 );
  1435. addVertex( nextPointR, u, 0 );
  1436. addVertex( currentPoint, u, 0.5 );
  1437. }
  1438. }
  1439. }
  1440. function createSegmentTrianglesWithMiddleSection( joinIsOnLeftSide, innerSideModified ) {
  1441. if ( innerSideModified ) {
  1442. if ( joinIsOnLeftSide ) {
  1443. addVertex( lastPointR, u0, 1 );
  1444. addVertex( lastPointL, u0, 0 );
  1445. addVertex( currentPointL, u1, 0 );
  1446. addVertex( lastPointR, u0, 1 );
  1447. addVertex( currentPointL, u1, 0 );
  1448. addVertex( innerPoint, u1, 1 );
  1449. addVertex( currentPointL, u0, 0 );
  1450. addVertex( currentPoint, u1, 0.5 );
  1451. addVertex( innerPoint, u1, 1 );
  1452. addVertex( currentPoint, u1, 0.5 );
  1453. addVertex( nextPointL, u0, 0 );
  1454. addVertex( innerPoint, u1, 1 );
  1455. } else {
  1456. addVertex( lastPointR, u0, 1 );
  1457. addVertex( lastPointL, u0, 0 );
  1458. addVertex( currentPointR, u1, 1 );
  1459. addVertex( lastPointL, u0, 0 );
  1460. addVertex( innerPoint, u1, 0 );
  1461. addVertex( currentPointR, u1, 1 );
  1462. addVertex( currentPointR, u0, 1 );
  1463. addVertex( innerPoint, u1, 0 );
  1464. addVertex( currentPoint, u1, 0.5 );
  1465. addVertex( currentPoint, u1, 0.5 );
  1466. addVertex( innerPoint, u1, 0 );
  1467. addVertex( nextPointR, u0, 1 );
  1468. }
  1469. }
  1470. }
  1471. function addCapGeometry( center, p1, p2, joinIsOnLeftSide, start, u ) {
  1472. // param center: End point of the path
  1473. // param p1, p2: Left and right cap points
  1474. switch ( style.strokeLineCap ) {
  1475. case 'round':
  1476. if ( start ) {
  1477. makeCircularSector( center, p2, p1, u, 0.5 );
  1478. } else {
  1479. makeCircularSector( center, p1, p2, u, 0.5 );
  1480. }
  1481. break;
  1482. case 'square':
  1483. if ( start ) {
  1484. tempV2_1.subVectors( p1, center );
  1485. tempV2_2.set( tempV2_1.y, - tempV2_1.x );
  1486. tempV2_3.addVectors( tempV2_1, tempV2_2 ).add( center );
  1487. tempV2_4.subVectors( tempV2_2, tempV2_1 ).add( center );
  1488. // Modify already existing vertices
  1489. if ( joinIsOnLeftSide ) {
  1490. tempV2_3.toArray( vertices, 1 * 3 );
  1491. tempV2_4.toArray( vertices, 0 * 3 );
  1492. tempV2_4.toArray( vertices, 3 * 3 );
  1493. } else {
  1494. tempV2_3.toArray( vertices, 1 * 3 );
  1495. tempV2_3.toArray( vertices, 3 * 3 );
  1496. tempV2_4.toArray( vertices, 0 * 3 );
  1497. }
  1498. } else {
  1499. tempV2_1.subVectors( p2, center );
  1500. tempV2_2.set( tempV2_1.y, - tempV2_1.x );
  1501. tempV2_3.addVectors( tempV2_1, tempV2_2 ).add( center );
  1502. tempV2_4.subVectors( tempV2_2, tempV2_1 ).add( center );
  1503. var vl = vertices.length;
  1504. // Modify already existing vertices
  1505. if ( joinIsOnLeftSide ) {
  1506. tempV2_3.toArray( vertices, vl - 1 * 3 );
  1507. tempV2_4.toArray( vertices, vl - 2 * 3 );
  1508. tempV2_4.toArray( vertices, vl - 4 * 3 );
  1509. } else {
  1510. tempV2_3.toArray( vertices, vl - 2 * 3 );
  1511. tempV2_4.toArray( vertices, vl - 1 * 3 );
  1512. tempV2_4.toArray( vertices, vl - 4 * 3 );
  1513. }
  1514. }
  1515. break;
  1516. case 'butt':
  1517. default:
  1518. // Nothing to do here
  1519. break;
  1520. }
  1521. }
  1522. function removeDuplicatedPoints( points ) {
  1523. // Creates a new array if necessary with duplicated points removed.
  1524. // This does not remove duplicated initial and ending points of a closed path.
  1525. var dupPoints = false;
  1526. for ( var i = 1, n = points.length - 1; i < n; i ++ ) {
  1527. if ( points[ i ].distanceTo( points[ i + 1 ] ) < minDistance ) {
  1528. dupPoints = true;
  1529. break;
  1530. }
  1531. }
  1532. if ( ! dupPoints ) return points;
  1533. var newPoints = [];
  1534. newPoints.push( points[ 0 ] );
  1535. for ( var i = 1, n = points.length - 1; i < n; i ++ ) {
  1536. if ( points[ i ].distanceTo( points[ i + 1 ] ) >= minDistance ) {
  1537. newPoints.push( points[ i ] );
  1538. }
  1539. }
  1540. newPoints.push( points[ points.length - 1 ] );
  1541. return newPoints;
  1542. }
  1543. };
  1544. }();
  1545. export { SVGLoader };