mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-05 20:32:00 -05:00
Add Base.formatNumber() to format numbers in the same way as Scriptographer (precision of up to 5 fractional digits) and use it in the various #toString() functions.
This commit is contained in:
parent
fb6955e509
commit
bc80c58558
8 changed files with 31 additions and 20 deletions
|
@ -193,9 +193,11 @@ var Matrix = this.Matrix = Base.extend({
|
|||
* @return {string} A string representation of this transform.
|
||||
*/
|
||||
toString: function() {
|
||||
return '[['
|
||||
+ [this._m00, this._m01, this._m02].join(', ') + '], ['
|
||||
+ [this._m10, this._m11, this._m12].join(', ') + ']]';
|
||||
var format = Base.formatNumber;
|
||||
return '[[' + [format(this._m00), format(this._m01),
|
||||
format(this._m02)].join(', ') + '], ['
|
||||
+ [format(this._m10), format(this._m11),
|
||||
format(this._m12)].join(', ') + ']]';
|
||||
},
|
||||
|
||||
/**
|
||||
|
|
|
@ -454,7 +454,8 @@ var Point = this.Point = Base.extend({
|
|||
},
|
||||
|
||||
toString: function() {
|
||||
return '{ x: ' + this.x + ', y: ' + this.y + ' }';
|
||||
var format = Base.formatNumber;
|
||||
return '{ x: ' + format(this.x) + ', y: ' + format(this.y) + ' }';
|
||||
},
|
||||
|
||||
statics: {
|
||||
|
|
|
@ -221,10 +221,11 @@ var Rectangle = this.Rectangle = Base.extend({
|
|||
},
|
||||
|
||||
toString: function() {
|
||||
return '{ x: ' + this.x
|
||||
+ ', y: ' + this.y
|
||||
+ ', width: ' + this.width
|
||||
+ ', height: ' + this.height
|
||||
var format = Base.formatNumber;
|
||||
return '{ x: ' + format(this.x)
|
||||
+ ', y: ' + format(this.y)
|
||||
+ ', width: ' + format(this.width)
|
||||
+ ', height: ' + format(this.height)
|
||||
+ ' }';
|
||||
},
|
||||
|
||||
|
|
|
@ -86,7 +86,9 @@ var Size = this.Size = Base.extend({
|
|||
},
|
||||
|
||||
toString: function() {
|
||||
return '{ x: ' + this.width + ', y: ' + this.height + ' }';
|
||||
var format = Base.formatNumber;
|
||||
return '{ x: ' + format(this.width)
|
||||
+ ', y: ' + format(this.height) + ' }';
|
||||
},
|
||||
|
||||
statics: {
|
||||
|
|
|
@ -173,13 +173,14 @@ var Color = this.Color = Base.extend(new function() {
|
|||
},
|
||||
|
||||
toString: function() {
|
||||
var parts = [];
|
||||
var parts = [],
|
||||
format = Base.formatNumber;
|
||||
for (var i = 0, l = this._components.length; i < l; i++) {
|
||||
var component = this._components[i];
|
||||
var value = this['_' + component];
|
||||
var component = this._components[i],
|
||||
value = this['_' + component];
|
||||
if (component === 'alpha' && value == null)
|
||||
value = 1;
|
||||
parts.push(component + ': ' + value);
|
||||
parts.push(component + ': ' + format(value));
|
||||
}
|
||||
return '{ ' + parts.join(', ') + ' }';
|
||||
},
|
||||
|
|
|
@ -83,6 +83,10 @@ Base.inject({
|
|||
return str.replace(/\b[a-z]/g, function(match) {
|
||||
return match.toUpperCase();
|
||||
});
|
||||
},
|
||||
|
||||
formatNumber: function(num) {
|
||||
return (Math.round(num * 100000) / 100000).toString();
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
@ -129,8 +129,8 @@ CurveLocation = Base.extend({
|
|||
},
|
||||
|
||||
toString: function() {
|
||||
var parts = [];
|
||||
var point = this.getPoint();
|
||||
var parts = [],
|
||||
point = this.getPoint();
|
||||
if (point)
|
||||
parts.push('point: ' + point);
|
||||
var index = this.getIndex();
|
||||
|
@ -138,7 +138,7 @@ CurveLocation = Base.extend({
|
|||
parts.push('index: ' + index);
|
||||
var parameter = this.getParameter();
|
||||
if (parameter != null)
|
||||
parts.push('parameter: ' + parameter);
|
||||
parts.push('parameter: ' + Base.formatNumber(parameter));
|
||||
return '{ ' + parts.join(', ') + ' }';
|
||||
}
|
||||
});
|
||||
|
|
|
@ -42,10 +42,10 @@ var ToolEvent = this.ToolEvent = Base.extend({
|
|||
|
||||
toString: function() {
|
||||
return '{ type: ' + this.type
|
||||
+ ', point: ' + this.point
|
||||
+ ', count: ' + this.count
|
||||
+ ', modifiers: ' + this.modifiers
|
||||
+ ' }';
|
||||
+ ', point: ' + this.point
|
||||
+ ', count: ' + this.count
|
||||
+ ', modifiers: ' + this.modifiers
|
||||
+ ' }';
|
||||
},
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue