Introduce Base.toFloat() and rename Base.formatNumber() to Base.formatFloat()

This commit is contained in:
Jürg Lehni 2012-11-14 01:31:08 -08:00
parent a7320cf2e2
commit d671a08205
10 changed files with 24 additions and 21 deletions

View file

@ -238,7 +238,7 @@ var Matrix = this.Matrix = Base.extend(/** @lends Matrix# */{
* @return {String} A string representation of this transform. * @return {String} A string representation of this transform.
*/ */
toString: function() { toString: function() {
var format = Base.formatNumber; var format = Base.formatFloat;
return '[[' + [format(this._a), format(this._b), return '[[' + [format(this._a), format(this._b),
format(this._tx)].join(', ') + '], [' format(this._tx)].join(', ') + '], ['
+ [format(this._c), format(this._d), + [format(this._c), format(this._d),

View file

@ -209,7 +209,7 @@ var Point = this.Point = Base.extend(/** @lends Point# */{
* @return {String} A string representation of the point. * @return {String} A string representation of the point.
*/ */
toString: function() { toString: function() {
var format = Base.formatNumber; var format = Base.formatFloat;
return '{ x: ' + format(this.x) + ', y: ' + format(this.y) + ' }'; return '{ x: ' + format(this.x) + ', y: ' + format(this.y) + ' }';
}, },

View file

@ -398,7 +398,7 @@ var Rectangle = this.Rectangle = Base.extend(/** @lends Rectangle# */{
* @return {String} A string representation of this rectangle. * @return {String} A string representation of this rectangle.
*/ */
toString: function() { toString: function() {
var format = Base.formatNumber; var format = Base.formatFloat;
return '{ x: ' + format(this.x) return '{ x: ' + format(this.x)
+ ', y: ' + format(this.y) + ', y: ' + format(this.y)
+ ', width: ' + format(this.width) + ', width: ' + format(this.width)

View file

@ -129,7 +129,7 @@ var Size = this.Size = Base.extend(/** @lends Size# */{
* @return {String} A string representation of the size. * @return {String} A string representation of the size.
*/ */
toString: function() { toString: function() {
var format = Base.formatNumber; var format = Base.formatFloat;
return '{ width: ' + format(this.width) return '{ width: ' + format(this.width)
+ ', height: ' + format(this.height) + ' }'; + ', height: ' + format(this.height) + ' }';
}, },

View file

@ -459,7 +459,7 @@ var Color = this.Color = Base.extend(new function() {
*/ */
toString: function() { toString: function() {
var parts = [], var parts = [],
format = Base.formatNumber; format = Base.formatFloat;
for (var i = 0, l = this._components.length; i < l; i++) { for (var i = 0, l = this._components.length; i < l; i++) {
var component = this._components[i], var component = this._components[i],
value = this['_' + component]; value = this['_' + component];

View file

@ -44,7 +44,7 @@ this.Base = Base.inject(/** @lends Base# */{
if (key.charAt(0) != '_') { if (key.charAt(0) != '_') {
var type = typeof value; var type = typeof value;
this.push(key + ': ' + (type === 'number' this.push(key + ': ' + (type === 'number'
? Base.formatNumber(value) ? Base.formatFloat(value)
: type === 'string' ? "'" + value + "'" : value)); : type === 'string' ? "'" + value + "'" : value));
} }
}, []).join(', ') + ' }'; }, []).join(', ') + ' }';
@ -239,8 +239,12 @@ this.Base = Base.inject(/** @lends Base# */{
* *
* @param {Number} num the number to be converted to a string * @param {Number} num the number to be converted to a string
*/ */
formatNumber: function(num) { formatFloat: function(num) {
return (Math.round(num * 100000) / 100000).toString(); return (Math.round(num * 100000) / 100000).toString();
},
toFloat: function(str) {
return parseFloat(str, 10);
} }
} }
}); });

View file

@ -204,9 +204,8 @@ var PaperScript = this.PaperScript = new function() {
var xhr = new (window.ActiveXObject || XMLHttpRequest)( var xhr = new (window.ActiveXObject || XMLHttpRequest)(
'Microsoft.XMLHTTP'); 'Microsoft.XMLHTTP');
xhr.open('GET', url, true); xhr.open('GET', url, true);
if (xhr.overrideMimeType) { if (xhr.overrideMimeType)
xhr.overrideMimeType('text/plain'); xhr.overrideMimeType('text/plain');
}
xhr.onreadystatechange = function() { xhr.onreadystatechange = function() {
if (xhr.readyState === 4) { if (xhr.readyState === 4) {
return evaluate(xhr.responseText, scope); return evaluate(xhr.responseText, scope);

View file

@ -201,9 +201,9 @@ CurveLocation = Base.extend(/** @lends CurveLocation# */{
parts.push('index: ' + index); parts.push('index: ' + index);
var parameter = this.getParameter(); var parameter = this.getParameter();
if (parameter != null) if (parameter != null)
parts.push('parameter: ' + Base.formatNumber(parameter)); parts.push('parameter: ' + Base.formatFloat(parameter));
if (this._distance != null) if (this._distance != null)
parts.push('distance: ' + Base.formatNumber(this._distance)); parts.push('distance: ' + Base.formatFloat(this._distance));
return '{ ' + parts.join(', ') + ' }'; return '{ ' + parts.join(', ') + ' }';
} }
}); });

View file

@ -22,18 +22,18 @@
*/ */
new function() { new function() {
// Shortcut to Base.formatNumber // Shortcut to Base.formatFloat
var formatNumber = Base.formatNumber; var formatFloat = Base.formatFloat;
function formatPoint(point) { function formatPoint(point) {
return formatNumber(point.x) + ',' + formatNumber(point.y); return formatFloat(point.x) + ',' + formatFloat(point.y);
} }
function setAttributes(svg, attrs) { function setAttributes(svg, attrs) {
for (var key in attrs) { for (var key in attrs) {
var val = attrs[key]; var val = attrs[key];
if (typeof val === 'number') if (typeof val === 'number')
val = formatNumber(val); val = formatFloat(val);
svg.setAttribute(key, val); svg.setAttribute(key, val);
} }
return svg; return svg;
@ -70,7 +70,7 @@ new function() {
scale = matrix.getScaling(); scale = matrix.getScaling();
if (angle != null) { if (angle != null) {
transform.push(angle transform.push(angle
? 'rotate(' + formatNumber(angle) + ')' ? 'rotate(' + formatFloat(angle) + ')'
: 'scale(' + formatPoint(scale) +')'); : 'scale(' + formatPoint(scale) +')');
} else { } else {
transform.push('matrix(' + matrix.getValues().join(',') + ')'); transform.push('matrix(' + matrix.getValues().join(',') + ')');
@ -309,7 +309,7 @@ new function() {
break; break;
} }
if (angle) { if (angle) {
attrs.transform = 'rotate(' + formatNumber(angle) + ',' attrs.transform = 'rotate(' + formatFloat(angle) + ','
+ formatPoint(center) + ')'; + formatPoint(center) + ')';
} }
var svg = createElement(type, attrs); var svg = createElement(type, attrs);
@ -348,7 +348,7 @@ new function() {
: entry.type === 'array' : entry.type === 'array'
? value.join(',') ? value.join(',')
: entry.type === 'number' : entry.type === 'number'
? formatNumber(value) ? formatFloat(value)
: value; : value;
} }
}); });

View file

@ -62,7 +62,7 @@ new function() {
return value === 'none' return value === 'none'
? null ? null
: type === 'number' : type === 'number'
? parseFloat(value, 10) ? Base.toFloat(value)
: type === 'array' : type === 'array'
? value.split(/[\s,]+/g).map(parseFloat) ? value.split(/[\s,]+/g).map(parseFloat)
: type === 'color' && getDefinition(value) : type === 'color' && getDefinition(value)
@ -394,7 +394,7 @@ new function() {
case 'stop-opacity': case 'stop-opacity':
// http://www.w3.org/TR/SVG/masking.html#OpacityProperty // http://www.w3.org/TR/SVG/masking.html#OpacityProperty
case 'opacity': case 'opacity':
var opacity = parseFloat(value, 10); var opacity = Base.toFloat(value);
if (name === 'stop-opacity') { if (name === 'stop-opacity') {
item.color.setAlpha(opacity); item.color.setAlpha(opacity);
} else { } else {
@ -459,7 +459,7 @@ new function() {
item.setFont(value.split(',')[0].replace(/^\s+|\s+$/g, '')); item.setFont(value.split(',')[0].replace(/^\s+|\s+$/g, ''));
break; break;
case 'font-size': case 'font-size':
item.setFontSize(parseFloat(value, 10)); item.setFontSize(Base.toFloat(value));
break; break;
case 'text-anchor': case 'text-anchor':
item.setJustification({ item.setJustification({