SVGLoader.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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 ( style.fill !== 'none' ) paths.push( parsePathNode( node, style ) );
  29. break;
  30. case 'rect':
  31. style = parseStyle( node, style );
  32. if ( style.fill !== 'none' ) paths.push( parseRectNode( node, style ) );
  33. break;
  34. case 'polygon':
  35. style = parseStyle( node, style );
  36. if ( style.fill !== 'none' ) paths.push( parsePolygonNode( node, style ) );
  37. break;
  38. case 'polyline':
  39. style = parseStyle( node, style );
  40. if ( style.fill !== 'none' ) paths.push( parsePolylineNode( node, style ) );
  41. break;
  42. case 'circle':
  43. style = parseStyle( node, style );
  44. if ( style.fill !== 'none' ) paths.push( parseCircleNode( node, style ) );
  45. break;
  46. case 'ellipse':
  47. style = parseStyle( node, style );
  48. if ( style.fill !== 'none' ) paths.push( parseEllipseNode( node, style ) );
  49. break;
  50. case 'line':
  51. style = parseStyle( node, style );
  52. if ( style.fill !== 'none' ) 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; i < commands.length; 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. point.fromArray( numbers );
  78. control.x = point.x;
  79. control.y = point.y;
  80. path.moveTo( point.x, point.y );
  81. break;
  82. case 'H':
  83. var numbers = parseFloats( data );
  84. point.x = numbers[ 0 ];
  85. control.x = point.x;
  86. control.y = point.y;
  87. path.lineTo( point.x, point.y );
  88. break;
  89. case 'V':
  90. var numbers = parseFloats( data );
  91. point.y = numbers[ 0 ];
  92. control.x = point.x;
  93. control.y = point.y;
  94. path.lineTo( point.x, point.y );
  95. break;
  96. case 'L':
  97. var numbers = parseFloats( data );
  98. point.x = numbers[ 0 ];
  99. point.y = numbers[ 1 ];
  100. control.x = point.x;
  101. control.y = point.y;
  102. path.lineTo( point.x, point.y );
  103. break;
  104. case 'C':
  105. var numbers = parseFloats( data );
  106. path.bezierCurveTo(
  107. numbers[ 0 ],
  108. numbers[ 1 ],
  109. numbers[ 2 ],
  110. numbers[ 3 ],
  111. numbers[ 4 ],
  112. numbers[ 5 ]
  113. );
  114. control.x = numbers[ 2 ];
  115. control.y = numbers[ 3 ];
  116. point.x = numbers[ 4 ];
  117. point.y = numbers[ 5 ];
  118. break;
  119. case 'S':
  120. var numbers = parseFloats( data );
  121. path.bezierCurveTo(
  122. getReflection( point.x, control.x ),
  123. getReflection( point.y, control.y ),
  124. numbers[ 0 ],
  125. numbers[ 1 ],
  126. numbers[ 2 ],
  127. numbers[ 3 ]
  128. );
  129. control.x = numbers[ 0 ];
  130. control.y = numbers[ 1 ];
  131. point.x = numbers[ 2 ];
  132. point.y = numbers[ 3 ];
  133. break;
  134. case 'Q':
  135. var numbers = parseFloats( data );
  136. path.quadraticCurveTo(
  137. numbers[ 0 ],
  138. numbers[ 1 ],
  139. numbers[ 2 ],
  140. numbers[ 3 ]
  141. );
  142. control.x = numbers[ 0 ];
  143. control.y = numbers[ 1 ];
  144. point.x = numbers[ 2 ];
  145. point.y = numbers[ 3 ];
  146. break;
  147. case 'T':
  148. var numbers = parseFloats( data );
  149. var rx = getReflection( point.x, control.x );
  150. var ry = getReflection( point.y, control.y );
  151. path.quadraticCurveTo(
  152. rx,
  153. ry,
  154. numbers[ 0 ],
  155. numbers[ 1 ]
  156. );
  157. control.x = rx;
  158. control.y = ry;
  159. point.x = numbers[ 0 ];
  160. point.y = numbers[ 1 ];
  161. break;
  162. // case 'A': break;
  163. case 'm':
  164. var numbers = parseFloats( data );
  165. point.x += numbers[ 0 ];
  166. point.y += numbers[ 1 ];
  167. control.x = point.x;
  168. control.y = point.y;
  169. path.moveTo( point.x, point.y );
  170. break;
  171. case 'h':
  172. var numbers = parseFloats( data );
  173. point.x += numbers[ 0 ];
  174. control.x = point.x;
  175. control.y = point.y;
  176. path.lineTo( point.x, point.y );
  177. break;
  178. case 'v':
  179. var numbers = parseFloats( data );
  180. point.y += numbers[ 0 ];
  181. control.x = point.x;
  182. control.y = point.y;
  183. path.lineTo( point.x, point.y );
  184. break;
  185. case 'l':
  186. var numbers = parseFloats( data );
  187. point.x += numbers[ 0 ];
  188. point.y += numbers[ 1 ];
  189. control.x = point.x;
  190. control.y = point.y;
  191. path.lineTo( point.x, point.y );
  192. break;
  193. case 'c':
  194. var numbers = parseFloats( data );
  195. path.bezierCurveTo(
  196. point.x + numbers[ 0 ],
  197. point.y + numbers[ 1 ],
  198. point.x + numbers[ 2 ],
  199. point.y + numbers[ 3 ],
  200. point.x + numbers[ 4 ],
  201. point.y + numbers[ 5 ]
  202. );
  203. point.x += numbers[ 4 ];
  204. point.y += numbers[ 5 ];
  205. break;
  206. case 's':
  207. var numbers = parseFloats( data );
  208. path.bezierCurveTo(
  209. // TODO: Not sure if point needs
  210. // to be added to reflection...
  211. getReflection( point.x, control.x ),
  212. getReflection( point.y, control.y ),
  213. point.x + numbers[ 0 ],
  214. point.y + numbers[ 1 ],
  215. point.x + numbers[ 2 ],
  216. point.y + numbers[ 3 ]
  217. );
  218. control.x = point.x + numbers[ 0 ];
  219. control.y = point.y + numbers[ 1 ];
  220. point.x += numbers[ 2 ];
  221. point.y += numbers[ 3 ];
  222. break;
  223. case 'q':
  224. var numbers = parseFloats( data );
  225. path.quadraticCurveTo(
  226. point.x + numbers[ 0 ],
  227. point.y + numbers[ 1 ],
  228. point.x + numbers[ 2 ],
  229. point.y + numbers[ 3 ]
  230. );
  231. control.x = point.x + numbers[ 0 ];
  232. control.y = point.y + numbers[ 1 ];
  233. point.x += numbers[ 2 ];
  234. point.y += numbers[ 3 ];
  235. break;
  236. case 't':
  237. var numbers = parseFloats( data );
  238. var rx = getReflection( point.x, control.x );
  239. var ry = getReflection( point.y, control.y );
  240. path.quadraticCurveTo(
  241. rx,
  242. ry,
  243. point.x + numbers[ 0 ],
  244. point.y + numbers[ 1 ]
  245. );
  246. control.x = rx;
  247. control.y = ry;
  248. point.x = point.x + numbers[ 0 ];
  249. point.y = point.y + numbers[ 1 ];
  250. break;
  251. // case 'a': break;
  252. case 'Z':
  253. case 'z':
  254. path.currentPath.autoClose = true;
  255. break;
  256. default:
  257. console.log( command );
  258. }
  259. }
  260. return path;
  261. }
  262. function parseRectNode( node, style ) {
  263. var x = parseFloat( node.getAttribute( 'x' ) || 0 );
  264. var y = parseFloat( node.getAttribute( 'y' ) || 0 );
  265. var w = parseFloat( node.getAttribute( 'width' ) );
  266. var h = parseFloat( node.getAttribute( 'height' ) );
  267. var path = new THREE.ShapePath();
  268. path.color.setStyle( style.fill );
  269. path.moveTo( x, y );
  270. path.lineTo( x + w, y );
  271. path.lineTo( x + w, y + h );
  272. path.lineTo( x, y + h );
  273. return path;
  274. }
  275. function parsePolygonNode( node, style ) {
  276. function iterator( match, a, b ) {
  277. var x = parseFloat( a );
  278. var y = parseFloat( b );
  279. if ( index === 0 ) {
  280. path.moveTo( x, y );
  281. } else {
  282. path.lineTo( x, y );
  283. }
  284. index ++;
  285. }
  286. var regex = /(-?[\d\.?]+)[,|\s](-?[\d\.?]+)/g;
  287. var path = new THREE.ShapePath();
  288. path.color.setStyle( style.fill );
  289. var index = 0;
  290. node.getAttribute( 'points' ).replace(regex, iterator);
  291. path.currentPath.autoClose = true;
  292. return path;
  293. }
  294. function parsePolylineNode( node, style ) {
  295. function iterator( match, a, b ) {
  296. var x = parseFloat( a );
  297. var y = parseFloat( b );
  298. if ( index === 0 ) {
  299. path.moveTo( x, y );
  300. } else {
  301. path.lineTo( x, y );
  302. }
  303. index ++;
  304. }
  305. var regex = /(-?[\d\.?]+)[,|\s](-?[\d\.?]+)/g;
  306. var path = new THREE.ShapePath();
  307. path.color.setStyle( style.fill );
  308. var index = 0;
  309. node.getAttribute( 'points' ).replace(regex, iterator);
  310. path.currentPath.autoClose = false;
  311. return path;
  312. }
  313. function parseCircleNode( node, style ) {
  314. var x = parseFloat( node.getAttribute( 'cx' ) );
  315. var y = parseFloat( node.getAttribute( 'cy' ) );
  316. var r = parseFloat( node.getAttribute( 'r' ) );
  317. var subpath = new THREE.Path();
  318. subpath.absarc( x, y, r, 0, Math.PI * 2 );
  319. var path = new THREE.ShapePath();
  320. path.color.setStyle( style.fill );
  321. path.subPaths.push( subpath );
  322. return path;
  323. }
  324. function parseEllipseNode( node, style ) {
  325. var x = parseFloat( node.getAttribute( 'cx' ) );
  326. var y = parseFloat( node.getAttribute( 'cy' ) );
  327. var rx = parseFloat( node.getAttribute( 'rx' ) );
  328. var ry = parseFloat( node.getAttribute( 'ry' ) );
  329. var subpath = new THREE.Path();
  330. subpath.absellipse( x, y, rx, ry, 0, Math.PI * 2 );
  331. var path = new THREE.ShapePath();
  332. path.color.setStyle( style.fill );
  333. path.subPaths.push( subpath );
  334. return path;
  335. }
  336. function parseLineNode( node, style ) {
  337. var x1 = parseFloat( node.getAttribute( 'x1' ) );
  338. var y1 = parseFloat( node.getAttribute( 'y1' ) );
  339. var x2 = parseFloat( node.getAttribute( 'x2' ) );
  340. var y2 = parseFloat( node.getAttribute( 'y2' ) );
  341. var path = new THREE.ShapePath();
  342. path.moveTo( x1, y1 );
  343. path.lineTo( x2, y2 );
  344. path.currentPath.autoClose = false;
  345. return path;
  346. }
  347. //
  348. function parseStyle( node, style ) {
  349. style = Object.assign( {}, style ); // clone style
  350. if ( node.hasAttribute( 'fill' ) ) style.fill = node.getAttribute( 'fill' );
  351. if ( node.style.fill !== '' ) style.fill = node.style.fill;
  352. return style;
  353. }
  354. // http://www.w3.org/TR/SVG11/implnote.html#PathElementImplementationNotes
  355. function getReflection( a, b ) {
  356. return 2 * a - ( b - a );
  357. }
  358. function parseFloats( string ) {
  359. var array = string.split( /[\s,]+|(?=\s?[+\-])/ );
  360. for ( var i = 0; i < array.length; i ++ ) {
  361. array[ i ] = parseFloat( array[ i ] );
  362. }
  363. return array;
  364. }
  365. //
  366. var paths = [];
  367. var xml = new DOMParser().parseFromString( text, 'image/svg+xml' ); // application/xml
  368. parseNode( xml.documentElement, { fill: '#000' } );
  369. return paths;
  370. }
  371. };