Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SVGLoader: Added support for multi data in some commands. #13878

Merged
merged 1 commit into from
Apr 17, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 96 additions & 60 deletions examples/js/loaders/SVGLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ THREE.SVGLoader.prototype = {

case 'path':
style = parseStyle( node, style );
if ( style.fill !== 'none' ) paths.push( parsePathNode( node, style ) );
if ( style.fill !== 'none' && node.hasAttribute( 'd' ) ) paths.push( parsePathNode( node, style ) );
break;

case 'rect':
Expand Down Expand Up @@ -105,7 +105,7 @@ THREE.SVGLoader.prototype = {

var commands = d.match( /[a-df-z][^a-df-z]*/ig );

for ( var i = 0; i < commands.length; i ++ ) {
for ( var i = 0, l = commands.length; i < l; i ++ ) {

var command = commands[ i ];

Expand All @@ -116,51 +116,62 @@ THREE.SVGLoader.prototype = {

case 'M':
var numbers = parseFloats( data );
point.fromArray( numbers );
control.x = point.x;
control.y = point.y;
path.moveTo( point.x, point.y );
for ( var j = 0, jl = numbers.length; j < jl; j += 2 ) {
point.x = numbers[ j + 0 ];
point.y = numbers[ j + 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 );
for ( var j = 0, jl = numbers.length; j < jl; j ++ ) {
point.x = numbers[ j ];
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 );
for ( var j = 0, jl = numbers.length; j < jl; j ++ ) {
point.y = numbers[ j ];
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 );
for ( var j = 0, jl = numbers.length; j < jl; j += 2 ) {
point.x = numbers[ j + 0 ];
point.y = numbers[ j + 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 ];
for ( var j = 0, jl = numbers.length; j < jl; j += 6 ) {
path.bezierCurveTo(
numbers[ j + 0 ],
numbers[ j + 1 ],
numbers[ j + 2 ],
numbers[ j + 3 ],
numbers[ j + 4 ],
numbers[ j + 5 ]
);
control.x = numbers[ j + 2 ];
control.y = numbers[ j + 3 ];
point.x = numbers[ j + 4 ];
point.y = numbers[ j + 5 ];
}
break;

case 'S':
Expand Down Expand Up @@ -213,50 +224,60 @@ THREE.SVGLoader.prototype = {

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 );
for ( var j = 0, jl = numbers.length; j < jl; j += 2 ) {
point.x += numbers[ j + 0 ];
point.y += numbers[ j + 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 );
for ( var j = 0, jl = numbers.length; j < jl; j ++ ) {
point.x += numbers[ j ];
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 );
for ( var j = 0, jl = numbers.length; j < jl; j ++ ) {
point.y += numbers[ j ];
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 );
for ( var j = 0, jl = numbers.length; j < jl; j += 2 ) {
point.x += numbers[ j + 0 ];
point.y += numbers[ j + 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 ];
for ( var j = 0, jl = numbers.length; j < jl; j += 6 ) {
path.bezierCurveTo(
point.x + numbers[ j + 0 ],
point.y + numbers[ j + 1 ],
point.x + numbers[ j + 2 ],
point.y + numbers[ j + 3 ],
point.x + numbers[ j + 4 ],
point.y + numbers[ j + 5 ]
);
point.x += numbers[ j + 4 ];
point.y += numbers[ j + 5 ];
}
break;

case 's':
Expand Down Expand Up @@ -315,10 +336,12 @@ THREE.SVGLoader.prototype = {
break;

default:
console.log( command );
console.warn( command );

}

// console.log( type, parseFloats( data ), parseFloats( data ).length )

}

return path;
Expand Down Expand Up @@ -511,11 +534,24 @@ THREE.SVGLoader.prototype = {

//

console.log( 'THREE.SVGLoader' );

var paths = [];

console.time( 'THREE.SVGLoader: DOMParser' );

var xml = new DOMParser().parseFromString( text, 'image/svg+xml' ); // application/xml

console.timeEnd( 'THREE.SVGLoader: DOMParser' );

console.time( 'THREE.SVGLoader: Parse' );

parseNode( xml.documentElement, { fill: '#000' } );

// console.log( paths );

console.timeEnd( 'THREE.SVGLoader: Parse' );

return paths;

}
Expand Down