SVGLoader.js 23 KB

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