SVGLoader.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author zz85 / http://joshuakoo.com/
  4. * @author yomboprime / https://yombo.org
  5. */
  6. THREE.SVGLoader = function ( manager ) {
  7. this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
  8. };
  9. THREE.SVGLoader.prototype = {
  10. constructor: THREE.SVGLoader,
  11. load: function ( url, onLoad, onProgress, onError ) {
  12. var scope = this;
  13. var loader = new THREE.FileLoader( scope.manager );
  14. loader.setPath( scope.path );
  15. loader.load( url, function ( text ) {
  16. onLoad( scope.parse( text ) );
  17. }, onProgress, onError );
  18. },
  19. setPath: function ( value ) {
  20. this.path = value;
  21. return this;
  22. },
  23. parse: function ( text ) {
  24. function parseNode( node, style ) {
  25. if ( node.nodeType !== 1 ) return;
  26. var transform = getNodeTransform( node );
  27. var path = null;
  28. switch ( node.nodeName ) {
  29. case 'svg':
  30. break;
  31. case 'g':
  32. style = parseStyle( node, style );
  33. break;
  34. case 'path':
  35. style = parseStyle( node, style );
  36. if ( node.hasAttribute( 'd' ) && isVisible( style ) ) path = parsePathNode( node, style );
  37. break;
  38. case 'rect':
  39. style = parseStyle( node, style );
  40. if ( isVisible( style ) ) path = parseRectNode( node, style );
  41. break;
  42. case 'polygon':
  43. style = parseStyle( node, style );
  44. if ( isVisible( style ) ) path = parsePolygonNode( node, style );
  45. break;
  46. case 'polyline':
  47. style = parseStyle( node, style );
  48. if ( isVisible( style ) ) path = parsePolylineNode( node, style );
  49. break;
  50. case 'circle':
  51. style = parseStyle( node, style );
  52. if ( isVisible( style ) ) path = parseCircleNode( node, style );
  53. break;
  54. case 'ellipse':
  55. style = parseStyle( node, style );
  56. if ( isVisible( style ) ) path = parseEllipseNode( node, style );
  57. break;
  58. case 'line':
  59. style = parseStyle( node, style );
  60. if ( isVisible( style ) ) path = parseLineNode( node, style );
  61. break;
  62. default:
  63. console.log( node );
  64. }
  65. if ( path ) {
  66. transformPath( path, currentTransform );
  67. paths.push( path );
  68. }
  69. var nodes = node.childNodes;
  70. for ( var i = 0; i < nodes.length; i ++ ) {
  71. parseNode( nodes[ i ], style );
  72. }
  73. if ( transform ) {
  74. currentTransform.copy( transformStack.pop() );
  75. }
  76. }
  77. function parsePathNode( node, style ) {
  78. var path = new THREE.ShapePath();
  79. path.color.setStyle( style.fill );
  80. var point = new THREE.Vector2();
  81. var control = new THREE.Vector2();
  82. var firstPoint = new THREE.Vector2();
  83. var isFirstPoint = true;
  84. var doSetFirstPoint = false;
  85. var d = node.getAttribute( 'd' );
  86. // console.log( d );
  87. var commands = d.match( /[a-df-z][^a-df-z]*/ig );
  88. for ( var i = 0, l = commands.length; i < l; i ++ ) {
  89. var command = commands[ i ];
  90. var type = command.charAt( 0 );
  91. var data = command.substr( 1 ).trim();
  92. if ( isFirstPoint ) {
  93. doSetFirstPoint = true;
  94. }
  95. isFirstPoint = false;
  96. switch ( type ) {
  97. case 'M':
  98. var numbers = parseFloats( data );
  99. for ( var j = 0, jl = numbers.length; j < jl; j += 2 ) {
  100. point.x = numbers[ j + 0 ];
  101. point.y = numbers[ j + 1 ];
  102. control.x = point.x;
  103. control.y = point.y;
  104. if ( j === 0 ) {
  105. path.moveTo( point.x, point.y );
  106. } else {
  107. path.lineTo( point.x, point.y );
  108. }
  109. }
  110. break;
  111. case 'H':
  112. var numbers = parseFloats( data );
  113. for ( var j = 0, jl = numbers.length; j < jl; j ++ ) {
  114. point.x = numbers[ j ];
  115. control.x = point.x;
  116. control.y = point.y;
  117. path.lineTo( point.x, point.y );
  118. }
  119. break;
  120. case 'V':
  121. var numbers = parseFloats( data );
  122. for ( var j = 0, jl = numbers.length; j < jl; j ++ ) {
  123. point.y = numbers[ j ];
  124. control.x = point.x;
  125. control.y = point.y;
  126. path.lineTo( point.x, point.y );
  127. }
  128. break;
  129. case 'L':
  130. var numbers = parseFloats( data );
  131. for ( var j = 0, jl = numbers.length; j < jl; j += 2 ) {
  132. point.x = numbers[ j + 0 ];
  133. point.y = numbers[ j + 1 ];
  134. control.x = point.x;
  135. control.y = point.y;
  136. path.lineTo( point.x, point.y );
  137. }
  138. break;
  139. case 'C':
  140. var numbers = parseFloats( data );
  141. for ( var j = 0, jl = numbers.length; j < jl; j += 6 ) {
  142. path.bezierCurveTo(
  143. numbers[ j + 0 ],
  144. numbers[ j + 1 ],
  145. numbers[ j + 2 ],
  146. numbers[ j + 3 ],
  147. numbers[ j + 4 ],
  148. numbers[ j + 5 ]
  149. );
  150. control.x = numbers[ j + 2 ];
  151. control.y = numbers[ j + 3 ];
  152. point.x = numbers[ j + 4 ];
  153. point.y = numbers[ j + 5 ];
  154. }
  155. break;
  156. case 'S':
  157. var numbers = parseFloats( data );
  158. for ( var j = 0, jl = numbers.length; j < jl; j += 4 ) {
  159. path.bezierCurveTo(
  160. getReflection( point.x, control.x ),
  161. getReflection( point.y, control.y ),
  162. numbers[ j + 0 ],
  163. numbers[ j + 1 ],
  164. numbers[ j + 2 ],
  165. numbers[ j + 3 ]
  166. );
  167. control.x = numbers[ j + 0 ];
  168. control.y = numbers[ j + 1 ];
  169. point.x = numbers[ j + 2 ];
  170. point.y = numbers[ j + 3 ];
  171. }
  172. break;
  173. case 'Q':
  174. var numbers = parseFloats( data );
  175. for ( var j = 0, jl = numbers.length; j < jl; j += 4 ) {
  176. path.quadraticCurveTo(
  177. numbers[ j + 0 ],
  178. numbers[ j + 1 ],
  179. numbers[ j + 2 ],
  180. numbers[ j + 3 ]
  181. );
  182. control.x = numbers[ j + 0 ];
  183. control.y = numbers[ j + 1 ];
  184. point.x = numbers[ j + 2 ];
  185. point.y = numbers[ j + 3 ];
  186. }
  187. break;
  188. case 'T':
  189. var numbers = parseFloats( data );
  190. for ( var j = 0, jl = numbers.length; j < jl; j += 2 ) {
  191. var rx = getReflection( point.x, control.x );
  192. var ry = getReflection( point.y, control.y );
  193. path.quadraticCurveTo(
  194. rx,
  195. ry,
  196. numbers[ j + 0 ],
  197. numbers[ j + 1 ]
  198. );
  199. control.x = rx;
  200. control.y = ry;
  201. point.x = numbers[ j + 0 ];
  202. point.y = numbers[ j + 1 ];
  203. }
  204. break;
  205. case 'A':
  206. var numbers = parseFloats( data );
  207. for ( var j = 0, jl = numbers.length; j < jl; j += 7 ) {
  208. var start = point.clone();
  209. point.x = numbers[ j + 5 ];
  210. point.y = numbers[ j + 6 ];
  211. control.x = point.x;
  212. control.y = point.y;
  213. parseArcCommand(
  214. path, numbers[ j ], numbers[ j + 1 ], numbers[ j + 2 ], numbers[ j + 3 ], numbers[ j + 4 ], start, point
  215. );
  216. }
  217. break;
  218. //
  219. case 'm':
  220. var numbers = parseFloats( data );
  221. for ( var j = 0, jl = numbers.length; j < jl; j += 2 ) {
  222. point.x += numbers[ j + 0 ];
  223. point.y += numbers[ j + 1 ];
  224. control.x = point.x;
  225. control.y = point.y;
  226. if ( j === 0 ) {
  227. path.moveTo( point.x, point.y );
  228. } else {
  229. path.lineTo( point.x, point.y );
  230. }
  231. }
  232. break;
  233. case 'h':
  234. var numbers = parseFloats( data );
  235. for ( var j = 0, jl = numbers.length; j < jl; j ++ ) {
  236. point.x += numbers[ j ];
  237. control.x = point.x;
  238. control.y = point.y;
  239. path.lineTo( point.x, point.y );
  240. }
  241. break;
  242. case 'v':
  243. var numbers = parseFloats( data );
  244. for ( var j = 0, jl = numbers.length; j < jl; j ++ ) {
  245. point.y += numbers[ j ];
  246. control.x = point.x;
  247. control.y = point.y;
  248. path.lineTo( point.x, point.y );
  249. }
  250. break;
  251. case 'l':
  252. var numbers = parseFloats( data );
  253. for ( var j = 0, jl = numbers.length; j < jl; j += 2 ) {
  254. point.x += numbers[ j + 0 ];
  255. point.y += numbers[ j + 1 ];
  256. control.x = point.x;
  257. control.y = point.y;
  258. path.lineTo( point.x, point.y );
  259. }
  260. break;
  261. case 'c':
  262. var numbers = parseFloats( data );
  263. for ( var j = 0, jl = numbers.length; j < jl; j += 6 ) {
  264. path.bezierCurveTo(
  265. point.x + numbers[ j + 0 ],
  266. point.y + numbers[ j + 1 ],
  267. point.x + numbers[ j + 2 ],
  268. point.y + numbers[ j + 3 ],
  269. point.x + numbers[ j + 4 ],
  270. point.y + numbers[ j + 5 ]
  271. );
  272. control.x = point.x + numbers[ j + 2 ];
  273. control.y = point.y + numbers[ j + 3 ];
  274. point.x += numbers[ j + 4 ];
  275. point.y += numbers[ j + 5 ];
  276. }
  277. break;
  278. case 's':
  279. var numbers = parseFloats( data );
  280. for ( var j = 0, jl = numbers.length; j < jl; j += 4 ) {
  281. path.bezierCurveTo(
  282. getReflection( point.x, control.x ),
  283. getReflection( point.y, control.y ),
  284. point.x + numbers[ j + 0 ],
  285. point.y + numbers[ j + 1 ],
  286. point.x + numbers[ j + 2 ],
  287. point.y + numbers[ j + 3 ]
  288. );
  289. control.x = point.x + numbers[ j + 0 ];
  290. control.y = point.y + numbers[ j + 1 ];
  291. point.x += numbers[ j + 2 ];
  292. point.y += numbers[ j + 3 ];
  293. }
  294. break;
  295. case 'q':
  296. var numbers = parseFloats( data );
  297. for ( var j = 0, jl = numbers.length; j < jl; j += 4 ) {
  298. path.quadraticCurveTo(
  299. point.x + numbers[ j + 0 ],
  300. point.y + numbers[ j + 1 ],
  301. point.x + numbers[ j + 2 ],
  302. point.y + numbers[ j + 3 ]
  303. );
  304. control.x = point.x + numbers[ j + 0 ];
  305. control.y = point.y + numbers[ j + 1 ];
  306. point.x += numbers[ j + 2 ];
  307. point.y += numbers[ j + 3 ];
  308. }
  309. break;
  310. case 't':
  311. var numbers = parseFloats( data );
  312. for ( var j = 0, jl = numbers.length; j < jl; j += 2 ) {
  313. var rx = getReflection( point.x, control.x );
  314. var ry = getReflection( point.y, control.y );
  315. path.quadraticCurveTo(
  316. rx,
  317. ry,
  318. point.x + numbers[ j + 0 ],
  319. point.y + numbers[ j + 1 ]
  320. );
  321. control.x = rx;
  322. control.y = ry;
  323. point.x = point.x + numbers[ j + 0 ];
  324. point.y = point.y + numbers[ j + 1 ];
  325. }
  326. break;
  327. case 'a':
  328. var numbers = parseFloats( data );
  329. for ( var j = 0, jl = numbers.length; j < jl; j += 7 ) {
  330. var start = point.clone();
  331. point.x += numbers[ j + 5 ];
  332. point.y += numbers[ j + 6 ];
  333. control.x = point.x;
  334. control.y = point.y;
  335. parseArcCommand(
  336. path, numbers[ j ], numbers[ j + 1 ], numbers[ j + 2 ], numbers[ j + 3 ], numbers[ j + 4 ], start, point
  337. );
  338. }
  339. break;
  340. //
  341. case 'Z':
  342. case 'z':
  343. path.currentPath.autoClose = true;
  344. if ( path.currentPath.curves.length > 0 ) {
  345. // Reset point to beginning of Path
  346. point.copy( firstPoint );
  347. path.currentPath.currentPoint.copy( point );
  348. isFirstPoint = true;
  349. }
  350. break;
  351. default:
  352. console.warn( command );
  353. }
  354. // console.log( type, parseFloats( data ), parseFloats( data ).length )
  355. if ( doSetFirstPoint ) {
  356. firstPoint.copy( point );
  357. doSetFirstPoint = false;
  358. }
  359. }
  360. return path;
  361. }
  362. /**
  363. * https://www.w3.org/TR/SVG/implnote.html#ArcImplementationNotes
  364. * https://mortoray.com/2017/02/16/rendering-an-svg-elliptical-arc-as-bezier-curves/ Appendix: Endpoint to center arc conversion
  365. * From
  366. * rx ry x-axis-rotation large-arc-flag sweep-flag x y
  367. * To
  368. * aX, aY, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise, aRotation
  369. */
  370. function parseArcCommand( path, rx, ry, x_axis_rotation, large_arc_flag, sweep_flag, start, end ) {
  371. x_axis_rotation = x_axis_rotation * Math.PI / 180;
  372. // Ensure radii are positive
  373. rx = Math.abs( rx );
  374. ry = Math.abs( ry );
  375. // Compute (x1′, y1′)
  376. var dx2 = ( start.x - end.x ) / 2.0;
  377. var dy2 = ( start.y - end.y ) / 2.0;
  378. var x1p = Math.cos( x_axis_rotation ) * dx2 + Math.sin( x_axis_rotation ) * dy2;
  379. var y1p = - Math.sin( x_axis_rotation ) * dx2 + Math.cos( x_axis_rotation ) * dy2;
  380. // Compute (cx′, cy′)
  381. var rxs = rx * rx;
  382. var rys = ry * ry;
  383. var x1ps = x1p * x1p;
  384. var y1ps = y1p * y1p;
  385. // Ensure radii are large enough
  386. var cr = x1ps / rxs + y1ps / rys;
  387. if ( cr > 1 ) {
  388. // scale up rx,ry equally so cr == 1
  389. var s = Math.sqrt( cr );
  390. rx = s * rx;
  391. ry = s * ry;
  392. rxs = rx * rx;
  393. rys = ry * ry;
  394. }
  395. var dq = ( rxs * y1ps + rys * x1ps );
  396. var pq = ( rxs * rys - dq ) / dq;
  397. var q = Math.sqrt( Math.max( 0, pq ) );
  398. if ( large_arc_flag === sweep_flag ) q = - q;
  399. var cxp = q * rx * y1p / ry;
  400. var cyp = - q * ry * x1p / rx;
  401. // Step 3: Compute (cx, cy) from (cx′, cy′)
  402. var cx = Math.cos( x_axis_rotation ) * cxp - Math.sin( x_axis_rotation ) * cyp + ( start.x + end.x ) / 2;
  403. var cy = Math.sin( x_axis_rotation ) * cxp + Math.cos( x_axis_rotation ) * cyp + ( start.y + end.y ) / 2;
  404. // Step 4: Compute θ1 and Δθ
  405. var theta = svgAngle( 1, 0, ( x1p - cxp ) / rx, ( y1p - cyp ) / ry );
  406. var delta = svgAngle( ( x1p - cxp ) / rx, ( y1p - cyp ) / ry, ( - x1p - cxp ) / rx, ( - y1p - cyp ) / ry ) % ( Math.PI * 2 );
  407. path.currentPath.absellipse( cx, cy, rx, ry, theta, theta + delta, sweep_flag === 0, x_axis_rotation );
  408. }
  409. function svgAngle( ux, uy, vx, vy ) {
  410. var dot = ux * vx + uy * vy;
  411. var len = Math.sqrt( ux * ux + uy * uy ) * Math.sqrt( vx * vx + vy * vy );
  412. var ang = Math.acos( Math.max( -1, Math.min( 1, dot / len ) ) ); // floating point precision, slightly over values appear
  413. if ( ( ux * vy - uy * vx ) < 0 ) ang = - ang;
  414. return ang;
  415. }
  416. /*
  417. * According to https://www.w3.org/TR/SVG/shapes.html#RectElementRXAttribute
  418. * rounded corner should be rendered to elliptical arc, but bezier curve does the job well enough
  419. */
  420. function parseRectNode( node, style ) {
  421. var x = parseFloat( node.getAttribute( 'x' ) || 0 );
  422. var y = parseFloat( node.getAttribute( 'y' ) || 0 );
  423. var rx = parseFloat( node.getAttribute( 'rx' ) || 0 );
  424. var ry = parseFloat( node.getAttribute( 'ry' ) || 0 );
  425. var w = parseFloat( node.getAttribute( 'width' ) );
  426. var h = parseFloat( node.getAttribute( 'height' ) );
  427. var path = new THREE.ShapePath();
  428. path.color.setStyle( style.fill );
  429. path.moveTo( x + 2 * rx, y );
  430. path.lineTo( x + w - 2 * rx, y );
  431. if ( rx !== 0 || ry !== 0 ) path.bezierCurveTo( x + w, y, x + w, y, x + w, y + 2 * ry );
  432. path.lineTo( x + w, y + h - 2 * ry );
  433. if ( rx !== 0 || ry !== 0 ) path.bezierCurveTo( x + w, y + h, x + w, y + h, x + w - 2 * rx, y + h );
  434. path.lineTo( x + 2 * rx, y + h );
  435. if ( rx !== 0 || ry !== 0 ) {
  436. path.bezierCurveTo( x, y + h, x, y + h, x, y + h - 2 * ry );
  437. }
  438. path.lineTo( x, y + 2 * ry );
  439. if ( rx !== 0 || ry !== 0 ) {
  440. path.bezierCurveTo( x, y, x, y, x + 2 * rx, y );
  441. }
  442. return path;
  443. }
  444. function parsePolygonNode( node, style ) {
  445. function iterator( match, a, b ) {
  446. var x = parseFloat( a );
  447. var y = parseFloat( b );
  448. if ( index === 0 ) {
  449. path.moveTo( x, y );
  450. } else {
  451. path.lineTo( x, y );
  452. }
  453. index ++;
  454. }
  455. var regex = /(-?[\d\.?]+)[,|\s](-?[\d\.?]+)/g;
  456. var path = new THREE.ShapePath();
  457. path.color.setStyle( style.fill );
  458. var index = 0;
  459. node.getAttribute( 'points' ).replace(regex, iterator);
  460. path.currentPath.autoClose = true;
  461. return path;
  462. }
  463. function parsePolylineNode( node, style ) {
  464. function iterator( match, a, b ) {
  465. var x = parseFloat( a );
  466. var y = parseFloat( b );
  467. if ( index === 0 ) {
  468. path.moveTo( x, y );
  469. } else {
  470. path.lineTo( x, y );
  471. }
  472. index ++;
  473. }
  474. var regex = /(-?[\d\.?]+)[,|\s](-?[\d\.?]+)/g;
  475. var path = new THREE.ShapePath();
  476. path.color.setStyle( style.fill );
  477. var index = 0;
  478. node.getAttribute( 'points' ).replace(regex, iterator);
  479. path.currentPath.autoClose = false;
  480. return path;
  481. }
  482. function parseCircleNode( node, style ) {
  483. var x = parseFloat( node.getAttribute( 'cx' ) );
  484. var y = parseFloat( node.getAttribute( 'cy' ) );
  485. var r = parseFloat( node.getAttribute( 'r' ) );
  486. var subpath = new THREE.Path();
  487. subpath.absarc( x, y, r, 0, Math.PI * 2 );
  488. var path = new THREE.ShapePath();
  489. path.color.setStyle( style.fill );
  490. path.subPaths.push( subpath );
  491. return path;
  492. }
  493. function parseEllipseNode( node, style ) {
  494. var x = parseFloat( node.getAttribute( 'cx' ) );
  495. var y = parseFloat( node.getAttribute( 'cy' ) );
  496. var rx = parseFloat( node.getAttribute( 'rx' ) );
  497. var ry = parseFloat( node.getAttribute( 'ry' ) );
  498. var subpath = new THREE.Path();
  499. subpath.absellipse( x, y, rx, ry, 0, Math.PI * 2 );
  500. var path = new THREE.ShapePath();
  501. path.color.setStyle( style.fill );
  502. path.subPaths.push( subpath );
  503. return path;
  504. }
  505. function parseLineNode( node, style ) {
  506. var x1 = parseFloat( node.getAttribute( 'x1' ) );
  507. var y1 = parseFloat( node.getAttribute( 'y1' ) );
  508. var x2 = parseFloat( node.getAttribute( 'x2' ) );
  509. var y2 = parseFloat( node.getAttribute( 'y2' ) );
  510. var path = new THREE.ShapePath();
  511. path.moveTo( x1, y1 );
  512. path.lineTo( x2, y2 );
  513. path.currentPath.autoClose = false;
  514. return path;
  515. }
  516. //
  517. function parseStyle( node, style ) {
  518. style = Object.assign( {}, style ); // clone style
  519. if ( node.hasAttribute( 'fill' ) ) style.fill = node.getAttribute( 'fill' );
  520. if ( node.style.fill !== '' ) style.fill = node.style.fill;
  521. return style;
  522. }
  523. function isVisible( style ) {
  524. return style.fill !== 'none' && style.fill !== 'transparent';
  525. }
  526. // http://www.w3.org/TR/SVG11/implnote.html#PathElementImplementationNotes
  527. function getReflection( a, b ) {
  528. return a - ( b - a );
  529. }
  530. function parseFloats( string ) {
  531. var array = string.split( /[\s,]+|(?=\s?[+\-])/ );
  532. for ( var i = 0; i < array.length; i ++ ) {
  533. var number = array[ i ];
  534. // Handle values like 48.6037.7.8
  535. // TODO Find a regex for this
  536. if ( number.indexOf( '.' ) !== number.lastIndexOf( '.' ) ) {
  537. var split = number.split( '.' );
  538. for ( var s = 2; s < split.length; s ++ ) {
  539. array.splice( i + s - 1, 0, '0.' + split[ s ] );
  540. }
  541. }
  542. array[ i ] = parseFloat( number );
  543. }
  544. return array;
  545. }
  546. function getNodeTransform( node ) {
  547. if ( ! node.hasAttribute( 'transform' ) ) {
  548. return null;
  549. }
  550. var transform = parseTransformNode( node );
  551. if ( transform ) {
  552. if ( transformStack.length > 0 ) {
  553. transform.premultiply( transformStack[ transformStack.length - 1 ] );
  554. }
  555. currentTransform.copy( transform );
  556. transformStack.push( transform );
  557. }
  558. return transform;
  559. }
  560. function parseTransformNode( node ) {
  561. var transform = null;
  562. var transformsTexts = node.getAttribute( 'transform' ).split( ' ' );
  563. for ( var tIndex = transformsTexts.length - 1; tIndex >= 0; tIndex-- ) {
  564. var newTransform = null;
  565. var transformText = transformsTexts[ tIndex ];
  566. var openParPos = transformText.indexOf( "(" );
  567. var closeParPos = transformText.indexOf( ")" );
  568. if ( openParPos > 0 && openParPos < closeParPos ) {
  569. var transformType = transformText.substr( 0, openParPos );
  570. var array = parseFloats( transformText.substr( openParPos + 1, closeParPos - openParPos - 1 ) );
  571. switch ( transformType ) {
  572. case "translate":
  573. if ( array.length >= 1 ) {
  574. newTransform = new THREE.Matrix3();
  575. var tx = array[ 0 ];
  576. var ty = tx;
  577. if ( array.length >= 2 ) {
  578. ty = array[ 1 ];
  579. }
  580. newTransform.translate( tx, ty );
  581. }
  582. break;
  583. case "rotate":
  584. if ( array.length >= 1 ) {
  585. var angle = 0;
  586. var cx = 0;
  587. var cy = 0;
  588. newTransform = new THREE.Matrix3();
  589. // Angle
  590. angle = - array[ 0 ] * Math.PI / 180;
  591. if ( array.length >= 3 ) {
  592. // Center x, y
  593. cx = array[ 1 ];
  594. cy = array[ 2 ];
  595. }
  596. // Rotate around center (cx, cy)
  597. tempTransform1.identity().translate( -cx, -cy );
  598. tempTransform2.identity().rotate( angle );
  599. tempTransform3.multiplyMatrices( tempTransform2, tempTransform1 );
  600. tempTransform1.identity().translate( cx, cy );
  601. newTransform.multiplyMatrices( tempTransform1, tempTransform3 );
  602. }
  603. break;
  604. case "scale":
  605. if ( array.length >= 1 ) {
  606. newTransform = new THREE.Matrix3();
  607. var scaleX = array[ 0 ];
  608. var scaleY = scaleX;
  609. if ( array.length >= 2 ) {
  610. scaleY = array[ 1 ];
  611. }
  612. newTransform.scale( scaleX, scaleY );
  613. }
  614. break;
  615. case "skewX":
  616. if ( array.length === 1 ) {
  617. newTransform = new THREE.Matrix3();
  618. newTransform.set(
  619. 1, Math.tan( array[ 0 ] * Math.PI / 180 ), 0,
  620. 0, 1, 0,
  621. 0, 0, 1
  622. );
  623. }
  624. break;
  625. case "skewY":
  626. if ( array.length === 1 ) {
  627. newTransform = new THREE.Matrix3();
  628. newTransform.set(
  629. 1, 0, 0,
  630. Math.tan( array[ 0 ] * Math.PI / 180 ), 1, 0,
  631. 0, 0, 1
  632. );
  633. }
  634. break;
  635. case "matrix":
  636. if ( array.length === 6 ) {
  637. newTransform = new THREE.Matrix3();
  638. newTransform.set(
  639. array[ 0 ], array[ 2 ], array[ 4 ],
  640. array[ 1 ], array[ 3 ], array[ 5 ],
  641. 0, 0, 1
  642. );
  643. }
  644. break;
  645. }
  646. }
  647. if ( newTransform ) {
  648. if ( transform ) {
  649. newTransform.multiply( transform );
  650. }
  651. transform = newTransform;
  652. }
  653. }
  654. return transform;
  655. }
  656. function transformPath( path, m ) {
  657. function transfVec2( v2 ) {
  658. tempV3.set( v2.x, v2.y, 1 ).applyMatrix3( m );
  659. v2.set( tempV3.x, tempV3.y );
  660. }
  661. var isRotated = isTransformRotated( m );
  662. var tempV2 = new THREE.Vector2();
  663. var tempV3 = new THREE.Vector3();
  664. var subPaths = path.subPaths;
  665. for ( var i = 0, n = subPaths.length; i < n; i++ ) {
  666. var subPath = subPaths[ i ];
  667. var curves = subPath.curves;
  668. for ( var j = 0; j < curves.length; j++ ) {
  669. var curve = curves[ j ];
  670. if ( curve.isLineCurve ) {
  671. transfVec2( curve.v1 );
  672. transfVec2( curve.v2 );
  673. } else if ( curve.isCubicBezierCurve ) {
  674. transfVec2( curve.v0 );
  675. transfVec2( curve.v1 );
  676. transfVec2( curve.v2 );
  677. transfVec2( curve.v3 );
  678. } else if ( curve.isQuadraticBezierCurve ) {
  679. transfVec2( curve.v0 );
  680. transfVec2( curve.v1 );
  681. transfVec2( curve.v2 );
  682. } else if ( curve.isEllipseCurve ) {
  683. if ( isRotated ) {
  684. console.warn( "SVGLoader: Elliptic arc or ellipse rotation or skewing is not implemented." );
  685. }
  686. tempV2.set( curve.aX, curve.aY );
  687. transfVec2( tempV2 );
  688. curve.aX = tempV2.x;
  689. curve.aY = tempV2.y;
  690. curve.xRadius *= getTransformScaleX( m );
  691. curve.yRadius *= getTransformScaleY( m );
  692. }
  693. }
  694. }
  695. }
  696. function isTransformRotated( m ) {
  697. return m.elements[ 1 ] !== 0 || m.elements[ 3 ] !== 0;
  698. }
  699. function getTransformScaleX( m ) {
  700. var te = m.elements;
  701. return Math.sqrt( te[ 0 ] * te[ 0 ] + te[ 1 ] * te[ 1 ] )
  702. }
  703. function getTransformScaleY( m ) {
  704. var te = m.elements;
  705. return Math.sqrt( te[ 3 ] * te[ 3 ] + te[ 4 ] * te[ 4 ] )
  706. }
  707. //
  708. console.log( 'THREE.SVGLoader' );
  709. var paths = [];
  710. var transformStack = [];
  711. var tempTransform1 = new THREE.Matrix3();
  712. var tempTransform2 = new THREE.Matrix3();
  713. var tempTransform3 = new THREE.Matrix3();
  714. var currentTransform = new THREE.Matrix3();
  715. console.time( 'THREE.SVGLoader: DOMParser' );
  716. var xml = new DOMParser().parseFromString( text, 'image/svg+xml' ); // application/xml
  717. console.timeEnd( 'THREE.SVGLoader: DOMParser' );
  718. console.time( 'THREE.SVGLoader: Parse' );
  719. parseNode( xml.documentElement, { fill: '#000' } );
  720. // console.log( paths );
  721. console.timeEnd( 'THREE.SVGLoader: Parse' );
  722. return paths;
  723. }
  724. };