123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507 |
- /**
- * @author mrdoob / http://mrdoob.com/
- * @author zz85 / http://joshuakoo.com/
- */
- THREE.SVGLoader = function ( manager ) {
- this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
- };
- THREE.SVGLoader.prototype = {
- constructor: THREE.SVGLoader,
- load: function ( url, onLoad, onProgress, onError ) {
- var scope = this;
- var loader = new THREE.FileLoader( scope.manager );
- loader.load( url, function ( text ) {
- onLoad( scope.parse( text ) );
- }, onProgress, onError );
- },
- parse: function ( text ) {
- function parseNode( node, style ) {
- if ( node.nodeType !== 1 ) return;
- switch ( node.nodeName ) {
- case 'svg':
- break;
- case 'g':
- style = parseStyle( node, style );
- break;
- case 'path':
- style = parseStyle( node, style );
- if ( style.fill !== 'none' ) paths.push( parsePathNode( node, style ) );
- break;
- case 'rect':
- style = parseStyle( node, style );
- if ( style.fill !== 'none' ) paths.push( parseRectNode( node, style ) );
- break;
- case 'polygon':
- style = parseStyle( node, style );
- if ( style.fill !== 'none' ) paths.push( parsePolygonNode( node, style ) );
- break;
- case 'polyline':
- style = parseStyle( node, style );
- if ( style.fill !== 'none' ) paths.push( parsePolylineNode( node, style ) );
- break;
- case 'circle':
- style = parseStyle( node, style );
- if ( style.fill !== 'none' ) paths.push( parseCircleNode( node, style ) );
- break;
- case 'ellipse':
- style = parseStyle( node, style );
- if ( style.fill !== 'none' ) paths.push( parseEllipseNode( node, style ) );
- break;
- case 'line':
- style = parseStyle( node, style );
- if ( style.fill !== 'none' ) paths.push( parseLineNode( node, style ) );
- break;
- default:
- console.log( node );
- }
- var nodes = node.childNodes;
- for ( var i = 0; i < nodes.length; i ++ ) {
- parseNode( nodes[ i ], style );
- }
- }
- function parsePathNode( node, style ) {
- var path = new THREE.ShapePath();
- path.color.setStyle( style.fill );
- var point = new THREE.Vector2();
- var control = new THREE.Vector2();
- var d = node.getAttribute( 'd' );
- // console.log( d );
- var commands = d.match( /[a-df-z][^a-df-z]*/ig );
- for ( var i = 0; i < commands.length; i ++ ) {
- var command = commands[ i ];
- var type = command.charAt( 0 );
- var data = command.substr( 1 ).trim();
- switch ( type ) {
- case 'M':
- var numbers = parseFloats( data );
- point.fromArray( numbers );
- control.x = point.x;
- control.y = point.y;
- path.moveTo( point.x, point.y );
- break;
- case 'H':
- var numbers = parseFloats( data );
- point.x = numbers[ 0 ];
- control.x = point.x;
- control.y = point.y;
- path.lineTo( point.x, point.y );
- break;
- case 'V':
- var numbers = parseFloats( data );
- point.y = numbers[ 0 ];
- control.x = point.x;
- control.y = point.y;
- path.lineTo( point.x, point.y );
- break;
- case 'L':
- var numbers = parseFloats( data );
- point.x = numbers[ 0 ];
- point.y = numbers[ 1 ];
- control.x = point.x;
- control.y = point.y;
- path.lineTo( point.x, point.y );
- break;
- case 'C':
- var numbers = parseFloats( data );
- path.bezierCurveTo(
- numbers[ 0 ],
- numbers[ 1 ],
- numbers[ 2 ],
- numbers[ 3 ],
- numbers[ 4 ],
- numbers[ 5 ]
- );
- control.x = numbers[ 2 ];
- control.y = numbers[ 3 ];
- point.x = numbers[ 4 ];
- point.y = numbers[ 5 ];
- break;
- case 'S':
- var numbers = parseFloats( data );
- path.bezierCurveTo(
- getReflection( point.x, control.x ),
- getReflection( point.y, control.y ),
- numbers[ 0 ],
- numbers[ 1 ],
- numbers[ 2 ],
- numbers[ 3 ]
- );
- control.x = numbers[ 0 ];
- control.y = numbers[ 1 ];
- point.x = numbers[ 2 ];
- point.y = numbers[ 3 ];
- break;
- case 'Q':
- var numbers = parseFloats( data );
- path.quadraticCurveTo(
- numbers[ 0 ],
- numbers[ 1 ],
- numbers[ 2 ],
- numbers[ 3 ]
- );
- control.x = numbers[ 0 ];
- control.y = numbers[ 1 ];
- point.x = numbers[ 2 ];
- point.y = numbers[ 3 ];
- break;
- case 'T':
- var numbers = parseFloats( data );
- var rx = getReflection( point.x, control.x );
- var ry = getReflection( point.y, control.y );
- path.quadraticCurveTo(
- rx,
- ry,
- numbers[ 0 ],
- numbers[ 1 ]
- );
- control.x = rx;
- control.y = ry;
- point.x = numbers[ 0 ];
- point.y = numbers[ 1 ];
- break;
- // case 'A': break;
- case 'm':
- var numbers = parseFloats( data );
- point.x += numbers[ 0 ];
- point.y += numbers[ 1 ];
- control.x = point.x;
- control.y = point.y;
- path.moveTo( point.x, point.y );
- break;
- case 'h':
- var numbers = parseFloats( data );
- point.x += numbers[ 0 ];
- control.x = point.x;
- control.y = point.y;
- path.lineTo( point.x, point.y );
- break;
- case 'v':
- var numbers = parseFloats( data );
- point.y += numbers[ 0 ];
- control.x = point.x;
- control.y = point.y;
- path.lineTo( point.x, point.y );
- break;
- case 'l':
- var numbers = parseFloats( data );
- point.x += numbers[ 0 ];
- point.y += numbers[ 1 ];
- control.x = point.x;
- control.y = point.y;
- path.lineTo( point.x, point.y );
- break;
- case 'c':
- var numbers = parseFloats( data );
- path.bezierCurveTo(
- point.x + numbers[ 0 ],
- point.y + numbers[ 1 ],
- point.x + numbers[ 2 ],
- point.y + numbers[ 3 ],
- point.x + numbers[ 4 ],
- point.y + numbers[ 5 ]
- );
- point.x += numbers[ 4 ];
- point.y += numbers[ 5 ];
- break;
- case 's':
- var numbers = parseFloats( data );
- path.bezierCurveTo(
- // TODO: Not sure if point needs
- // to be added to reflection...
- getReflection( point.x, control.x ),
- getReflection( point.y, control.y ),
- point.x + numbers[ 0 ],
- point.y + numbers[ 1 ],
- point.x + numbers[ 2 ],
- point.y + numbers[ 3 ]
- );
- control.x = point.x + numbers[ 0 ];
- control.y = point.y + numbers[ 1 ];
- point.x += numbers[ 2 ];
- point.y += numbers[ 3 ];
- break;
- case 'q':
- var numbers = parseFloats( data );
- path.quadraticCurveTo(
- point.x + numbers[ 0 ],
- point.y + numbers[ 1 ],
- point.x + numbers[ 2 ],
- point.y + numbers[ 3 ]
- );
- control.x = point.x + numbers[ 0 ];
- control.y = point.y + numbers[ 1 ];
- point.x += numbers[ 2 ];
- point.y += numbers[ 3 ];
- break;
- case 't':
- var numbers = parseFloats( data );
- var rx = getReflection( point.x, control.x );
- var ry = getReflection( point.y, control.y );
- path.quadraticCurveTo(
- rx,
- ry,
- point.x + numbers[ 0 ],
- point.y + numbers[ 1 ]
- );
- control.x = rx;
- control.y = ry;
- point.x = point.x + numbers[ 0 ];
- point.y = point.y + numbers[ 1 ];
- break;
- // case 'a': break;
- case 'Z':
- case 'z':
- path.currentPath.autoClose = true;
- break;
- default:
- console.log( command );
- }
- }
- return path;
- }
- function parseRectNode( node, style ) {
- var x = parseFloat( node.getAttribute( 'x' ) || 0 );
- var y = parseFloat( node.getAttribute( 'y' ) || 0 );
- var w = parseFloat( node.getAttribute( 'width' ) );
- var h = parseFloat( node.getAttribute( 'height' ) );
- var path = new THREE.ShapePath();
- path.color.setStyle( style.fill );
- path.moveTo( x, y );
- path.lineTo( x + w, y );
- path.lineTo( x + w, y + h );
- path.lineTo( x, y + h );
- return path;
- }
- function parsePolygonNode( node, style ) {
- function iterator( match, a, b ) {
- var x = parseFloat( a );
- var y = parseFloat( b );
- if ( index === 0 ) {
- path.moveTo( x, y );
- } else {
- path.lineTo( x, y );
- }
- index ++;
- }
- var regex = /(-?[\d\.?]+)[,|\s](-?[\d\.?]+)/g;
- var path = new THREE.ShapePath();
- path.color.setStyle( style.fill );
- var index = 0;
- node.getAttribute( 'points' ).replace(regex, iterator);
- path.currentPath.autoClose = true;
- return path;
- }
- function parsePolylineNode( node, style ) {
- function iterator( match, a, b ) {
- var x = parseFloat( a );
- var y = parseFloat( b );
- if ( index === 0 ) {
- path.moveTo( x, y );
- } else {
- path.lineTo( x, y );
- }
- index ++;
- }
- var regex = /(-?[\d\.?]+)[,|\s](-?[\d\.?]+)/g;
- var path = new THREE.ShapePath();
- path.color.setStyle( style.fill );
- var index = 0;
- node.getAttribute( 'points' ).replace(regex, iterator);
- path.currentPath.autoClose = false;
- return path;
- }
- function parseCircleNode( node, style ) {
- var x = parseFloat( node.getAttribute( 'cx' ) );
- var y = parseFloat( node.getAttribute( 'cy' ) );
- var r = parseFloat( node.getAttribute( 'r' ) );
- var subpath = new THREE.Path();
- subpath.absarc( x, y, r, 0, Math.PI * 2 );
- var path = new THREE.ShapePath();
- path.color.setStyle( style.fill );
- path.subPaths.push( subpath );
- return path;
- }
- function parseEllipseNode( node, style ) {
- var x = parseFloat( node.getAttribute( 'cx' ) );
- var y = parseFloat( node.getAttribute( 'cy' ) );
- var rx = parseFloat( node.getAttribute( 'rx' ) );
- var ry = parseFloat( node.getAttribute( 'ry' ) );
- var subpath = new THREE.Path();
- subpath.absellipse( x, y, rx, ry, 0, Math.PI * 2 );
- var path = new THREE.ShapePath();
- path.color.setStyle( style.fill );
- path.subPaths.push( subpath );
- return path;
- }
- function parseLineNode( node, style ) {
- var x1 = parseFloat( node.getAttribute( 'x1' ) );
- var y1 = parseFloat( node.getAttribute( 'y1' ) );
- var x2 = parseFloat( node.getAttribute( 'x2' ) );
- var y2 = parseFloat( node.getAttribute( 'y2' ) );
- var path = new THREE.ShapePath();
- path.moveTo( x1, y1 );
- path.lineTo( x2, y2 );
- path.currentPath.autoClose = false;
- return path;
- }
- //
- function parseStyle( node, style ) {
- style = Object.assign( {}, style ); // clone style
- if ( node.hasAttribute( 'fill' ) ) style.fill = node.getAttribute( 'fill' );
- if ( node.style.fill !== '' ) style.fill = node.style.fill;
- return style;
- }
- // http://www.w3.org/TR/SVG11/implnote.html#PathElementImplementationNotes
- function getReflection( a, b ) {
- return 2 * a - ( b - a );
- }
- function parseFloats( string ) {
- var array = string.split( /[\s,]+|(?=\s?[+\-])/ );
- for ( var i = 0; i < array.length; i ++ ) {
- array[ i ] = parseFloat( array[ i ] );
- }
- return array;
- }
- //
- var paths = [];
- var xml = new DOMParser().parseFromString( text, 'image/svg+xml' ); // application/xml
- parseNode( xml.documentElement, { fill: '#000' } );
- return paths;
- }
- };
|