SVGLoader.js 24 KB

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