mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-03 19:45:44 -05:00
Introduce Base.toFloat() and rename Base.formatNumber() to Base.formatFloat()
This commit is contained in:
parent
a7320cf2e2
commit
d671a08205
10 changed files with 24 additions and 21 deletions
|
@ -238,7 +238,7 @@ var Matrix = this.Matrix = Base.extend(/** @lends Matrix# */{
|
|||
* @return {String} A string representation of this transform.
|
||||
*/
|
||||
toString: function() {
|
||||
var format = Base.formatNumber;
|
||||
var format = Base.formatFloat;
|
||||
return '[[' + [format(this._a), format(this._b),
|
||||
format(this._tx)].join(', ') + '], ['
|
||||
+ [format(this._c), format(this._d),
|
||||
|
|
|
@ -209,7 +209,7 @@ var Point = this.Point = Base.extend(/** @lends Point# */{
|
|||
* @return {String} A string representation of the point.
|
||||
*/
|
||||
toString: function() {
|
||||
var format = Base.formatNumber;
|
||||
var format = Base.formatFloat;
|
||||
return '{ x: ' + format(this.x) + ', y: ' + format(this.y) + ' }';
|
||||
},
|
||||
|
||||
|
|
|
@ -398,7 +398,7 @@ var Rectangle = this.Rectangle = Base.extend(/** @lends Rectangle# */{
|
|||
* @return {String} A string representation of this rectangle.
|
||||
*/
|
||||
toString: function() {
|
||||
var format = Base.formatNumber;
|
||||
var format = Base.formatFloat;
|
||||
return '{ x: ' + format(this.x)
|
||||
+ ', y: ' + format(this.y)
|
||||
+ ', width: ' + format(this.width)
|
||||
|
|
|
@ -129,7 +129,7 @@ var Size = this.Size = Base.extend(/** @lends Size# */{
|
|||
* @return {String} A string representation of the size.
|
||||
*/
|
||||
toString: function() {
|
||||
var format = Base.formatNumber;
|
||||
var format = Base.formatFloat;
|
||||
return '{ width: ' + format(this.width)
|
||||
+ ', height: ' + format(this.height) + ' }';
|
||||
},
|
||||
|
|
|
@ -459,7 +459,7 @@ var Color = this.Color = Base.extend(new function() {
|
|||
*/
|
||||
toString: function() {
|
||||
var parts = [],
|
||||
format = Base.formatNumber;
|
||||
format = Base.formatFloat;
|
||||
for (var i = 0, l = this._components.length; i < l; i++) {
|
||||
var component = this._components[i],
|
||||
value = this['_' + component];
|
||||
|
|
|
@ -44,7 +44,7 @@ this.Base = Base.inject(/** @lends Base# */{
|
|||
if (key.charAt(0) != '_') {
|
||||
var type = typeof value;
|
||||
this.push(key + ': ' + (type === 'number'
|
||||
? Base.formatNumber(value)
|
||||
? Base.formatFloat(value)
|
||||
: type === 'string' ? "'" + value + "'" : value));
|
||||
}
|
||||
}, []).join(', ') + ' }';
|
||||
|
@ -239,8 +239,12 @@ this.Base = Base.inject(/** @lends Base# */{
|
|||
*
|
||||
* @param {Number} num the number to be converted to a string
|
||||
*/
|
||||
formatNumber: function(num) {
|
||||
formatFloat: function(num) {
|
||||
return (Math.round(num * 100000) / 100000).toString();
|
||||
},
|
||||
|
||||
toFloat: function(str) {
|
||||
return parseFloat(str, 10);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
|
@ -204,9 +204,8 @@ var PaperScript = this.PaperScript = new function() {
|
|||
var xhr = new (window.ActiveXObject || XMLHttpRequest)(
|
||||
'Microsoft.XMLHTTP');
|
||||
xhr.open('GET', url, true);
|
||||
if (xhr.overrideMimeType) {
|
||||
if (xhr.overrideMimeType)
|
||||
xhr.overrideMimeType('text/plain');
|
||||
}
|
||||
xhr.onreadystatechange = function() {
|
||||
if (xhr.readyState === 4) {
|
||||
return evaluate(xhr.responseText, scope);
|
||||
|
|
|
@ -201,9 +201,9 @@ CurveLocation = Base.extend(/** @lends CurveLocation# */{
|
|||
parts.push('index: ' + index);
|
||||
var parameter = this.getParameter();
|
||||
if (parameter != null)
|
||||
parts.push('parameter: ' + Base.formatNumber(parameter));
|
||||
parts.push('parameter: ' + Base.formatFloat(parameter));
|
||||
if (this._distance != null)
|
||||
parts.push('distance: ' + Base.formatNumber(this._distance));
|
||||
parts.push('distance: ' + Base.formatFloat(this._distance));
|
||||
return '{ ' + parts.join(', ') + ' }';
|
||||
}
|
||||
});
|
||||
|
|
|
@ -22,18 +22,18 @@
|
|||
*/
|
||||
new function() {
|
||||
|
||||
// Shortcut to Base.formatNumber
|
||||
var formatNumber = Base.formatNumber;
|
||||
// Shortcut to Base.formatFloat
|
||||
var formatFloat = Base.formatFloat;
|
||||
|
||||
function formatPoint(point) {
|
||||
return formatNumber(point.x) + ',' + formatNumber(point.y);
|
||||
return formatFloat(point.x) + ',' + formatFloat(point.y);
|
||||
}
|
||||
|
||||
function setAttributes(svg, attrs) {
|
||||
for (var key in attrs) {
|
||||
var val = attrs[key];
|
||||
if (typeof val === 'number')
|
||||
val = formatNumber(val);
|
||||
val = formatFloat(val);
|
||||
svg.setAttribute(key, val);
|
||||
}
|
||||
return svg;
|
||||
|
@ -70,7 +70,7 @@ new function() {
|
|||
scale = matrix.getScaling();
|
||||
if (angle != null) {
|
||||
transform.push(angle
|
||||
? 'rotate(' + formatNumber(angle) + ')'
|
||||
? 'rotate(' + formatFloat(angle) + ')'
|
||||
: 'scale(' + formatPoint(scale) +')');
|
||||
} else {
|
||||
transform.push('matrix(' + matrix.getValues().join(',') + ')');
|
||||
|
@ -309,7 +309,7 @@ new function() {
|
|||
break;
|
||||
}
|
||||
if (angle) {
|
||||
attrs.transform = 'rotate(' + formatNumber(angle) + ','
|
||||
attrs.transform = 'rotate(' + formatFloat(angle) + ','
|
||||
+ formatPoint(center) + ')';
|
||||
}
|
||||
var svg = createElement(type, attrs);
|
||||
|
@ -348,7 +348,7 @@ new function() {
|
|||
: entry.type === 'array'
|
||||
? value.join(',')
|
||||
: entry.type === 'number'
|
||||
? formatNumber(value)
|
||||
? formatFloat(value)
|
||||
: value;
|
||||
}
|
||||
});
|
||||
|
|
|
@ -62,7 +62,7 @@ new function() {
|
|||
return value === 'none'
|
||||
? null
|
||||
: type === 'number'
|
||||
? parseFloat(value, 10)
|
||||
? Base.toFloat(value)
|
||||
: type === 'array'
|
||||
? value.split(/[\s,]+/g).map(parseFloat)
|
||||
: type === 'color' && getDefinition(value)
|
||||
|
@ -394,7 +394,7 @@ new function() {
|
|||
case 'stop-opacity':
|
||||
// http://www.w3.org/TR/SVG/masking.html#OpacityProperty
|
||||
case 'opacity':
|
||||
var opacity = parseFloat(value, 10);
|
||||
var opacity = Base.toFloat(value);
|
||||
if (name === 'stop-opacity') {
|
||||
item.color.setAlpha(opacity);
|
||||
} else {
|
||||
|
@ -459,7 +459,7 @@ new function() {
|
|||
item.setFont(value.split(',')[0].replace(/^\s+|\s+$/g, ''));
|
||||
break;
|
||||
case 'font-size':
|
||||
item.setFontSize(parseFloat(value, 10));
|
||||
item.setFontSize(Base.toFloat(value));
|
||||
break;
|
||||
case 'text-anchor':
|
||||
item.setJustification({
|
||||
|
|
Loading…
Reference in a new issue