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