Use changes in latest Straps.js to remove as many hidden parameters as possible.

Hidden parameters through arguments[] are bad news for JS optimizer engines.
This commit is contained in:
Jürg Lehni 2014-01-05 17:40:54 +01:00
parent ea63d4c288
commit 6cb25fafe3
12 changed files with 95 additions and 77 deletions

View file

@ -14,7 +14,7 @@
"test" "test"
], ],
"devDependencies": { "devDependencies": {
"straps": "~1.4.2", "straps": "~1.4.3",
"acorn": "git://github.com/paperjs/acorn#0.3.2", "acorn": "git://github.com/paperjs/acorn#0.3.2",
"esprima": "~1.0.3", "esprima": "~1.0.3",
"stats.js": "r11" "stats.js": "r11"

View file

@ -491,7 +491,6 @@ var Point = Base.extend(/** @lends Point# */{
this.y * scale this.y * scale
); );
} }
return this;
}, },
/** /**
@ -533,28 +532,16 @@ var Point = Base.extend(/** @lends Point# */{
* @type Number * @type Number
*/ */
getAngle: function(/* point */) { getAngle: function(/* point */) {
// Hide parameters from Bootstrap so it injects bean too
return this.getAngleInRadians.apply(this, arguments) * 180 / Math.PI; return this.getAngleInRadians.apply(this, arguments) * 180 / Math.PI;
}, },
setAngle: function(angle) { setAngle: function(angle) {
// We store a reference to _angle internally so we still preserve it this.setAngleInRadians.call(this, angle * Math.PI / 180);
// when the vector's length is set to zero, and then anything else.
// Note that we cannot rely on it if x and y are something else than 0,
// since updating x / y does not automatically change _angle!
angle = this._angle = angle * Math.PI / 180;
if (!this.isZero()) {
var length = this.getLength();
// Use #set() instead of direct assignment of x/y, so LinkedPoint
// does not report changes twice.
this.set(
Math.cos(angle) * length,
Math.sin(angle) * length
);
}
return this;
}, },
getAngleInDegrees: '#getAngle',
setAngleInDegrees: '#setAngle',
/** /**
* Returns the smaller angle between two vectors in radians. The angle is * Returns the smaller angle between two vectors in radians. The angle is
* unsigned, no information about rotational direction is given. * unsigned, no information about rotational direction is given.
@ -572,8 +559,7 @@ var Point = Base.extend(/** @lends Point# */{
* @type Number * @type Number
*/ */
getAngleInRadians: function(/* point */) { getAngleInRadians: function(/* point */) {
// Hide parameters from Bootstrap so it injects bean too if (!arguments.length) {
if (arguments[0] === undefined) {
return this.isZero() return this.isZero()
// Return the preseved angle in case the vector has no // Return the preseved angle in case the vector has no
// length, and update the internal _angle in case the // length, and update the internal _angle in case the
@ -592,8 +578,21 @@ var Point = Base.extend(/** @lends Point# */{
} }
}, },
getAngleInDegrees: function(/* point */) { setAngleInRadians: function(angle) {
return this.getAngle(arguments[0]); // We store a reference to _angle internally so we still preserve it
// when the vector's length is set to zero, and then anything else.
// Note that we cannot rely on it if x and y are something else than 0,
// since updating x / y does not automatically change _angle!
this._angle = angle;
if (!this.isZero()) {
var length = this.getLength();
// Use #set() instead of direct assignment of x/y, so LinkedPoint
// does not report changes twice.
this.set(
Math.cos(angle) * length,
Math.sin(angle) * length
);
}
}, },
/** /**

View file

@ -237,8 +237,8 @@ var Rectangle = Base.extend(/** @lends Rectangle# */{
* @type Point * @type Point
* @bean * @bean
*/ */
getPoint: function(/* dontLink */) { getPoint: function(_dontLink) {
var ctor = arguments[0] ? Point : LinkedPoint; var ctor = _dontLink ? Point : LinkedPoint;
return new ctor(this.x, this.y, this, 'setPoint'); return new ctor(this.x, this.y, this, 'setPoint');
}, },
@ -255,8 +255,8 @@ var Rectangle = Base.extend(/** @lends Rectangle# */{
* @type Size * @type Size
* @bean * @bean
*/ */
getSize: function(/* dontLink */) { getSize: function(_dontLink) {
var ctor = arguments[0] ? Size : LinkedSize; var ctor = _dontLink ? Size : LinkedSize;
return new ctor(this.width, this.height, this, 'setSize'); return new ctor(this.width, this.height, this, 'setSize');
}, },
@ -401,8 +401,8 @@ var Rectangle = Base.extend(/** @lends Rectangle# */{
* @type Point * @type Point
* @bean * @bean
*/ */
getCenter: function(/* dontLink */) { getCenter: function(_dontLink) {
var ctor = arguments[0] ? Point : LinkedPoint; var ctor = _dontLink ? Point : LinkedPoint;
return new ctor(this.getCenterX(), this.getCenterY(), this, 'setCenter'); return new ctor(this.getCenterX(), this.getCenterY(), this, 'setCenter');
}, },
@ -812,8 +812,8 @@ var Rectangle = Base.extend(/** @lends Rectangle# */{
setY = 'set' + y, setY = 'set' + y,
get = 'get' + part, get = 'get' + part,
set = 'set' + part; set = 'set' + part;
this[get] = function(/* dontLink */) { this[get] = function(_dontLink) {
var ctor = arguments[0] ? Point : LinkedPoint; var ctor = _dontLink ? Point : LinkedPoint;
return new ctor(this[getX](), this[getY](), this, set); return new ctor(this[getX](), this[getY](), this, set);
}; };
this[set] = function(/* point */) { this[set] = function(/* point */) {

View file

@ -762,11 +762,11 @@ var Item = Base.extend(Callback, /** @lends Item# */{
* // Move the circle 100 points to the right * // Move the circle 100 points to the right
* circle.position.x += 100; * circle.position.x += 100;
*/ */
getPosition: function(/* dontLink */) { getPosition: function(_dontLink) {
// Cache position value. // Cache position value.
// Pass true for dontLink in getCenter(), so receive back a normal point // Pass true for _dontLink in getCenter(), so receive back a normal point
var position = this._position, var position = this._position,
ctor = arguments[0] ? Point : LinkedPoint; ctor = _dontLink ? Point : LinkedPoint;
// Do not cache LinkedPoints directly, since we would not be able to // Do not cache LinkedPoints directly, since we would not be able to
// use them to calculate the difference in #setPosition, as when it is // use them to calculate the difference in #setPosition, as when it is
// modified, it would hold new values already and only then cause the // modified, it would hold new values already and only then cause the
@ -784,7 +784,7 @@ var Item = Base.extend(Callback, /** @lends Item# */{
setPosition: function(/* point */) { setPosition: function(/* point */) {
// Calculate the distance to the current position, by which to // Calculate the distance to the current position, by which to
// translate the item. Pass true for dontLink, as we do not need a // translate the item. Pass true for _dontLink, as we do not need a
// LinkedPoint to simply calculate this distance. // LinkedPoint to simply calculate this distance.
this.translate(Point.read(arguments).subtract(this.getPosition(true))); this.translate(Point.read(arguments).subtract(this.getPosition(true)));
}, },
@ -802,10 +802,10 @@ var Item = Base.extend(Callback, /** @lends Item# */{
* *
* @example {@paperscript} * @example {@paperscript}
*/ */
getPivot: function(/* dontLink */) { getPivot: function(_dontLink) {
var pivot = this._pivot; var pivot = this._pivot;
if (pivot) { if (pivot) {
var ctor = arguments[0] ? Point : LinkedPoint; var ctor = _dontLink ? Point : LinkedPoint;
pivot = new ctor(pivot.x, pivot.y, this, 'setAnchor'); pivot = new ctor(pivot.x, pivot.y, this, 'setAnchor');
} }
return pivot; return pivot;
@ -2790,7 +2790,7 @@ var Item = Base.extend(Callback, /** @lends Item# */{
// color styles (only gradients so far) and pivot point: // color styles (only gradients so far) and pivot point:
var pivot = this._pivot, var pivot = this._pivot,
style = this._style, style = this._style,
// pass true for dontMerge so we don't recursively transform // pass true for _dontMerge so we don't recursively transform
// styles on groups' children. // styles on groups' children.
fillColor = style.getFillColor(true), fillColor = style.getFillColor(true),
strokeColor = style.getStrokeColor(true); strokeColor = style.getStrokeColor(true);

View file

@ -244,13 +244,13 @@ var Raster = Item.extend(/** @lends Raster# */{
* @type Context * @type Context
* @bean * @bean
*/ */
getContext: function(/* modify */) { getContext: function(modify) {
if (!this._context) if (!this._context)
this._context = this.getCanvas().getContext('2d'); this._context = this.getCanvas().getContext('2d');
// Support a hidden parameter that indicates if the context will be used // Support a hidden parameter that indicates if the context will be used
// to modify the Raster object. We can notify such changes ahead since // to modify the Raster object. We can notify such changes ahead since
// they are only used afterwards for redrawing. // they are only used afterwards for redrawing.
if (arguments[0]) { if (modify) {
// Also set _image to null since the Raster stops representing it. // Also set _image to null since the Raster stops representing it.
// NOTE: This should theoretically be in our own _changed() handler // NOTE: This should theoretically be in our own _changed() handler
// for ChangeFlag.PIXELS, but since it's only happening in one place // for ChangeFlag.PIXELS, but since it's only happening in one place

View file

@ -228,11 +228,11 @@ var CompoundPath = PathItem.extend(/** @lends CompoundPath# */{
return area; return area;
}, },
getPathData: function(/* precision */) { getPathData: function(precision) {
var children = this._children, var children = this._children,
paths = []; paths = [];
for (var i = 0, l = children.length; i < l; i++) for (var i = 0, l = children.length; i < l; i++)
paths.push(children[i].getPathData(arguments[0])); paths.push(children[i].getPathData(precision));
return paths.join(' '); return paths.join(' ');
}, },

View file

@ -61,13 +61,13 @@ var CurveLocation = Base.extend(/** @lends CurveLocation# */{
* @type Segment * @type Segment
* @bean * @bean
*/ */
getSegment: function(/* preferFirst */) { getSegment: function(_preferFirst) {
if (!this._segment) { if (!this._segment) {
var curve = this.getCurve(), var curve = this.getCurve(),
parameter = this.getParameter(); parameter = this.getParameter();
if (parameter === 1) { if (parameter === 1) {
this._segment = curve._segment2; this._segment = curve._segment2;
} else if (parameter === 0 || arguments[0]) { } else if (parameter === 0 || _preferFirst) {
this._segment = curve._segment1; this._segment = curve._segment1;
} else if (parameter == null) { } else if (parameter == null) {
return null; return null;
@ -82,14 +82,21 @@ var CurveLocation = Base.extend(/** @lends CurveLocation# */{
return this._segment; return this._segment;
}, },
setSegment: function(segment) {
// NOTE: We only include this setter so the above getter can declare
// the _preferFirst parameter without having to hide it.
// See Strap.js beans conventions.
this._segment = segment;
},
/** /**
* The curve by which the location is defined. * The curve by which the location is defined.
* *
* @type Curve * @type Curve
* @bean * @bean
*/ */
getCurve: function(/* uncached */) { getCurve: function(_uncached) {
if (!this._curve || arguments[0]) { if (!this._curve || _uncached) {
// If we're asked to get the curve uncached, access current curve // If we're asked to get the curve uncached, access current curve
// objects through segment1 / segment2. Since path splitting or // objects through segment1 / segment2. Since path splitting or
// dividing might have happened in the meantime, try segment1's // dividing might have happened in the meantime, try segment1's
@ -102,6 +109,11 @@ var CurveLocation = Base.extend(/** @lends CurveLocation# */{
return this._curve; return this._curve;
}, },
setCurve: function(curve) {
// See #setSegment()
this._curve = curve;
},
/** /**
* The curve location on the intersecting curve, if this location is the * The curve location on the intersecting curve, if this location is the
* result of a call to {@link PathItem#getIntersections(path)} / * result of a call to {@link PathItem#getIntersections(path)} /
@ -179,14 +191,19 @@ var CurveLocation = Base.extend(/** @lends CurveLocation# */{
* @type Number * @type Number
* @bean * @bean
*/ */
getParameter: function(/* uncached */) { getParameter: function(_uncached) {
if ((this._parameter == null || arguments[0]) && this._point) { if ((this._parameter == null || _uncached) && this._point) {
var curve = this.getCurve(arguments[0] && this._point); var curve = this.getCurve(_uncached && this._point);
this._parameter = curve && curve.getParameterOf(this._point); this._parameter = curve && curve.getParameterOf(this._point);
} }
return this._parameter; return this._parameter;
}, },
setParameter: function(parameter) {
// See #setSegment()
this._parameter = parameter;
},
/** /**
* The point which is defined by the {@link #curve} and * The point which is defined by the {@link #curve} and
* {@link #parameter}. * {@link #parameter}.
@ -194,14 +211,19 @@ var CurveLocation = Base.extend(/** @lends CurveLocation# */{
* @type Point * @type Point
* @bean * @bean
*/ */
getPoint: function(/* uncached */) { getPoint: function(_uncached) {
if ((!this._point || arguments[0]) && this._parameter != null) { if ((!this._point || _uncached) && this._parameter != null) {
var curve = this.getCurve(); var curve = this.getCurve();
this._point = curve && curve.getPointAt(this._parameter, true); this._point = curve && curve.getPointAt(this._parameter, true);
} }
return this._point; return this._point;
}, },
setPoint: function(point) {
// See #setSegment()
this._point = point;
},
/** /**
* The tangential vector to the {@link #curve} at the given location. * The tangential vector to the {@link #curve} at the given location.
* *

View file

@ -245,9 +245,8 @@ var Path = PathItem.extend(/** @lends Path# */{
* @type String * @type String
* @bean * @bean
*/ */
getPathData: function(/* precision */) { getPathData: function(precision) {
var segments = this._segments, var segments = this._segments,
precision = arguments[0],
f = Formatter.instance, f = Formatter.instance,
parts = []; parts = [];
@ -690,7 +689,7 @@ var Path = PathItem.extend(/** @lends Path# */{
* // Select the path, so we can see its segments: * // Select the path, so we can see its segments:
* path.selected = true; * path.selected = true;
*/ */
removeSegments: function(from, to/*, includeCurves */) { removeSegments: function(from, to, _includeCurves) {
from = from || 0; from = from || 0;
to = Base.pick(to, this._segments.length); to = Base.pick(to, this._segments.length);
var segments = this._segments, var segments = this._segments,
@ -724,7 +723,7 @@ var Path = PathItem.extend(/** @lends Path# */{
// Return the removed curves as well, if we're asked to include // Return the removed curves as well, if we're asked to include
// them, but exclude the first curve, since that's shared with the // them, but exclude the first curve, since that's shared with the
// previous segment and does not connect the returned segments. // previous segment and does not connect the returned segments.
if (arguments[2]) if (_includeCurves)
removed._curves = curves.slice(1); removed._curves = curves.slice(1);
// Adjust segments for the curves before and after the removed ones // Adjust segments for the curves before and after the removed ones
this._adjustCurves(index, index); this._adjustCurves(index, index);

View file

@ -310,26 +310,24 @@ var Segment = Base.extend(/** @lends Segment# */{
* // Select the third segment point: * // Select the third segment point:
* path.segments[2].selected = true; * path.segments[2].selected = true;
*/ */
isSelected: function(/* point */) { isSelected: function(_point) {
var point = arguments[0], // Hidden, only used in SegmentPoint var state = this._selectionState;
state = this._selectionState; return !_point ? !!(state & /*#=*/ SelectionState.SEGMENT)
return !point ? !!(state & /*#=*/ SelectionState.SEGMENT) : _point === this._point ? !!(state & /*#=*/ SelectionState.POINT)
: point === this._point ? !!(state & /*#=*/ SelectionState.POINT) : _point === this._handleIn ? !!(state & /*#=*/ SelectionState.HANDLE_IN)
: point === this._handleIn ? !!(state & /*#=*/ SelectionState.HANDLE_IN) : _point === this._handleOut ? !!(state & /*#=*/ SelectionState.HANDLE_OUT)
: point === this._handleOut ? !!(state & /*#=*/ SelectionState.HANDLE_OUT)
: false; : false;
}, },
setSelected: function(selected /*, point */) { setSelected: function(selected, _point) {
var point = arguments[1]; // Hidden, only used in SegmentPoint var path = this._path,
path = this._path,
selected = !!selected, // convert to boolean selected = !!selected, // convert to boolean
state = this._selectionState, state = this._selectionState,
oldState = state, oldState = state,
flag = !point ? /*#=*/ SelectionState.SEGMENT flag = !_point ? /*#=*/ SelectionState.SEGMENT
: point === this._point ? /*#=*/ SelectionState.POINT : _point === this._point ? /*#=*/ SelectionState.POINT
: point === this._handleIn ? /*#=*/ SelectionState.HANDLE_IN : _point === this._handleIn ? /*#=*/ SelectionState.HANDLE_IN
: point === this._handleOut ? /*#=*/ SelectionState.HANDLE_OUT : _point === this._handleOut ? /*#=*/ SelectionState.HANDLE_OUT
: 0; : 0;
if (selected) { if (selected) {
state |= flag; state |= flag;

View file

@ -115,7 +115,7 @@ var Symbol = Base.extend(/** @lends Symbol# */{
return this._definition; return this._definition;
}, },
setDefinition: function(item /*, dontCenter */) { setDefinition: function(item, _dontCenter) {
// Make sure we're not steatling another symbol's definition // Make sure we're not steatling another symbol's definition
if (item._parentSymbol) if (item._parentSymbol)
item = item.clone(); item = item.clone();
@ -127,7 +127,7 @@ var Symbol = Base.extend(/** @lends Symbol# */{
item.remove(); item.remove();
item.setSelected(false); item.setSelected(false);
// Move position to 0, 0, so it's centered when placed. // Move position to 0, 0, so it's centered when placed.
if (!arguments[1]) if (!_dontCenter)
item.setPosition(new Point()); item.setPosition(new Point());
item._parentSymbol = this; item._parentSymbol = this;
this._changed(/*#=*/ Change.GEOMETRY); this._changed(/*#=*/ Change.GEOMETRY);

View file

@ -168,13 +168,13 @@ var Style = Base.extend(new function() {
} }
}; };
fields[get] = function(/* dontMerge */) { fields[get] = function(_dontMerge) {
var value, var value,
children = this._item && this._item._children; children = this._item && this._item._children;
// If this item has children, walk through all of them and see if // If this item has children, walk through all of them and see if
// they all have the same style. // they all have the same style.
// If true is passed for dontMerge, don't merge children styles // If true is passed for _dontMerge, don't merge children styles
if (!children || children.length === 0 || arguments[0] if (!children || children.length === 0 || _dontMerge
|| this._item instanceof CompoundPath) { || this._item instanceof CompoundPath) {
var value = this._values[key]; var value = this._values[key];
if (value === undefined) { if (value === undefined) {

View file

@ -334,8 +334,8 @@ var View = Base.extend(Callback, /** @lends View# */{
* @type Size * @type Size
* @bean * @bean
*/ */
getSize: function(/* dontLink */) { getSize: function() {
return this.getBounds().getSize(arguments[0]); return this.getBounds().getSize();
}, },
/** /**
@ -344,8 +344,8 @@ var View = Base.extend(Callback, /** @lends View# */{
* @type Point * @type Point
* @bean * @bean
*/ */
getCenter: function(/* dontLink */) { getCenter: function() {
return this.getBounds().getCenter(arguments[0]); return this.getBounds().getCenter();
}, },
setCenter: function(center) { setCenter: function(center) {