Browse Source

SVGRenderer: let configure precision for SVG coordinates

While SVG is text format, 10-digits for each coordinate is overkill. In
many cases on can use precion=1 or even precision=0. For instance,
instead of 
path
d="M95.67875432291896,61.48774395485388L99.22967055503229,-179.38931463976644"

one gets (with precision 1)

path d="M95.7,61.5L99.2,-179.4"

This significantly (factor 2) reduces size of SVG when stored into the
file.
Sergey Linev 8 years ago
parent
commit
3422cb0560
1 changed files with 16 additions and 4 deletions
  1. 16 4
      examples/js/renderers/SVGRenderer.js

+ 16 - 4
examples/js/renderers/SVGRenderer.js

@@ -46,7 +46,7 @@ THREE.SVGRenderer = function () {
 
 	_svgPathPool = [],
 	_svgNode, _pathCount = 0, _currPath, _currStyle,
-	_quality = 1;
+	_quality = 1, _precision = -1;
 
 	this.domElement = _svg;
 
@@ -104,6 +104,12 @@ THREE.SVGRenderer = function () {
 
 	};
 
+	this.setPrecision = function ( precision ) {
+
+		_precision = precision;
+
+	};
+
 	function removeChildNodes() {
 
 		_pathCount = 0;
@@ -126,6 +132,12 @@ THREE.SVGRenderer = function () {
 
 	}
 
+	function convert ( c ) {
+
+		return _precision < 0 ? c : c.toFixed(_precision);
+
+	}
+
 	this.clear = function () {
 
 		removeChildNodes();
@@ -342,7 +354,7 @@ THREE.SVGRenderer = function () {
 			scaleY *= material.size;
 		}
 
-		var path = 'M' + ( v1.x - scaleX * 0.5 ) + ',' + ( v1.y - scaleY * 0.5 ) + 'h' + scaleX + 'v' + scaleY + 'h' + (-scaleX) + 'z';
+		var path = 'M' + convert( v1.x - scaleX * 0.5 ) + ',' + convert( v1.y - scaleY * 0.5 ) + 'h' + convert( scaleX ) + 'v' + convert( scaleY ) + 'h' + convert(-scaleX) + 'z';
 		var style = "";
 
 		if ( material.isSpriteMaterial || material.isPointsMaterial ) {
@@ -357,7 +369,7 @@ THREE.SVGRenderer = function () {
 
 	function renderLine( v1, v2, element, material ) {
 
-		var path = 'M' + v1.positionScreen.x + ',' + v1.positionScreen.y + 'L' + v2.positionScreen.x + ',' + v2.positionScreen.y;
+		var path = 'M' + convert( v1.positionScreen.x ) + ',' + convert( v1.positionScreen.y ) + 'L' + convert( v2.positionScreen.x ) + ',' + convert( v2.positionScreen.y );
 
 		if ( material instanceof THREE.LineBasicMaterial ) {
 
@@ -374,7 +386,7 @@ THREE.SVGRenderer = function () {
 		_this.info.render.vertices += 3;
 		_this.info.render.faces ++;
 
-		var path = 'M' + v1.positionScreen.x + ',' + v1.positionScreen.y + 'L' + v2.positionScreen.x + ',' + v2.positionScreen.y + 'L' + v3.positionScreen.x + ',' + v3.positionScreen.y + 'z';
+		var path = 'M' + convert( v1.positionScreen.x ) + ',' + convert( v1.positionScreen.y ) + 'L' + convert( v2.positionScreen.x ) + ',' + convert( v2.positionScreen.y ) + 'L' + convert( v3.positionScreen.x ) + ',' + convert( v3.positionScreen.y ) + 'z';
 		var style = '';
 
 		if ( material instanceof THREE.MeshBasicMaterial ) {