SVGLoader.js 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  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. path.fillOpacity = style.fillOpacity;
  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.fillOpacity = style.fillOpacity;
  446. path.moveTo( x + 2 * rx, y );
  447. path.lineTo( x + w - 2 * rx, y );
  448. if ( rx !== 0 || ry !== 0 ) path.bezierCurveTo( x + w, y, x + w, y, x + w, y + 2 * ry );
  449. path.lineTo( x + w, y + h - 2 * ry );
  450. if ( rx !== 0 || ry !== 0 ) path.bezierCurveTo( x + w, y + h, x + w, y + h, x + w - 2 * rx, y + h );
  451. path.lineTo( x + 2 * rx, y + h );
  452. if ( rx !== 0 || ry !== 0 ) {
  453. path.bezierCurveTo( x, y + h, x, y + h, x, y + h - 2 * ry );
  454. }
  455. path.lineTo( x, y + 2 * ry );
  456. if ( rx !== 0 || ry !== 0 ) {
  457. path.bezierCurveTo( x, y, x, y, x + 2 * rx, y );
  458. }
  459. return path;
  460. }
  461. function parsePolygonNode( node, style ) {
  462. function iterator( match, a, b ) {
  463. var x = parseFloat( a );
  464. var y = parseFloat( b );
  465. if ( index === 0 ) {
  466. path.moveTo( x, y );
  467. } else {
  468. path.lineTo( x, y );
  469. }
  470. index ++;
  471. }
  472. var regex = /(-?[\d\.?]+)[,|\s](-?[\d\.?]+)/g;
  473. var path = new THREE.ShapePath();
  474. path.color.setStyle( style.fill );
  475. path.fillOpacity = style.fillOpacity;
  476. var index = 0;
  477. node.getAttribute( 'points' ).replace(regex, iterator);
  478. path.currentPath.autoClose = true;
  479. return path;
  480. }
  481. function parsePolylineNode( node, style ) {
  482. function iterator( match, a, b ) {
  483. var x = parseFloat( a );
  484. var y = parseFloat( b );
  485. if ( index === 0 ) {
  486. path.moveTo( x, y );
  487. } else {
  488. path.lineTo( x, y );
  489. }
  490. index ++;
  491. }
  492. var regex = /(-?[\d\.?]+)[,|\s](-?[\d\.?]+)/g;
  493. var path = new THREE.ShapePath();
  494. path.color.setStyle( style.fill );
  495. path.fillOpacity = style.fillOpacity;
  496. var index = 0;
  497. node.getAttribute( 'points' ).replace(regex, iterator);
  498. path.currentPath.autoClose = false;
  499. return path;
  500. }
  501. function parseCircleNode( node, style ) {
  502. var x = parseFloat( node.getAttribute( 'cx' ) );
  503. var y = parseFloat( node.getAttribute( 'cy' ) );
  504. var r = parseFloat( node.getAttribute( 'r' ) );
  505. var subpath = new THREE.Path();
  506. subpath.absarc( x, y, r, 0, Math.PI * 2 );
  507. var path = new THREE.ShapePath();
  508. path.color.setStyle( style.fill );
  509. path.fillOpacity = style.fillOpacity;
  510. path.subPaths.push( subpath );
  511. return path;
  512. }
  513. function parseEllipseNode( node, style ) {
  514. var x = parseFloat( node.getAttribute( 'cx' ) );
  515. var y = parseFloat( node.getAttribute( 'cy' ) );
  516. var rx = parseFloat( node.getAttribute( 'rx' ) );
  517. var ry = parseFloat( node.getAttribute( 'ry' ) );
  518. var subpath = new THREE.Path();
  519. subpath.absellipse( x, y, rx, ry, 0, Math.PI * 2 );
  520. var path = new THREE.ShapePath();
  521. path.color.setStyle( style.fill );
  522. path.fillOpacity = style.fillOpacity;
  523. path.subPaths.push( subpath );
  524. return path;
  525. }
  526. function parseLineNode( node, style ) {
  527. var x1 = parseFloat( node.getAttribute( 'x1' ) );
  528. var y1 = parseFloat( node.getAttribute( 'y1' ) );
  529. var x2 = parseFloat( node.getAttribute( 'x2' ) );
  530. var y2 = parseFloat( node.getAttribute( 'y2' ) );
  531. var path = new THREE.ShapePath();
  532. path.moveTo( x1, y1 );
  533. path.lineTo( x2, y2 );
  534. path.currentPath.autoClose = false;
  535. return path;
  536. }
  537. //
  538. function parseStyle( node, style ) {
  539. style = Object.assign( {}, style ); // clone style
  540. if ( node.hasAttribute( 'fill' ) ) style.fill = node.getAttribute( 'fill' );
  541. if ( node.style.fill !== '' ) style.fill = node.style.fill;
  542. if ( node.hasAttribute( 'fill-opacity' ) ) style.fillOpacity = node.getAttribute( 'fill-opacity' );
  543. if ( node.style.fillOpacity !== '' ) style.fillOpacity = node.style.fillOpacity;
  544. return style;
  545. }
  546. function isVisible( style ) {
  547. return style.fill !== 'none' && style.fill !== 'transparent';
  548. }
  549. // http://www.w3.org/TR/SVG11/implnote.html#PathElementImplementationNotes
  550. function getReflection( a, b ) {
  551. return a - ( b - a );
  552. }
  553. function parseFloats( string ) {
  554. var array = string.split( /[\s,]+|(?=\s?[+\-])/ );
  555. for ( var i = 0; i < array.length; i ++ ) {
  556. var number = array[ i ];
  557. // Handle values like 48.6037.7.8
  558. // TODO Find a regex for this
  559. if ( number.indexOf( '.' ) !== number.lastIndexOf( '.' ) ) {
  560. var split = number.split( '.' );
  561. for ( var s = 2; s < split.length; s ++ ) {
  562. array.splice( i + s - 1, 0, '0.' + split[ s ] );
  563. }
  564. }
  565. array[ i ] = parseFloat( number );
  566. }
  567. return array;
  568. }
  569. function getNodeTransform( node ) {
  570. if ( ! node.hasAttribute( 'transform' ) ) {
  571. return null;
  572. }
  573. var transform = parseTransformNode( node );
  574. if ( transform ) {
  575. if ( transformStack.length > 0 ) {
  576. transform.premultiply( transformStack[ transformStack.length - 1 ] );
  577. }
  578. currentTransform.copy( transform );
  579. transformStack.push( transform );
  580. }
  581. return transform;
  582. }
  583. function parseTransformNode( node ) {
  584. var transform = new THREE.Matrix3();
  585. var currentTransform = tempTransform0;
  586. var transformsTexts = node.getAttribute( 'transform' ).split( ' ' );
  587. for ( var tIndex = transformsTexts.length - 1; tIndex >= 0; tIndex-- ) {
  588. var transformText = transformsTexts[ tIndex ];
  589. var openParPos = transformText.indexOf( "(" );
  590. var closeParPos = transformText.indexOf( ")" );
  591. if ( openParPos > 0 && openParPos < closeParPos ) {
  592. var transformType = transformText.substr( 0, openParPos );
  593. var array = parseFloats( transformText.substr( openParPos + 1, closeParPos - openParPos - 1 ) );
  594. currentTransform.identity();
  595. switch ( transformType ) {
  596. case "translate":
  597. if ( array.length >= 1 ) {
  598. var tx = array[ 0 ];
  599. var ty = tx;
  600. if ( array.length >= 2 ) {
  601. ty = array[ 1 ];
  602. }
  603. currentTransform.translate( tx, ty );
  604. }
  605. break;
  606. case "rotate":
  607. if ( array.length >= 1 ) {
  608. var angle = 0;
  609. var cx = 0;
  610. var cy = 0;
  611. // Angle
  612. angle = - array[ 0 ] * Math.PI / 180;
  613. if ( array.length >= 3 ) {
  614. // Center x, y
  615. cx = array[ 1 ];
  616. cy = array[ 2 ];
  617. }
  618. // Rotate around center (cx, cy)
  619. tempTransform1.identity().translate( -cx, -cy );
  620. tempTransform2.identity().rotate( angle );
  621. tempTransform3.multiplyMatrices( tempTransform2, tempTransform1 );
  622. tempTransform1.identity().translate( cx, cy );
  623. currentTransform.multiplyMatrices( tempTransform1, tempTransform3 );
  624. }
  625. break;
  626. case "scale":
  627. if ( array.length >= 1 ) {
  628. var scaleX = array[ 0 ];
  629. var scaleY = scaleX;
  630. if ( array.length >= 2 ) {
  631. scaleY = array[ 1 ];
  632. }
  633. currentTransform.scale( scaleX, scaleY );
  634. }
  635. break;
  636. case "skewX":
  637. if ( array.length === 1 ) {
  638. currentTransform.set(
  639. 1, Math.tan( array[ 0 ] * Math.PI / 180 ), 0,
  640. 0, 1, 0,
  641. 0, 0, 1
  642. );
  643. }
  644. break;
  645. case "skewY":
  646. if ( array.length === 1 ) {
  647. currentTransform.set(
  648. 1, 0, 0,
  649. Math.tan( array[ 0 ] * Math.PI / 180 ), 1, 0,
  650. 0, 0, 1
  651. );
  652. }
  653. break;
  654. case "matrix":
  655. if ( array.length === 6 ) {
  656. currentTransform.set(
  657. array[ 0 ], array[ 2 ], array[ 4 ],
  658. array[ 1 ], array[ 3 ], array[ 5 ],
  659. 0, 0, 1
  660. );
  661. }
  662. break;
  663. }
  664. }
  665. transform.premultiply( currentTransform );
  666. }
  667. return transform;
  668. }
  669. function transformPath( path, m ) {
  670. function transfVec2( v2 ) {
  671. tempV3.set( v2.x, v2.y, 1 ).applyMatrix3( m );
  672. v2.set( tempV3.x, tempV3.y );
  673. }
  674. var isRotated = isTransformRotated( m );
  675. var tempV2 = new THREE.Vector2();
  676. var tempV3 = new THREE.Vector3();
  677. var subPaths = path.subPaths;
  678. for ( var i = 0, n = subPaths.length; i < n; i++ ) {
  679. var subPath = subPaths[ i ];
  680. var curves = subPath.curves;
  681. for ( var j = 0; j < curves.length; j++ ) {
  682. var curve = curves[ j ];
  683. if ( curve.isLineCurve ) {
  684. transfVec2( curve.v1 );
  685. transfVec2( curve.v2 );
  686. } else if ( curve.isCubicBezierCurve ) {
  687. transfVec2( curve.v0 );
  688. transfVec2( curve.v1 );
  689. transfVec2( curve.v2 );
  690. transfVec2( curve.v3 );
  691. } else if ( curve.isQuadraticBezierCurve ) {
  692. transfVec2( curve.v0 );
  693. transfVec2( curve.v1 );
  694. transfVec2( curve.v2 );
  695. } else if ( curve.isEllipseCurve ) {
  696. if ( isRotated ) {
  697. console.warn( "SVGLoader: Elliptic arc or ellipse rotation or skewing is not implemented." );
  698. }
  699. tempV2.set( curve.aX, curve.aY );
  700. transfVec2( tempV2 );
  701. curve.aX = tempV2.x;
  702. curve.aY = tempV2.y;
  703. curve.xRadius *= getTransformScaleX( m );
  704. curve.yRadius *= getTransformScaleY( m );
  705. }
  706. }
  707. }
  708. }
  709. function isTransformRotated( m ) {
  710. return m.elements[ 1 ] !== 0 || m.elements[ 3 ] !== 0;
  711. }
  712. function getTransformScaleX( m ) {
  713. var te = m.elements;
  714. return Math.sqrt( te[ 0 ] * te[ 0 ] + te[ 1 ] * te[ 1 ] )
  715. }
  716. function getTransformScaleY( m ) {
  717. var te = m.elements;
  718. return Math.sqrt( te[ 3 ] * te[ 3 ] + te[ 4 ] * te[ 4 ] )
  719. }
  720. //
  721. console.log( 'THREE.SVGLoader' );
  722. var paths = [];
  723. var transformStack = [];
  724. var tempTransform0 = new THREE.Matrix3();
  725. var tempTransform1 = new THREE.Matrix3();
  726. var tempTransform2 = new THREE.Matrix3();
  727. var tempTransform3 = new THREE.Matrix3();
  728. var currentTransform = new THREE.Matrix3();
  729. console.time( 'THREE.SVGLoader: DOMParser' );
  730. var xml = new DOMParser().parseFromString( text, 'image/svg+xml' ); // application/xml
  731. console.timeEnd( 'THREE.SVGLoader: DOMParser' );
  732. console.time( 'THREE.SVGLoader: Parse' );
  733. parseNode( xml.documentElement, { fill: '#000' } );
  734. // console.log( paths );
  735. console.timeEnd( 'THREE.SVGLoader: Parse' );
  736. return paths;
  737. }
  738. };