paper.js/src/util/Formatter.js

59 lines
1.6 KiB
JavaScript
Raw Normal View History

/*
* Paper.js - The Swiss Army Knife of Vector Graphics Scripting.
* http://paperjs.org/
*
2014-01-03 19:47:16 -05:00
* Copyright (c) 2011 - 2014, Juerg Lehni & Jonathan Puckey
* http://scratchdisk.com/ & http://jonathanpuckey.com/
*
* Distributed under the MIT license. See LICENSE file for details.
*
* All rights reserved.
*/
/**
* @name Formatter
* @class
* @private
*/
var Formatter = Base.extend(/** @lends Formatter# */{
2014-08-16 13:24:54 -04:00
/**
* @param {Number} [precision=5] the amount of fractional digits.
*/
initialize: function(precision) {
this.precision = precision || 5;
this.multiplier = Math.pow(10, this.precision);
},
2014-08-16 13:24:54 -04:00
/**
* Utility function for rendering numbers as strings at a precision of
* up to the amount of fractional digits.
*
* @param {Number} num the number to be converted to a string
*/
number: function(val) {
// It would be nice to use Number#toFixed() instead, but it pads with 0,
// unecessarily consuming space.
return Math.round(val * this.multiplier) / this.multiplier;
},
2014-08-16 13:24:54 -04:00
pair: function(val1, val2, separator) {
return this.number(val1) + (separator || ',') + this.number(val2);
},
2014-05-13 07:23:37 -04:00
2014-08-16 13:24:54 -04:00
point: function(val, separator) {
return this.number(val.x) + (separator || ',') + this.number(val.y);
},
2014-08-16 13:24:54 -04:00
size: function(val, separator) {
return this.number(val.width) + (separator || ',')
+ this.number(val.height);
},
2014-08-16 13:24:54 -04:00
rectangle: function(val, separator) {
return this.point(val, separator) + (separator || ',')
+ this.size(val, separator);
}
});
2013-10-10 17:09:18 -04:00
Formatter.instance = new Formatter();