SVGLoader.js 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
  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. var transform = null;
  21. if ( node.hasAttribute( 'transform' ) ) {
  22. transform = parseTransformNode( node );
  23. if ( transform ) {
  24. if ( transformStack.length > 0 ) {
  25. multiplyTransforms( transform, transformStack[ transformStack.length - 1 ], tempTransform );
  26. copyTransform( tempTransform, transform );
  27. }
  28. copyTransform( transform, currentTransform );
  29. transformStack.push( transform );
  30. }
  31. }
  32. switch ( node.nodeName ) {
  33. case 'svg':
  34. break;
  35. case 'g':
  36. style = parseStyle( node, style );
  37. break;
  38. case 'path':
  39. style = parseStyle( node, style );
  40. if ( node.hasAttribute( 'd' ) && isVisible( style ) ) paths.push( parsePathNode( node, style ) );
  41. break;
  42. case 'rect':
  43. style = parseStyle( node, style );
  44. if ( isVisible( style ) ) paths.push( parseRectNode( node, style ) );
  45. break;
  46. case 'polygon':
  47. style = parseStyle( node, style );
  48. if ( isVisible( style ) ) paths.push( parsePolygonNode( node, style ) );
  49. break;
  50. case 'polyline':
  51. style = parseStyle( node, style );
  52. if ( isVisible( style ) ) paths.push( parsePolylineNode( node, style ) );
  53. break;
  54. case 'circle':
  55. style = parseStyle( node, style );
  56. if ( isVisible( style ) ) paths.push( parseCircleNode( node, style ) );
  57. break;
  58. case 'ellipse':
  59. style = parseStyle( node, style );
  60. if ( isVisible( style ) ) paths.push( parseEllipseNode( node, style ) );
  61. break;
  62. case 'line':
  63. style = parseStyle( node, style );
  64. if ( isVisible( style ) ) paths.push( parseLineNode( node, style ) );
  65. break;
  66. default:
  67. console.log( node );
  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. copyTransform( transformStack.pop(), currentTransform );
  75. }
  76. }
  77. function parsePathNode( node, style ) {
  78. var path = new THREE.ShapePath();
  79. path.color.setStyle( style.fill );
  80. var point = new THREE.Vector2();
  81. var control = new THREE.Vector2();
  82. var control2 = new THREE.Vector2();
  83. var transfPoint = new THREE.Vector2();
  84. var transfControl = new THREE.Vector2();
  85. var transfControl2 = new THREE.Vector2();
  86. var reflected = new THREE.Vector2();
  87. var firstPoint = new THREE.Vector2();
  88. var isFirstPoint = true;
  89. function setFirstPoint() {
  90. if ( isFirstPoint ) {
  91. firstPoint.x = point.x;
  92. firstPoint.y = point.y;
  93. isFirstPoint = false;
  94. }
  95. }
  96. var d = node.getAttribute( 'd' );
  97. // console.log( d );
  98. var commands = d.match( /[a-df-z][^a-df-z]*/ig );
  99. for ( var i = 0, l = commands.length; i < l; i ++ ) {
  100. var command = commands[ i ];
  101. var type = command.charAt( 0 );
  102. var data = command.substr( 1 ).trim();
  103. switch ( type ) {
  104. case 'M':
  105. var numbers = parseFloats( data );
  106. for ( var j = 0, jl = numbers.length; j < jl; j += 2 ) {
  107. point.x = numbers[ j + 0 ];
  108. point.y = numbers[ j + 1 ];
  109. control.x = point.x;
  110. control.y = point.y;
  111. transfVec2( point, currentTransform, transfPoint );
  112. transfVec2( control, currentTransform, transfControl );
  113. setFirstPoint();
  114. if ( j === 0 ) {
  115. path.moveTo( transfPoint.x, transfPoint.y );
  116. }
  117. else {
  118. path.lineTo( transfPoint.x, transfPoint.y );
  119. }
  120. }
  121. break;
  122. case 'H':
  123. var numbers = parseFloats( data );
  124. for ( var j = 0, jl = numbers.length; j < jl; j ++ ) {
  125. point.x = numbers[ j ];
  126. control.x = point.x;
  127. control.y = point.y;
  128. transfVec2( point, currentTransform, transfPoint );
  129. transfVec2( control, currentTransform, transfControl );
  130. setFirstPoint();
  131. path.lineTo( transfPoint.x, transfPoint.y );
  132. }
  133. break;
  134. case 'V':
  135. var numbers = parseFloats( data );
  136. for ( var j = 0, jl = numbers.length; j < jl; j ++ ) {
  137. point.y = numbers[ j ];
  138. control.x = point.x;
  139. control.y = point.y;
  140. transfVec2( point, currentTransform, transfPoint );
  141. transfVec2( control, currentTransform, transfControl );
  142. setFirstPoint();
  143. path.lineTo( transfPoint.x, transfPoint.y );
  144. }
  145. break;
  146. case 'L':
  147. var numbers = parseFloats( data );
  148. for ( var j = 0, jl = numbers.length; j < jl; j += 2 ) {
  149. point.x = numbers[ j + 0 ];
  150. point.y = numbers[ j + 1 ];
  151. control.x = point.x;
  152. control.y = point.y;
  153. transfVec2( point, currentTransform, transfPoint );
  154. transfVec2( control, currentTransform, transfControl );
  155. setFirstPoint();
  156. path.lineTo( transfPoint.x, transfPoint.y );
  157. }
  158. break;
  159. case 'C':
  160. var numbers = parseFloats( data );
  161. for ( var j = 0, jl = numbers.length; j < jl; j += 6 ) {
  162. control2.set( numbers[ j + 0 ], numbers[ j + 1 ] );
  163. control.set( numbers[ j + 2 ], numbers[ j + 3 ] );
  164. point.set( numbers[ j + 4 ], numbers[ j + 5 ] );
  165. transfVec2( point, currentTransform, transfPoint );
  166. transfVec2( control, currentTransform, transfControl );
  167. transfVec2( control2, currentTransform, transfControl2 );
  168. setFirstPoint();
  169. path.bezierCurveTo(
  170. transfControl2.x,
  171. transfControl2.y,
  172. transfControl.x,
  173. transfControl.y,
  174. transfPoint.x,
  175. transfPoint.y
  176. );
  177. }
  178. break;
  179. case 'S':
  180. var numbers = parseFloats( data );
  181. for ( var j = 0, jl = numbers.length; j < jl; j += 4 ) {
  182. reflected.set(
  183. getReflection( transfPoint.x, transfControl.x ),
  184. getReflection( transfPoint.y, transfControl.y )
  185. );
  186. control.set( numbers[ j + 0 ], numbers[ j + 1 ] );
  187. point.set( numbers[ j + 2 ], numbers[ j + 3 ] );
  188. transfVec2( point, currentTransform, transfPoint );
  189. transfVec2( control, currentTransform, transfControl );
  190. setFirstPoint();
  191. path.bezierCurveTo(
  192. reflected.x,
  193. reflected.y,
  194. transfControl.x,
  195. transfControl.y,
  196. transfPoint.x,
  197. transfPoint.y
  198. );
  199. }
  200. break;
  201. case 'Q':
  202. var numbers = parseFloats( data );
  203. for ( var j = 0, jl = numbers.length; j < jl; j += 4 ) {
  204. control.set( numbers[ j + 0 ], numbers[ j + 1 ] );
  205. point.set( numbers[ j + 2 ], numbers[ j + 3 ] );
  206. transfVec2( point, currentTransform, transfPoint );
  207. transfVec2( control, currentTransform, transfControl );
  208. setFirstPoint();
  209. path.quadraticCurveTo(
  210. transfControl.x,
  211. transfControl.y,
  212. transfPoint.x,
  213. transfPoint.y
  214. );
  215. }
  216. break;
  217. case 'T':
  218. var numbers = parseFloats( data );
  219. for ( var j = 0, jl = numbers.length; j < jl; j += 2 ) {
  220. reflected.set(
  221. getReflection( transfPoint.x, transfControl.x ),
  222. getReflection( transfPoint.y, transfControl.y )
  223. );
  224. control.set( reflected.x, reflected.y );
  225. point.set( numbers[ j + 0 ], numbers[ j + 1 ] );
  226. transfVec2( point, currentTransform, transfPoint );
  227. setFirstPoint();
  228. path.quadraticCurveTo(
  229. reflected.x,
  230. reflected.y,
  231. transfPoint.x,
  232. transfPoint.y
  233. );
  234. }
  235. break;
  236. case 'A':
  237. var numbers = parseFloats( data );
  238. for ( var j = 0, jl = numbers.length; j < jl; j += 7 ) {
  239. var transfStart = transfPoint.clone();
  240. point.set( numbers[ j + 5 ], numbers[ j + 6 ] );
  241. control.x = point.x;
  242. control.y = point.y;
  243. transfVec2( point, currentTransform, transfPoint );
  244. setFirstPoint();
  245. var rx = numbers[ j + 0 ] * getTransformScaleX( currentTransform );
  246. var ry = numbers[ j + 1 ] * getTransformScaleX( currentTransform );
  247. if ( isTransformRotated( currentTransform ) && rx !== ry ) {
  248. console.warn( "SVGLoader: Elliptic arc rotation or skewing is not implemented." );
  249. }
  250. parseArcCommand(
  251. path,
  252. rx,
  253. ry,
  254. numbers[ j + 2 ],
  255. numbers[ j + 3 ],
  256. numbers[ j + 4 ],
  257. transfStart,
  258. transfPoint
  259. );
  260. }
  261. break;
  262. //
  263. case 'm':
  264. var numbers = parseFloats( data );
  265. for ( var j = 0, jl = numbers.length; j < jl; j += 2 ) {
  266. point.x += numbers[ j + 0 ];
  267. point.y += numbers[ j + 1 ];
  268. control.x = point.x;
  269. control.y = point.y;
  270. transfVec2( point, currentTransform, transfPoint );
  271. transfVec2( control, currentTransform, transfControl );
  272. setFirstPoint();
  273. if ( j === 0 ) {
  274. path.moveTo( transfPoint.x, transfPoint.y );
  275. }
  276. else {
  277. path.lineTo( transfPoint.x, transfPoint.y );
  278. }
  279. }
  280. break;
  281. case 'h':
  282. var numbers = parseFloats( data );
  283. for ( var j = 0, jl = numbers.length; j < jl; j ++ ) {
  284. point.x += numbers[ j ];
  285. control.x = point.x;
  286. control.y = point.y;
  287. transfVec2( point, currentTransform, transfPoint );
  288. transfVec2( control, currentTransform, transfControl );
  289. setFirstPoint();
  290. path.lineTo( transfPoint.x, transfPoint.y );
  291. }
  292. break;
  293. case 'v':
  294. var numbers = parseFloats( data );
  295. for ( var j = 0, jl = numbers.length; j < jl; j ++ ) {
  296. point.y += numbers[ j ];
  297. control.x = point.x;
  298. control.y = point.y;
  299. transfVec2( point, currentTransform, transfPoint );
  300. transfVec2( control, currentTransform, transfControl );
  301. setFirstPoint();
  302. path.lineTo( transfPoint.x, transfPoint.y );
  303. }
  304. break;
  305. case 'l':
  306. var numbers = parseFloats( data );
  307. for ( var j = 0, jl = numbers.length; j < jl; j += 2 ) {
  308. point.x += numbers[ j + 0 ];
  309. point.y += numbers[ j + 1 ];
  310. control.x = point.x;
  311. control.y = point.y;
  312. transfVec2( point, currentTransform, transfPoint );
  313. transfVec2( control, currentTransform, transfControl );
  314. setFirstPoint();
  315. path.lineTo( transfPoint.x, transfPoint.y );
  316. }
  317. break;
  318. case 'c':
  319. var numbers = parseFloats( data );
  320. for ( var j = 0, jl = numbers.length; j < jl; j += 6 ) {
  321. control2.set( point.x + numbers[ j + 0 ], point.y + numbers[ j + 1 ] );
  322. control.set( point.x + numbers[ j + 2 ], point.y + numbers[ j + 3 ] );
  323. point.x += numbers[ j + 4 ];
  324. point.y += numbers[ j + 5 ];
  325. transfVec2( point, currentTransform, transfPoint );
  326. transfVec2( control, currentTransform, transfControl );
  327. transfVec2( control2, currentTransform, transfControl2 );
  328. setFirstPoint();
  329. path.bezierCurveTo(
  330. transfControl2.x,
  331. transfControl2.y,
  332. transfControl.x,
  333. transfControl.y,
  334. transfPoint.x,
  335. transfPoint.y
  336. );
  337. }
  338. break;
  339. case 's':
  340. var numbers = parseFloats( data );
  341. for ( var j = 0, jl = numbers.length; j < jl; j += 4 ) {
  342. reflected.set(
  343. getReflection( transfPoint.x, transfControl.x ),
  344. getReflection( transfPoint.y, transfControl.y )
  345. );
  346. control.set( point.x + numbers[ j + 0 ], point.y + numbers[ j + 1 ] );
  347. point.x += numbers[ j + 2 ];
  348. point.y += numbers[ j + 3 ];
  349. transfVec2( point, currentTransform, transfPoint );
  350. transfVec2( control, currentTransform, transfControl );
  351. setFirstPoint();
  352. path.bezierCurveTo(
  353. reflected.x,
  354. reflected.y,
  355. transfControl.x,
  356. transfControl.y,
  357. transfPoint.x,
  358. transfPoint.y
  359. );
  360. }
  361. break;
  362. case 'q':
  363. var numbers = parseFloats( data );
  364. for ( var j = 0, jl = numbers.length; j < jl; j += 4 ) {
  365. control.set( point.x + numbers[ j + 0 ], point.y + numbers[ j + 1 ] );
  366. point.x += numbers[ j + 2 ];
  367. point.y += numbers[ j + 3 ];
  368. transfVec2( point, currentTransform, transfPoint );
  369. transfVec2( control, currentTransform, transfControl );
  370. setFirstPoint();
  371. path.quadraticCurveTo(
  372. transfControl.x,
  373. transfControl.y,
  374. transfPoint.x,
  375. transfPoint.y
  376. );
  377. }
  378. break;
  379. case 't':
  380. var numbers = parseFloats( data );
  381. for ( var j = 0, jl = numbers.length; j < jl; j += 2 ) {
  382. reflected.set(
  383. getReflection( transfPoint.x, transfControl.x ),
  384. getReflection( transfPoint.y, transfControl.y )
  385. );
  386. control.set( reflected.x, reflected.y );
  387. point.x += numbers[ j + 0 ];
  388. point.y += numbers[ j + 1 ];
  389. transfVec2( point, currentTransform, transfPoint );
  390. setFirstPoint();
  391. path.quadraticCurveTo(
  392. reflected.x,
  393. reflected.y,
  394. transfPoint.x,
  395. transfPoint.y
  396. );
  397. }
  398. break;
  399. case 'a':
  400. var numbers = parseFloats( data );
  401. for ( var j = 0, jl = numbers.length; j < jl; j += 7 ) {
  402. var transfStart = transfPoint.clone();
  403. point.x += numbers[ j + 5 ];
  404. point.y += numbers[ j + 6 ];
  405. control.x = point.x;
  406. control.y = point.y;
  407. transfVec2( point, currentTransform, transfPoint );
  408. setFirstPoint();
  409. var rx = numbers[ j + 0 ] * getTransformScaleX( currentTransform );
  410. var ry = numbers[ j + 1 ] * getTransformScaleX( currentTransform );
  411. if ( isTransformRotated( currentTransform ) && rx !== ry ) {
  412. console.warn( "SVGLoader: Elliptic arc rotation or skewing is not implemented." );
  413. }
  414. parseArcCommand(
  415. path,
  416. rx,
  417. ry,
  418. numbers[ j + 2 ],
  419. numbers[ j + 3 ],
  420. numbers[ j + 4 ],
  421. transfStart,
  422. transfPoint
  423. );
  424. }
  425. break;
  426. //
  427. case 'Z':
  428. case 'z':
  429. path.currentPath.autoClose = true;
  430. if ( path.currentPath.curves.length > 0 ) {
  431. // Reset point to beginning of Path
  432. point.x = firstPoint.x;
  433. point.y = firstPoint.y;
  434. transfVec2( point, currentTransform, transfPoint );
  435. path.currentPath.currentPoint.copy( transfPoint );
  436. isFirstPoint = true;
  437. }
  438. break;
  439. default:
  440. console.warn( command );
  441. }
  442. // console.log( type, parseFloats( data ), parseFloats( data ).length )
  443. }
  444. return path;
  445. }
  446. /**
  447. * https://www.w3.org/TR/SVG/implnote.html#ArcImplementationNotes
  448. * https://mortoray.com/2017/02/16/rendering-an-svg-elliptical-arc-as-bezier-curves/ Appendix: Endpoint to center arc conversion
  449. * From
  450. * rx ry x-axis-rotation large-arc-flag sweep-flag x y
  451. * To
  452. * aX, aY, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise, aRotation
  453. */
  454. function parseArcCommand( path, rx, ry, x_axis_rotation, large_arc_flag, sweep_flag, start, end ) {
  455. x_axis_rotation = x_axis_rotation * Math.PI / 180;
  456. // Ensure radii are positive
  457. rx = Math.abs( rx );
  458. ry = Math.abs( ry );
  459. // Compute (x1′, y1′)
  460. var dx2 = ( start.x - end.x ) / 2.0;
  461. var dy2 = ( start.y - end.y ) / 2.0;
  462. var x1p = Math.cos( x_axis_rotation ) * dx2 + Math.sin( x_axis_rotation ) * dy2;
  463. var y1p = - Math.sin( x_axis_rotation ) * dx2 + Math.cos( x_axis_rotation ) * dy2;
  464. // Compute (cx′, cy′)
  465. var rxs = rx * rx;
  466. var rys = ry * ry;
  467. var x1ps = x1p * x1p;
  468. var y1ps = y1p * y1p;
  469. // Ensure radii are large enough
  470. var cr = x1ps / rxs + y1ps / rys;
  471. if ( cr > 1 ) {
  472. // scale up rx,ry equally so cr == 1
  473. var s = Math.sqrt( cr );
  474. rx = s * rx;
  475. ry = s * ry;
  476. rxs = rx * rx;
  477. rys = ry * ry;
  478. }
  479. var dq = ( rxs * y1ps + rys * x1ps );
  480. var pq = ( rxs * rys - dq ) / dq;
  481. var q = Math.sqrt( Math.max( 0, pq ) );
  482. if ( large_arc_flag === sweep_flag ) q = - q;
  483. var cxp = q * rx * y1p / ry;
  484. var cyp = - q * ry * x1p / rx;
  485. // Step 3: Compute (cx, cy) from (cx′, cy′)
  486. var cx = Math.cos( x_axis_rotation ) * cxp - Math.sin( x_axis_rotation ) * cyp + ( start.x + end.x ) / 2;
  487. var cy = Math.sin( x_axis_rotation ) * cxp + Math.cos( x_axis_rotation ) * cyp + ( start.y + end.y ) / 2;
  488. // Step 4: Compute θ1 and Δθ
  489. var theta = svgAngle( 1, 0, ( x1p - cxp ) / rx, ( y1p - cyp ) / ry );
  490. var delta = svgAngle( ( x1p - cxp ) / rx, ( y1p - cyp ) / ry, ( - x1p - cxp ) / rx, ( - y1p - cyp ) / ry ) % ( Math.PI * 2 );
  491. path.currentPath.absellipse( cx, cy, rx, ry, theta, theta + delta, sweep_flag === 0, x_axis_rotation );
  492. }
  493. function svgAngle( ux, uy, vx, vy ) {
  494. var dot = ux * vx + uy * vy;
  495. var len = Math.sqrt( ux * ux + uy * uy ) * Math.sqrt( vx * vx + vy * vy );
  496. var ang = Math.acos( Math.max( -1, Math.min( 1, dot / len ) ) ); // floating point precision, slightly over values appear
  497. if ( ( ux * vy - uy * vx ) < 0 ) ang = - ang;
  498. return ang;
  499. }
  500. /*
  501. * According to https://www.w3.org/TR/SVG/shapes.html#RectElementRXAttribute
  502. * rounded corner should be rendered to elliptical arc, but bezier curve does the job well enough
  503. */
  504. function parseRectNode( node, style ) {
  505. var x = parseFloat( node.getAttribute( 'x' ) || 0 );
  506. var y = parseFloat( node.getAttribute( 'y' ) || 0 );
  507. var rx = parseFloat( node.getAttribute( 'rx' ) || 0 );
  508. var ry = parseFloat( node.getAttribute( 'ry' ) || 0 );
  509. var w = parseFloat( node.getAttribute( 'width' ) );
  510. var h = parseFloat( node.getAttribute( 'height' ) );
  511. var path = new THREE.ShapePath();
  512. path.color.setStyle( style.fill );
  513. tempVecA.set( x + 2 * rx, y );
  514. transfVec2( tempVecA, currentTransform, tempVecB );
  515. path.moveTo( tempVecB.x, tempVecB.y );
  516. tempVecA.set( x + w - 2 * rx, y );
  517. transfVec2( tempVecA, currentTransform, tempVecB );
  518. path.lineTo( tempVecB.x, tempVecB.y );
  519. if ( rx !== 0 || ry !== 0 ) {
  520. tempVecA.set( x + w, y );
  521. transfVec2( tempVecA, currentTransform, tempVecB );
  522. tempVecA.set( x + w, y + 2 * ry );
  523. transfVec2( tempVecA, currentTransform, tempVecC );
  524. path.bezierCurveTo( tempVecB.x, tempVecB.y, tempVecB.x, tempVecB.y, tempVecC.x, tempVecC.y );
  525. }
  526. tempVecA.set( x + w, y + h - 2 * ry );
  527. transfVec2( tempVecA, currentTransform, tempVecB );
  528. path.lineTo( tempVecB.x, tempVecB.y );
  529. if ( rx !== 0 || ry !== 0 ) {
  530. tempVecA.set( x + w, y + h );
  531. transfVec2( tempVecA, currentTransform, tempVecB );
  532. tempVecA.set( x + w - 2 * rx, y + h );
  533. transfVec2( tempVecA, currentTransform, tempVecC );
  534. path.bezierCurveTo( tempVecB.x, tempVecB.y, tempVecB.x, tempVecB.y, tempVecC.x, tempVecC.y );
  535. }
  536. tempVecA.set( x + 2 * rx, y + h );
  537. transfVec2( tempVecA, currentTransform, tempVecB );
  538. path.lineTo( tempVecB.x, tempVecB.y );
  539. if ( rx !== 0 || ry !== 0 ) {
  540. tempVecA.set( x, y + h );
  541. transfVec2( tempVecA, currentTransform, tempVecB );
  542. tempVecA.set( x, y + h - 2 * ry );
  543. transfVec2( tempVecA, currentTransform, tempVecC );
  544. path.bezierCurveTo( tempVecB.x, tempVecB.y, tempVecB.x, tempVecB.y, tempVecC.x, tempVecC.y );
  545. }
  546. tempVecA.set( x, y + 2 * ry );
  547. transfVec2( tempVecA, currentTransform, tempVecB );
  548. path.lineTo( tempVecB.x, tempVecB.y );
  549. if ( rx !== 0 || ry !== 0 ) {
  550. tempVecA.set( x, y );
  551. transfVec2( tempVecA, currentTransform, tempVecB );
  552. tempVecA.set( x + 2 * rx, y );
  553. transfVec2( tempVecA, currentTransform, tempVecC );
  554. path.bezierCurveTo( tempVecB.x, tempVecB.y, tempVecB.x, tempVecB.y, tempVecC.x, tempVecC.y );
  555. }
  556. return path;
  557. }
  558. function parsePolygonNode( node, style ) {
  559. function iterator( match, a, b ) {
  560. var x = parseFloat( a );
  561. var y = parseFloat( b );
  562. tempVecA.set( x, y );
  563. transfVec2( tempVecA, currentTransform, tempVecB );
  564. x = tempVecB.x;
  565. y = tempVecB.y;
  566. if ( index === 0 ) {
  567. path.moveTo( x, y );
  568. } else {
  569. path.lineTo( x, y );
  570. }
  571. index ++;
  572. }
  573. var regex = /(-?[\d\.?]+)[,|\s](-?[\d\.?]+)/g;
  574. var path = new THREE.ShapePath();
  575. path.color.setStyle( style.fill );
  576. var index = 0;
  577. node.getAttribute( 'points' ).replace(regex, iterator);
  578. path.currentPath.autoClose = true;
  579. return path;
  580. }
  581. function parsePolylineNode( node, style ) {
  582. function iterator( match, a, b ) {
  583. var x = parseFloat( a );
  584. var y = parseFloat( b );
  585. tempVecA.set( x, y );
  586. transfVec2( tempVecA, currentTransform, tempVecB );
  587. x = tempVecB.x;
  588. y = tempVecB.y;
  589. if ( index === 0 ) {
  590. path.moveTo( x, y );
  591. } else {
  592. path.lineTo( x, y );
  593. }
  594. index ++;
  595. }
  596. var regex = /(-?[\d\.?]+)[,|\s](-?[\d\.?]+)/g;
  597. var path = new THREE.ShapePath();
  598. path.color.setStyle( style.fill );
  599. var index = 0;
  600. node.getAttribute( 'points' ).replace(regex, iterator);
  601. path.currentPath.autoClose = false;
  602. return path;
  603. }
  604. function parseCircleNode( node, style ) {
  605. var x = parseFloat( node.getAttribute( 'cx' ) );
  606. var y = parseFloat( node.getAttribute( 'cy' ) );
  607. var r = parseFloat( node.getAttribute( 'r' ) );
  608. // Transform of center
  609. tempVecA.set( x, y );
  610. transfVec2( tempVecA, currentTransform, tempVecB );
  611. x = tempVecB.x;
  612. y = tempVecB.y;
  613. // Scale
  614. var scaleX = getTransformScaleX( currentTransform );
  615. var scaleY = getTransformScaleY( currentTransform );
  616. var subpath = new THREE.Path();
  617. console.warn( "AMEM: " + scaleX + ", " + scaleY );
  618. if ( Math.abs( scaleX - scaleY ) > 0.00000001 ) {
  619. // Circle scaled differently in x and y becomes an ellipse
  620. // Rotation
  621. if ( isTransformRotated( currentTransform ) ) {
  622. console.warn( "SVGLoader: Circle rotation in conjuction with non uniform scaling is not implemented." );
  623. }
  624. subpath.absellipse( x, y, r * scaleX, r * scaleY, 0, Math.PI * 2 );
  625. }
  626. else {
  627. subpath.absarc( x, y, r, 0, Math.PI * 2 );
  628. }
  629. var path = new THREE.ShapePath();
  630. path.color.setStyle( style.fill );
  631. path.subPaths.push( subpath );
  632. return path;
  633. }
  634. function parseEllipseNode( node, style ) {
  635. var x = parseFloat( node.getAttribute( 'cx' ) );
  636. var y = parseFloat( node.getAttribute( 'cy' ) );
  637. var rx = parseFloat( node.getAttribute( 'rx' ) );
  638. var ry = parseFloat( node.getAttribute( 'ry' ) );
  639. // Transform of center
  640. tempVecA.set( x, y );
  641. transfVec2( tempVecA, currentTransform, tempVecB );
  642. x = tempVecB.x;
  643. y = tempVecB.y;
  644. // Rotation
  645. if ( isTransformRotated( currentTransform ) ) {
  646. console.warn( "SVGLoader: Ellipse rotation or skewing is not implemented." );
  647. }
  648. // Scale
  649. rx *= getTransformScaleX( currentTransform );
  650. ry *= getTransformScaleY( currentTransform );
  651. var subpath = new THREE.Path();
  652. subpath.absellipse( x, y, rx, ry, 0, Math.PI * 2 );
  653. var path = new THREE.ShapePath();
  654. path.color.setStyle( style.fill );
  655. path.subPaths.push( subpath );
  656. return path;
  657. }
  658. function parseLineNode( node, style ) {
  659. var x1 = parseFloat( node.getAttribute( 'x1' ) );
  660. var y1 = parseFloat( node.getAttribute( 'y1' ) );
  661. var x2 = parseFloat( node.getAttribute( 'x2' ) );
  662. var y2 = parseFloat( node.getAttribute( 'y2' ) );
  663. tempVecA.set( x1, y1 );
  664. transfVec2( tempVecA, currentTransform, tempVecB );
  665. x1 = tempVecB.x;
  666. y1 = tempVecB.y;
  667. tempVecA.set( x2, y2 );
  668. transfVec2( tempVecA, currentTransform, tempVecB );
  669. x2 = tempVecB.x;
  670. y2 = tempVecB.y;
  671. var path = new THREE.ShapePath();
  672. path.moveTo( x1, y1 );
  673. path.lineTo( x2, y2 );
  674. path.currentPath.autoClose = false;
  675. return path;
  676. }
  677. //
  678. function parseStyle( node, style ) {
  679. style = Object.assign( {}, style ); // clone style
  680. if ( node.hasAttribute( 'fill' ) ) style.fill = node.getAttribute( 'fill' );
  681. if ( node.style.fill !== '' && node.style.fill !== 'none' ) style.fill = node.style.fill;
  682. if ( ! isVisible( style ) ) {
  683. if ( node.hasAttribute( 'stroke' ) ) style.fill = node.getAttribute( 'stroke' );
  684. if ( node.style.stroke !== '' && node.style.stroke !== 'none' ) style.fill = node.style.stroke;
  685. }
  686. return style;
  687. }
  688. function isVisible( style ) {
  689. return ( style.fill !== 'none' && style.fill !== 'transparent' ) || ( style.stroke !== 'none' && style.stroke !== 'transparent' );
  690. }
  691. // http://www.w3.org/TR/SVG11/implnote.html#PathElementImplementationNotes
  692. function getReflection( a, b ) {
  693. return a - ( b - a );
  694. }
  695. function parseFloats( string ) {
  696. var array = string.split( /[\s,]+|(?=\s?[+\-])/ );
  697. for ( var i = 0; i < array.length; i ++ ) {
  698. var number = array[ i ];
  699. // Handle values like 48.6037.7.8
  700. // TODO Find a regex for this
  701. if ( number.indexOf( '.' ) !== number.lastIndexOf( '.' ) ) {
  702. var split = number.split( '.' );
  703. for ( var s = 2; s < split.length; s ++ ) {
  704. array.splice( i + s - 1, 0, '0.' + split[ s ] );
  705. }
  706. }
  707. array[ i ] = parseFloat( number );
  708. }
  709. return array;
  710. }
  711. function parseTransformNode( node ) {
  712. var transformAttr = node.getAttribute( 'transform' );
  713. var transform = null;
  714. var openParPos = transformAttr.indexOf( "(" );
  715. var closeParPos = transformAttr.indexOf( ")" );
  716. if ( openParPos > 0 && openParPos < closeParPos ) {
  717. var transformType = transformAttr.substr( 0, openParPos );
  718. var array = parseFloats( transformAttr.substr( openParPos + 1, closeParPos - openParPos - 1 ) );
  719. switch ( transformType ) {
  720. case "translate":
  721. if ( array.length >= 1 ) {
  722. transform = createIdTransform();
  723. // Translation X
  724. transform[ 4 ] = array[ 0 ];
  725. }
  726. if ( array.length >= 2 ) {
  727. // Translation Y
  728. transform[ 5 ] = array[ 1 ];
  729. }
  730. break;
  731. case "rotate":
  732. if ( array.length >= 1 ) {
  733. var angle = 0;
  734. var cx = 0;
  735. var cy = 0;
  736. transform = createIdTransform();
  737. // Angle
  738. angle = array[ 0 ] * Math.PI / 180;
  739. if ( array.length >= 3 ) {
  740. // Center x, y
  741. cx = array[ 1 ];
  742. cy = array[ 2 ];
  743. }
  744. // Rotate around center (cx, cy)
  745. var traslation = createIdTransform();
  746. traslation[ 4 ] = -cx;
  747. traslation[ 5 ] = -cy;
  748. var rotation = createRotationTransform( angle );
  749. var traslRot = createIdTransform();
  750. multiplyTransforms( traslation, rotation, traslRot );
  751. traslation[ 4 ] = cx;
  752. traslation[ 5 ] = cy;
  753. multiplyTransforms( traslRot, traslation, transform );
  754. }
  755. break;
  756. case "scale":
  757. if ( array.length >= 1 ) {
  758. transform = createIdTransform();
  759. var scaleX = array[ 0 ];
  760. var scaleY = scaleX;
  761. if ( array.length >= 2 ) {
  762. scaleY = array[ 1 ];
  763. }
  764. transform[ 0 ] = scaleX;
  765. transform[ 3 ] = scaleY;
  766. }
  767. break;
  768. case "matrix":
  769. if ( array.length === 6 ) {
  770. transform = createIdTransform();
  771. copyTransform( array, transform );
  772. }
  773. break;
  774. }
  775. }
  776. return transform;
  777. }
  778. function createIdTransform( m ) {
  779. // 2 x 3 matrix:
  780. // 1 0
  781. // 0 1
  782. // tx ty
  783. if ( ! m ) {
  784. m = [];
  785. }
  786. m[ 0 ] = 1;
  787. m[ 1 ] = 0;
  788. m[ 2 ] = 0;
  789. m[ 3 ] = 1;
  790. m[ 4 ] = 0;
  791. m[ 5 ] = 0;
  792. return m;
  793. }
  794. function copyTransform( orig, dest ) {
  795. for ( var i = 0; i < 6; i++ ) {
  796. dest[ i ] = orig[ i ]
  797. }
  798. }
  799. function transfVec2( v, m, r ) {
  800. r.x = v.x * m[ 0 ] + v.y * m[ 2 ] + m[ 4 ];
  801. r.y = v.x * m[ 1 ] + v.y * m[ 3 ] + m[ 5 ];
  802. }
  803. function multiplyTransforms( a, b, r ) {
  804. r[ 0 ] = a[ 0 ] * b[ 0 ] + a[ 1 ] * b[ 2 ];
  805. r[ 1 ] = a[ 0 ] * b[ 1 ] + a[ 1 ] * b[ 3 ];
  806. r[ 2 ] = a[ 2 ] * b[ 0 ] + a[ 3 ] * b[ 2 ];
  807. r[ 3 ] = a[ 2 ] * b[ 1 ] + a[ 3 ] * b[ 3 ];
  808. r[ 4 ] = a[ 4 ] * b[ 0 ] + a[ 5 ] * b[ 2 ] + b[ 4 ];
  809. r[ 5 ] = a[ 4 ] * b[ 1 ] + a[ 5 ] * b[ 3 ] + b[ 5 ];
  810. }
  811. function isTransformRotated( m ) {
  812. return m[ 1 ] !== 0 || m[ 2 ] !== 0;
  813. }
  814. function getTransformScaleX( m ) {
  815. return Math.sqrt( m[ 0 ] * m[ 0 ] + m[ 1 ] * m[ 1 ] )
  816. }
  817. function getTransformScaleY( m ) {
  818. return Math.sqrt( m[ 2 ] * m[ 2 ] + m[ 3 ] * m[ 3 ] )
  819. }
  820. function createRotationTransform( angle ) {
  821. var rotation = createIdTransform();
  822. rotation[ 0 ] = Math.cos( angle );
  823. rotation[ 1 ] = Math.sin( angle );
  824. rotation[ 2 ] = -Math.sin( angle );
  825. rotation[ 3 ] = Math.cos( angle );
  826. return rotation;
  827. }
  828. //
  829. console.log( 'THREE.SVGLoader' );
  830. var paths = [];
  831. var transformStack = [];
  832. var currentTransform = createIdTransform();
  833. var tempTransform = createIdTransform();
  834. var tempVecA = new THREE.Vector2();
  835. var tempVecB = new THREE.Vector2();
  836. var tempVecC = new THREE.Vector2();
  837. console.time( 'THREE.SVGLoader: DOMParser' );
  838. var xml = new DOMParser().parseFromString( text, 'image/svg+xml' ); // application/xml
  839. console.timeEnd( 'THREE.SVGLoader: DOMParser' );
  840. console.time( 'THREE.SVGLoader: Parse' );
  841. parseNode( xml.documentElement, { fill: '#000' } );
  842. // console.log( paths );
  843. console.timeEnd( 'THREE.SVGLoader: Parse' );
  844. return paths;
  845. }
  846. };