SVGLoader.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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. path.bezierCurveTo(
  133. getReflection( point.x, control.x ),
  134. getReflection( point.y, control.y ),
  135. numbers[ 0 ],
  136. numbers[ 1 ],
  137. numbers[ 2 ],
  138. numbers[ 3 ]
  139. );
  140. control.x = numbers[ 0 ];
  141. control.y = numbers[ 1 ];
  142. point.x = numbers[ 2 ];
  143. point.y = numbers[ 3 ];
  144. break;
  145. case 'Q':
  146. var numbers = parseFloats( data );
  147. path.quadraticCurveTo(
  148. numbers[ 0 ],
  149. numbers[ 1 ],
  150. numbers[ 2 ],
  151. numbers[ 3 ]
  152. );
  153. control.x = numbers[ 0 ];
  154. control.y = numbers[ 1 ];
  155. point.x = numbers[ 2 ];
  156. point.y = numbers[ 3 ];
  157. break;
  158. case 'T':
  159. var numbers = parseFloats( data );
  160. var rx = getReflection( point.x, control.x );
  161. var ry = getReflection( point.y, control.y );
  162. path.quadraticCurveTo(
  163. rx,
  164. ry,
  165. numbers[ 0 ],
  166. numbers[ 1 ]
  167. );
  168. control.x = rx;
  169. control.y = ry;
  170. point.x = numbers[ 0 ];
  171. point.y = numbers[ 1 ];
  172. break;
  173. case 'A':
  174. var numbers = parseFloats( data );
  175. for ( var j = 0, jl = numbers.length; j < jl; j += 7 ) {
  176. point.x = numbers[ j + 5 ];
  177. point.y = numbers[ j + 6 ];
  178. var radius = { x: numbers[ j ], y: numbers[ j + 1 ] };
  179. var x_axis_rotation = numbers[ j + 2 ] * Math.PI / 180;
  180. svgEllipsisToThreeEllipsis( path, control, radius, x_axis_rotation, numbers[ j + 3 ], numbers[ j + 4 ], point );
  181. control.x = point.x;
  182. control.y = point.y;
  183. }
  184. break;
  185. //
  186. case 'm':
  187. var numbers = parseFloats( data );
  188. for ( var j = 0, jl = numbers.length; j < jl; j += 2 ) {
  189. point.x += numbers[ j + 0 ];
  190. point.y += numbers[ j + 1 ];
  191. control.x = point.x;
  192. control.y = point.y;
  193. path.moveTo( point.x, point.y );
  194. }
  195. break;
  196. case 'h':
  197. var numbers = parseFloats( data );
  198. for ( var j = 0, jl = numbers.length; j < jl; j ++ ) {
  199. point.x += numbers[ j ];
  200. control.x = point.x;
  201. control.y = point.y;
  202. path.lineTo( point.x, point.y );
  203. }
  204. break;
  205. case 'v':
  206. var numbers = parseFloats( data );
  207. for ( var j = 0, jl = numbers.length; j < jl; j ++ ) {
  208. point.y += numbers[ j ];
  209. control.x = point.x;
  210. control.y = point.y;
  211. path.lineTo( point.x, point.y );
  212. }
  213. break;
  214. case 'l':
  215. var numbers = parseFloats( data );
  216. for ( var j = 0, jl = numbers.length; j < jl; j += 2 ) {
  217. point.x += numbers[ j + 0 ];
  218. point.y += numbers[ j + 1 ];
  219. control.x = point.x;
  220. control.y = point.y;
  221. path.lineTo( point.x, point.y );
  222. }
  223. break;
  224. case 'c':
  225. var numbers = parseFloats( data );
  226. for ( var j = 0, jl = numbers.length; j < jl; j += 6 ) {
  227. path.bezierCurveTo(
  228. point.x + numbers[ j + 0 ],
  229. point.y + numbers[ j + 1 ],
  230. point.x + numbers[ j + 2 ],
  231. point.y + numbers[ j + 3 ],
  232. point.x + numbers[ j + 4 ],
  233. point.y + numbers[ j + 5 ]
  234. );
  235. point.x += numbers[ j + 4 ];
  236. point.y += numbers[ j + 5 ];
  237. }
  238. break;
  239. case 's':
  240. var numbers = parseFloats( data );
  241. path.bezierCurveTo(
  242. // TODO: Not sure if point needs
  243. // to be added to reflection...
  244. getReflection( point.x, control.x ),
  245. getReflection( point.y, control.y ),
  246. point.x + numbers[ 0 ],
  247. point.y + numbers[ 1 ],
  248. point.x + numbers[ 2 ],
  249. point.y + numbers[ 3 ]
  250. );
  251. control.x = point.x + numbers[ 0 ];
  252. control.y = point.y + numbers[ 1 ];
  253. point.x += numbers[ 2 ];
  254. point.y += numbers[ 3 ];
  255. break;
  256. case 'q':
  257. var numbers = parseFloats( data );
  258. path.quadraticCurveTo(
  259. point.x + numbers[ 0 ],
  260. point.y + numbers[ 1 ],
  261. point.x + numbers[ 2 ],
  262. point.y + numbers[ 3 ]
  263. );
  264. control.x = point.x + numbers[ 0 ];
  265. control.y = point.y + numbers[ 1 ];
  266. point.x += numbers[ 2 ];
  267. point.y += numbers[ 3 ];
  268. break;
  269. case 't':
  270. var numbers = parseFloats( data );
  271. var rx = getReflection( point.x, control.x );
  272. var ry = getReflection( point.y, control.y );
  273. path.quadraticCurveTo(
  274. rx,
  275. ry,
  276. point.x + numbers[ 0 ],
  277. point.y + numbers[ 1 ]
  278. );
  279. control.x = rx;
  280. control.y = ry;
  281. point.x = point.x + numbers[ 0 ];
  282. point.y = point.y + numbers[ 1 ];
  283. break;
  284. case 'a':
  285. console.warn( command );
  286. var numbers = parseFloats( data );
  287. for ( var j = 0, jl = numbers.length; j < jl; j += 7 ) {
  288. // TODO
  289. point.x += numbers[ j + 5 ];
  290. point.y += numbers[ j + 6 ];
  291. control.x = point.x;
  292. control.y = point.y;
  293. }
  294. break;
  295. //
  296. case 'Z':
  297. case 'z':
  298. path.currentPath.autoClose = true;
  299. break;
  300. default:
  301. console.warn( command );
  302. }
  303. // console.log( type, parseFloats( data ), parseFloats( data ).length )
  304. }
  305. return path;
  306. }
  307. /**
  308. * https://www.w3.org/TR/SVG/implnote.html#ArcImplementationNotes
  309. * https://mortoray.com/2017/02/16/rendering-an-svg-elliptical-arc-as-bezier-curves/ Appendix: Endpoint to center arc conversion
  310. * From
  311. * rx ry x-axis-rotation large-arc-flag sweep-flag x y
  312. * To
  313. * aX, aY, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise, aRotation
  314. */
  315. function svgEllipsisToThreeEllipsis( path, start, radius, x_axis_rotation, large_arc_flag, sweep_flag, end ) {
  316. //F.6.6 Correction of out-of-range radii
  317. //Step 2: Ensure radii are positive
  318. var rX = Math.abs( radius.x );
  319. var rY = Math.abs( radius.y );
  320. // Step 1: Compute (x1′, y1′)
  321. var midDist = new THREE.Vector2().subVectors( start, end ).multiplyScalar( 0.5 );
  322. var x1p = Math.cos( x_axis_rotation ) * midDist.x + Math.sin( x_axis_rotation ) * midDist.y;
  323. var y1p = - Math.sin( x_axis_rotation ) * midDist.x + Math.cos( x_axis_rotation ) * midDist.y;
  324. // Step 2: Compute (cx′, cy′)
  325. var rxs = rX * rX;
  326. var rys = rY * rY;
  327. var x1ps = x1p * x1p;
  328. var y1ps = y1p * y1p;
  329. //Step 3: Ensure radii are large enough
  330. var cr = x1ps / rxs + y1ps / rys;
  331. if (cr > 1) {
  332. //scale up rX,rY equally so cr == 1
  333. var s = Math.sqrt(cr);
  334. rX = s * rX;
  335. rY = s * rY;
  336. rxs = rX * rX;
  337. rys = rY * rY;
  338. }
  339. var dq = ( rxs * y1ps + rys * x1ps );
  340. var pq = ( rxs * rys - dq ) / dq;
  341. var q = Math.sqrt( pq );
  342. if ( large_arc_flag === sweep_flag ) q = - q;
  343. var cxp = q * rX * y1p / rY;
  344. var cyp = - q * rY * x1p / rX;
  345. // Step 3: Compute (cx, cy) from (cx′, cy′)
  346. var cx = Math.cos( x_axis_rotation ) * cxp - Math.sin( x_axis_rotation ) * cyp + ( start.x + end.x ) / 2;
  347. var cy = Math.sin( x_axis_rotation ) * cxp + Math.cos( x_axis_rotation ) * cyp + ( start.y + end.y ) / 2;
  348. // Step 4: Compute θ1 and Δθ
  349. var startAngle = new THREE.Vector2( ( x1p - cxp ) / rX, ( y1p - cyp ) / rY ).angle();
  350. var endAngle = new THREE.Vector2( ( - x1p - cxp ) / rX, ( - y1p - cyp ) / rY ).angle();
  351. if ( ! sweep_flag ) endAngle -= 2 * Math.PI;
  352. path.currentPath.absellipse( cx, cy, rX, rY, startAngle, endAngle, endAngle > startAngle, x_axis_rotation );
  353. }
  354. /*
  355. * According to https://www.w3.org/TR/SVG/shapes.html#RectElementRXAttribute
  356. * rounded corner should be rendered to elliptical arc, but bezier curve does the job well enough
  357. */
  358. function parseRectNode( node, style ) {
  359. var x = parseFloat( node.getAttribute( 'x' ) || 0 );
  360. var y = parseFloat( node.getAttribute( 'y' ) || 0 );
  361. var rx = parseFloat( node.getAttribute( 'rx' ) || 0 );
  362. var ry = parseFloat( node.getAttribute( 'ry' ) || 0 );
  363. var w = parseFloat( node.getAttribute( 'width' ) );
  364. var h = parseFloat( node.getAttribute( 'height' ) );
  365. var path = new THREE.ShapePath();
  366. path.color.setStyle( style.fill );
  367. path.moveTo( x + 2 * rx, y );
  368. path.lineTo( x + w - 2 * rx, y );
  369. if ( rx !== 0 || ry !== 0 ) path.bezierCurveTo( x + w, y, x + w, y, x + w, y + 2 * ry );
  370. path.lineTo( x + w, y + h - 2 * ry );
  371. if ( rx !== 0 || ry !== 0 ) path.bezierCurveTo( x + w, y + h, x + w, y + h, x + w - 2 * rx, y + h );
  372. path.lineTo( x + 2 * rx, y + h );
  373. if ( rx !== 0 || ry !== 0 ) {
  374. path.bezierCurveTo( x, y + h, x, y + h, x, y + h - 2 * ry );
  375. path.lineTo( x, y + 2 * ry );
  376. path.bezierCurveTo( x, y, x, y, x + 2 * rx, y );
  377. }
  378. return path;
  379. }
  380. function parsePolygonNode( node, style ) {
  381. function iterator( match, a, b ) {
  382. var x = parseFloat( a );
  383. var y = parseFloat( b );
  384. if ( index === 0 ) {
  385. path.moveTo( x, y );
  386. } else {
  387. path.lineTo( x, y );
  388. }
  389. index ++;
  390. }
  391. var regex = /(-?[\d\.?]+)[,|\s](-?[\d\.?]+)/g;
  392. var path = new THREE.ShapePath();
  393. path.color.setStyle( style.fill );
  394. var index = 0;
  395. node.getAttribute( 'points' ).replace(regex, iterator);
  396. path.currentPath.autoClose = true;
  397. return path;
  398. }
  399. function parsePolylineNode( node, style ) {
  400. function iterator( match, a, b ) {
  401. var x = parseFloat( a );
  402. var y = parseFloat( b );
  403. if ( index === 0 ) {
  404. path.moveTo( x, y );
  405. } else {
  406. path.lineTo( x, y );
  407. }
  408. index ++;
  409. }
  410. var regex = /(-?[\d\.?]+)[,|\s](-?[\d\.?]+)/g;
  411. var path = new THREE.ShapePath();
  412. path.color.setStyle( style.fill );
  413. var index = 0;
  414. node.getAttribute( 'points' ).replace(regex, iterator);
  415. path.currentPath.autoClose = false;
  416. return path;
  417. }
  418. function parseCircleNode( node, style ) {
  419. var x = parseFloat( node.getAttribute( 'cx' ) );
  420. var y = parseFloat( node.getAttribute( 'cy' ) );
  421. var r = parseFloat( node.getAttribute( 'r' ) );
  422. var subpath = new THREE.Path();
  423. subpath.absarc( x, y, r, 0, Math.PI * 2 );
  424. var path = new THREE.ShapePath();
  425. path.color.setStyle( style.fill );
  426. path.subPaths.push( subpath );
  427. return path;
  428. }
  429. function parseEllipseNode( node, style ) {
  430. var x = parseFloat( node.getAttribute( 'cx' ) );
  431. var y = parseFloat( node.getAttribute( 'cy' ) );
  432. var rx = parseFloat( node.getAttribute( 'rx' ) );
  433. var ry = parseFloat( node.getAttribute( 'ry' ) );
  434. var subpath = new THREE.Path();
  435. subpath.absellipse( x, y, rx, ry, 0, Math.PI * 2 );
  436. var path = new THREE.ShapePath();
  437. path.color.setStyle( style.fill );
  438. path.subPaths.push( subpath );
  439. return path;
  440. }
  441. function parseLineNode( node, style ) {
  442. var x1 = parseFloat( node.getAttribute( 'x1' ) );
  443. var y1 = parseFloat( node.getAttribute( 'y1' ) );
  444. var x2 = parseFloat( node.getAttribute( 'x2' ) );
  445. var y2 = parseFloat( node.getAttribute( 'y2' ) );
  446. var path = new THREE.ShapePath();
  447. path.moveTo( x1, y1 );
  448. path.lineTo( x2, y2 );
  449. path.currentPath.autoClose = false;
  450. return path;
  451. }
  452. //
  453. function parseStyle( node, style ) {
  454. style = Object.assign( {}, style ); // clone style
  455. if ( node.hasAttribute( 'fill' ) ) style.fill = node.getAttribute( 'fill' );
  456. if ( node.style.fill !== '' ) style.fill = node.style.fill;
  457. return style;
  458. }
  459. function isVisible( style ) {
  460. return style.fill !== 'none' && style.fill !== 'transparent';
  461. }
  462. // http://www.w3.org/TR/SVG11/implnote.html#PathElementImplementationNotes
  463. function getReflection( a, b ) {
  464. return 2 * a - ( b - a );
  465. }
  466. function parseFloats( string ) {
  467. var array = string.split( /[\s,]+|(?=\s?[+\-])/ );
  468. for ( var i = 0; i < array.length; i ++ ) {
  469. array[ i ] = parseFloat( array[ i ] );
  470. }
  471. return array;
  472. }
  473. //
  474. console.log( 'THREE.SVGLoader' );
  475. var paths = [];
  476. console.time( 'THREE.SVGLoader: DOMParser' );
  477. var xml = new DOMParser().parseFromString( text, 'image/svg+xml' ); // application/xml
  478. console.timeEnd( 'THREE.SVGLoader: DOMParser' );
  479. console.time( 'THREE.SVGLoader: Parse' );
  480. parseNode( xml.documentElement, { fill: '#000' } );
  481. // console.log( paths );
  482. console.timeEnd( 'THREE.SVGLoader: Parse' );
  483. return paths;
  484. }
  485. };