SVGLoader.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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':
  163. // TODO:
  164. break;
  165. case 'm':
  166. var numbers = parseFloats( data );
  167. point.x += numbers[ 0 ];
  168. point.y += numbers[ 1 ];
  169. control.x = point.x;
  170. control.y = point.y;
  171. path.moveTo( point.x, point.y );
  172. break;
  173. case 'h':
  174. var numbers = parseFloats( data );
  175. point.x += numbers[ 0 ];
  176. control.x = point.x;
  177. control.y = point.y;
  178. path.lineTo( point.x, point.y );
  179. break;
  180. case 'v':
  181. var numbers = parseFloats( data );
  182. point.y += numbers[ 0 ];
  183. control.x = point.x;
  184. control.y = point.y;
  185. path.lineTo( point.x, point.y );
  186. break;
  187. case 'l':
  188. var numbers = parseFloats( data );
  189. point.x += numbers[ 0 ];
  190. point.y += numbers[ 1 ];
  191. control.x = point.x;
  192. control.y = point.y;
  193. path.lineTo( point.x, point.y );
  194. break;
  195. case 'c':
  196. var numbers = parseFloats( data );
  197. path.bezierCurveTo(
  198. point.x + numbers[ 0 ],
  199. point.y + numbers[ 1 ],
  200. point.x + numbers[ 2 ],
  201. point.y + numbers[ 3 ],
  202. point.x + numbers[ 4 ],
  203. point.y + numbers[ 5 ]
  204. );
  205. point.x += numbers[ 4 ];
  206. point.y += numbers[ 5 ];
  207. break;
  208. case 's':
  209. var numbers = parseFloats( data );
  210. path.bezierCurveTo(
  211. // TODO: Not sure if point needs
  212. // to be added to reflection...
  213. getReflection( point.x, control.x ),
  214. getReflection( point.y, control.y ),
  215. point.x + numbers[ 0 ],
  216. point.y + numbers[ 1 ],
  217. point.x + numbers[ 2 ],
  218. point.y + numbers[ 3 ]
  219. );
  220. control.x = point.x + numbers[ 0 ];
  221. control.y = point.y + numbers[ 1 ];
  222. point.x += numbers[ 2 ];
  223. point.y += numbers[ 3 ];
  224. break;
  225. case 'q':
  226. var numbers = parseFloats( data );
  227. path.quadraticCurveTo(
  228. point.x + numbers[ 0 ],
  229. point.y + numbers[ 1 ],
  230. point.x + numbers[ 2 ],
  231. point.y + numbers[ 3 ]
  232. );
  233. control.x = point.x + numbers[ 0 ];
  234. control.y = point.y + numbers[ 1 ];
  235. point.x += numbers[ 2 ];
  236. point.y += numbers[ 3 ];
  237. break;
  238. case 't':
  239. var numbers = parseFloats( data );
  240. var rx = getReflection( point.x, control.x );
  241. var ry = getReflection( point.y, control.y );
  242. path.quadraticCurveTo(
  243. rx,
  244. ry,
  245. point.x + numbers[ 0 ],
  246. point.y + numbers[ 1 ]
  247. );
  248. control.x = rx;
  249. control.y = ry;
  250. point.x = point.x + numbers[ 0 ];
  251. point.y = point.y + numbers[ 1 ];
  252. break;
  253. case 'a':
  254. // TODO:
  255. break;
  256. case 'Z':
  257. case 'z':
  258. path.currentPath.autoClose = true;
  259. break;
  260. default:
  261. console.log( command );
  262. }
  263. }
  264. return path;
  265. }
  266. function parseRectNode( node, style ) {
  267. var x = parseFloat( node.getAttribute( 'x' ) );
  268. var y = parseFloat( node.getAttribute( 'y' ) );
  269. var w = parseFloat( node.getAttribute( 'width' ) );
  270. var h = parseFloat( node.getAttribute( 'height' ) );
  271. var path = new THREE.ShapePath();
  272. path.color.setStyle( style.fill );
  273. path.moveTo( x, y );
  274. path.lineTo( x + w, y );
  275. path.lineTo( x + w, y + h );
  276. path.lineTo( x, y + h );
  277. return path;
  278. }
  279. function parsePolygonNode( node, style ) {
  280. function iterator( match, a, b ) {
  281. var x = parseFloat( a );
  282. var y = parseFloat( b );
  283. if ( index === 0 ) {
  284. path.moveTo( x, y );
  285. } else {
  286. path.lineTo( x, y );
  287. }
  288. index++;
  289. }
  290. var regex = /(-?[\d\.?]+)[,|\s](-?[\d\.?]+)/g;
  291. var path = new THREE.ShapePath();
  292. path.color.setStyle( parseFill( node, style ) );
  293. var index = 0;
  294. node.getAttribute( 'points' ).replace(regex, iterator);
  295. path.currentPath.autoClose = true;
  296. return path;
  297. }
  298. function parsePolylineNode( node, style ) {
  299. function iterator( match, a, b ) {
  300. var x = parseFloat( a );
  301. var y = parseFloat( b );
  302. if ( index === 0 ) {
  303. path.moveTo( x, y );
  304. } else {
  305. path.lineTo( x, y );
  306. }
  307. index++;
  308. }
  309. var regex = /(-?[\d\.?]+)[,|\s](-?[\d\.?]+)/g;
  310. var path = new THREE.ShapePath();
  311. path.color.setStyle( style.fill );
  312. var index = 0;
  313. node.getAttribute( 'points' ).replace(regex, iterator);
  314. path.currentPath.autoClose = false;
  315. return path;
  316. }
  317. function parseCircleNode( node, style ) {
  318. var x = parseFloat( node.getAttribute( 'cx' ) );
  319. var y = parseFloat( node.getAttribute( 'cy' ) );
  320. var r = parseFloat( node.getAttribute( 'r' ) );
  321. var subpath = new THREE.Path();
  322. subpath.absarc( x, y, r, 0, Math.PI * 2 );
  323. var path = new THREE.ShapePath();
  324. path.color.setStyle( style.fill );
  325. path.subPaths.push( subpath );
  326. return path;
  327. }
  328. function parseEllipseNode( node, style ) {
  329. var x = parseFloat( node.getAttribute( 'cx' ) );
  330. var y = parseFloat( node.getAttribute( 'cy' ) );
  331. var rx = parseFloat( node.getAttribute( 'rx' ) );
  332. var ry = parseFloat( node.getAttribute( 'ry' ) );
  333. var subpath = new THREE.Path();
  334. subpath.absellipse( x, y, rx, ry, 0, Math.PI * 2 );
  335. var path = new THREE.ShapePath();
  336. path.color.setStyle( style.fill );
  337. path.subPaths.push( subpath );
  338. return path;
  339. }
  340. function parseLineNode( node, style ) {
  341. var x1 = parseFloat( node.getAttribute( 'x1' ) );
  342. var y1 = parseFloat( node.getAttribute( 'y1' ) );
  343. var x2 = parseFloat( node.getAttribute( 'x2' ) );
  344. var y2 = parseFloat( node.getAttribute( 'y2' ) );
  345. var path = new THREE.ShapePath();
  346. path.moveTo( x1, y1 );
  347. path.lineTo( x2, y2 );
  348. path.currentPath.autoClose = false;
  349. return path;
  350. }
  351. //
  352. function parseStyle( node, style ) {
  353. style = Object.assign( {}, style ); // clone style
  354. if ( node.hasAttribute( 'fill' ) ) style.fill = node.getAttribute( 'fill' );
  355. if ( node.style.fill !== '' ) style.fill = node.style.fill;
  356. return style;
  357. }
  358. // http://www.w3.org/TR/SVG11/implnote.html#PathElementImplementationNotes
  359. function getReflection( a, b ) {
  360. return 2 * a - ( b - a );
  361. }
  362. function parseFloats( string ) {
  363. var array = string.split( /[\s,]+|(?=\s?[+\-])/ );
  364. for ( var i = 0; i < array.length; i ++ ) {
  365. array[ i ] = parseFloat( array[ i ] );
  366. }
  367. return array;
  368. }
  369. //
  370. var xml = new DOMParser().parseFromString( text, 'image/svg+xml' ); // application/xml
  371. var svg = xml.documentElement;
  372. var paths = [];
  373. parseNode( svg, { fill: '#000' } );
  374. return paths;
  375. }
  376. };