SVGLoader.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author zz85 / http://joshuakoo.com/
  4. */
  5. THREE.SVGLoader = function ( manager ) {
  6. this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
  7. };
  8. THREE.SVGLoader.prototype = {
  9. constructor: THREE.SVGLoader,
  10. load: function ( url, onLoad, onProgress, onError ) {
  11. var scope = this;
  12. var loader = new THREE.FileLoader( scope.manager );
  13. loader.load( url, function ( text ) {
  14. onLoad( scope.parse( text ) );
  15. }, onProgress, onError );
  16. },
  17. parse: function ( text ) {
  18. function parseNode( node, style ) {
  19. if ( node.nodeType !== 1 ) return;
  20. switch ( node.nodeName ) {
  21. case 'svg':
  22. break;
  23. case 'g':
  24. style = parseStyle( node, style );
  25. break;
  26. case 'path':
  27. style = parseStyle( node, style );
  28. if ( node.hasAttribute( 'd' ) && isVisible( style ) ) paths.push( parsePathNode( node, style ) );
  29. break;
  30. case 'rect':
  31. style = parseStyle( node, style );
  32. if ( isVisible( style ) ) paths.push( parseRectNode( node, style ) );
  33. break;
  34. case 'polygon':
  35. style = parseStyle( node, style );
  36. if ( isVisible( style ) ) paths.push( parsePolygonNode( node, style ) );
  37. break;
  38. case 'polyline':
  39. style = parseStyle( node, style );
  40. if ( isVisible( style ) ) paths.push( parsePolylineNode( node, style ) );
  41. break;
  42. case 'circle':
  43. style = parseStyle( node, style );
  44. if ( isVisible( style ) ) paths.push( parseCircleNode( node, style ) );
  45. break;
  46. case 'ellipse':
  47. style = parseStyle( node, style );
  48. if ( isVisible( style ) ) paths.push( parseEllipseNode( node, style ) );
  49. break;
  50. case 'line':
  51. style = parseStyle( node, style );
  52. if ( isVisible( style ) ) paths.push( parseLineNode( node, style ) );
  53. break;
  54. default:
  55. console.log( node );
  56. }
  57. var nodes = node.childNodes;
  58. for ( var i = 0; i < nodes.length; i ++ ) {
  59. parseNode( nodes[ i ], style );
  60. }
  61. }
  62. function parsePathNode( node, style ) {
  63. var path = new THREE.ShapePath();
  64. path.color.setStyle( style.fill );
  65. var point = new THREE.Vector2();
  66. var control = new THREE.Vector2();
  67. var d = node.getAttribute( 'd' );
  68. // console.log( d );
  69. var commands = d.match( /[a-df-z][^a-df-z]*/ig );
  70. for ( var i = 0, l = commands.length; i < l; i ++ ) {
  71. var command = commands[ i ];
  72. var type = command.charAt( 0 );
  73. var data = command.substr( 1 ).trim();
  74. switch ( type ) {
  75. case 'M':
  76. var numbers = parseFloats( data );
  77. for ( var j = 0, jl = numbers.length; j < jl; j += 2 ) {
  78. point.x = numbers[ j + 0 ];
  79. point.y = numbers[ j + 1 ];
  80. control.x = point.x;
  81. control.y = point.y;
  82. path.moveTo( point.x, point.y );
  83. }
  84. break;
  85. case 'H':
  86. var numbers = parseFloats( data );
  87. for ( var j = 0, jl = numbers.length; j < jl; j ++ ) {
  88. point.x = numbers[ j ];
  89. control.x = point.x;
  90. control.y = point.y;
  91. path.lineTo( point.x, point.y );
  92. }
  93. break;
  94. case 'V':
  95. var numbers = parseFloats( data );
  96. for ( var j = 0, jl = numbers.length; j < jl; j ++ ) {
  97. point.y = numbers[ j ];
  98. control.x = point.x;
  99. control.y = point.y;
  100. path.lineTo( point.x, point.y );
  101. }
  102. break;
  103. case 'L':
  104. var numbers = parseFloats( data );
  105. for ( var j = 0, jl = numbers.length; j < jl; j += 2 ) {
  106. point.x = numbers[ j + 0 ];
  107. point.y = numbers[ j + 1 ];
  108. control.x = point.x;
  109. control.y = point.y;
  110. path.lineTo( point.x, point.y );
  111. }
  112. break;
  113. case 'C':
  114. var numbers = parseFloats( data );
  115. for ( var j = 0, jl = numbers.length; j < jl; j += 6 ) {
  116. path.bezierCurveTo(
  117. numbers[ j + 0 ],
  118. numbers[ j + 1 ],
  119. numbers[ j + 2 ],
  120. numbers[ j + 3 ],
  121. numbers[ j + 4 ],
  122. numbers[ j + 5 ]
  123. );
  124. control.x = numbers[ j + 2 ];
  125. control.y = numbers[ j + 3 ];
  126. point.x = numbers[ j + 4 ];
  127. point.y = numbers[ j + 5 ];
  128. }
  129. break;
  130. case 'S':
  131. var numbers = parseFloats( data );
  132. for ( var j = 0, jl = numbers.length; j < jl; j += 4 ) {
  133. path.bezierCurveTo(
  134. getReflection( point.x, control.x ),
  135. getReflection( point.y, control.y ),
  136. numbers[ j + 0 ],
  137. numbers[ j + 1 ],
  138. numbers[ j + 2 ],
  139. numbers[ j + 3 ]
  140. );
  141. control.x = numbers[ j + 0 ];
  142. control.y = numbers[ j + 1 ];
  143. point.x = numbers[ j + 2 ];
  144. point.y = numbers[ j + 3 ];
  145. }
  146. break;
  147. case 'Q':
  148. var numbers = parseFloats( data );
  149. for ( var j = 0, jl = numbers.length; j < jl; j += 4 ) {
  150. path.quadraticCurveTo(
  151. numbers[ j + 0 ],
  152. numbers[ j + 1 ],
  153. numbers[ j + 2 ],
  154. numbers[ j + 3 ]
  155. );
  156. control.x = numbers[ j + 0 ];
  157. control.y = numbers[ j + 1 ];
  158. point.x = numbers[ j + 2 ];
  159. point.y = numbers[ j + 3 ];
  160. }
  161. break;
  162. case 'T':
  163. var numbers = parseFloats( data );
  164. for ( var j = 0, jl = numbers.length; j < jl; j += 2 ) {
  165. var rx = getReflection( point.x, control.x );
  166. var ry = getReflection( point.y, control.y );
  167. path.quadraticCurveTo(
  168. rx,
  169. ry,
  170. numbers[ j + 0 ],
  171. numbers[ j + 1 ]
  172. );
  173. control.x = rx;
  174. control.y = ry;
  175. point.x = numbers[ j + 0 ];
  176. point.y = numbers[ j + 1 ];
  177. }
  178. break;
  179. case 'A':
  180. var numbers = parseFloats( data );
  181. for ( var j = 0, jl = numbers.length; j < jl; j += 7 ) {
  182. var start = point.clone();
  183. point.x = numbers[ j + 5 ];
  184. point.y = numbers[ j + 6 ];
  185. control.x = point.x;
  186. control.y = point.y;
  187. parseArcCommand(
  188. path, numbers[ j ], numbers[ j + 1 ], numbers[ j + 2 ], numbers[ j + 3 ], numbers[ j + 4 ], start, point
  189. );
  190. }
  191. break;
  192. //
  193. case 'm':
  194. var numbers = parseFloats( data );
  195. for ( var j = 0, jl = numbers.length; j < jl; j += 2 ) {
  196. point.x += numbers[ j + 0 ];
  197. point.y += numbers[ j + 1 ];
  198. control.x = point.x;
  199. control.y = point.y;
  200. console.log( point.x, point.y );
  201. path.moveTo( point.x, point.y );
  202. }
  203. break;
  204. case 'h':
  205. var numbers = parseFloats( data );
  206. for ( var j = 0, jl = numbers.length; j < jl; j ++ ) {
  207. point.x += numbers[ j ];
  208. control.x = point.x;
  209. control.y = point.y;
  210. path.lineTo( point.x, point.y );
  211. }
  212. break;
  213. case 'v':
  214. var numbers = parseFloats( data );
  215. for ( var j = 0, jl = numbers.length; j < jl; j ++ ) {
  216. point.y += numbers[ j ];
  217. control.x = point.x;
  218. control.y = point.y;
  219. path.lineTo( point.x, point.y );
  220. }
  221. break;
  222. case 'l':
  223. var numbers = parseFloats( data );
  224. for ( var j = 0, jl = numbers.length; j < jl; j += 2 ) {
  225. point.x += numbers[ j + 0 ];
  226. point.y += numbers[ j + 1 ];
  227. control.x = point.x;
  228. control.y = point.y;
  229. path.lineTo( point.x, point.y );
  230. }
  231. break;
  232. case 'c':
  233. var numbers = parseFloats( data );
  234. for ( var j = 0, jl = numbers.length; j < jl; j += 6 ) {
  235. path.bezierCurveTo(
  236. point.x + numbers[ j + 0 ],
  237. point.y + numbers[ j + 1 ],
  238. point.x + numbers[ j + 2 ],
  239. point.y + numbers[ j + 3 ],
  240. point.x + numbers[ j + 4 ],
  241. point.y + numbers[ j + 5 ]
  242. );
  243. control.x = point.x + numbers[ j + 2 ];
  244. control.y = point.y + numbers[ j + 3 ];
  245. point.x += numbers[ j + 4 ];
  246. point.y += numbers[ j + 5 ];
  247. }
  248. break;
  249. case 's':
  250. var numbers = parseFloats( data );
  251. for ( var j = 0, jl = numbers.length; j < jl; j += 4 ) {
  252. path.bezierCurveTo(
  253. getReflection( point.x, control.x ),
  254. getReflection( point.y, control.y ),
  255. point.x + numbers[ j + 0 ],
  256. point.y + numbers[ j + 1 ],
  257. point.x + numbers[ j + 2 ],
  258. point.y + numbers[ j + 3 ]
  259. );
  260. control.x = point.x + numbers[ j + 0 ];
  261. control.y = point.y + numbers[ j + 1 ];
  262. point.x += numbers[ j + 2 ];
  263. point.y += numbers[ j + 3 ];
  264. }
  265. break;
  266. case 'q':
  267. var numbers = parseFloats( data );
  268. for ( var j = 0, jl = numbers.length; j < jl; j += 4 ) {
  269. path.quadraticCurveTo(
  270. point.x + numbers[ j + 0 ],
  271. point.y + numbers[ j + 1 ],
  272. point.x + numbers[ j + 2 ],
  273. point.y + numbers[ j + 3 ]
  274. );
  275. control.x = point.x + numbers[ j + 0 ];
  276. control.y = point.y + numbers[ j + 1 ];
  277. point.x += numbers[ j + 2 ];
  278. point.y += numbers[ j + 3 ];
  279. }
  280. break;
  281. case 't':
  282. var numbers = parseFloats( data );
  283. for ( var j = 0, jl = numbers.length; j < jl; j += 2 ) {
  284. var rx = getReflection( point.x, control.x );
  285. var ry = getReflection( point.y, control.y );
  286. path.quadraticCurveTo(
  287. rx,
  288. ry,
  289. point.x + numbers[ j + 0 ],
  290. point.y + numbers[ j + 1 ]
  291. );
  292. control.x = rx;
  293. control.y = ry;
  294. point.x = point.x + numbers[ j + 0 ];
  295. point.y = point.y + numbers[ j + 1 ];
  296. }
  297. break;
  298. case 'a':
  299. var numbers = parseFloats( data );
  300. for ( var j = 0, jl = numbers.length; j < jl; j += 7 ) {
  301. var start = point.clone();
  302. point.x += numbers[ j + 5 ];
  303. point.y += numbers[ j + 6 ];
  304. control.x = point.x;
  305. control.y = point.y;
  306. parseArcCommand(
  307. path, numbers[ j ], numbers[ j + 1 ], numbers[ j + 2 ], numbers[ j + 3 ], numbers[ j + 4 ], start, point
  308. );
  309. }
  310. break;
  311. //
  312. case 'Z':
  313. case 'z':
  314. path.currentPath.autoClose = true;
  315. // Reset point to beginning of Path
  316. point.x = path.currentPath.curves[ 0 ].v0.x;
  317. point.y = path.currentPath.curves[ 0 ].v0.y;
  318. path.currentPath.currentPoint.copy( point );
  319. break;
  320. default:
  321. console.warn( command );
  322. }
  323. // console.log( type, parseFloats( data ), parseFloats( data ).length )
  324. }
  325. return path;
  326. }
  327. /**
  328. * https://www.w3.org/TR/SVG/implnote.html#ArcImplementationNotes
  329. * https://mortoray.com/2017/02/16/rendering-an-svg-elliptical-arc-as-bezier-curves/ Appendix: Endpoint to center arc conversion
  330. * From
  331. * rx ry x-axis-rotation large-arc-flag sweep-flag x y
  332. * To
  333. * aX, aY, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise, aRotation
  334. */
  335. function parseArcCommand( path, rx, ry, x_axis_rotation, large_arc_flag, sweep_flag, start, end ) {
  336. x_axis_rotation = x_axis_rotation * Math.PI / 180;
  337. // Ensure radii are positive
  338. rx = Math.abs( rx );
  339. ry = Math.abs( ry );
  340. // Compute (x1′, y1′)
  341. var dx2 = ( start.x - end.x ) / 2.0;
  342. var dy2 = ( start.y - end.y ) / 2.0;
  343. var x1p = Math.cos( x_axis_rotation ) * dx2 + Math.sin( x_axis_rotation ) * dy2;
  344. var y1p = - Math.sin( x_axis_rotation ) * dx2 + Math.cos( x_axis_rotation ) * dy2;
  345. // Compute (cx′, cy′)
  346. var rxs = rx * rx;
  347. var rys = ry * ry;
  348. var x1ps = x1p * x1p;
  349. var y1ps = y1p * y1p;
  350. // Ensure radii are large enough
  351. var cr = x1ps / rxs + y1ps / rys;
  352. if ( cr > 1 ) {
  353. // scale up rx,ry equally so cr == 1
  354. var s = Math.sqrt( cr );
  355. rx = s * rx;
  356. ry = s * ry;
  357. rxs = rx * rx;
  358. rys = ry * ry;
  359. }
  360. var dq = ( rxs * y1ps + rys * x1ps );
  361. var pq = ( rxs * rys - dq ) / dq;
  362. var q = Math.sqrt( Math.max( 0, pq ) );
  363. if ( large_arc_flag === sweep_flag ) q = - q;
  364. var cxp = q * rx * y1p / ry;
  365. var cyp = - q * ry * x1p / rx;
  366. // Step 3: Compute (cx, cy) from (cx′, cy′)
  367. var cx = Math.cos( x_axis_rotation ) * cxp - Math.sin( x_axis_rotation ) * cyp + ( start.x + end.x ) / 2;
  368. var cy = Math.sin( x_axis_rotation ) * cxp + Math.cos( x_axis_rotation ) * cyp + ( start.y + end.y ) / 2;
  369. // Step 4: Compute θ1 and Δθ
  370. var theta = svgAngle( 1, 0, ( x1p - cxp ) / rx, ( y1p - cyp ) / ry );
  371. var delta = svgAngle( ( x1p - cxp ) / rx, ( y1p - cyp ) / ry, ( - x1p - cxp ) / rx, ( - y1p - cyp ) / ry ) % ( Math.PI * 2 );
  372. path.currentPath.absellipse( cx, cy, rx, ry, theta, theta + delta, sweep_flag === 0, x_axis_rotation );
  373. }
  374. function svgAngle( ux, uy, vx, vy ) {
  375. var dot = ux * vx + uy * vy;
  376. var len = Math.sqrt( ux * ux + uy * uy ) * Math.sqrt( vx * vx + vy * vy );
  377. var ang = Math.acos( Math.max( -1, Math.min( 1, dot / len ) ) ); // floating point precision, slightly over values appear
  378. if ( ( ux * vy - uy * vx ) < 0 ) ang = - ang;
  379. return ang;
  380. }
  381. /*
  382. * According to https://www.w3.org/TR/SVG/shapes.html#RectElementRXAttribute
  383. * rounded corner should be rendered to elliptical arc, but bezier curve does the job well enough
  384. */
  385. function parseRectNode( node, style ) {
  386. var x = parseFloat( node.getAttribute( 'x' ) || 0 );
  387. var y = parseFloat( node.getAttribute( 'y' ) || 0 );
  388. var rx = parseFloat( node.getAttribute( 'rx' ) || 0 );
  389. var ry = parseFloat( node.getAttribute( 'ry' ) || 0 );
  390. var w = parseFloat( node.getAttribute( 'width' ) );
  391. var h = parseFloat( node.getAttribute( 'height' ) );
  392. var path = new THREE.ShapePath();
  393. path.color.setStyle( style.fill );
  394. path.moveTo( x + 2 * rx, y );
  395. path.lineTo( x + w - 2 * rx, y );
  396. if ( rx !== 0 || ry !== 0 ) path.bezierCurveTo( x + w, y, x + w, y, x + w, y + 2 * ry );
  397. path.lineTo( x + w, y + h - 2 * ry );
  398. if ( rx !== 0 || ry !== 0 ) path.bezierCurveTo( x + w, y + h, x + w, y + h, x + w - 2 * rx, y + h );
  399. path.lineTo( x + 2 * rx, y + h );
  400. if ( rx !== 0 || ry !== 0 ) {
  401. path.bezierCurveTo( x, y + h, x, y + h, x, y + h - 2 * ry );
  402. path.lineTo( x, y + 2 * ry );
  403. path.bezierCurveTo( x, y, x, y, x + 2 * rx, y );
  404. }
  405. return path;
  406. }
  407. function parsePolygonNode( node, style ) {
  408. function iterator( match, a, b ) {
  409. var x = parseFloat( a );
  410. var y = parseFloat( b );
  411. if ( index === 0 ) {
  412. path.moveTo( x, y );
  413. } else {
  414. path.lineTo( x, y );
  415. }
  416. index ++;
  417. }
  418. var regex = /(-?[\d\.?]+)[,|\s](-?[\d\.?]+)/g;
  419. var path = new THREE.ShapePath();
  420. path.color.setStyle( style.fill );
  421. var index = 0;
  422. node.getAttribute( 'points' ).replace(regex, iterator);
  423. path.currentPath.autoClose = true;
  424. return path;
  425. }
  426. function parsePolylineNode( node, style ) {
  427. function iterator( match, a, b ) {
  428. var x = parseFloat( a );
  429. var y = parseFloat( b );
  430. if ( index === 0 ) {
  431. path.moveTo( x, y );
  432. } else {
  433. path.lineTo( x, y );
  434. }
  435. index ++;
  436. }
  437. var regex = /(-?[\d\.?]+)[,|\s](-?[\d\.?]+)/g;
  438. var path = new THREE.ShapePath();
  439. path.color.setStyle( style.fill );
  440. var index = 0;
  441. node.getAttribute( 'points' ).replace(regex, iterator);
  442. path.currentPath.autoClose = false;
  443. return path;
  444. }
  445. function parseCircleNode( node, style ) {
  446. var x = parseFloat( node.getAttribute( 'cx' ) );
  447. var y = parseFloat( node.getAttribute( 'cy' ) );
  448. var r = parseFloat( node.getAttribute( 'r' ) );
  449. var subpath = new THREE.Path();
  450. subpath.absarc( x, y, r, 0, Math.PI * 2 );
  451. var path = new THREE.ShapePath();
  452. path.color.setStyle( style.fill );
  453. path.subPaths.push( subpath );
  454. return path;
  455. }
  456. function parseEllipseNode( node, style ) {
  457. var x = parseFloat( node.getAttribute( 'cx' ) );
  458. var y = parseFloat( node.getAttribute( 'cy' ) );
  459. var rx = parseFloat( node.getAttribute( 'rx' ) );
  460. var ry = parseFloat( node.getAttribute( 'ry' ) );
  461. var subpath = new THREE.Path();
  462. subpath.absellipse( x, y, rx, ry, 0, Math.PI * 2 );
  463. var path = new THREE.ShapePath();
  464. path.color.setStyle( style.fill );
  465. path.subPaths.push( subpath );
  466. return path;
  467. }
  468. function parseLineNode( node, style ) {
  469. var x1 = parseFloat( node.getAttribute( 'x1' ) );
  470. var y1 = parseFloat( node.getAttribute( 'y1' ) );
  471. var x2 = parseFloat( node.getAttribute( 'x2' ) );
  472. var y2 = parseFloat( node.getAttribute( 'y2' ) );
  473. var path = new THREE.ShapePath();
  474. path.moveTo( x1, y1 );
  475. path.lineTo( x2, y2 );
  476. path.currentPath.autoClose = false;
  477. return path;
  478. }
  479. //
  480. function parseStyle( node, style ) {
  481. style = Object.assign( {}, style ); // clone style
  482. if ( node.hasAttribute( 'fill' ) ) style.fill = node.getAttribute( 'fill' );
  483. if ( node.style.fill !== '' ) style.fill = node.style.fill;
  484. return style;
  485. }
  486. function isVisible( style ) {
  487. return style.fill !== 'none' && style.fill !== 'transparent';
  488. }
  489. // http://www.w3.org/TR/SVG11/implnote.html#PathElementImplementationNotes
  490. function getReflection( a, b ) {
  491. return a - ( b - a );
  492. }
  493. function parseFloats( string ) {
  494. var array = string.split( /[\s,]+|(?=\s?[+\-])/ );
  495. for ( var i = 0; i < array.length; i ++ ) {
  496. var number = array[ i ];
  497. // Handle values like 48.6037.7
  498. // TODO Find a regex for this
  499. if ( number.indexOf( '.' ) !== number.lastIndexOf( '.' ) ) {
  500. array.splice( i + 1, 0, '0.' + number.split( '.' )[ 2 ] );
  501. }
  502. array[ i ] = parseFloat( number );
  503. }
  504. return array;
  505. }
  506. //
  507. console.log( 'THREE.SVGLoader' );
  508. var paths = [];
  509. console.time( 'THREE.SVGLoader: DOMParser' );
  510. var xml = new DOMParser().parseFromString( text, 'image/svg+xml' ); // application/xml
  511. console.timeEnd( 'THREE.SVGLoader: DOMParser' );
  512. console.time( 'THREE.SVGLoader: Parse' );
  513. parseNode( xml.documentElement, { fill: '#000' } );
  514. // console.log( paths );
  515. console.timeEnd( 'THREE.SVGLoader: Parse' );
  516. return paths;
  517. }
  518. };